diff --git a/starknet/compiler/rust/Cargo.toml b/starknet/compiler/rust/Cargo.toml index e23bddfb07..c6bbe4a1ae 100644 --- a/starknet/compiler/rust/Cargo.toml +++ b/starknet/compiler/rust/Cargo.toml @@ -5,6 +5,9 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[profile.release] +overflow-checks = true + [dependencies] serde = "1.0.208" serde_json = { version = "1.0.125", features = ["raw_value"] } diff --git a/starknet/compiler/rust/src/lib.rs b/starknet/compiler/rust/src/lib.rs index 4d78bb55cc..a1027a3e3b 100644 --- a/starknet/compiler/rust/src/lib.rs +++ b/starknet/compiler/rust/src/lib.rs @@ -1,5 +1,6 @@ -use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; +use cairo_lang_starknet_classes::casm_contract_class::{CasmContractClass, StarknetSierraCompilationError}; use std::ffi::{c_char, CStr, CString}; +use std::panic::{self,AssertUnwindSafe}; #[no_mangle] #[allow(clippy::not_unsafe_ptr_arg_deref)] @@ -24,7 +25,18 @@ pub extern "C" fn compileSierraToCasm(sierra_json: *const c_char, result: *mut * } }; - let casm_class = match CasmContractClass::from_contract_class(sierra_class, true, usize::MAX) { + let mut casm_class_result: Option> = None; + let compilation_result = panic::catch_unwind(AssertUnwindSafe(|| { + casm_class_result = Some(CasmContractClass::from_contract_class(sierra_class, true, usize::MAX)); + })); + if let Err(_) = compilation_result { + unsafe { + *result = raw_cstr("panic during compilation".to_string()); + } + return 0; + } + + let casm_class = match casm_class_result.unwrap() { Ok(value) => value, Err(e) => { unsafe {