Skip to content

Make native trace dump available. #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ cairo-lang-sierra-to-casm = "2.8.2"
cairo-lang-starknet-classes = "2.8.2"
cairo-lang-utils = "2.8.2"
# This is a temporary dependency, will be removed once the new version of cairo-native is released to main.
cairo-native = { git = "https://github.com/lambdaclass/cairo_native" }
sierra-emu = { git = "https://github.com/lambdaclass/sierra-emu.git" }
cairo-native = { path = "../cairo_native" }
cairo-vm = "1.0.1"
camelpaste = "0.1.0"
chrono = "0.4.26"
Expand Down Expand Up @@ -172,6 +171,7 @@ serde_repr = "0.1.19"
serde_yaml = "0.9.16"
sha2 = "0.10.8"
sha3 = "0.10.8"
sierra-emu = { path = "../sierra-emu" }
simple_logger = "4.0.0"
starknet-core = "0.6.0"
starknet-crypto = "0.5.1"
Expand Down
5 changes: 3 additions & 2 deletions crates/blockifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ concurrency = []
jemalloc = ["dep:tikv-jemallocator"]
testing = ["rand", "rstest"]
use-sierra-emu = []
with-trace-dump = ["cairo-native/with-trace-dump"]


# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -31,11 +32,9 @@ cairo-lang-sierra.workspace = true
cairo-lang-starknet-classes.workspace = true
cairo-lang-utils.workspace = true
cairo-native.workspace = true
sierra-emu.workspace = true
cairo-vm.workspace = true
derive_more.workspace = true
indexmap.workspace = true
tracing.workspace = true
itertools.workspace = true
keccak.workspace = true
log.workspace = true
Expand All @@ -52,6 +51,7 @@ serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true, features = ["arbitrary_precision"] }
sha2.workspace = true
sha3.workspace = true
sierra-emu.workspace = true
starknet-types-core.workspace = true
starknet_api = { workspace = true, features = ["testing"] }
strum.workspace = true
Expand All @@ -60,6 +60,7 @@ tempfile.workspace = true
thiserror.workspace = true
tikv-jemallocator = { workspace = true, optional = true }
toml.workspace = true
tracing.workspace = true

[dev-dependencies]
assert_matches.workspace = true
Expand Down
70 changes: 69 additions & 1 deletion crates/blockifier/src/execution/native/entry_point_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,75 @@ pub fn execute_entry_point_call(
);
run_sierra_emu_executor(vm, function_id, call.clone())
} else {
run_native_executor(&contract_class.executor, function_id, call, syscall_handler)
#[cfg(feature = "with-trace-dump")]
let counter_value = {
use std::collections::HashMap;
use std::sync::atomic::AtomicUsize;
use std::sync::Mutex;

use cairo_lang_sierra::program_registry::ProgramRegistry;
use cairo_native::runtime::trace_dump::TraceDump;
use cairo_native::types::TypeBuilder;

// Since the library is statically linked, then dynamically loaded, each instance of
// `TRACE_DUMP` for each contract is separate (probably). That's why we need this
// getter and cannot use `cairo_native::runtime::TRACE_DUMP` directly.
let trace_dump = unsafe {
let fn_ptr = contract_class
.executor
.library
.get::<extern "C" fn() -> &'static Mutex<HashMap<u64, TraceDump>>>(
b"get_trace_dump_ptr\0",
)
.unwrap();

fn_ptr()
};
let mut trace_dump = trace_dump.lock().unwrap();

static COUNTER: AtomicUsize = AtomicUsize::new(0);
let counter_value = COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
trace_dump.insert(
u64::try_from(counter_value).unwrap(),
TraceDump::new(
ProgramRegistry::new(&contract_class.program).unwrap(),
|x, registry| x.layout(registry).unwrap(),
),
);

// Set the active trace id.
let trace_id_ref = unsafe {
contract_class
.executor
.library
.get::<u64>(b"TRACE_DUMP__TRACE_ID\0")
.unwrap()
.try_as_raw_ptr()
.unwrap()
.cast::<u64>()
.as_mut()
.unwrap()
};
*trace_id_ref = u64::try_from(counter_value).unwrap();

println!("Execution started for trace #{counter_value}.");
dbg!(trace_dump.keys().collect::<Vec<_>>());
counter_value
};

let x = run_native_executor(
&contract_class.executor,
function_id,
call,
syscall_handler,
#[cfg(feature = "with-trace-dump")]
counter_value,
);

#[cfg(feature = "with-trace-dump")]
println!("Execution finished for trace #{counter_value}.");

x
};
let execution_time = pre_execution_instant.elapsed().as_millis();

Expand Down
39 changes: 35 additions & 4 deletions crates/blockifier/src/execution/native/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub fn run_native_executor(
function_id: &FunctionId,
call: CallEntryPoint,
mut syscall_handler: NativeSyscallHandler<'_>,
#[cfg(feature = "with-trace-dump")] trace_id: usize,
) -> EntryPointExecutionResult<CallInfo> {
let execution_result = native_executor.invoke_contract_dynamic(
function_id,
Expand All @@ -59,6 +60,33 @@ pub fn run_native_executor(
&mut syscall_handler,
);

#[cfg(feature = "with-trace-dump")]
#[allow(warnings)]
{
use std::sync::Mutex;

use cairo_native::runtime::trace_dump::TraceDump;

let trace = serde_json::to_string_pretty(&{
let trace_dump = unsafe {
let fn_ptr = native_executor
.library
.get::<extern "C" fn() -> &'static Mutex<HashMap<u64, TraceDump>>>(
b"get_trace_dump_ptr\0",
)
.unwrap();

fn_ptr()
};
let mut trace_dump = trace_dump.lock().unwrap();

trace_dump.remove(&u64::try_from(trace_id).unwrap()).unwrap().trace
})
.unwrap();
std::fs::create_dir_all("traces/native/").unwrap();
std::fs::write(&format!("traces/native/trace_{}.json", trace_id), trace).unwrap();
}

let run_result = match execution_result {
Ok(res) if res.failure_flag => Err(EntryPointExecutionError::NativeExecutionError {
info: if !res.return_values.is_empty() {
Expand Down Expand Up @@ -100,6 +128,9 @@ pub fn run_sierra_emu_executor(
std::fs::create_dir_all("traces/emu/").unwrap();
std::fs::write(format!("traces/emu/trace_{}.json", counter_value), trace).unwrap();

std::fs::write(format!("traces/program_{}.sierra", counter_value), format!("{}", vm.program))
.unwrap();

if execution_result.failure_flag {
Err(EntryPointExecutionError::NativeExecutionError {
info: if !execution_result.return_values.is_empty() {
Expand Down Expand Up @@ -129,8 +160,8 @@ fn create_callinfo(
syscall_handler: NativeSyscallHandler<'_>,
) -> Result<CallInfo, EntryPointExecutionError> {
let gas_consumed = {
let low = run_result.remaining_gas as u64;
let high = (run_result.remaining_gas >> 64) as u64;
let low = u64::try_from(run_result.remaining_gas & u128::from(u64::MAX)).unwrap();
let high = u64::try_from(run_result.remaining_gas >> 64).unwrap();
if high != 0 {
return Err(EntryPointExecutionError::NativeExecutionError {
info: "Overflow: gas consumed bigger than 64 bit".into(),
Expand Down Expand Up @@ -169,8 +200,8 @@ pub fn create_callinfo_emu(
accessed_storage_keys: HashSet<StorageKey, RandomState>,
) -> Result<CallInfo, EntryPointExecutionError> {
let gas_consumed = {
let low = run_result.remaining_gas as u64;
let high = (run_result.remaining_gas >> 64) as u64;
let low = u64::try_from(run_result.remaining_gas & u128::from(u64::MAX)).unwrap();
let high = u64::try_from(run_result.remaining_gas >> 64).unwrap();
if high != 0 {
return Err(EntryPointExecutionError::NativeExecutionError {
info: "Overflow: gas consumed bigger than 64 bit".into(),
Expand Down