-
Notifications
You must be signed in to change notification settings - Fork 91
Base implementation of catching exceptions made in C stubs #433
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
Open
SturdyPose
wants to merge
15
commits into
mirage:main
Choose a base branch
from
SturdyPose:sturdypose/add-stack-tracing-for-nullptrs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a740ed7
Add base implementation
SturdyPose 77f7efd
Update stubs, test, export SegFault exception
SturdyPose cdd6dbb
add linux impls
821ef85
Update exit codes, add fail expected states
SturdyPose 87fcef5
Fix endlines
SturdyPose 588162d
Use clang-format, apply suggestions, credit author, add rule for js …
SturdyPose dcda527
Fix js build by adding stub
SturdyPose 9a51e3c
Add accepted exit code
SturdyPose 8cc6d0c
use preallocated static buffer
SturdyPose ea752f0
Update test and expected code
SturdyPose 65f1ee0
Use alternative stack during crash
SturdyPose 8c774cd
Fix line endings, remove unnecessary header files, reformat
SturdyPose fb6e903
Fix windows build
SturdyPose 697d0ad
Add formating, add headers
SturdyPose 86c3047
Try to trigger segfault on mac
SturdyPose File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| // caml headers | ||
| #include <caml/alloc.h> | ||
| #include <caml/callback.h> | ||
| #include <caml/fail.h> | ||
| #include <caml/memory.h> | ||
| #include <caml/mlvalues.h> | ||
|
|
||
| #include <stdalign.h> | ||
| #include <stdbool.h> | ||
|
|
||
|
SturdyPose marked this conversation as resolved.
|
||
| #define CRASH_BUFFER_SIZE 10240 | ||
| static char crash_buffer[CRASH_BUFFER_SIZE]; | ||
|
|
||
| typedef struct { | ||
| char *buffer; | ||
| size_t capacity; | ||
| size_t offset; | ||
| } StackTraceBuffer; | ||
|
|
||
| static bool finit_stack_trace_buffer(StackTraceBuffer *pStackTraceBuffer, | ||
| size_t size) { | ||
| pStackTraceBuffer->capacity = size; | ||
| pStackTraceBuffer->offset = 0; | ||
| pStackTraceBuffer->buffer = crash_buffer; | ||
| pStackTraceBuffer->buffer[0] = '\0'; | ||
| return true; | ||
| } | ||
|
|
||
| static void append_to_buffer(StackTraceBuffer *sb, const char *format, ...) { | ||
| if (sb->offset >= sb->capacity) | ||
| return; // Buffer full | ||
|
|
||
| va_list args; | ||
| va_start(args, format); | ||
|
|
||
| size_t remaining = sb->capacity - sb->offset; | ||
| int written = vsnprintf(sb->buffer + sb->offset, remaining, format, args); | ||
|
|
||
| va_end(args); | ||
|
|
||
| if (written > 0) { | ||
| if ((size_t)written < remaining) { | ||
| sb->offset += written; | ||
| } else { | ||
| // Truncated or filled exactly; ensure null termination at the end | ||
| sb->offset = sb->capacity - 1; | ||
| sb->buffer[sb->offset] = '\0'; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static const char *CAML_ERROR_ID = "segfault exception"; | ||
|
|
||
| #if defined(_WIN32) | ||
| // clang-format off | ||
| #include <windows.h> | ||
| #include <dbghelp.h> | ||
| #include <excpt.h> | ||
| // clang-format on | ||
|
|
||
| // Stacktrace collection inspired by | ||
| // https://smhk.net/note/2025/03/c-stack-trace-in-windows/ | ||
| static void create_stacktrace(StackTraceBuffer *pStackTraceBuffer) { | ||
| HANDLE process = GetCurrentProcess(); | ||
| HANDLE thread = GetCurrentThread(); | ||
| CONTEXT context; | ||
| STACKFRAME64 stack; | ||
| DWORD machine_type; | ||
|
|
||
| RtlCaptureContext(&context); | ||
|
|
||
| ZeroMemory(&stack, sizeof(STACKFRAME64)); | ||
|
|
||
| #if defined(_M_IX86) || defined(__i386__) | ||
| machine_type = IMAGE_FILE_MACHINE_I386; | ||
| stack.AddrPC.Offset = context.Eip; | ||
| stack.AddrFrame.Offset = context.Ebp; | ||
| stack.AddrStack.Offset = context.Esp; | ||
| #elif defined(_M_X64) || defined(__x86_64__) | ||
| machine_type = IMAGE_FILE_MACHINE_AMD64; | ||
| stack.AddrPC.Offset = context.Rip; | ||
| stack.AddrFrame.Offset = context.Rsp; | ||
| stack.AddrStack.Offset = context.Rsp; | ||
| #elif defined(_M_ARM64) || defined(__aarch64__) | ||
| machine_type = IMAGE_FILE_MACHINE_ARM64; | ||
| stack.AddrPC.Offset = context.Pc; | ||
| stack.AddrFrame.Offset = context.Fp; | ||
| stack.AddrStack.Offset = context.Sp; | ||
| #else | ||
| #error "Unsupported platform" | ||
| #endif | ||
|
|
||
| stack.AddrPC.Mode = AddrModeFlat; | ||
| stack.AddrFrame.Mode = AddrModeFlat; | ||
| stack.AddrStack.Mode = AddrModeFlat; | ||
|
|
||
| SymInitialize(process, NULL, TRUE); | ||
| SymSetOptions(SYMOPT_LOAD_LINES | SYMOPT_UNDNAME); | ||
|
|
||
| append_to_buffer(pStackTraceBuffer, "Stack trace:\n"); | ||
| append_to_buffer(pStackTraceBuffer, " %-40s %-18s %s\n", "Function", | ||
| "Address", "Line"); | ||
| append_to_buffer(pStackTraceBuffer, " %-40s %-18s %s\n", "--------", | ||
| "-------", "----"); | ||
|
|
||
| while (StackWalk64(machine_type, process, thread, &stack, &context, NULL, | ||
| SymFunctionTableAccess64, SymGetModuleBase64, NULL)) { | ||
| if (stack.AddrPC.Offset == 0) | ||
| break; | ||
|
|
||
| DWORD64 symbol_addr = stack.AddrPC.Offset; | ||
| DWORD64 displacement = 0; | ||
| alignas(SYMBOL_INFO *) char | ||
| symbol_buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)] = {0}; | ||
| SYMBOL_INFO *symbol = (SYMBOL_INFO *)symbol_buffer; | ||
| symbol->SizeOfStruct = sizeof(SYMBOL_INFO); | ||
| symbol->MaxNameLen = MAX_SYM_NAME; | ||
|
|
||
| // Get line information | ||
| IMAGEHLP_LINE64 line = {0}; | ||
| line.SizeOfStruct = sizeof(IMAGEHLP_LINE64); | ||
| DWORD line_displacement = 0; | ||
| BOOL has_line = | ||
| SymGetLineFromAddr64(process, symbol_addr, &line_displacement, &line); | ||
|
|
||
| char function_name[MAX_SYM_NAME] = "Unknown"; | ||
| if (SymFromAddr(process, symbol_addr, &displacement, symbol)) { | ||
| strncpy(function_name, symbol->Name, MAX_SYM_NAME - 1); | ||
| function_name[MAX_SYM_NAME - 1] = '\0'; // Ensure null termination | ||
| } | ||
| // Format line information | ||
| char line_info[256] = "Unknown"; | ||
| if (has_line) { | ||
| snprintf(line_info, sizeof(line_info), "%s:%lu", line.FileName, | ||
| line.LineNumber); | ||
| } | ||
|
|
||
| // Print with better alignment using format specifiers | ||
| append_to_buffer(pStackTraceBuffer, " %-40.40s 0x%016llX %s\n", | ||
| function_name, symbol_addr, line_info); | ||
| append_to_buffer(pStackTraceBuffer, "\0"); | ||
| } | ||
|
|
||
| SymCleanup(process); | ||
| } | ||
|
|
||
| static LONG WINAPI | ||
| windows_exception_handler(EXCEPTION_POINTERS *pExceptionInfo) { | ||
| const DWORD exceptionCode = pExceptionInfo->ExceptionRecord->ExceptionCode; | ||
| switch (exceptionCode) { | ||
| case EXCEPTION_ACCESS_VIOLATION: { | ||
| void *faulting_address = | ||
| (void *)pExceptionInfo->ExceptionRecord->ExceptionInformation[1]; | ||
| StackTraceBuffer stack_trace_buffer; | ||
| if (!finit_stack_trace_buffer(&stack_trace_buffer, CRASH_BUFFER_SIZE)) { | ||
| caml_failwith("Can't create stack trace buffer"); | ||
| return EXCEPTION_CONTINUE_SEARCH; | ||
| } | ||
| create_stacktrace(&stack_trace_buffer); | ||
|
|
||
| caml_raise_with_string(*caml_named_value(CAML_ERROR_ID), | ||
| stack_trace_buffer.buffer); | ||
| free(stack_trace_buffer.buffer); | ||
| ExitProcess(STATUS_ACCESS_VIOLATION); | ||
| } | ||
| default: | ||
| break; | ||
| } | ||
| return EXCEPTION_CONTINUE_SEARCH; | ||
| } | ||
| #else | ||
| #include <execinfo.h> | ||
| #include <signal.h> | ||
| #include <stdlib.h> | ||
| #include <unistd.h> | ||
|
|
||
| #define STACK_TRACE_LENGTH 20 | ||
|
|
||
| static void unix_signal_handler(int sig, siginfo_t *si, void *unused) { | ||
|
|
||
| StackTraceBuffer stack_trace_buffer; | ||
| if (!finit_stack_trace_buffer(&stack_trace_buffer, CRASH_BUFFER_SIZE)) { | ||
| caml_failwith("Can't create stack trace buffer"); | ||
| return; | ||
| } | ||
|
|
||
| void *trace[STACK_TRACE_LENGTH]; | ||
| size_t trace_size = backtrace(trace, STACK_TRACE_LENGTH); | ||
|
|
||
| if (trace_size == 0) { | ||
| caml_failwith("Couldn't get backtrace"); | ||
| return; | ||
| } | ||
|
|
||
| append_to_buffer(&stack_trace_buffer, | ||
| "Access violation caught, stacktrace:\n"); | ||
|
|
||
| char **pSymbols = backtrace_symbols(trace, trace_size); | ||
| for (int i = 0; i < trace_size; ++i) { | ||
| append_to_buffer(&stack_trace_buffer, "%s\n", pSymbols[i]); | ||
| } | ||
| free(pSymbols); | ||
|
|
||
| caml_raise_with_string(*caml_named_value(CAML_ERROR_ID), | ||
| stack_trace_buffer.buffer); | ||
| exit(WEXITED); | ||
| } | ||
| #endif | ||
|
|
||
| CAMLprim value caml_setup_stub_exception_handler(void) { | ||
| CAMLparam0(); | ||
| #if defined(_WIN32) | ||
| AddVectoredExceptionHandler(1, windows_exception_handler); | ||
| #else | ||
| struct sigaction sa; | ||
| sa.sa_flags = SA_SIGINFO | SA_ONSTACK; | ||
| sigemptyset(&sa.sa_mask); | ||
| sa.sa_sigaction = unix_signal_handler; | ||
| sigaction(SIGSEGV, &sa, NULL); | ||
| sigaction(SIGBUS, &sa, NULL); // Catch SIGBUS as well for macOS | ||
| #endif | ||
| CAMLreturn(Val_unit); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| //Provides: caml_setup_stub_exception_handler | ||
| function caml_setup_stub_exception_handler(unit) { | ||
| // Intentionally left empty | ||
| // This function is mainly for C stubs and Segfault errors | ||
| return; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| (executable | ||
| (name nullexception) | ||
| (libraries alcotest) | ||
| (foreign_stubs | ||
| (language c) | ||
| (names nullexceptionstub)) | ||
| ) | ||
|
|
||
| (rule | ||
| (target stubs.actual) | ||
| (action | ||
| (with-accepted-exit-codes | ||
| (or 1 2 124 125) | ||
| (with-outputs-to | ||
| %{target} | ||
| (run %{dep:nullexception.exe} --color=auto))))) | ||
|
|
||
| (rule | ||
| (target stubs.processed) | ||
| (action | ||
| (with-outputs-to | ||
| %{target} | ||
| (run ../../strip_randomness.exe %{dep:stubs.actual})))) | ||
|
|
||
| (rule | ||
| (alias runtest) | ||
| (package alcotest) | ||
| (action | ||
| (diff stubs.expected stubs.processed))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| external segfault_call : unit -> unit = "caml_segfault_call" | ||
|
|
||
| (* This test should fail *) | ||
| let () = | ||
| let open Alcotest in | ||
| let call_seg () = | ||
| try | ||
| segfault_call (); | ||
| (check pass) "Should get segfault exception" () () | ||
| with | ||
| | SegFault _ -> | ||
| fail "Got segfault" | ||
| | _ -> | ||
| fail "Got uncategorized exception" | ||
| in | ||
| run __FILE__ | ||
| [ | ||
| ("segfault", [ test_case "nullexcept" `Quick (function _ -> call_seg ())]); | ||
| ] | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #include <caml/mlvalues.h> | ||
| #include <caml/memory.h> | ||
|
|
||
| #if !defined(_WIN32) | ||
| #include <signal.h> | ||
| #endif | ||
|
|
||
| value caml_segfault_call(void) { | ||
| CAMLparam0(); | ||
| volatile int *p = (volatile int *)0; | ||
| *p = 0xDEADBEEF; | ||
| #if !defined(_WIN32) | ||
| // in case mac won't call segfault | ||
| raise(SIGSEGV); | ||
| #endif | ||
| CAMLreturn(Int_val(0)); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.