Commit 1c3f7fb1

1c3f7fb151e6Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +1 added · ~2 modified

Description

Replace ucontext fibers with a portable asm context switch (OpenBSD)

Spinel's Fiber runtime (and the Thread = single-threaded Fiber model) used POSIX ucontext (getcontext/makecontext/swapcontext). OpenBSD removed those in 5.7 (W^X / ASLR hardening), so #include <ucontext.h> failed and Spinel wouldn't build there; they're also deprecated on macOS.

Replace the switch with a tiny cooperative register swap. The machine context is just the saved stack pointer (sp_fiber_ctx); sp_ctx_swap saves the callee-saved registers onto the coroutine's own stack and switches sp, and sp_ctx_make lays out a fresh stack so the first swap rets into the trampoline. It's written as file-scope inline asm for x86_64 and aarch64 (so it lands in .text -- W^X-safe, unlike libco's executable data blob), does no syscall (unlike swapcontext's sigprocmask, so it's faster), and other architectures fall back to ucontext. Fiber stacks are mmap'd MAP_STACK where available (OpenBSD requires it). No <ucontext.h> on the asm path, so OpenBSD compiles.

x86_64 validated locally: optcarrot checksum 59662 (its PPU is a Fiber-heavy coroutine) at fps 721 -- slightly faster than the swapcontext build, as expected from dropping the per-switch sigprocmask -- plus all fiber/thread tests under SPINEL_GC_STRESS and the full gate. aarch64 is validated by the macOS CI runner. Fiber#transfer to the root fiber was already a pre-existing segfault on the ucontext code and is unchanged here.

Refs the OpenBSD build request.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
1c3f7fb151e6
Parent
00cc658fd7d3

lib/sp_fiber.cmodifiedView file

@@ -7,6 +7,74 @@
77 #include <string.h>
88 #include <sys/mman.h>
99
10+/* OpenBSD requires the stack a coroutine runs on to be MAP_STACK, else the
11+ first push faults. Linux defines MAP_STACK as a (currently) no-op; keep it
12+ portable. */
13+#ifndef MAP_STACK
14+#define MAP_STACK 0
15+#endif
16+
17+/* ---- Portable coroutine context switch (replaces swapcontext) ----
18+ sp_ctx_swap saves the callee-saved registers onto the *current* stack, stows
19+ the resulting stack pointer in *from, loads *to's stack pointer, and pops the
20+ callee-saved registers (the very first switch into a coroutine pops the zeroed
21+ slots sp_ctx_make laid out and `ret`s into the trampoline). It lives in .text
22+ (W^X-safe) and issues no syscall. See sp_fiber_ctx.h for sp_ctx_make and the
23+ stack layout it must mirror. */
24+#if SP_FIBER_ASM
25+#if defined(__APPLE__)
26+ #define SP_CTX_SYM "_sp_ctx_swap"
27+#else
28+ #define SP_CTX_SYM "sp_ctx_swap"
29+#endif
30+#if defined(__x86_64__)
31+__asm__(
32+ ".text\n"
33+ ".globl " SP_CTX_SYM "\n"
34+ SP_CTX_SYM ":\n"
35+ " pushq %rbp\n pushq %rbx\n pushq %r12\n pushq %r13\n pushq %r14\n pushq %r15\n"
36+ " movq %rsp, (%rdi)\n" /* from->sp = rsp */
37+ " movq (%rsi), %rsp\n" /* rsp = to->sp */
38+ " popq %r15\n popq %r14\n popq %r13\n popq %r12\n popq %rbx\n popq %rbp\n"
39+ " ret\n"
40+);
41+#elif defined(__aarch64__)
42+__asm__(
43+ ".text\n"
44+ ".globl " SP_CTX_SYM "\n"
45+ SP_CTX_SYM ":\n"
46+ /* save x19..x30 (12*8) + d8..d15 (8*8) = 160 bytes onto the current stack */
47+ " stp x19, x20, [sp, #-160]!\n"
48+ " stp x21, x22, [sp, #16]\n"
49+ " stp x23, x24, [sp, #32]\n"
50+ " stp x25, x26, [sp, #48]\n"
51+ " stp x27, x28, [sp, #64]\n"
52+ " stp x29, x30, [sp, #80]\n"
53+ " stp d8, d9, [sp, #96]\n"
54+ " stp d10, d11, [sp, #112]\n"
55+ " stp d12, d13, [sp, #128]\n"
56+ " stp d14, d15, [sp, #144]\n"
57+ " mov x2, sp\n"
58+ " str x2, [x0]\n" /* from->sp = sp */
59+ " ldr x2, [x1]\n" /* sp = to->sp */
60+ " mov sp, x2\n"
61+ " ldp d14, d15, [sp, #144]\n"
62+ " ldp d12, d13, [sp, #128]\n"
63+ " ldp d10, d11, [sp, #112]\n"
64+ " ldp d8, d9, [sp, #96]\n"
65+ " ldp x29, x30, [sp, #80]\n"
66+ " ldp x27, x28, [sp, #64]\n"
67+ " ldp x25, x26, [sp, #48]\n"
68+ " ldp x23, x24, [sp, #32]\n"
69+ " ldp x21, x22, [sp, #16]\n"
70+ " ldp x19, x20, [sp], #160\n"
71+ " ret\n"
72+);
73+#endif
74+#else /* ucontext fallback */
75+void sp_ctx_swap(sp_fiber_ctx *from, sp_fiber_ctx *to) { swapcontext(&from->uc, &to->uc); }
76+#endif
77+
1078 /* ---- Reached by name in the generated TU ---- */
1179 void *sp_gc_alloc(size_t sz, void (*fin)(void *), void (*scn)(void *));
1280 void sp_raise_cls(const char *cls, const char *msg);
@@ -68,13 +136,13 @@
68136 static void sp_fiber_install_gc_hook(void){if(!sp_gc_mark_suspended_fibers_hook)sp_gc_mark_suspended_fibers_hook=sp_mark_suspended_fibers;}
69137 static void sp_Fiber_fin(void*p){sp_Fiber*f=(sp_Fiber*)p;if(f->stack)munmap(f->stack,SP_FIBER_STACK_SIZE);if(f->saved_roots)free(f->saved_roots);sp_fiber_list_remove(f);}
70138 static void sp_Fiber_scan(void*p){sp_Fiber*f=(sp_Fiber*)p;if(f->user_data)sp_gc_mark(f->user_data);if(f->storage)sp_gc_mark(f->storage);}
71-sp_Fiber*sp_Fiber_new(void(*body)(sp_Fiber*)){sp_Fiber*f=(sp_Fiber*)sp_gc_alloc(sizeof(sp_Fiber),sp_Fiber_fin,sp_Fiber_scan);f->stack=(char*)mmap(NULL,SP_FIBER_STACK_SIZE,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,-1,0);if(f->stack==MAP_FAILED){f->stack=NULL;sp_raise_cls("FiberError","failed to allocate fiber stack");}f->state=0;f->transferred=0;f->body=body;f->yielded_value=sp_box_nil();f->resumed_value=sp_box_nil();f->user_data=NULL;f->saved_exc_top=0;f->saved_catch_top=0;f->storage=NULL;f->saved_roots=NULL;f->saved_nroots=0;f->saved_roots_cap=0;f->fiber_next=NULL;f->fiber_prev=NULL;sp_fiber_list_add(f);sp_fiber_install_gc_hook();if(sp_fiber_current&&sp_fiber_current->storage){sp_Fiber*volatile _froot=f;int _pushed=0;if(sp_gc_nroots<SP_GC_STACK_MAX){sp_gc_roots[sp_gc_nroots++]=(void**)&_froot;_pushed=1;}f->storage=sp_FiberStore_dup((sp_FiberStore*)sp_fiber_current->storage);if(_pushed)sp_gc_nroots--;}return f;}
139+sp_Fiber*sp_Fiber_new(void(*body)(sp_Fiber*)){sp_Fiber*f=(sp_Fiber*)sp_gc_alloc(sizeof(sp_Fiber),sp_Fiber_fin,sp_Fiber_scan);f->stack=(char*)mmap(NULL,SP_FIBER_STACK_SIZE,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK,-1,0);if(f->stack==MAP_FAILED){f->stack=NULL;sp_raise_cls("FiberError","failed to allocate fiber stack");}f->state=0;f->transferred=0;f->body=body;f->yielded_value=sp_box_nil();f->resumed_value=sp_box_nil();f->user_data=NULL;f->saved_exc_top=0;f->saved_catch_top=0;f->storage=NULL;f->saved_roots=NULL;f->saved_nroots=0;f->saved_roots_cap=0;f->fiber_next=NULL;f->fiber_prev=NULL;sp_fiber_list_add(f);sp_fiber_install_gc_hook();if(sp_fiber_current&&sp_fiber_current->storage){sp_Fiber*volatile _froot=f;int _pushed=0;if(sp_gc_nroots<SP_GC_STACK_MAX){sp_gc_roots[sp_gc_nroots++]=(void**)&_froot;_pushed=1;}f->storage=sp_FiberStore_dup((sp_FiberStore*)sp_fiber_current->storage);if(_pushed)sp_gc_nroots--;}return f;}
72140 sp_RbVal sp_Fiber_storage_get(sp_Fiber*f,sp_sym k){if(!f->storage)return sp_box_nil();return sp_FiberStore_get((sp_FiberStore*)f->storage,k);}
73141 void sp_Fiber_storage_set(sp_Fiber*f,sp_sym k,sp_RbVal v){if(!f->storage)f->storage=sp_FiberStore_new();sp_FiberStore_set((sp_FiberStore*)f->storage,k,v);}
74-static void sp_fiber_trampoline(void){sp_Fiber*f=sp_fiber_current;f->body(f);f->state=3;if(f->transferred){sp_fiber_current=&sp_fiber_root;setcontext(&sp_fiber_root.ctx);}else{swapcontext(&f->ctx,&f->caller_ctx);}}
75-sp_RbVal sp_Fiber_resume(sp_Fiber*f,sp_RbVal val){if(f->state==3){sp_raise_cls("FiberError","attempt to resume a terminated fiber");}f->resumed_value=val;sp_Fiber*prev=sp_fiber_current;sp_fiber_save_roots(prev);sp_fiber_restore_roots(f);sp_fiber_current=f;if(f->state==0){f->state=1;getcontext(&f->ctx);f->ctx.uc_stack.ss_sp=f->stack;f->ctx.uc_stack.ss_size=SP_FIBER_STACK_SIZE;f->ctx.uc_link=&f->caller_ctx;makecontext(&f->ctx,(void(*)(void))sp_fiber_trampoline,0);swapcontext(&f->caller_ctx,&f->ctx);}else{f->state=1;swapcontext(&f->caller_ctx,&f->ctx);}sp_fiber_save_roots(f);sp_fiber_restore_roots(prev);sp_fiber_current=prev;return f->yielded_value;}
76-sp_RbVal sp_Fiber_yield(sp_RbVal val){sp_Fiber*f=sp_fiber_current;f->yielded_value=val;f->state=2;swapcontext(&f->ctx,&f->caller_ctx);return f->resumed_value;}
142+static void sp_fiber_trampoline(void){sp_Fiber*f=sp_fiber_current;f->body(f);f->state=3;if(f->transferred){sp_fiber_current=&sp_fiber_root;sp_ctx_swap(&f->ctx,&sp_fiber_root.ctx);}else{sp_ctx_swap(&f->ctx,&f->caller_ctx);}}
143+sp_RbVal sp_Fiber_resume(sp_Fiber*f,sp_RbVal val){if(f->state==3){sp_raise_cls("FiberError","attempt to resume a terminated fiber");}f->resumed_value=val;sp_Fiber*prev=sp_fiber_current;sp_fiber_save_roots(prev);sp_fiber_restore_roots(f);sp_fiber_current=f;if(f->state==0){f->state=1;sp_ctx_make(&f->ctx,f->stack,SP_FIBER_STACK_SIZE,sp_fiber_trampoline);sp_ctx_swap(&f->caller_ctx,&f->ctx);}else{f->state=1;sp_ctx_swap(&f->caller_ctx,&f->ctx);}sp_fiber_save_roots(f);sp_fiber_restore_roots(prev);sp_fiber_current=prev;return f->yielded_value;}
144+sp_RbVal sp_Fiber_yield(sp_RbVal val){sp_Fiber*f=sp_fiber_current;f->yielded_value=val;f->state=2;sp_ctx_swap(&f->ctx,&f->caller_ctx);return f->resumed_value;}
77145 mrb_bool sp_Fiber_alive(sp_Fiber*f){return f->state!=3;}
78-sp_RbVal sp_Fiber_transfer(sp_Fiber*f,sp_RbVal val){f->resumed_value=val;sp_Fiber*prev=sp_fiber_current;sp_fiber_save_roots(prev);sp_fiber_restore_roots(f);sp_fiber_current=f;if(f->state==0){f->state=1;f->transferred=1;getcontext(&f->ctx);f->ctx.uc_stack.ss_sp=f->stack;f->ctx.uc_stack.ss_size=SP_FIBER_STACK_SIZE;f->ctx.uc_link=&prev->ctx;makecontext(&f->ctx,(void(*)(void))sp_fiber_trampoline,0);swapcontext(&prev->ctx,&f->ctx);}else{f->state=1;swapcontext(&prev->ctx,&f->ctx);}sp_fiber_save_roots(f);sp_fiber_restore_roots(prev);sp_fiber_current=prev;return prev->resumed_value;}
146+sp_RbVal sp_Fiber_transfer(sp_Fiber*f,sp_RbVal val){f->resumed_value=val;sp_Fiber*prev=sp_fiber_current;sp_fiber_save_roots(prev);sp_fiber_restore_roots(f);sp_fiber_current=f;if(f->state==0){f->state=1;f->transferred=1;sp_ctx_make(&f->ctx,f->stack,SP_FIBER_STACK_SIZE,sp_fiber_trampoline);sp_ctx_swap(&prev->ctx,&f->ctx);}else{f->state=1;sp_ctx_swap(&prev->ctx,&f->ctx);}sp_fiber_save_roots(f);sp_fiber_restore_roots(prev);sp_fiber_current=prev;return prev->resumed_value;}
79147
80148 void sp_mark_fiber_root_storage(void){if(sp_fiber_root.storage)sp_gc_mark(sp_fiber_root.storage);}