Skip to content

Commit

Permalink
Added failure for bad types in building an executable.
Browse files Browse the repository at this point in the history
commit-id:db822d43
  • Loading branch information
orizi committed Jan 23, 2025
1 parent 7f623ad commit 9ddf275
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
30 changes: 24 additions & 6 deletions crates/cairo-lang-executable/src/compile_test_data/basic
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CompileExecutableTestRunner(expect_diagnostics: false)
#[executable]
fn main() {}

//! > generated_casm_code
//! > result
# builtins: output
# header #
%{ raise NotImplementedError("memory[ap + 0].. = params[0])") %}
Expand Down Expand Up @@ -59,7 +59,7 @@ fn main(a: felt252, b: felt252) -> felt252 {
a + b
}

//! > generated_casm_code
//! > result
# builtins: output
# header #
%{ raise NotImplementedError("memory[ap + 0].. = params[0])") %}
Expand Down Expand Up @@ -147,7 +147,7 @@ fn fib(a: u128, b: u128, n: u128) -> u128 {
}
}

//! > generated_casm_code
//! > result
# builtins: output, range_check
# header #
[ap + 0] = [fp + -3], ap++;
Expand Down Expand Up @@ -386,7 +386,7 @@ CompileExecutableTestRunner(expect_diagnostics: false)
#[executable_raw]
fn main(mut _input: Span<felt252>, ref _output: Array<felt252>) {}

//! > generated_casm_code
//! > result
# builtins: output
# header #
%{ raise NotImplementedError("memory[ap + 0].. = params[0])") %}
Expand Down Expand Up @@ -423,7 +423,7 @@ fn require_gas() -> felt252 {
}
}

//! > generated_casm_code
//! > result
# builtins: output
# header #
%{ raise NotImplementedError("memory[ap + 0].. = params[0])") %}
Expand Down Expand Up @@ -477,7 +477,7 @@ fn use_dict() {
let _dict: Felt252Dict<u8> = Default::default();
}

//! > generated_casm_code
//! > result
# builtins: output, range_check
# header #
ap += 2;
Expand Down Expand Up @@ -792,3 +792,21 @@ ret;
ret;

//! > expected_diagnostics

//! > ==========================================================================

//! > Test use System implicit.

//! > test_runner_name
CompileExecutableTestRunner(expect_diagnostics: false)

//! > cairo_code
#[executable]
fn use_system() {
core::keccak::compute_keccak_byte_array(@"not allowed without starknet.");
}

//! > result
Emulated builtin `System` not allowed for executable.

//! > expected_diagnostics
5 changes: 3 additions & 2 deletions crates/cairo-lang-executable/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ impl TestFileRunner for CompileExecutableTestRunner {
vec![test_module.crate_id],
DiagnosticsReporter::stderr(),
)
.unwrap();
.map(|compiled| compiled.to_string())
.unwrap_or_else(|e| e.to_string());
let error = verify_diagnostics_expectation(args, &semantic_diagnostics);
TestRunnerResult {
outputs: OrderedHashMap::from([
("generated_casm_code".into(), result.to_string()),
("result".into(), result),
("expected_diagnostics".into(), semantic_diagnostics),
]),
error,
Expand Down
13 changes: 11 additions & 2 deletions crates/cairo-lang-runnable-utils/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub enum BuildError {
SierraCompilationError(#[from] Box<CompilationError>),
#[error(transparent)]
ApChangeError(#[from] ApChangeError),
#[error("Emulated builtin `{0}` not allowed for executable.")]
DisallowedBuiltinForExecutable(GenericTypeId),
}

impl BuildError {
Expand Down Expand Up @@ -244,19 +246,21 @@ pub struct EntryCodeConfig {
/// Array<felt252>`. And will inject the output builtin to be the supplied array input, and
/// to be the result of the output.
pub outputting_function: bool,
/// Whether the compiled code is for performing a proof.
pub is_proof_mode: bool,
}
impl EntryCodeConfig {
/// Returns a configuration for testing purposes.
///
/// This configuration will not finalize the segment arena after calling the function, to
/// prevent failure in case of functions returning values.
pub fn testing() -> Self {
Self { finalize_segment_arena: false, outputting_function: false }
Self { finalize_segment_arena: false, outputting_function: false, is_proof_mode: false }
}

/// Returns a configuration for execution purposes.
pub fn executable() -> Self {
Self { finalize_segment_arena: true, outputting_function: true }
Self { finalize_segment_arena: true, outputting_function: true, is_proof_mode: true }
}
}

Expand Down Expand Up @@ -369,6 +373,11 @@ pub fn create_entry_code_from_params(
let var = builtin_vars[&name];
casm_build_extend!(ctx, tempvar _builtin = var;);
} else if emulated_builtins.contains(generic_ty) {
if config.is_proof_mode {
// Emulated builtins are not supported when compiling for proof, as they are not
// proven.
return Err(BuildError::DisallowedBuiltinForExecutable(generic_ty.clone()));
}
casm_build_extend! {ctx,
tempvar system;
hint AllocSegment into {dst: system};
Expand Down

0 comments on commit 9ddf275

Please sign in to comment.