Skip to content

Commit

Permalink
fix(execution): forbid calling cairo0 contract with cairo1 only builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
meship-starkware committed Jul 28, 2024
1 parent 82c148c commit 58af0ae
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -71,9 +82,19 @@ pub fn initialize_execution_context<'a>(
resources: &'a mut ExecutionResources,
context: &'a mut EntryPointExecutionContext,
) -> Result<VmExecutionContext<'a>, PreExecutionError> {
// Verify use of cairo0 builtins only.
let program_builtins: HashSet<&BuiltinName> =
HashSet::from_iter(contract_class.program.iter_builtins());
let unsupported_builtins =
&program_builtins - &HashSet::from_iter(CAIRO0_BUILTINS_NAMES.iter());
if !unsupported_builtins.is_empty() {
return Err(PreExecutionError::UnsupportedCairo0Builtin(
unsupported_builtins.iter().map(|&item| *item).collect(),
));
}

// Resolve initial PC from EP indicator.
let entry_point_pc = resolve_entry_point_pc(call, &contract_class)?;

// Instantiate Cairo runner.
let proof_mode = false;
let trace_enabled = false;
Expand Down
5 changes: 5 additions & 0 deletions crates/blockifier/src/execution/errors.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<BuiltinName>),
}

impl From<RunnerError> for PreExecutionError {
Expand Down

0 comments on commit 58af0ae

Please sign in to comment.