Skip to content

feat(starknet_os): verify syscall_ptr #6327

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ impl DeprecatedSyscallExecutor for DeprecatedSyscallHintProcessor {
actual_ptr,
});
}

Ok(())
}

Expand Down
3 changes: 3 additions & 0 deletions crates/starknet_os/src/hints/error.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)]
Expand Down
37 changes: 31 additions & 6 deletions crates/starknet_os/src/hints/hint_implementation/syscalls.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
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::execution::execution_utils::felt_from_ptr;
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::OsHintError::DeprecatedSyscallExecution;
use crate::hints::error::OsHintResult;
use crate::hints::types::HintArgs;
use crate::hints::vars::{Ids, Scope};
Expand All @@ -27,18 +32,38 @@ macro_rules! create_syscall_func {
($($name:ident),+) => {
$(
pub(crate) fn $name<S: StateReader>(
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::<SyscallHandlerType>(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(
DeprecatedSyscallExecution(
DeprecatedSyscallExecutionError::BadSyscallSelector {
expected_selector,
actual_selector: syscall_selector,
}
)
);
}
syscall_hint_processor.set_syscall_ptr(syscall_ptr);
Ok(
execute_next_deprecated_syscall(
syscall_hint_processor,
Expand Down
Loading