diff --git a/crates/blockifier/src/execution/deprecated_syscalls/hint_processor.rs b/crates/blockifier/src/execution/deprecated_syscalls/hint_processor.rs index 0cef2972b88..78835a1638b 100644 --- a/crates/blockifier/src/execution/deprecated_syscalls/hint_processor.rs +++ b/crates/blockifier/src/execution/deprecated_syscalls/hint_processor.rs @@ -103,6 +103,11 @@ use crate::transaction::objects::TransactionInfo; pub enum DeprecatedSyscallExecutionError { #[error("Bad syscall_ptr; expected: {expected_ptr:?}, got: {actual_ptr:?}.")] BadSyscallPointer { expected_ptr: Relocatable, actual_ptr: Relocatable }, + #[error("Bad syscall selector; expected: {expected_selector:?}, got: {actual_selector:?}.")] + BadSyscallSelector { + expected_selector: DeprecatedSyscallSelector, + actual_selector: DeprecatedSyscallSelector, + }, #[error(transparent)] EntryPointExecutionError(#[from] EntryPointExecutionError), #[error(transparent)] diff --git a/crates/starknet_os/src/hint_processor/snos_deprecated_syscall_executor.rs b/crates/starknet_os/src/hint_processor/snos_deprecated_syscall_executor.rs index 6d3cea30cb4..f01a328bf86 100644 --- a/crates/starknet_os/src/hint_processor/snos_deprecated_syscall_executor.rs +++ b/crates/starknet_os/src/hint_processor/snos_deprecated_syscall_executor.rs @@ -55,7 +55,6 @@ impl DeprecatedSyscallExecutor for DeprecatedSyscallHintProcessor { actual_ptr, }); } - Ok(()) } diff --git a/crates/starknet_os/src/hints/error.rs b/crates/starknet_os/src/hints/error.rs index f2e0e48e27b..c2549c1c795 100644 --- a/crates/starknet_os/src/hints/error.rs +++ b/crates/starknet_os/src/hints/error.rs @@ -1,3 +1,4 @@ +use blockifier::execution::deprecated_syscalls::hint_processor::DeprecatedSyscallExecutionError; use blockifier::state::errors::StateError; use cairo_vm::hint_processor::hint_processor_definition::HintExtension; use cairo_vm::types::errors::math_errors::MathError; @@ -39,6 +40,8 @@ pub enum OsHintError { BooleanIdExpected { id: Ids, felt: Felt }, #[error("Failed to convert {variant:?} felt value {felt:?} to type {ty}: {reason:?}.")] ConstConversion { variant: Const, felt: Felt, ty: String, reason: String }, + #[error(transparent)] + DeprecatedSyscallExecution(#[from] DeprecatedSyscallExecutionError), #[error("Tried to iterate past the end of {item_type}.")] EndOfIterator { item_type: String }, #[error(transparent)] diff --git a/crates/starknet_os/src/hints/hint_implementation/syscalls.rs b/crates/starknet_os/src/hints/hint_implementation/syscalls.rs index 0639a2b4f34..c3e0789b13e 100644 --- a/crates/starknet_os/src/hints/hint_implementation/syscalls.rs +++ b/crates/starknet_os/src/hints/hint_implementation/syscalls.rs @@ -1,6 +1,9 @@ use blockifier::execution::deprecated_syscalls::deprecated_syscall_executor::execute_next_deprecated_syscall; +use blockifier::execution::deprecated_syscalls::hint_processor::DeprecatedSyscallExecutionError; +use blockifier::execution::deprecated_syscalls::DeprecatedSyscallSelector; use blockifier::state::state_api::StateReader; use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::get_ptr_from_var_name; +use paste::paste; use crate::hints::error::OsHintResult; use crate::hints::types::HintArgs; @@ -17,28 +20,72 @@ use crate::syscall_handler_utils::SyscallHandlerType; /// Expands to: /// /// pub(crate) fn get_block_number( -/// HintArgs { hint_processor, vm, ids_data, ap_tracking, .. }: HintArgs<'_, '_, S>, +/// HintArgs { hint_processor, vm, ids_data, ap_tracking, exec_scopes, .. }: HintArgs<'_, '_, S>, /// ) -> OsHintResult /// { +/// assert_eq!( +/// exec_scopes.get(Scope::SyscallHandlerType.into())?, +/// SyscallHandlerType::DeprecatedSyscallHandler +/// ); /// let syscall_hint_processor = &mut hint_processor.deprecated_syscall_hint_processor; -/// Ok(execute_next_deprecated_syscall(syscall_hint_processor, vm, ids_data, ap_tracking)?) +/// let syscall_ptr = get_ptr_from_var_name( +/// Ids::SyscallPtr.into(), vm, ids_data, ap_tracking)?; +/// let syscall_selector = DeprecatedSyscallSelector::try_from( +/// vm.get_integer(syscall_ptr)?.into_owned() +/// )?; +/// let expected_selector = paste! { DeprecatedSyscallSelector::[$name:camel] }; +/// if syscall_selector != expected_selector { +/// return Err( +/// DeprecatedSyscallExecutionError::BadSyscallSelector { +/// expected_selector, +/// actual_selector: syscall_selector, +/// }.into() +/// ); +/// } +/// syscall_hint_processor.set_syscall_ptr(syscall_ptr); +/// Ok( +/// execute_next_deprecated_syscall( +/// syscall_hint_processor, +/// vm, +/// ids_data, +/// ap_tracking +/// )? +/// ) /// } macro_rules! create_syscall_func { ($($name:ident),+) => { $( pub(crate) fn $name( - HintArgs { hint_processor, vm, ids_data, ap_tracking, exec_scopes, .. }: HintArgs<'_, '_, S> + HintArgs { + hint_processor, + vm, + ids_data, + ap_tracking, + exec_scopes, + .. + }: HintArgs<'_, '_, S> ) -> OsHintResult { assert_eq!( exec_scopes.get::(Scope::SyscallHandlerType.into())?, SyscallHandlerType::DeprecatedSyscallHandler ); let syscall_hint_processor = &mut hint_processor.deprecated_syscall_hint_processor; - // TODO(Aner): need to verify that the correct syscall is being called (i.e., - // syscall_ptr matches the fn name). E.g., set syscall_ptr from fn name. - syscall_hint_processor.set_syscall_ptr( - get_ptr_from_var_name(Ids::SyscallPtr.into(), vm, ids_data, ap_tracking)? - ); + let syscall_ptr = get_ptr_from_var_name( + Ids::SyscallPtr.into(), vm, ids_data, ap_tracking + )?; + let syscall_selector = DeprecatedSyscallSelector::try_from( + vm.get_integer(syscall_ptr)?.into_owned() + )?; + let expected_selector = paste! { DeprecatedSyscallSelector::[<$name:camel>] }; + if syscall_selector != expected_selector { + return Err( + DeprecatedSyscallExecutionError::BadSyscallSelector { + expected_selector, + actual_selector: syscall_selector, + }.into() + ); + } + syscall_hint_processor.set_syscall_ptr(syscall_ptr); Ok( execute_next_deprecated_syscall( syscall_hint_processor,