From d13501f4c63dad9ecb55ed8679040520d648ba30 Mon Sep 17 00:00:00 2001 From: meship-starkware Date: Tue, 23 Jul 2024 13:40:10 +0300 Subject: [PATCH] fix(execution): forbid calling cairo0 contract with cairo1 only builtins --- .../deprecated_entry_point_execution.rs | 22 ++++++++++++++++++- crates/blockifier/src/execution/errors.rs | 5 +++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/crates/blockifier/src/execution/deprecated_entry_point_execution.rs b/crates/blockifier/src/execution/deprecated_entry_point_execution.rs index 878c2e276b..4fa1ed6053 100644 --- a/crates/blockifier/src/execution/deprecated_entry_point_execution.rs +++ b/crates/blockifier/src/execution/deprecated_entry_point_execution.rs @@ -1,3 +1,5 @@ +use std::collections::HashSet; + use cairo_vm::types::builtin_name::BuiltinName; use cairo_vm::types::layout_name::LayoutName; use cairo_vm::types::relocatable::{MaybeRelocatable, Relocatable}; @@ -29,6 +31,15 @@ pub struct VmExecutionContext<'a> { pub entry_point_pc: usize, } +pub const CAIRO0_BUILTINS_NAMES: [BuiltinName; 6] = [ + BuiltinName::range_check, + BuiltinName::pedersen, + BuiltinName::ecdsa, + BuiltinName::bitwise, + BuiltinName::ec_op, + BuiltinName::poseidon, +]; + /// Executes a specific call to a contract entry point and returns its output. pub fn execute_entry_point_call( call: CallEntryPoint, @@ -72,8 +83,17 @@ pub fn initialize_execution_context<'a>( context: &'a mut EntryPointExecutionContext, ) -> Result, PreExecutionError> { // Resolve initial PC from EP indicator. - let entry_point_pc = resolve_entry_point_pc(call, &contract_class)?; + let program_builtins_set: HashSet<&BuiltinName> = + HashSet::from_iter(contract_class.program.iter_builtins()); + let unsupported_builtins_set = + &program_builtins_set - &HashSet::from_iter(CAIRO0_BUILTINS_NAMES.iter()); + if !unsupported_builtins_set.is_empty() { + return Err(PreExecutionError::UnsupportedCairo0Builtin( + unsupported_builtins_set.iter().map(|&item| *item).collect(), + )); + } + let entry_point_pc = resolve_entry_point_pc(call, &contract_class)?; // Instantiate Cairo runner. let proof_mode = false; let trace_enabled = false; diff --git a/crates/blockifier/src/execution/errors.rs b/crates/blockifier/src/execution/errors.rs index 1fa6a1d5b2..cc0e87a753 100644 --- a/crates/blockifier/src/execution/errors.rs +++ b/crates/blockifier/src/execution/errors.rs @@ -1,3 +1,6 @@ +use std::collections::HashSet; + +use cairo_vm::types::builtin_name::BuiltinName; use cairo_vm::types::errors::math_errors::MathError; use cairo_vm::vm::errors::cairo_run_errors::CairoRunError; use cairo_vm::vm::errors::memory_errors::MemoryError; @@ -42,6 +45,8 @@ pub enum PreExecutionError { StateError(#[from] StateError), #[error("Requested contract address {:#064x} is not deployed.", .0.key())] UninitializedStorageAddress(ContractAddress), + #[error("Called builtins: {0:?} are unsupported in a Cairo0 contract")] + UnsupportedCairo0Builtin(HashSet), } impl From for PreExecutionError {