frontpage.
newsnewestaskshowjobs

Open Source @Github

fp.

Open in hackernews

Memory Safe Context Switching (longjmp, setjmp) in Fil-C

https://fil-c.org/context_switches
33•modeless•1h ago

Comments

brcmthrowaway•59m ago
Is Fil-C now using Claude for dev?
bitbasher•54m ago
Claude.md was added 8 months ago.

https://github.com/pizlonator/fil-c/blob/deluge/CLAUDE.md

pizlonator•19m ago
Claude only ever wrote some tests

I have used Kimi K2.6-code and GLM 5.2, but only for things that are easy to verify. I did not use any LLMs for the longjmp/ucontext work.

gruntled-worker•56m ago
No complaints about this in particular, but code that uses setjmp/longjmp often has a risk profile that's way bigger than memory safety alone. If you're stuck with them then by all means, mitigate all you can.
pizlonator•17m ago
What misuse are you imagining that isn’t a memory safety problem?

You might find that Fil-C prevents those too. It’s pretty strict. You can only use longjmp to pop stack like an exception would

lstodd•55m ago
longjmp, setjmp, setcontext, getcontext, makecontext, and swapcontext and whatever have no bearing on safety, memory or otherwise. What you have to deal with is what is represented by sigaction(2) and only and much later then by what you use to drive the context switch, be it io, or preemptive.
anitil•44m ago
The article mentions that you typically have to longjmp within the same function as setjump (or a descendant function) otherwise your stack gets cleared and you longjmp to a garbage stack. I believe this counts as memory safety? Though I don't quite understand your comment about sigaction, so maybe there's some context I'm missing.

Edit: The extra context- https://usenix.org/legacy/publications/library/proceedings/u...

pizlonator•23m ago
These functions can easily be misused to corrupt memory, so they very much have something to do with safety. Fil-C goes to great lengths to prevent your use of those functions leading to memory corruption or any violation of the capability model.

Fil-C also makes sigaction memory safe. That protection does allow for signal handlers to longjmp or setcontext or swapcontext

anitil•47m ago
How interesting! I thought that setjmp and longjmp were probably incompatible with Fil-C. And I'd somehow never heard of ucontext at all.

I suppose managing the stack is still managing memory after all, even if we typically don't think of it that way, so Fil-C has something to add here.

It's really worth reading the section here about the complexity of setjmp/longjmp and how they interact with register allocation and stack spilling. I knew they're tricky, but going in to the specifics is delicious.

nanolith•30m ago
> For example, Boost uses ucontext as part of its fiber implementation.

Maybe for the incredibly slow fallback, it does. Boost context and Boost fiber has ABI support for *nix / MacOS / Windows for x86_64 and ARM/ARM64. The overhead for a fiber switch using this support is about as heavy as a virtual function call. In comparison, ucontext is very heavy.

I wrote my own fiber library for C. I got the idea from an old implementation I saw that used setjmp and longjmp, which took me down the rabbit hole of figuring out how to do this more efficiently and with an improved margin of safety. I chose to follow Boost's example, and in fact, used some of their fiber switch assembler with attribution in my library.

pizlonator•7m ago
> In comparison, ucontext is very heavy

It's heavy because it switches the signal masks.

Indeed, Fil-C's ucontext logic does this today, because I'm relying on glibc, and that's what glibc does.

But it would be straightforward to teach the internal Fil-C zfiber_context API to not save the sigmasks. It would just mean using some other backend for setcontext/swapcontext. Considering that there are multiple open source projects (including Boost!) that have code that does this, it would be easy to set that up.

But I'm taking baby steps here. And the first step is just to provide a memory safe wrapper around these quite dangerous APIs. Probably the next step is to just write a lot more tests to try to break it. Then, later, I can worry about adding alternative backends to expose the sigmask-free version of this that Boost (and most others) want.

nanolith•1m ago
Fair enough. I use my fiber library for cooperative multitasking, as an alternative to async I/O. It's still non-blocking, but as far as user code knows, it behaves as if it is blocking.

To do this, I disable signals on threads that are fiber threads, and instead rely on a signal thread to intercept signals and alert the appropriate fibers.

matheusmoreira•23m ago
This is an article I wish I could have read many months ago.

> Hence, the most basic safety issue with setjmp is that if we call it and then return from the function that had called it, the context saved by setjmp is not valid to longjmp to.

> longjmp is only safe if it's called at a time when the stack frame used by setjmp could not have possibly been overwritten, since that is the only way to guarantee that the register state restored by longjmp matches the stack frame that the stack pointer points to.

That limitation could be lifted by simply copying the stack frames somewhere else prior to long jumping, and then spilling that entire thing on top of the current stack instead of just restoring the registers in from the jump buffer. This is how delimited continuations work! What ruins this for C is the existence of pointers. Stacks aren't freely relocatable since pointers into the stack could exist. Other languages don't have this problem.

So much fun stuff in this article! The "fibers with ucontext", essentially swapping stack pointers back and forth, are how I implemented generators! I too reached for musl source code in order to understand setjmp, but for a different reason: its ability to spill the registers onto the stack was instrumental for my garbage collector.

Blogged about all of these things too, in case anyone is curious:

https://www.matheusmoreira.com/articles/delimited-continuati...

https://www.matheusmoreira.com/articles/generators-in-lone-l...

https://www.matheusmoreira.com/articles/babys-second-garbage...

Onavo•1m ago
> What ruins this for C is the existence of pointers. Stacks aren't freely relocatable since pointers into the stack could exist. Other languages don't have this problem*

What about languages with pass by reference?

.self: A new top-level domain designed to support self-hosting

https://hccf.onmy.cloud/2026/06/21/reclaiming-our-digital-selves-hccfs-vision-for-a-human-centere...
352•HumanCCF•6h ago•199 comments

Qwen 3.6 27B is the sweet spot for local development

https://quesma.com/blog/qwen-36-is-awesome/
630•stared•9h ago•522 comments

Free the Icons

https://weblog.rogueamoeba.com/2026/06/26/free-the-icons/
236•zdw•2d ago•62 comments

Memory Safe Context Switching (longjmp, setjmp) in Fil-C

https://fil-c.org/context_switches
33•modeless•1h ago•14 comments

Exploring PDP-1 Lisp (1960)

https://obsolescence.dev/pdp1-lisp-introduction.html
17•ozymandiax•1h ago•12 comments

LongCat-2.0, a large-scale MoE model with 1.6T total and 48B Active

https://longcat.chat/blog/longcat-2.0/
29•benjiro29•2h ago•6 comments

Why Won't Europe Build AI Data Centers in Iceland?

https://mrkt30.com/why-wont-europe-build-ai-data-centers-in-iceland/
12•type0•1h ago•6 comments

Rocketlab acquires Iridium

https://investors.rocketlabcorp.com/news-releases/news-release-details/rocket-lab-acquire-iridium...
374•everfrustrated•12h ago•231 comments

Scientists find molecular-level evidence for two structures in liquid water

https://phys.org/news/2026-06-scientists-molecular-evidence-liquid.html
67•wglb•4h ago•22 comments

Ornith-1.0: self-improving open-source models for agentic coding

https://github.com/deepreinforce-ai/Ornith-1
161•danboarder•9h ago•32 comments

A native graphical shell for SSH

https://probablymarcus.com/blocks/2026/06/28/native-graphical-shell-for-SSH.html
254•mrcslws•10h ago•117 comments

US Supreme Court rules geofence warrants require constitutional protections

https://www.theguardian.com/us-news/2026/jun/29/supreme-court-geofence-warrants-case-decision
458•cdrnsf•10h ago•215 comments

30-year sentence for transporting zines is a five-alarm fire for free speech

https://theintercept.com/2026/06/26/daniel-sanchez-estrada-zines-prairieland-free-speech/
341•xrd•1d ago•194 comments

South Korea to spend $1T on more memory chip production and humanoid robots

https://arstechnica.com/ai/2026/06/south-korea-to-spend-1t-on-more-memory-chip-production-and-hum...
132•jnord•4h ago•79 comments

Apple Neural Engine: Architecture, Programming, and Performance

https://arxiv.org/abs/2606.22283
128•Jimmc414•2d ago•18 comments

WATaBoy: JIT-Ing Game Boy Instructions to WASM Beats a Native Interpreter

https://humphri.es/blog/WATaBoy/
179•energeticbark•11h ago•29 comments

One million passports leaked online

https://cambridgeanalytica.org/data-breaches-scandals/passports-driver-licenses-exposed-public-in...
153•jruohonen•1d ago•87 comments

Wallace the 6 inch f/2.8 telescope, building it, and hiking with it

https://lucassifoni.info/blog/hiking-with-wallace/
117•chantepierre•3d ago•20 comments

Kb – Prolog Knowledge Base

https://github.com/mat-mgm/kb-prolog
29•triska•2d ago•4 comments

SQLite improving performance with pre-sort

https://andersmurphy.com/2026/06/07/sqlite-improving-performance-with-pre-sort.html
32•tosh•3d ago•3 comments

Dark Sky Lighting

https://www.savingourstars.org/darkskylighting#whatisdarkskylighting
165•alexandrehtrb•4d ago•30 comments

Open Memory Protocol – One Memory Store for Claude, ChatGPT, Curso

https://github.com/SMJAI/open-memory-protocol
10•soji_mathew•2h ago•5 comments

Netflix Simplified Batch Compute with Kueue

https://netflixtechblog.com/how-netflix-simplified-batch-compute-with-kueue-87860682629c
16•dalvrosa•2d ago•2 comments

What happens when you run a CUDA kernel?

https://fergusfinn.com/blog/what-happens-when-you-run-a-gpu-kernel/
214•mezark•13h ago•28 comments

Philae's extraordinary comet landing relived (2024)

https://www.esa.int/Science_Exploration/Space_Science/Rosetta/Philae_s_extraordinary_comet_landin...
5•1970-01-01•5d ago•0 comments

Micro-Agent: Beat Frontier Models with Collaboration Inside Model API

https://vllm.ai/blog/2026-06-29-micro-agent-frontier-models
55•matt_d•8h ago•18 comments

Working With AI: A concrete example

https://htmx.org/essays/working-with-ai/
95•comma_at•11h ago•33 comments

Sandia National Labs SA3000 8085 CPU

https://www.cpushack.com/2026/06/03/sandia-national-labs-sa3000-8085-cpu/
164•rbanffy•16h ago•40 comments

What can you confidently guarantee about your software?

https://queue.acm.org/detail.cfm?id=3819084
98•eatonphil•12h ago•45 comments

Ornith-1.0: Self-scaffolding LLMs for agentic coding

https://deep-reinforce.com/ornith_1_0.html
58•kordlessagain•1d ago•7 comments