fix(values): un-box memory-allocated enum payload in Value::to_ptr#1650
Merged
Conversation
The `Enum` arm of `Value::to_ptr` copied the payload straight from the pointer returned by the payload's own `to_ptr`. When the payload is a memory-allocated type (e.g. a nested >=2-variant enum), that pointer is a wrapper (a pointer to the data pointer), so the copy read the wrapper pointer bytes plus adjacent arena garbage into the variant slot instead of the actual payload — silently corrupting nested enums with no crash. Un-box the payload when it is memory-allocated, mirroring the existing `Struct` arm. Only reachable via the `invoke_dynamic(&[Value])` API; the Starknet contract path marshals felts directly and is unaffected. Adds a VM-vs-native regression test (`nested_enum_argument_matches_vm`) covering all four variant combinations of a nested 2-variant enum argument. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Benchmarking resultsBenchmark for program
|
| Command | Mean [s] | Min [s] | Max [s] | Relative |
|---|---|---|---|---|
Cairo-vm (Rust, Cairo 1) |
10.674 ± 0.113 | 10.495 | 10.894 | 5.23 ± 0.09 |
cairo-native (embedded AOT) |
2.042 ± 0.029 | 1.993 | 2.080 | 1.00 |
cairo-native (embedded JIT using LLVM's ORC Engine) |
2.059 ± 0.028 | 2.019 | 2.114 | 1.01 ± 0.02 |
Benchmark for program dict_snapshot
Open benchmarks
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
Cairo-vm (Rust, Cairo 1) |
591.5 ± 9.8 | 577.1 | 605.3 | 1.00 |
cairo-native (embedded AOT) |
1794.6 ± 25.0 | 1763.5 | 1827.8 | 3.03 ± 0.07 |
cairo-native (embedded JIT using LLVM's ORC Engine) |
1816.6 ± 14.9 | 1790.9 | 1842.3 | 3.07 ± 0.06 |
Benchmark for program factorial_2M
Open benchmarks
| Command | Mean [s] | Min [s] | Max [s] | Relative |
|---|---|---|---|---|
Cairo-vm (Rust, Cairo 1) |
4.621 ± 0.056 | 4.562 | 4.769 | 2.46 ± 0.04 |
cairo-native (embedded AOT) |
1.881 ± 0.020 | 1.850 | 1.903 | 1.00 |
cairo-native (embedded JIT using LLVM's ORC Engine) |
1.891 ± 0.028 | 1.855 | 1.938 | 1.01 ± 0.02 |
Benchmark for program fib_2M
Open benchmarks
| Command | Mean [s] | Min [s] | Max [s] | Relative |
|---|---|---|---|---|
Cairo-vm (Rust, Cairo 1) |
4.527 ± 0.022 | 4.491 | 4.566 | 2.48 ± 0.03 |
cairo-native (embedded AOT) |
1.828 ± 0.024 | 1.797 | 1.868 | 1.00 |
cairo-native (embedded JIT using LLVM's ORC Engine) |
1.859 ± 0.022 | 1.817 | 1.885 | 1.02 ± 0.02 |
Benchmark for program linear_search
Open benchmarks
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
Cairo-vm (Rust, Cairo 1) |
633.1 ± 7.4 | 622.4 | 644.0 | 1.00 |
cairo-native (embedded AOT) |
1830.8 ± 15.4 | 1803.8 | 1849.7 | 2.89 ± 0.04 |
cairo-native (embedded JIT using LLVM's ORC Engine) |
1843.1 ± 21.4 | 1817.0 | 1880.1 | 2.91 ± 0.05 |
Benchmark for program logistic_map
Open benchmarks
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
Cairo-vm (Rust, Cairo 1) |
537.6 ± 8.5 | 526.9 | 555.8 | 1.00 |
cairo-native (embedded AOT) |
1805.4 ± 22.0 | 1778.6 | 1855.7 | 3.36 ± 0.07 |
cairo-native (embedded JIT using LLVM's ORC Engine) |
1843.8 ± 16.5 | 1817.3 | 1869.4 | 3.43 ± 0.06 |
Benchmark results Main vs HEAD.Base
Head
Base
Head
Base
Head
Base
Head
Base
Head
Base
Head
|
orizi
approved these changes
Jul 19, 2026
orizi
left a comment
Collaborator
There was a problem hiding this comment.
@orizi reviewed 3 files and all commit messages, and made 1 comment.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).
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.
What
The
Enumarm ofValue::to_ptr(src/values.rs) copied the payload straight from the pointer returned by the payload's ownto_ptr. When the payload is a memory-allocated type — e.g. a nested>=2-variant enum, whichis_memory_allocatedmarks true — that pointer is a wrapper (a pointer to the data pointer). The copy therefore read the 8-byte wrapper pointer plus adjacent bump-arena garbage into the parent variant slot, instead of the actual payload data.Because a 2-variant enum accepts any tag bit pattern, the corrupted value never trips an "invalid tag" assert: execution completes and returns a wrong result with no crash and no signal — a silent correctness bug.
The sibling
Structarm a few lines above already handles this by un-boxing memory-allocated members; this change makes theEnumarm do the same.Fix
Reachability / scope
Only reachable through the generic
JitNativeExecutor::invoke_dynamic(&[Value], …)API (used bycairo-native-run/cairo-native-testand library consumers passing structuredValuearguments). The Starknet contract path (executor/contract.rs) marshals arguments and results as raw felts and never callsValue::to_ptr/Value::from_ptr, so sequencer/contract execution is unaffected.Test
Adds
nested_enum_argument_matches_vm(tests/tests/enums.rs) with a nested 2-variant-enum program (test_data/programs/nested_enum_arg.cairo), comparing native vs VM across all four(outer_tag, inner_tag)combinations.Outer::X(Inner::A(0x1234))the VM returns4660while native returns garbage (859618275080168…).🤖 Generated with Claude Code
This change is