Fix #3108: don't rely on C arg eval order in PUTFIELD/MULTIANEWARRAY#4985
Merged
Conversation
ParparVM emitted C of the form set_field_X(POP_DOUBLE(), POP_OBJ()) for primitive instance-field writes when the value/target operands could not be folded into literals, and alloc3DArray/alloc4DArray(td, POP_INT(), POP_INT(), ...) for MULTIANEWARRAY. C does not specify the order of function-argument evaluation, so clang on iOS evaluated the POPs right-to-left and: - PUTFIELD: popped the double's bits as an object reference, dereferenced garbage, and crashed with the NPE reported in #3108. Reproduced via chained assignment with widening conversion (a.x = a.y = (int)expr), which forces a dup2_x1 sequence that defeats the existing operand folding in Field.tryReduce. - MULTIANEWARRAY: silently swapped the array dimensions, so new int[a][b][c] could be allocated as if it were new int[c][b][a]. Both sites now use PEEK semantics with an explicit SP adjustment, mirror- ing the pattern already used by the object PUTFIELD path and the 2D MULTIANEWARRAY case. The MultiArray fix also corrects the (currently dead, but defensive) dims<actualDim arms so unspecified inner dimensions land in the right alloc?DArray slots, not just by luck of POP order. Regression tests cover both code paths: putfieldUnfoldedPathUsesPeekNotPop and multiArrayEmissionIsArgumentOrderSafe. Both assert the emitted C uses PEEK + SP-=N (or POP_MANY), and explicitly forbid POP_X(), POP_Y() chains. Verified each fails on the pre-fix code with the buggy emission visible in the failure diagnostic. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Collaborator
Author
|
Compared 20 screenshots: 20 matched. |
Contributor
Contributor
✅ ByteCodeTranslator Quality ReportTest & Coverage
Benchmark Results
Static Analysis
Generated automatically by the PR CI workflow. |
Collaborator
Author
|
Compared 110 screenshots: 110 matched. Benchmark Results
Build and Run Timing
Detailed Performance Metrics
|
Collaborator
Author
|
Compared 110 screenshots: 110 matched. Benchmark Results
Build and Run Timing
Detailed Performance Metrics
|
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.
Summary
ParparVM's bytecode-to-C translator emitted C code that relied on the order of evaluation of function arguments, which C leaves unspecified. Clang on iOS evaluates such arguments right-to-left, so two opcodes were silently miscompiled:
PUTFIELDwith a primitive value -- emittedset_field_X(POP_DOUBLE(), POP_OBJ())(orPOP_FLOAT/LONG/INTanalogues) when neither operand could be folded into a literal. With right-to-left evaluation,POP_OBJ()runs first, pops the double's raw bits, interprets them as an object pointer, and the subsequent field write dereferences garbage. This is the NPE reported in #3108. The trigger reported there --a.x = a.y = (int)expr-- produces bytecode withdup2_x1+ twoputfields, which is exactly the shape that defeatsField.tryReduceand so falls through to the buggy fallback.MULTIANEWARRAYfor 3D/4D arrays -- emittedalloc3DArray(td, POP_INT(), POP_INT(), POP_INT())(and the 4D equivalent). With right-to-left evaluation the dimensions silently swap:new int[a][b][c]allocates as if it werenew int[c][b][a]. (The 2D case already used the safeSP -= dims; ...(*(SP+1)).data.i, (*SP).data.ipattern.)Both sites now use PEEK semantics with an explicit
SP -= Nadjustment, mirroring the pattern already used by the object PUTFIELD path and the 2D MULTIANEWARRAY case. The MultiArray fix also corrects the (currently dead, but defensive)dims<actualDimarms so unspecified inner dimensions land in the rightalloc?DArrayslots, not just by luck of POP order.Files changed
vm/ByteCodeTranslator/src/com/codename1/tools/translator/bytecodes/Field.java-- primitive PUTFIELD fallback now usesPEEK_TYPE(1), PEEK_OBJ(2)+SP -= 2.vm/ByteCodeTranslator/src/com/codename1/tools/translator/bytecodes/MultiArray.java-- 3D and 4D MULTIANEWARRAY useSP -= dims; alloc?DArray(td, (*(SP+N)).data.i, ..., (*SP).data.i, ...).vm/tests/src/test/java/com/codename1/tools/translator/BytecodeInstructionIntegrationTest.java-- two new regression tests.Test plan
putfieldUnfoldedPathUsesPeekNotPop-- constructsFielddirectly for each primitive descriptor + the object case, asserts emitted C usesPEEK_*(1), PEEK_OBJ(2)+ explicit pop, and explicitly forbids the buggyPOP_X(), POP_OBJ()shape.multiArrayEmissionIsArgumentOrderSafe-- covers 2D/3D/4DMULTIANEWARRAY. Asserts the emitted C reads dimensions with PEEK after a singleSP -= N, and explicitly forbidsPOP_INT(), POP_INT()chains.set_field_MyClass_myField(POP_DOUBLE(), POP_OBJ());andalloc3DArray(threadStateData, POP_INT(), POP_INT(), POP_INT(), ...)).BytecodeInstructionIntegrationTestpass.boardScaleIndex = newBoardScaleIndex = Math.min(...)) on a physical iOS build, confirm no NPE.new int[a][b][c]on a physical iOS build and confirma.length == a,a[0].length == b,a[0][0].length == c(this was likely broken before too, just less visibly than the PUTFIELD crash).Refs: #3108
🤖 Generated with Claude Code