Skip to content

fix(values): un-box memory-allocated enum payload in Value::to_ptr#1650

Merged
TomerStarkware merged 1 commit into
mainfrom
tomer/fix-nested-enum-to-ptr-unbox
Jul 22, 2026
Merged

fix(values): un-box memory-allocated enum payload in Value::to_ptr#1650
TomerStarkware merged 1 commit into
mainfrom
tomer/fix-nested-enum-to-ptr-unbox

Conversation

@TomerStarkware

@TomerStarkware TomerStarkware commented Jul 19, 2026

Copy link
Copy Markdown
Collaborator

What

The Enum arm of Value::to_ptr (src/values.rs) 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, which is_memory_allocated marks 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 Struct arm a few lines above already handles this by un-boxing memory-allocated members; this change makes the Enum arm do the same.

Fix

let payload_type_id = &info.variants[*tag];
let payload_ty = registry.get_type(payload_type_id)?;
let payload = value.to_ptr(arena, registry, payload_type_id)?;
// Undo the wrapper pointer added when the payload is memory
// allocated (e.g. a nested >=2-variant enum).
let payload = if payload_ty.is_memory_allocated(registry)? {
    *payload.cast::<NonNull<()>>().as_ref()
} else {
    payload
};

Reachability / scope

Only reachable through the generic JitNativeExecutor::invoke_dynamic(&[Value], …) API (used by cairo-native-run / cairo-native-test and library consumers passing structured Value arguments). The Starknet contract path (executor/contract.rs) marshals arguments and results as raw felts and never calls Value::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.

  • Before the fix: fails — for Outer::X(Inner::A(0x1234)) the VM returns 4660 while native returns garbage (859618275080168…).
  • After the fix: passes; full enum/result suite green (29/29).

🤖 Generated with Claude Code


This change is Reviewable

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>
@TomerStarkware
TomerStarkware requested a review from orizi July 19, 2026 13:22
@github-actions

Copy link
Copy Markdown

Benchmarking results

Benchmark for program dict_insert

Open benchmarks
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

@github-actions

Copy link
Copy Markdown

Benchmark results Main vs HEAD.

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_insert.cairo (JIT) 1.330 ± 0.047 1.282 1.416 1.02 ± 0.05
base dict_insert.cairo (AOT) 1.310 ± 0.042 1.272 1.377 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_insert.cairo (JIT) 1.790 ± 0.011 1.776 1.814 1.02 ± 0.01
head dict_insert.cairo (AOT) 1.763 ± 0.008 1.752 1.780 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_snapshot.cairo (JIT) 1.138 ± 0.005 1.130 1.146 1.01 ± 0.01
base dict_snapshot.cairo (AOT) 1.122 ± 0.009 1.111 1.135 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_snapshot.cairo (JIT) 1.651 ± 0.037 1.621 1.721 1.03 ± 0.02
head dict_snapshot.cairo (AOT) 1.606 ± 0.007 1.597 1.620 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base factorial_2M.cairo (JIT) 1.244 ± 0.048 1.185 1.313 1.06 ± 0.04
base factorial_2M.cairo (AOT) 1.172 ± 0.017 1.154 1.213 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head factorial_2M.cairo (JIT) 1.678 ± 0.013 1.659 1.699 1.01 ± 0.01
head factorial_2M.cairo (AOT) 1.662 ± 0.015 1.642 1.692 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base fib_2M.cairo (JIT) 1.140 ± 0.010 1.127 1.155 1.00
base fib_2M.cairo (AOT) 1.165 ± 0.047 1.112 1.256 1.02 ± 0.04

Head

Command Mean [s] Min [s] Max [s] Relative
head fib_2M.cairo (JIT) 1.629 ± 0.006 1.619 1.639 1.01 ± 0.01
head fib_2M.cairo (AOT) 1.613 ± 0.006 1.605 1.620 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base linear_search.cairo (JIT) 1.244 ± 0.050 1.207 1.342 1.05 ± 0.07
base linear_search.cairo (AOT) 1.180 ± 0.058 1.131 1.275 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head linear_search.cairo (JIT) 1.643 ± 0.011 1.626 1.664 1.02 ± 0.01
head linear_search.cairo (AOT) 1.617 ± 0.012 1.597 1.631 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base logistic_map.cairo (JIT) 1.156 ± 0.040 1.128 1.262 1.00 ± 0.05
base logistic_map.cairo (AOT) 1.155 ± 0.042 1.125 1.238 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head logistic_map.cairo (JIT) 1.629 ± 0.013 1.615 1.651 1.02 ± 0.01
head logistic_map.cairo (AOT) 1.600 ± 0.009 1.586 1.613 1.00

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

@orizi reviewed 3 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

@TomerStarkware
TomerStarkware added this pull request to the merge queue Jul 22, 2026
Merged via the queue into main with commit c22527c Jul 22, 2026
15 checks passed
@TomerStarkware
TomerStarkware deleted the tomer/fix-nested-enum-to-ptr-unbox branch July 22, 2026 14:26
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.

2 participants