Hasty Briefsbeta

Bilingual

Memory Safe Context Switching

22 days ago
  • Fil-C supports memory-safe context switching via longjmp/setjmp and ucontext APIs, preventing stack corruption.
  • setjmp/longjmp are used for exception handling, while ucontext APIs (getcontext, setcontext, makecontext, swapcontext) implement coroutines/fibers.
  • Fil-C makes setjmp/longjmp safe by storing jmp_buf as an opaque zjmp_buf, restricting direct calls, and validating stack frame ancestry.
  • ucontext safety is achieved using opaque zfiber_context objects, a strict state machine, thread affinity, and GC integration with grey fiber tracking.
  • The implementation ensures no misuse leads to dangling stack execution; unsafe operations cause panics or compile-time errors.
  • GC integration handles fiber stacks during marking via grey fiber lists to prevent race conditions in on-the-fly garbage collection.
  • Fil-C's ucontext support is available in glibc builds (from source after version 0.680), with setjmp/longjmp being more mature.
  • Examples demonstrate how volatile variables and compiler optimizations affect setjmp behavior, emphasizing Fil-C's compliance with C semantics.