Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fatal runtime panic passing invalid pointer #40

Merged
merged 2 commits into from
Nov 23, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions hyperscan/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ DEFINE_ALLOCTOR(Scratch, scratch);
DEFINE_ALLOCTOR(Stream, stream);

extern int hsMatchEventCallback(unsigned int id, unsigned long long from, unsigned long long to, unsigned int flags, void *context);

static inline hs_error_t safe_hs_scan(const hs_database_t *db, const char *data, unsigned int length, unsigned int flags, hs_scratch_t *scratch, match_event_handler onEvent, unsigned long context) {
return hs_scan(db, data, length, flags, scratch, onEvent, (void*)(context));
}
*/
import "C"

Expand Down Expand Up @@ -1013,7 +1009,8 @@ type hsMatchEventContext struct {

//export hsMatchEventCallback
func hsMatchEventCallback(id C.uint, from, to C.ulonglong, flags C.uint, data unsafe.Pointer) C.int {
ctx, ok := handle.Handle(data).Value().(hsMatchEventContext)
h := (*handle.Handle)(data)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*(*handle.Handle)(data)

The current code is not wrong because of the implicit dereference in h.Value, but h should be a Handle, not a pointer to one.

ctx, ok := h.Value().(hsMatchEventContext)
if !ok {
return C.HS_INVALID
}
Expand Down Expand Up @@ -1041,13 +1038,13 @@ func hsScan(db hsDatabase, data []byte, flags ScanFlag, scratch hsScratch, onEve

hdr := (*reflect.SliceHeader)(unsafe.Pointer(&data)) // FIXME: Zero-copy access to go data

ret := C.safe_hs_scan(db,
ret := C.hs_scan(db,
(*C.char)(unsafe.Pointer(hdr.Data)),
C.uint(hdr.Len),
C.uint(flags),
scratch,
C.match_event_handler(C.hsMatchEventCallback),
(C.ulong)(h))
unsafe.Pointer(&h))

// Ensure go data is alive before the C function returns
runtime.KeepAlive(data)
Expand Down Expand Up @@ -1091,7 +1088,7 @@ func hsScanVector(db hsDatabase, data [][]byte, flags ScanFlag, scratch hsScratc
C.uint(flags),
scratch,
C.match_event_handler(C.hsMatchEventCallback),
unsafe.Pointer(h))
unsafe.Pointer(&h))

// Ensure go data is alive before the C function returns
runtime.KeepAlive(data)
Expand Down Expand Up @@ -1131,7 +1128,7 @@ func hsScanStream(stream hsStream, data []byte, flags ScanFlag, scratch hsScratc
C.uint(flags),
scratch,
C.match_event_handler(C.hsMatchEventCallback),
unsafe.Pointer(h))
unsafe.Pointer(&h))

// Ensure go data is alive before the C function returns
runtime.KeepAlive(data)
Expand All @@ -1150,7 +1147,7 @@ func hsCloseStream(stream hsStream, scratch hsScratch, onEvent hsMatchEventHandl
ret := C.hs_close_stream(stream,
scratch,
C.match_event_handler(C.hsMatchEventCallback),
unsafe.Pointer(h))
unsafe.Pointer(&h))

if ret != C.HS_SUCCESS {
return HsError(ret)
Expand All @@ -1167,7 +1164,7 @@ func hsResetStream(stream hsStream, flags ScanFlag, scratch hsScratch, onEvent h
C.uint(flags),
scratch,
C.match_event_handler(C.hsMatchEventCallback),
unsafe.Pointer(h))
unsafe.Pointer(&h))

if ret != C.HS_SUCCESS {
return HsError(ret)
Expand All @@ -1194,7 +1191,7 @@ func hsResetAndCopyStream(to, from hsStream, scratch hsScratch, onEvent hsMatchE
from,
scratch,
C.match_event_handler(C.hsMatchEventCallback),
unsafe.Pointer(h))
unsafe.Pointer(&h))

if ret != C.HS_SUCCESS {
return HsError(ret)
Expand Down Expand Up @@ -1242,7 +1239,7 @@ func hsResetAndExpandStream(stream hsStream, buf []byte, scratch hsScratch, onEv
C.size_t(len(buf)),
scratch,
C.match_event_handler(C.hsMatchEventCallback),
unsafe.Pointer(h))
unsafe.Pointer(&h))

runtime.KeepAlive(buf)

Expand Down