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

fix(EOF): returning to non-returning jumpf, enable validation error #1664

Merged
merged 1 commit into from
Jul 26, 2024
Merged
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
5 changes: 5 additions & 0 deletions bins/revme/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ pub enum Error {
KzgErrors(#[from] format_kzg_setup::KzgErrors),
#[error(transparent)]
EvmRunnerErrors(#[from] evmrunner::Errors),
#[error("Eof validation failed: {:?}/{total_tests}", total_tests-failed_test)]
EofValidation {
failed_test: usize,
total_tests: usize,
},
#[error("Custom error: {0}")]
Custom(&'static str),
}
Expand Down
15 changes: 11 additions & 4 deletions bins/revme/src/cmd/eofvalidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ impl Cmd {
if !self.path.exists() {
return Err(Error::Custom("The specified path does not exist"));
}
run_test(&self.path);
Ok(())
run_test(&self.path)
}
}

pub fn run_test(path: &Path) {
pub fn run_test(path: &Path) -> Result<(), Error> {
let test_files = find_all_json_tests(path);
let mut test_sum = 0;
let mut passed_tests = 0;
Expand Down Expand Up @@ -73,6 +72,14 @@ pub fn run_test(path: &Path) {
}
}
}
println!("Types of error: {:#?}", types_of_error);
println!("Passed tests: {}/{}", passed_tests, test_sum);
if passed_tests != test_sum {
println!("Types of error: {:#?}", types_of_error);
Err(Error::EofValidation {
failed_test: test_sum - passed_tests,
total_tests: test_sum,
})
} else {
Ok(())
}
}
34 changes: 16 additions & 18 deletions crates/interpreter/src/interpreter/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,9 @@ pub enum EofValidationError {
SubContainerNotAccessed,
/// Data size needs to be filled for ReturnContract type.
DataNotFilled,
/// RETF opcode found in non returning section.
RETFInNonReturningSection,
/// JUMPF opcode found in non returning section and it does
/// not jumps to non returning section
JUMPFInNonReturningSection,
/// Section is marked as non-returning but has either RETF or
/// JUMPF to returning section opcodes.
NonReturningSectionIsReturning,
}

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -449,8 +447,7 @@ impl fmt::Display for EofValidationError {
Self::SubContainerCalledInTwoModes => "Sub container called in two modes",
Self::SubContainerNotAccessed => "Sub container not accessed",
Self::DataNotFilled => "Data not filled",
Self::RETFInNonReturningSection => "RETF in non returning code section",
Self::JUMPFInNonReturningSection => "JUMPF in non returning code section",
Self::NonReturningSectionIsReturning => "Non returning section is returning",
}
)
}
Expand Down Expand Up @@ -518,6 +515,8 @@ pub fn validate_eof_code(
let mut next_smallest = this_types.inputs as i32;
let mut next_biggest = this_types.inputs as i32;

let mut is_returning = false;

let mut i = 0;
// We can check validity and jump destinations in one pass.
while i < code.len() {
Expand Down Expand Up @@ -652,16 +651,11 @@ pub fn validate_eof_code(
}
tracker.access_code(target_index);

// TODO check if this is correct.
if target_types.is_non_returning() != this_types.is_non_returning() {
// JUMPF in non returning code section and it does not jumps to non returning section
return Err(EofValidationError::JUMPFInNonReturningSection);
}

if target_types.is_non_returning() {
// if it is not returning
stack_requirement = target_types.inputs as i32;
} else {
is_returning = true;
// check if target code produces enough outputs.
if this_types.outputs < target_types.outputs {
return Err(EofValidationError::JUMPFEnoughOutputs);
Expand Down Expand Up @@ -724,10 +718,8 @@ pub fn validate_eof_code(
}
opcode::RETF => {
stack_requirement = this_types.outputs as i32;

if this_types.is_non_returning() {
return Err(EofValidationError::RETFInNonReturningSection);
}
// mark section as returning.
is_returning = true;

if this_instruction.biggest > stack_requirement {
return Err(EofValidationError::RETFBiggestStackNumMoreThenOutputs);
Expand Down Expand Up @@ -800,6 +792,12 @@ pub fn validate_eof_code(
i += 1 + opcode.immediate_size() as usize + rjumpv_additional_immediates;
}

// error if section is returning but marked as non-returning.
if is_returning == this_types.is_non_returning() {
// wrong termination.
return Err(EofValidationError::NonReturningSectionIsReturning);
}

// last opcode should be terminating
if !is_after_termination {
// wrong termination.
Expand Down Expand Up @@ -925,7 +923,7 @@ mod test {
assert_eq!(
eof,
Err(EofError::Validation(
EofValidationError::JUMPFInNonReturningSection
EofValidationError::NonReturningSectionIsReturning
))
);
}
Expand Down
Loading