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
|
3 tasks
shai-almog
added a commit
that referenced
this pull request
May 23, 2026
Follow-up to #4985. The PUTFIELD fix there was correct but only addressed half of the chained-assignment NPE. Repro (ddyer's original program): private double boardScaleIndex, newBoardScaleIndex; boardScaleIndex = newBoardScaleIndex = Math.min(n, (int) expr); javac emits PUSH_INT (Math.min result), I2D, DUP2_X1, two PUTFIELDs. The I2D / I2L / F2D / F2L / L2D macros only wrote SP[-1].data.* and left the runtime type tag untouched. BC_DUP2_X1 / BC_DUP2_X2 / BC_DUP_X2 dispatch via IS_DOUBLE_WORD(...) on that tag, so an I2D-produced double whose tag still reads CN1_TYPE_INT sent the dup down the cat-1 branch: - SP bumped by +2 instead of +1 - three slots reshuffled as if all single-word - first putfield happened to read the right operands (lucky) - second putfield read the double's IEEE-754 bit pattern as the object reference and crashed with NPE on iOS Reproduced end-to-end with a standalone C test that mirrors the BC_* macros: post-fix the second putfield's target resolves to the expected this pointer; pre-fix it resolved to 0x4028000000000000 (= bits of 12.0). Each cat-changing conversion now rewrites SP[-1].type to the new CN1_TYPE_*. The narrowing inverses (L2I/L2F, D2I/D2F) and the pure cat-1 conversion I2F do the same for symmetry — DUP2/DUP_X2 path can hit them in less obvious bytecode sequences. I2B/I2C/I2S already preserve CN1_TYPE_INT, so they're left alone. Regression test conversionOpcodesUpdateRuntimeTypeTag asserts each of the 12 conversion opcodes emits the corresponding SP[-1].type = CN1_TYPE_* write. Full BytecodeInstructionIntegrationTest (72 tests) passes; verified the fail-on-pre-fix-code shape too. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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