Skip to content

Commit b114059

Browse files
WIP: feat(starknet_os): verify syscall_ptr
1 parent 3dd7270 commit b114059

File tree

9 files changed

+51
-11
lines changed

9 files changed

+51
-11
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ chrono = "0.4.26"
171171
clap = "4.5.4"
172172
colored = "3"
173173
const_format = "0.2.30"
174+
convert_case = "0.8.0"
174175
criterion = "0.5.1"
175176
deadqueue = "0.2.4"
176177
defaultmap = "0.5.0"

crates/blockifier/src/execution/deprecated_entry_point_execution.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ pub fn validate_run(
333333
}
334334

335335
// Validate syscall segment end.
336-
syscall_handler.verify_syscall_ptr(syscall_end_ptr).map_err(|_| {
336+
syscall_handler.verify_syscall_ptr(&mut runner.vm, syscall_end_ptr).map_err(|_| {
337337
PostExecutionError::SecurityValidationError("Syscall segment end".to_string())
338338
})?;
339339

crates/blockifier/src/execution/deprecated_syscalls/deprecated_syscall_executor.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ pub trait DeprecatedSyscallExecutor {
5959

6060
fn increment_syscall_count(&mut self, selector: &DeprecatedSyscallSelector);
6161

62-
fn verify_syscall_ptr(&self, actual_ptr: Relocatable) -> DeprecatedSyscallResult<()>;
62+
fn verify_syscall_ptr(
63+
&self,
64+
vm: &mut VirtualMachine,
65+
actual_ptr: Relocatable,
66+
) -> DeprecatedSyscallResult<()>;
6367

6468
fn get_mut_syscall_ptr(&mut self) -> &mut Relocatable;
6569

@@ -284,11 +288,9 @@ pub fn execute_next_deprecated_syscall<T: DeprecatedSyscallExecutor>(
284288
ap_tracking: &ApTracking,
285289
) -> HintExecutionResult {
286290
let initial_syscall_ptr = get_ptr_from_var_name("syscall_ptr", vm, ids_data, ap_tracking)?;
287-
deprecated_syscall_executor.verify_syscall_ptr(initial_syscall_ptr)?;
291+
deprecated_syscall_executor.verify_syscall_ptr(vm, initial_syscall_ptr)?;
288292

289-
let selector = DeprecatedSyscallSelector::try_from(
290-
deprecated_syscall_executor.read_next_syscall_selector(vm)?,
291-
)?;
293+
let selector = deprecated_syscall_executor.read_next_syscall_selector(vm)?.try_into()?;
292294
deprecated_syscall_executor.increment_syscall_count(&selector);
293295

294296
execute_deprecated_syscall_from_selector(deprecated_syscall_executor, vm, selector)

crates/blockifier/src/execution/deprecated_syscalls/hint_processor.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,11 @@ impl HintProcessorLogic for DeprecatedSyscallHintProcessor<'_> {
442442
}
443443

444444
impl DeprecatedSyscallExecutor for DeprecatedSyscallHintProcessor<'_> {
445-
fn verify_syscall_ptr(&self, actual_ptr: Relocatable) -> DeprecatedSyscallResult<()> {
445+
fn verify_syscall_ptr(
446+
&self,
447+
_vm: &mut VirtualMachine,
448+
actual_ptr: Relocatable,
449+
) -> DeprecatedSyscallResult<()> {
446450
if actual_ptr != self.syscall_ptr {
447451
return Err(DeprecatedSyscallExecutionError::BadSyscallPointer {
448452
expected_ptr: self.syscall_ptr,

crates/starknet_os/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ cairo-vm = { workspace = true, features = [
3232
"cairo-0-secp-hints",
3333
"extensive_hints",
3434
] }
35+
convert_case = "0.8.0"
3536
derive_more.workspace = true
3637
indexmap.workspace = true
3738
indoc.workspace = true

crates/starknet_os/src/hint_processor/snos_deprecated_syscall_executor.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use blockifier::execution::deprecated_syscalls::{
3838
};
3939
use cairo_vm::types::relocatable::Relocatable;
4040
use cairo_vm::vm::vm_core::VirtualMachine;
41+
use starknet_types_core::felt::FromStrError;
4142

4243
use super::snos_hint_processor::DeprecatedSyscallHintProcessor;
4344

@@ -47,14 +48,23 @@ impl DeprecatedSyscallExecutor for DeprecatedSyscallHintProcessor {
4748
todo!()
4849
}
4950

50-
fn verify_syscall_ptr(&self, actual_ptr: Relocatable) -> DeprecatedSyscallResult<()> {
51+
fn verify_syscall_ptr(
52+
&self,
53+
vm: &mut VirtualMachine,
54+
actual_ptr: Relocatable,
55+
) -> DeprecatedSyscallResult<()> {
5156
let expected_ptr = self.syscall_ptr.expect("Syscall must be set at this point.");
5257
if actual_ptr != expected_ptr {
5358
return Err(DeprecatedSyscallExecutionError::BadSyscallPointer {
5459
expected_ptr,
5560
actual_ptr,
5661
});
5762
}
63+
if !self.syscall_matches_name(vm.get_integer(actual_ptr)?.into_owned()) {
64+
// TODO(Aner): Create a new error type for this case.
65+
// TODO(Aner): Add a test for this case.
66+
return Err(DeprecatedSyscallExecutionError::FromStr(FromStrError {}));
67+
};
5868

5969
Ok(())
6070
}

crates/starknet_os/src/hint_processor/snos_hint_processor.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -351,16 +351,35 @@ impl SyscallHintProcessor {
351351

352352
pub struct DeprecatedSyscallHintProcessor {
353353
pub(crate) syscall_ptr: Option<Relocatable>,
354+
pub(crate) syscall_name: Option<String>,
354355
}
355356

356357
// TODO(Dori): remove this #[allow] after the constructor is no longer trivial.
357358
#[allow(clippy::new_without_default)]
358359
impl DeprecatedSyscallHintProcessor {
359360
pub fn new() -> Self {
360-
Self { syscall_ptr: None }
361+
Self { syscall_ptr: None, syscall_name: None }
361362
}
362363

363364
pub fn set_syscall_ptr(&mut self, syscall_ptr: Relocatable) {
364365
self.syscall_ptr = Some(syscall_ptr);
365366
}
367+
368+
pub fn set_syscall_name(&mut self, syscall_name: String) {
369+
self.syscall_name = Some(syscall_name);
370+
}
371+
372+
// TODO(Aner): test this function.
373+
pub(crate) fn syscall_matches_name(&self, syscall_raw_selector: Felt) -> bool {
374+
if let Some(syscall_name) = &self.syscall_name {
375+
// Remove leading zero bytes from selector.
376+
let selector_bytes = syscall_raw_selector.to_bytes_be();
377+
let first_non_zero =
378+
selector_bytes.iter().position(|&byte| byte != b'\0').unwrap_or(32);
379+
380+
syscall_name.as_bytes() == &selector_bytes[first_non_zero..]
381+
} else {
382+
false
383+
}
384+
}
366385
}

crates/starknet_os/src/hints/hint_implementation/syscalls.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use blockifier::execution::deprecated_syscalls::deprecated_syscall_executor::execute_next_deprecated_syscall;
22
use blockifier::state::state_api::StateReader;
33
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::get_ptr_from_var_name;
4+
use convert_case::{Case, Casing};
45

56
use crate::hints::error::OsHintResult;
67
use crate::hints::types::HintArgs;
@@ -34,8 +35,9 @@ macro_rules! create_syscall_func {
3435
SyscallHandlerType::DeprecatedSyscallHandler
3536
);
3637
let syscall_hint_processor = &mut hint_processor.deprecated_syscall_hint_processor;
37-
// TODO(Aner): need to verify that the correct syscall is being called (i.e.,
38-
// syscall_ptr matches the fn name). E.g., set syscall_ptr from fn name.
38+
syscall_hint_processor.set_syscall_name(
39+
stringify!($name).to_string().to_case(Case::Pascal)
40+
);
3941
syscall_hint_processor.set_syscall_ptr(
4042
get_ptr_from_var_name(Ids::SyscallPtr.into(), vm, ids_data, ap_tracking)?
4143
);

0 commit comments

Comments
 (0)