3737#include <sys/mman.h>
3838#include <unistd.h>
3939#include <errno.h>
40+ #include <string.h>
41+
42+ // Helper macro for creating CharSlice from C strings at runtime
43+ // This safely converts a const char* to ddog_CharSlice with proper length calculation
44+ #define DDOG_CHARSLICE_FROM_CSTR (cstr ) \
45+ ((cstr != NULL) ? (ddog_CharSlice){ .ptr = (cstr), .len = strlen(cstr) } : (ddog_CharSlice){ .ptr = NULL, .len = 0 })
4046
4147// Include profiling stack walking functionality
4248// Note: rb_iseq_path and rb_iseq_base_label are already declared in MJIT header
@@ -52,8 +58,7 @@ static VALUE _native_register_runtime_stack_callback(VALUE _self, VALUE callback
5258static VALUE _native_is_runtime_callback_registered (DDTRACE_UNUSED VALUE _self );
5359
5460static void ruby_runtime_stack_callback (
55- void (* emit_frame )(const ddog_crasht_RuntimeStackFrame * ),
56- void (* emit_stacktrace_string )(const char * )
61+ void (* emit_frame )(const ddog_crasht_RuntimeStackFrame * )
5762);
5863
5964static bool first_init = true;
@@ -67,7 +72,7 @@ static bool is_pointer_readable(const void *ptr, size_t size) {
6772 size_t pages = ((char * )ptr + size - (char * )aligned_ptr + page_size - 1 ) / page_size ;
6873
6974 // Stack-allocate a small buffer for mincore results.. should be safe?
70- char vec [16 ];
75+ unsigned char vec [16 ];
7176 if (pages > 16 ) return false; // Too big to check safely
7277
7378 return mincore (aligned_ptr , pages * page_size , vec ) == 0 ;
@@ -248,12 +253,9 @@ static VALUE _native_stop(DDTRACE_UNUSED VALUE _self) {
248253 return Qtrue ;
249254}
250255
251-
252256static void ruby_runtime_stack_callback (
253- void (* emit_frame )(const ddog_crasht_RuntimeStackFrame * ),
254- void (* emit_stacktrace_string )(const char * )
257+ void (* emit_frame )(const ddog_crasht_RuntimeStackFrame * )
255258) {
256- (void )emit_stacktrace_string ;
257259
258260 VALUE current_thread = rb_thread_current ();
259261 if (current_thread == Qnil ) return ;
@@ -337,8 +339,8 @@ static void ruby_runtime_stack_callback(
337339 }
338340
339341 ddog_crasht_RuntimeStackFrame frame = {
340- .function_name = function_name ,
341- .file_name = file_name ,
342+ .function_name = DDOG_CHARSLICE_FROM_CSTR ( function_name ) ,
343+ .file_name = DDOG_CHARSLICE_FROM_CSTR ( file_name ) ,
342344 .line_number = line_no ,
343345 .column_number = 0
344346 };
@@ -365,8 +367,8 @@ static void ruby_runtime_stack_callback(
365367 }
366368
367369 ddog_crasht_RuntimeStackFrame frame = {
368- .function_name = function_name ,
369- .file_name = file_name ,
370+ .function_name = DDOG_CHARSLICE_FROM_CSTR ( function_name ) ,
371+ .file_name = DDOG_CHARSLICE_FROM_CSTR ( file_name ) ,
370372 .line_number = 0 ,
371373 .column_number = 0
372374 };
@@ -385,17 +387,15 @@ static VALUE _native_register_runtime_stack_callback(DDTRACE_UNUSED VALUE _self,
385387 rb_raise (rb_eArgError , "Invalid callback_type. Only :frame is supported" );
386388 }
387389
388- enum ddog_crasht_CallbackResult result = ddog_crasht_register_runtime_stack_callback (
389- ruby_runtime_stack_callback ,
390- DDOG_CRASHT_CALLBACK_TYPE_FRAME
390+ enum ddog_crasht_CallbackResult result = ddog_crasht_register_runtime_frame_callback (
391+ ruby_runtime_stack_callback
391392 );
392393
393394 switch (result ) {
394395 case DDOG_CRASHT_CALLBACK_RESULT_OK :
395396 return Qtrue ;
396- case DDOG_CRASHT_CALLBACK_RESULT_ERROR :
397- rb_raise (rb_eRuntimeError , "Failed to register runtime callback" );
398- break ;
397+ default :
398+ return Qfalse ;
399399 }
400400
401401 return Qfalse ;
0 commit comments