1use std::collections::HashMap;
3
4use cairo_lang_casm::hints::Hint;
5use cairo_lang_runnable_utils::builder::{BuildError, EntryCodeConfig, RunnableBuilder};
6use cairo_lang_sierra::extensions::NamedType;
7use cairo_lang_sierra::extensions::enm::EnumType;
8use cairo_lang_sierra::extensions::gas::{CostTokenType, GasBuiltinType};
9use cairo_lang_sierra::ids::{ConcreteTypeId, GenericTypeId};
10use cairo_lang_sierra::program::{Function, GenericArg};
11use cairo_lang_sierra_to_casm::metadata::MetadataComputationConfig;
12use cairo_lang_starknet::contract::ContractInfo;
13use cairo_lang_utils::casts::IntoOrPanic;
14use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
15use cairo_lang_utils::{extract_matches, require};
16use cairo_vm::hint_processor::hint_processor_definition::HintProcessor;
17use cairo_vm::serde::deserialize_program::HintParams;
18use cairo_vm::types::builtin_name::BuiltinName;
19use cairo_vm::vm::errors::cairo_run_errors::CairoRunError;
20use cairo_vm::vm::runners::cairo_runner::{ExecutionResources, RunResources};
21use cairo_vm::vm::vm_core::VirtualMachine;
22use casm_run::hint_to_hint_params;
23pub use casm_run::{CairoHintProcessor, StarknetState};
24use num_bigint::BigInt;
25use num_traits::ToPrimitive;
26use profiling::ProfilingInfo;
27use starknet_types_core::felt::Felt as Felt252;
28use thiserror::Error;
29
30use crate::casm_run::{RunFunctionResult, StarknetHintProcessor};
31use crate::profiling::ProfilerConfig;
32
33pub mod casm_run;
34pub mod clap;
35pub mod profiling;
36pub mod short_string;
37
38const MAX_STACK_TRACE_DEPTH_DEFAULT: usize = 100;
39
40#[derive(Debug, Error)]
41pub enum RunnerError {
42 #[error(transparent)]
43 BuildError(#[from] BuildError),
44 #[error("Not enough gas to call function.")]
45 NotEnoughGasToCall,
46 #[error("Function param {param_index} only partially contains argument {arg_index}.")]
47 ArgumentUnaligned { param_index: usize, arg_index: usize },
48 #[error("Function expects arguments of size {expected} and received {actual} instead.")]
49 ArgumentsSizeMismatch { expected: usize, actual: usize },
50 #[error(transparent)]
51 CairoRunError(#[from] Box<CairoRunError>),
52}
53
54pub struct RunResultStarknet {
56 pub gas_counter: Option<Felt252>,
57 pub memory: Vec<Option<Felt252>>,
58 pub value: RunResultValue,
59 pub starknet_state: StarknetState,
60 pub used_resources: StarknetExecutionResources,
61 pub profiling_info: Option<ProfilingInfo>,
63}
64
65#[derive(Debug, Eq, PartialEq, Clone)]
67pub struct RunResult {
68 pub gas_counter: Option<Felt252>,
69 pub memory: Vec<Option<Felt252>>,
70 pub value: RunResultValue,
71 pub used_resources: ExecutionResources,
72 pub profiling_info: Option<ProfilingInfo>,
74}
75
76#[derive(Debug, Eq, PartialEq, Clone, Default)]
79pub struct StarknetExecutionResources {
80 pub basic_resources: ExecutionResources,
82 pub syscalls: HashMap<String, usize>,
84}
85
86impl std::ops::AddAssign<StarknetExecutionResources> for StarknetExecutionResources {
87 fn add_assign(&mut self, other: Self) {
89 self.basic_resources += &other.basic_resources;
90 for (k, v) in other.syscalls {
91 *self.syscalls.entry(k).or_default() += v;
92 }
93 }
94}
95
96#[derive(Debug, Eq, PartialEq, Clone)]
98pub enum RunResultValue {
99 Success(Vec<Felt252>),
101 Panic(Vec<Felt252>),
103}
104
105pub fn token_gas_cost(token_type: CostTokenType) -> usize {
107 match token_type {
108 CostTokenType::Const => 1,
109 CostTokenType::Step
110 | CostTokenType::Hole
111 | CostTokenType::RangeCheck
112 | CostTokenType::RangeCheck96 => {
113 panic!("Token type {token_type:?} has no gas cost.")
114 }
115 CostTokenType::Pedersen => 4050,
116 CostTokenType::Poseidon => 491,
117 CostTokenType::Bitwise => 583,
118 CostTokenType::EcOp => 4085,
119 CostTokenType::AddMod => 230,
120 CostTokenType::MulMod => 604,
121 }
122}
123
124#[derive(Debug, Clone)]
126pub enum Arg {
127 Value(Felt252),
128 Array(Vec<Arg>),
129}
130impl Arg {
131 pub fn size(&self) -> usize {
133 match self {
134 Self::Value(_) => 1,
135 Self::Array(_) => 2,
136 }
137 }
138}
139impl From<Felt252> for Arg {
140 fn from(value: Felt252) -> Self {
141 Self::Value(value)
142 }
143}
144
145pub fn build_hints_dict(
147 hints: &[(usize, Vec<Hint>)],
148) -> (HashMap<usize, Vec<HintParams>>, HashMap<String, Hint>) {
149 let mut hints_dict: HashMap<usize, Vec<HintParams>> = HashMap::new();
150 let mut string_to_hint: HashMap<String, Hint> = HashMap::new();
151
152 for (offset, offset_hints) in hints {
153 for hint in offset_hints {
155 string_to_hint.insert(hint.representing_string(), hint.clone());
156 }
157 hints_dict.insert(*offset, offset_hints.iter().map(hint_to_hint_params).collect());
159 }
160 (hints_dict, string_to_hint)
161}
162
163pub struct PreparedStarknetContext {
171 pub hints_dict: HashMap<usize, Vec<HintParams>>,
172 pub bytecode: Vec<BigInt>,
173 pub builtins: Vec<BuiltinName>,
174}
175
176pub struct SierraCasmRunner {
178 builder: RunnableBuilder,
180 starknet_contracts_info: OrderedHashMap<Felt252, ContractInfo>,
182 run_profiler: Option<ProfilingInfoCollectionConfig>,
184}
185impl SierraCasmRunner {
186 pub fn new(
187 sierra_program: cairo_lang_sierra::program::Program,
188 metadata_config: Option<MetadataComputationConfig>,
189 starknet_contracts_info: OrderedHashMap<Felt252, ContractInfo>,
190 run_profiler: Option<ProfilingInfoCollectionConfig>,
191 ) -> Result<Self, RunnerError> {
192 Ok(Self {
193 builder: RunnableBuilder::new(sierra_program, metadata_config)?,
194 starknet_contracts_info,
195 run_profiler,
196 })
197 }
198
199 pub fn run_function_with_starknet_context(
201 &self,
202 func: &Function,
203 args: Vec<Arg>,
204 available_gas: Option<usize>,
205 starknet_state: StarknetState,
206 ) -> Result<RunResultStarknet, RunnerError> {
207 let (mut hint_processor, ctx) =
208 self.prepare_starknet_context(func, args, available_gas, starknet_state)?;
209 self.run_function_with_prepared_starknet_context(func, &mut hint_processor, ctx)
210 }
211
212 pub fn run_function_with_prepared_starknet_context(
215 &self,
216 func: &Function,
217 hint_processor: &mut dyn StarknetHintProcessor,
218 PreparedStarknetContext { hints_dict, bytecode, builtins }: PreparedStarknetContext,
219 ) -> Result<RunResultStarknet, RunnerError> {
220 let RunResult { gas_counter, memory, value, used_resources, profiling_info } =
221 self.run_function(func, hint_processor, hints_dict, bytecode.iter(), builtins)?;
222 let mut all_used_resources = hint_processor.take_syscalls_used_resources();
223 all_used_resources.basic_resources += &used_resources;
224 Ok(RunResultStarknet {
225 gas_counter,
226 memory,
227 value,
228 starknet_state: hint_processor.take_starknet_state(),
229 used_resources: all_used_resources,
230 profiling_info,
231 })
232 }
233
234 fn inner_type_from_panic_wrapper(
236 &self,
237 ty: &GenericTypeId,
238 func: &Function,
239 ) -> Option<ConcreteTypeId> {
240 let generic_args = &func
241 .signature
242 .ret_types
243 .iter()
244 .find_map(|rt| {
245 let long_id = self.builder.type_long_id(rt);
246 (long_id.generic_id == *ty).then_some(long_id)
247 })
248 .unwrap()
249 .generic_args;
250
251 if *ty == EnumType::ID
252 && matches!(&generic_args[0], GenericArg::UserType(ut)
253 if ut.debug_name.as_ref().unwrap().starts_with("core::panics::PanicResult::"))
254 {
255 return Some(extract_matches!(&generic_args[1], GenericArg::Type).clone());
256 }
257 None
258 }
259
260 pub fn run_function<'a, Bytecode>(
264 &self,
265 func: &Function,
266 hint_processor: &mut dyn HintProcessor,
267 hints_dict: HashMap<usize, Vec<HintParams>>,
268 bytecode: Bytecode,
269 builtins: Vec<BuiltinName>,
270 ) -> Result<RunResult, RunnerError>
271 where
272 Bytecode: ExactSizeIterator<Item = &'a BigInt> + Clone,
273 {
274 let return_types =
275 self.builder.generic_id_and_size_from_concrete(&func.signature.ret_types);
276 let data_len = bytecode.len();
277 let RunFunctionResult { ap, mut used_resources, memory, relocated_trace } =
278 casm_run::run_function(
279 bytecode,
280 builtins,
281 |vm| initialize_vm(vm, data_len),
282 hint_processor,
283 hints_dict,
284 )?;
285
286 let header_end = relocated_trace.last().unwrap().pc;
289 used_resources.n_steps -=
290 relocated_trace.iter().position(|e| e.pc > header_end).unwrap() - 1;
291 used_resources.n_steps -=
292 relocated_trace.iter().rev().position(|e| e.pc > header_end).unwrap() - 1;
293
294 let (results_data, gas_counter) = self.get_results_data(&return_types, &memory, ap);
295 assert!(results_data.len() <= 1);
296
297 let value = if results_data.is_empty() {
298 RunResultValue::Success(vec![])
300 } else {
301 let (ty, values) = results_data[0].clone();
302 let inner_ty =
303 self.inner_type_from_panic_wrapper(&ty, func).map(|it| self.builder.type_size(&it));
304 Self::handle_main_return_value(inner_ty, values, &memory)
305 };
306
307 let Self { builder, starknet_contracts_info: _, run_profiler } = self;
308
309 let load_offset = header_end + 1;
311
312 let profiling_info = run_profiler.as_ref().map(|config| {
313 ProfilingInfo::from_trace(builder, load_offset, config, &relocated_trace)
314 });
315
316 Ok(RunResult { gas_counter, memory, value, used_resources, profiling_info })
317 }
318
319 pub fn prepare_starknet_context(
325 &self,
326 func: &Function,
327 args: Vec<Arg>,
328 available_gas: Option<usize>,
329 starknet_state: StarknetState,
330 ) -> Result<(CairoHintProcessor<'_>, PreparedStarknetContext), RunnerError> {
331 let (assembled_program, builtins) =
332 self.builder.assemble_function_program(func, EntryCodeConfig::testing())?;
333 let (hints_dict, string_to_hint) = build_hints_dict(&assembled_program.hints);
334 let user_args = self.prepare_args(func, available_gas, args)?;
335 let hint_processor = CairoHintProcessor {
336 runner: Some(self),
337 user_args,
338 starknet_state,
339 string_to_hint,
340 run_resources: RunResources::default(),
341 syscalls_used_resources: Default::default(),
342 no_temporary_segments: true,
343 markers: Default::default(),
344 panic_traceback: Default::default(),
345 };
346 Ok((
347 hint_processor,
348 PreparedStarknetContext { hints_dict, bytecode: assembled_program.bytecode, builtins },
349 ))
350 }
351
352 fn prepare_args(
354 &self,
355 func: &Function,
356 available_gas: Option<usize>,
357 args: Vec<Arg>,
358 ) -> Result<Vec<Vec<Arg>>, RunnerError> {
359 let mut user_args = vec![];
360 if let Some(gas) = self
361 .requires_gas_builtin(func)
362 .then_some(self.get_initial_available_gas(func, available_gas)?)
363 {
364 user_args.push(vec![Arg::Value(Felt252::from(gas))]);
365 }
366 let mut expected_arguments_size = 0;
367 let actual_args_size = args_size(&args);
368 let mut arg_iter = args.into_iter().enumerate();
369 for (param_index, (_, param_size)) in self
370 .builder
371 .generic_id_and_size_from_concrete(&func.signature.param_types)
372 .into_iter()
373 .filter(|(ty, _)| self.builder.is_user_arg_type(ty))
374 .enumerate()
375 {
376 let mut curr_arg = vec![];
377 let param_size: usize = param_size.into_or_panic();
378 expected_arguments_size += param_size;
379 let mut taken_size = 0;
380 while taken_size < param_size {
381 let Some((arg_index, arg)) = arg_iter.next() else {
382 break;
383 };
384 taken_size += arg.size();
385 if taken_size > param_size {
386 return Err(RunnerError::ArgumentUnaligned { param_index, arg_index });
387 }
388 curr_arg.push(arg);
389 }
390 user_args.push(curr_arg);
391 }
392 if expected_arguments_size != actual_args_size {
393 return Err(RunnerError::ArgumentsSizeMismatch {
394 expected: expected_arguments_size,
395 actual: actual_args_size,
396 });
397 }
398 Ok(user_args)
399 }
400
401 pub fn handle_main_return_value(
403 inner_type_size: Option<i16>,
404 values: Vec<Felt252>,
405 cells: &[Option<Felt252>],
406 ) -> RunResultValue {
407 if let Some(inner_type_size) = inner_type_size {
408 if values[0] == Felt252::from(0) {
410 let inner_ty_size = inner_type_size as usize;
412 let skip_size = values.len() - inner_ty_size;
413 RunResultValue::Success(values.into_iter().skip(skip_size).collect())
414 } else {
415 let err_data_start = values[values.len() - 2].to_usize().unwrap();
417 let err_data_end = values[values.len() - 1].to_usize().unwrap();
418 RunResultValue::Panic(
419 cells[err_data_start..err_data_end]
420 .iter()
421 .cloned()
422 .map(Option::unwrap)
423 .collect(),
424 )
425 }
426 } else {
427 RunResultValue::Success(values)
429 }
430 }
431
432 pub fn get_results_data(
434 &self,
435 return_types: &[(GenericTypeId, i16)],
436 cells: &[Option<Felt252>],
437 mut ap: usize,
438 ) -> (Vec<(GenericTypeId, Vec<Felt252>)>, Option<Felt252>) {
439 let mut results_data = vec![];
440 for (ty, ty_size) in return_types.iter().rev() {
441 let size = *ty_size as usize;
442 let values: Vec<Felt252> =
443 ((ap - size)..ap).map(|index| cells[index].unwrap()).collect();
444 ap -= size;
445 results_data.push((ty.clone(), values));
446 }
447
448 let mut gas_counter = None;
450 results_data.retain_mut(|(ty, values)| {
451 let generic_ty = ty;
452 if *generic_ty == GasBuiltinType::ID {
453 gas_counter = Some(values.remove(0));
454 assert!(values.is_empty());
455 false
456 } else {
457 self.builder.is_user_arg_type(generic_ty)
458 }
459 });
460
461 (results_data, gas_counter)
462 }
463
464 pub fn find_function(&self, name_suffix: &str) -> Result<&Function, RunnerError> {
466 Ok(self.builder.find_function(name_suffix)?)
467 }
468
469 fn requires_gas_builtin(&self, func: &Function) -> bool {
471 func.signature
472 .param_types
473 .iter()
474 .any(|ty| self.builder.type_long_id(ty).generic_id == GasBuiltinType::ID)
475 }
476
477 pub fn get_initial_available_gas(
480 &self,
481 func: &Function,
482 available_gas: Option<usize>,
483 ) -> Result<usize, RunnerError> {
484 let Some(available_gas) = available_gas else {
485 return Ok(0);
486 };
487
488 let Some(required_gas) = self.initial_required_gas(func) else {
492 return Ok(0);
493 };
494
495 available_gas.checked_sub(required_gas).ok_or(RunnerError::NotEnoughGasToCall)
496 }
497
498 pub fn initial_required_gas(&self, func: &Function) -> Option<usize> {
499 let gas_info = &self.builder.metadata().gas_info;
500 require(!gas_info.function_costs.is_empty())?;
501 Some(
502 gas_info.function_costs[&func.id]
503 .iter()
504 .map(|(token_type, val)| val.into_or_panic::<usize>() * token_gas_cost(*token_type))
505 .sum(),
506 )
507 }
508}
509
510#[derive(Debug, Eq, PartialEq, Clone)]
512pub struct ProfilingInfoCollectionConfig {
513 pub max_stack_trace_depth: usize,
515 pub collect_scoped_sierra_statement_weights: bool,
521}
522
523impl ProfilingInfoCollectionConfig {
524 pub fn set_max_stack_trace_depth(&mut self, max_stack_depth: usize) -> &mut Self {
525 self.max_stack_trace_depth = max_stack_depth;
526 self
527 }
528
529 pub fn from_profiler_config(profiler_config: &ProfilerConfig) -> Self {
530 match profiler_config {
531 ProfilerConfig::Cairo | ProfilerConfig::Sierra => Self::default(),
532 ProfilerConfig::Scoped => ProfilingInfoCollectionConfig {
533 collect_scoped_sierra_statement_weights: true,
534 ..Self::default()
535 },
536 }
537 }
538}
539
540impl Default for ProfilingInfoCollectionConfig {
541 fn default() -> Self {
545 Self {
546 max_stack_trace_depth: if let Ok(max) = std::env::var("MAX_STACK_TRACE_DEPTH") {
547 if max.is_empty() {
548 MAX_STACK_TRACE_DEPTH_DEFAULT
549 } else {
550 max.parse::<usize>().expect("MAX_STACK_TRACE_DEPTH env var is not numeric")
551 }
552 } else {
553 MAX_STACK_TRACE_DEPTH_DEFAULT
554 },
555 collect_scoped_sierra_statement_weights: false,
556 }
557 }
558}
559
560pub fn initialize_vm(vm: &mut VirtualMachine, data_len: usize) -> Result<(), Box<CairoRunError>> {
563 let builtin_cost_segment = vm.add_memory_segment();
565 for token_type in CostTokenType::iter_precost() {
566 vm.insert_value(
567 (builtin_cost_segment + (token_type.offset_in_builtin_costs() as usize)).unwrap(),
568 Felt252::from(token_gas_cost(*token_type)),
569 )
570 .map_err(|e| Box::new(e.into()))?;
571 }
572 vm.insert_value((vm.get_pc() + data_len).unwrap(), builtin_cost_segment)
575 .map_err(|e| Box::new(e.into()))?;
576 Ok(())
577}
578
579fn args_size(args: &[Arg]) -> usize {
581 args.iter().map(Arg::size).sum()
582}