Skip to content
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

Add version check for cached AOT contracts. #981

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
15 changes: 15 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use std::{
mem::ManuallyDrop,
os::fd::FromRawFd,
ptr::{self, null, null_mut},
slice,
};
use std::{ops::Mul, vec::IntoIter};

Expand All @@ -35,6 +36,20 @@ lazy_static! {
(DICT_SQUASH_UNIQUE_KEY_COST.cost() - DICT_SQUASH_REPEATED_ACCESS_COST.cost()) as u64;
}

#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn cairo_native__get_version(target: *mut u8, length: usize) -> usize {
let target = slice::from_raw_parts_mut(target, length);

let version = env!("CARGO_PKG_VERSION");
assert!(length > version.len(), "version buffer not big enough");

target.copy_from_slice(version.as_bytes());
target[version.len()] = b'\0';

version.len()
}

/// Based on `cairo-lang-runner`'s implementation.
///
/// Source: <https://github.com/starkware-libs/cairo/blob/main/crates/cairo-lang-runner/src/casm_run/mod.rs#L1946-L1948>
Expand Down
3 changes: 1 addition & 2 deletions src/executor/aot.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::io;

use crate::{
error::Error,
execution_result::{ContractExecutionResult, ExecutionResult},
Expand All @@ -20,6 +18,7 @@ use educe::Educe;
use libc::c_void;
use libloading::Library;
use starknet_types_core::felt::Felt;
use std::io;
use tempfile::NamedTempFile;

#[derive(Educe)]
Expand Down
34 changes: 19 additions & 15 deletions src/executor/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,7 @@ pub enum BuiltinType {

impl BuiltinType {
pub const fn size_in_bytes(&self) -> usize {
match self {
BuiltinType::Bitwise => 8,
BuiltinType::EcOp => 8,
BuiltinType::RangeCheck => 8,
BuiltinType::SegmentArena => 8,
BuiltinType::Poseidon => 8,
BuiltinType::Pedersen => 8,
BuiltinType::RangeCheck96 => 8,
BuiltinType::CircuitAdd => 8,
BuiltinType::CircuitMul => 8,
BuiltinType::Gas => 16,
JulianGCalderon marked this conversation as resolved.
Show resolved Hide resolved
BuiltinType::System => 8,
BuiltinType::BuiltinCosts => 8,
}
size_of::<u64>()
}
}

Expand Down Expand Up @@ -279,8 +266,25 @@ impl AotContractExecutor {
pub fn load(library_path: &Path) -> Result<Self> {
let info_str = std::fs::read_to_string(library_path.with_extension("json"))?;
let contract_info: NativeContractInfo = serde_json::from_str(&info_str)?;

let library = Arc::new(unsafe { Library::new(library_path)? });
unsafe {
let get_version = library
.get::<extern "C" fn(*mut u8, usize) -> usize>(b"cairo_native__get_version")?;

let mut version_buffer = [0u8; 16];
let version_len = get_version(version_buffer.as_mut_ptr(), version_buffer.len());

let target_version = env!("CARGO_PKG_VERSION");
JulianGCalderon marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(
&version_buffer[..version_len],
target_version.as_bytes(),
"aot-compiled contract version mismatch"
);
};

Ok(Self {
library: Arc::new(unsafe { Library::new(library_path)? }),
library,
path: library_path.to_path_buf(),
is_temp_path: false,
contract_info,
Expand Down
Loading