[CM] s7: string_ref_p_p0 called as p_pi in fx_c_ti_direct (traps in WebAssembly)

Massimiliano Gubinelli m.gubinelli at gmail.com
Sat Oct 3 18:42:13 PDT 2026


Hi,

We run s7 (11.9, 21-Sep-2026; the code is unchanged in the 2-Oct-2026
tarball) inside GNU TeXmacs compiled to WebAssembly with Emscripten 
(https://mgubi.github.io/texmacs/), and hit a "function signature mismatch” 
trap. The cause is a direct function
installed with one signature and called with another.

In the tree rewriting of fx_c_si_direct into fx_c_ti_direct (around line
61674 of the 2-Oct s7.c), the p_pi case does

    if ((opt3_direct(cdr(expr)) == (s7_pointer)string_ref_p_pi) && (integer(caddr(expr)) == 0))
      set_opt3_direct(cdr(expr), string_ref_p_p0);
    return(with_fx(tree, ... fx_c_ti_direct));

but string_ref_p_p0 is a p_pp function (sc, str, unused_index), while
fx_c_ti_direct calls opt3_direct as s7_p_pi_t (sc, p, s7_int). Natively the
extra s7_int is harmless, which is presumably why it never showed up; in
WebAssembly, call_indirect checks the exact signature and traps. The two
other places that install string_ref_p_p0 are p_pp contexts and are fine.

Reproducer (traps in a WebAssembly build of s7 run under node):

    (define (f a b) (string-ref a 0))
    (define (g x) (f x (+ 1 2)))
    (g "abc")

Natively, clang's -fsanitize=function also reports it:

    s7.c:58048:74: runtime error: call to function string_ref_p_p0 through
      pointer to incorrect function type 'struct s7_cell *(*)(struct s7_scheme *,
      struct s7_cell *, long long)'
        #0 fx_c_ti_direct s7.c:58048
        #1 fx_c_aa s7.c:59701
        #2 op_tc_or_a_and_a_la s7.c:92257

The attached patch just keeps string_ref_p_pi in that case. With it, s7test.scm
under clang 21 -fsanitize=function (with the block and libc tests, without
gsl) reports no mismatched call, and the reproducer runs in WebAssembly. It
might be worth adding the reproducer to s7test.scm, since nothing there has
that shape (a two-argument closure whose body is (string-ref <first arg> 0),
called with two expressions).

Thanks for s7!

Massimiliano Gubinelli

--- patch (against the 2-Oct-2026 s7.c) ---
--- a/s7.c
+++ b/s7.c
@@ -61671,8 +61671,7 @@
 	{
 	  if (opt3_direct(cdr(expr)) == (s7_pointer)vector_ref_p_pi)
 	    return(with_fx(tree, fx_vector_ref_tc));
-	  if ((opt3_direct(cdr(expr)) == (s7_pointer)string_ref_p_pi) && (integer(caddr(expr)) == 0))
-	    set_opt3_direct(cdr(expr), string_ref_p_p0);
+	  /* not string_ref_p_p0 here: it is p_pp, but fx_c_ti_direct calls opt3_direct as p_pi */
 	  return(with_fx(tree, (opt3_direct(cdr(expr)) == (s7_pointer)remainder_p_pi) ? fx_c_ti_remainder : fx_c_ti_direct));
 	}
       if (pfunc == fx_is_eq_sc)    return(with_fx(tree, fx_is_eq_tc));

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cm-mail.stanford.edu/pipermail/cmdist/attachments/20261004/382c97d6/attachment.html>


More information about the Cmdist mailing list