Skip to content

Copy a cppia argument's default slots in and out of the script frame - #496

Merged
dimensionscape merged 3 commits into
mainfrom
fix/cppia-default-arg-frames
Nov 28, 2026
Merged

dimensionscape merged 3 commits into
mainfrom
fix/cppia-default-arg-frames

Conversation

@dimensionscape

@dimensionscape dimensionscape commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

For 6.0.3. A follow-up to #492, which fixed createEnum: this checks the other places a peer review named as still reading cppia frame slots in place, and fixes the one that can see a misaligned address. It is rebased onto main at 150bdef, and its Changes.md line is under libhxcpp 6.0.3 (unreleased).

What I checked

A script frame keeps 4-byte alignment, and its first slot is the 8-byte this, so on 64-bit any slot after an odd number of 4-byte slots (an Int, a Bool) is at an odd multiple of 4.

  • argToInt, argToDouble and argToString (CppiaFunction.cpp), and objectToDoublePointer and objectToStringPointer next to them, dereference ctx->pointer in place. None of them has a caller anywhere in the repository, and nor do the rest of that block (objectToInt, frameToDouble, objectToDouble, objectToString and the three *ToObject). CppiaVars.cpp has its own static intToObject and the others, which already use JitLoad and JitStore. So they never see an address, aligned or not. The second commit removes them so nobody calls them later.
  • CppiaStackVar::setDefault (CppiaVars.cpp) is reachable and misaligned. It is the interpreter's code for an argument's default value, called from runFunction and runFunctionClosure when a function has no compiled code. It read the argument's String or object slot in place, and when the argument was missing wrote a Float or Dynamic default in place too. With a temporary print, every default in the new test, each after an Int, was at frame + 12, 4 modulo 8, for String, Dynamic, Null<Float>, Float, Int and Bool arguments.
  • The JIT does not call setDefault. It compiles defaults into the function (genDefault), as sljit moves and a call to objToFloat, which already stores through JitStore. The print never fired under the JIT.

The change

  • setDefault reads and writes the slots with CppiaCtx::getSlot and setSlot, and a Float with SetFloatAligned, as setInFrame next to it does. JitLoad and friends are for the JIT's helpers; this is interpreter code, so it uses the accessors the interpreter's other frame code has used since 7a78380.
  • The unused helpers in CppiaFunction.cpp are removed (second commit).

Tests

  • test/regression/CppiaDefaultArgFrames: functions with String, Dynamic, Null<Float>, Float, Int and Bool defaults after an Int, each called with and without the argument, interpreted and jitted. The script is the first section, so it needs no emscripten-skip.txt line.

Run on 64-bit Windows with MSVC and Haxe 4.3.7: the 75 regression tests that use cppia (each runs its script interpreted and with the JIT), and test/cppia interpreted and with -jit (56 assertions each). All pass.

x86 does not fault on these loads, so the new test passes on MSVC with and without the fix. The real check is the UBSan job in CI, which should report CppiaVars.cpp on main with this test and nothing with the fix. There is no UBSan on the machine this was written on.

Not run here: the rest of test/regression, test/std, the sanitizers, 32-bit and the other platforms.

Not fixed here

CppiaStackVar::markClosure and visitClosure (CppiaVars.cpp) mark a closure's captured String or object with *(String *)(inBase + capturePos). Captures are packed by size from offset 8, so a closure that captures an Int and then a String has the String at +12 in the closure object. I have not confirmed it at run time; it is only reached when a collection marks a live interpreted closure.

When the interpreter fills in an argument's default value, it read the
argument's String or object slot in the frame in place, and wrote a
Float or Dynamic default the same way. A frame keeps 4-byte alignment
and starts with the 8-byte this, so on 64-bit an argument after an Int,
as b in f(a:Int, ?b:String = "x"), sits at an odd multiple of 4:
undefined behaviour, and a fault on a CPU that requires alignment.
7a78380 moved the interpreter's other frame accesses to copying slots
in and out and missed this one.

CppiaDefaultArgFrames calls functions with String, Dynamic, Null<Float>,
Float, Int and Bool defaults after an Int, with and without the
argument. A temporary print in setDefault showed every one of those
slots at 4 modulo 8 on 64-bit Windows. setDefault now copies them with
getSlot, setSlot and SetFloatAligned, as setInFrame does. The JIT
compiles defaults into the function (genDefault) and never calls
setDefault, so it was not affected.

Tests: CppiaDefaultArgFrames
argToInt, argToDouble, argToString and the block from objectToInt to
doubleToObject in CppiaFunction.cpp have no callers. Five of them, the
three argTo helpers, objectToDoublePointer and objectToStringPointer,
read a frame slot in place, which 7a78380 moved every live helper away
from, so they go rather than wait to be called.
@dimensionscape
dimensionscape force-pushed the fix/cppia-default-arg-frames branch from cac0ba5 to 7581a49 Compare November 27, 2026 22:06
@dimensionscape
dimensionscape merged commit acc5a26 into main Nov 28, 2026
5 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant