Repository navigation
Copy a cppia argument's default slots in and out of the script frame - #496
Merged
Merged
Conversation
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
force-pushed
the
fix/cppia-default-arg-frames
branch
from
November 27, 2026 22:06
cac0ba5 to
7581a49
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 itsChanges.mdline is underlibhxcpp 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,argToDoubleandargToString(CppiaFunction.cpp), andobjectToDoublePointerandobjectToStringPointernext to them, dereferencectx->pointerin place. None of them has a caller anywhere in the repository, and nor do the rest of that block (objectToInt,frameToDouble,objectToDouble,objectToStringand the three*ToObject).CppiaVars.cpphas its own staticintToObjectand the others, which already useJitLoadandJitStore. 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 fromrunFunctionandrunFunctionClosurewhen 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.setDefault. It compiles defaults into the function (genDefault), as sljit moves and a call toobjToFloat, which already stores throughJitStore. The print never fired under the JIT.The change
setDefaultreads and writes the slots withCppiaCtx::getSlotandsetSlot, and a Float withSetFloatAligned, assetInFramenext to it does.JitLoadand 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.CppiaFunction.cppare 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 noemscripten-skip.txtline.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/cppiainterpreted 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.cppon 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::markClosureandvisitClosure(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.