Skip to content

add run_with_libfunc_profile + AotWithProgram variant for ContractExecutor#1613

Open
avi-starkware wants to merge 2 commits into
avi/cairo_native/contract-executor-2from
avi/cairo_native/libfunc-profiling-2
Open

add run_with_libfunc_profile + AotWithProgram variant for ContractExecutor#1613
avi-starkware wants to merge 2 commits into
avi/cairo_native/contract-executor-2from
avi/cairo_native/libfunc-profiling-2

Conversation

@avi-starkware
Copy link
Copy Markdown
Collaborator

Summary

Exposes the libfunc-profiling primitives that downstream consumers (e.g. the blockifier in starkware-libs/sequencer) currently maintain locally. The profile-collection pattern is callback-driven so the per-call key (tx hash, etc.) stays out of cairo-native.

Replaces #1599 / #1609.

Changes

  • metadata::profiler::Profile is now pub (was a private type alias).
  • AotContractExecutor::run_with_libfunc_profile<H, F> (gated on with-libfunc-profiling, in new file src/executor/libfunc_profile.rs) wraps run: allocates a unique trace ID, points the executor's cairo_native__profiler__profile_id symbol at it, drains the resulting Profile after run returns, and hands it to a caller-supplied FnOnce(Profile). A ProfilerGuard restores the previous trace ID and drops the LIBFUNC_PROFILE slot on both the success and unwind paths.
  • ContractExecutor::AotWithProgram(AotWithProgram { executor, program }) is a new variant that bundles an AOT executor with the Sierra program it was built from. From<AotWithProgram> is provided.
  • ContractExecutor::run dispatches the new variant via run_with_libfunc_profile with a no-op profile callback.
  • ContractExecutor::run_with_profile<H, F> is the profile-capturing counterpart of run; for non-AotWithProgram variants it falls through to run (callback never fires).

Stack

  1. align sierra-emu StarknetSyscallHandler trait/types with cairo-native #1610 — trait alignment
  2. extract shared cairo-native-syscalls crate #1611 — extract shared crate
  3. add ContractExecutor dispatch enum (Aot + Emu) #1612ContractExecutor (supersedes add ContractExecutor dispatch enum (Aot + Emu) #1598 / add ContractExecutor dispatch enum (Aot + Emu) #1608)
  4. this PRrun_with_libfunc_profile + AotWithProgram (supersedes add run_with_libfunc_profile + AotWithProgram variant for ContractExecutor #1599 / add run_with_libfunc_profile + AotWithProgram variant for ContractExecutor #1609)

Test plan

  • cargo check (default features) clean
  • cargo check --features with-libfunc-profiling clean
  • cargo check --features sierra-emu,with-libfunc-profiling clean
  • cargo check --workspace --all-features clean
  • CI green

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 14, 2026

✅ Code is now correctly formatted.

@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from 98ed085 to 64c12b3 Compare May 14, 2026 12:26
@avi-starkware avi-starkware force-pushed the avi/cairo_native/libfunc-profiling-2 branch from 474462f to 13381fa Compare May 14, 2026 12:26
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from 64c12b3 to 7e5fcd7 Compare May 14, 2026 12:33
@avi-starkware avi-starkware force-pushed the avi/cairo_native/libfunc-profiling-2 branch from 13381fa to b5e8d6b Compare May 14, 2026 12:33
avi-starkware and others added 2 commits May 14, 2026 15:48
…cutor

Exposes the libfunc-profiling primitives that downstream consumers (e.g.
the blockifier in starkware-libs/sequencer) currently maintain locally.
Profile collection is callback-driven so the per-call key (tx hash, etc.)
stays out of cairo-native.

- metadata::profiler::Profile is now pub (was a private type alias).
- AotContractExecutor::run_with_libfunc_profile<H, F> (gated on
  with-libfunc-profiling, in new file src/executor/libfunc_profile.rs)
  wraps run: allocates a unique trace ID, points the executor's
  cairo_native__profiler__profile_id symbol at it, drains the resulting
  Profile after run returns, and hands it to a caller-supplied
  FnOnce(Profile). A ProfilerGuard restores the previous trace ID and
  drops the LIBFUNC_PROFILE slot on both the success and unwind paths.
- ContractExecutor::AotWithProgram(AotWithProgram { executor, program })
  is a new variant that bundles an AOT executor with the Sierra program
  it was built from. From<AotWithProgram> is provided.
- ContractExecutor::run dispatches the new variant via
  run_with_libfunc_profile with a no-op profile callback.
- ContractExecutor::run_with_profile<H, F> is the profile-capturing
  counterpart of run; for non-AotWithProgram variants it falls through
  to run (callback never fires).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Two correctness fixes to run_with_libfunc_profile, both inherited from
the source branch (lambdaclass/sequencer:libfunc_profiling_support):

1. Race condition. The trace_id global symbol that drives sample
   routing is process-wide; concurrent calls non-atomically write to
   the same address, and the second caller's ProfilerGuard clobbers
   the first's saved old_trace_id. Add a process-wide PROFILE_LOCK
   (Mutex<()>) and hold it for the entire call. Lock poisoning is
   recovered — the protected state is the symbol itself, and
   inner_into() is safe to reuse.

2. Error paths. find_symbol_ptr().unwrap() panicked when the .so was
   compiled without libfunc-profiling instrumentation; the
   LIBFUNC_PROFILE slot inserted earlier was leaked. Reorder: look up
   the symbol first, convert None to Error::UnexpectedValue, then
   insert the slot. The trailing drain now only invokes on_profile
   when the inner run succeeded — a partial profile from an aborted
   run isn't meaningful.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@avi-starkware avi-starkware force-pushed the avi/cairo_native/libfunc-profiling-2 branch from b5e8d6b to 94bc246 Compare May 14, 2026 12:49
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.

1 participant