[CM] segfault: call site reuses fx annotations for a closure with a different body
Massimiliano Gubinelli
m.gubinelli at gmail.com
Sun Oct 4 14:53:42 PDT 2026
Hi Bill,
In s7 11.9 (21-Sep-2026), and still in the 5-Oct-2026 tarball, the
following segfaults on the second call of f:
(define-macro (my-define head . body)
`(define ,(car head) (lambda ,(cdr head) , at body)))
(define (g l) (reverse (cdr (reverse l))))
(define (f t)
(my-define (pairs l)
(if (or (null? l) (null? (cdr l))) ()
(cons (cons (car l) (cadr l)) (pairs (cddr l)))))
(pairs (g (cdr t))))
(display (f '(with a 1 b 2 x))) ; ((a . 1) (b . 2))
(display (f '(with a 1 x))) ; segfault
$ gcc -O2 -DWITH_MAIN s7.c -o s7 -lm -ldl && ./s7 bug.scm
load bug.scm
Segmentation fault
With -DS7_DEBUGGING=1 it stops with:
op_safe_closure_p_a_1[...]: ((if (or (null? l) (null? (cdr l))) () (cons
(cons ...) (pairs (cddr l))))) opt2: 0x...->0x0 wants opt2_fx, debugger
bits are #x0 but expects #x40000
All of these are needed: the macro builds the lambda form (with the lambda
written in the source there is no crash); the local function is recursive;
its argument at the call site is a call of a Scheme function (g; with
reverse, cdr or (cdr (cdr t)) there is no crash); and f is called twice.
What I think happens: on the first call of f, (pairs (g (cdr t))) is
optimized as OP_SAFE_CLOSURE_P_A. optimize_closure_one_arg (or the fixup in
eval) records the closure in opt1_lambda and runs fx_annotate_arg on that
closure's body. Because my-define is a run-time macro, the second call of f
makes pairs from a new (lambda (l) ...) list. closure_is_fine_1 accepts the
new closure, since it is a safe closure of the same arity, and stores it
with set_opt1_lambda. op_safe_closure_p_a_1 then does fx_call on the new
body, which was never annotated. An ordinary internal define remakes the
closure from the same source list each time, so its body is the one that
was annotated, and the problem never shows.
We hit this in TeXmacs, where define in the user environment was a macro
for Guile-style curried definitions (it expands to
(define f (lambda args . body))). We no longer rebind define, which also
avoids re-expanding every internal definition at each call (see the PS).
But any run-time macro that builds a lambda and binds it to a name used at
a call site outside the macro can hit it.
The patch below makes closure_is_ok_1, closure_is_fine_1 and
closure_star_is_fine_1 also require the same body
(closure_body(old) == closure_body(clo)), so a call site gives up its
annotated fast path when the code is different. With it, the example and
our TeXmacs test suites run. s7test.scm gives the same output with and
without the patch. A loop calling an f written with a plain internal
define runs at the same speed. closure_np_is_ok_1 also swaps closures
without looking at the body. I haven't checked whether the ops behind it
rely on body annotations. You may prefer to annotate the new body instead
of dropping the fast path.
PS: for the curried definitions we now patch check_define instead. It
rewrites (define ((f a) b) . body) in place into
(define (f a) (lambda (b) . body)), to any depth and with rest arguments,
the first time it checks the form. This is Guile's define, and SRFI 219.
s7test.scm gives the same results with it. If you would like it in s7,
the 10-line patch is attached.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: curried-define.patch
Type: application/octet-stream
Size: 1191 bytes
Desc: not available
URL: <https://cm-mail.stanford.edu/pipermail/cmdist/attachments/20261004/8c398138/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: closure-same-body.patch
Type: application/octet-stream
Size: 2044 bytes
Desc: not available
URL: <https://cm-mail.stanford.edu/pipermail/cmdist/attachments/20261004/8c398138/attachment-0001.obj>
-------------- next part --------------
sc->last_function = val;
More information about the Cmdist
mailing list