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

feat: add tests for JUMP instruction #291

Merged
merged 1 commit into from
Sep 13, 2023
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
7 changes: 6 additions & 1 deletion crates/evm/src/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ const TYPE_CONVERSION_ERROR: felt252 = 'KKT: type conversion error';
// RETURNDATA
const RETURNDATA_OUT_OF_BOUNDS_ERROR: felt252 = 'KKT: ReturnDataOutOfBounds';

// JUMP
const INVALID_DESTINATION: felt252 = 'KKT: invalid JUMP destination';

#[derive(Drop, Copy, PartialEq)]
enum EVMError {
StackError: felt252,
InvalidProgramCounter: felt252,
TypeConversionError: felt252,
ReturnDataError: felt252
ReturnDataError: felt252,
JumpError: felt252,
}


Expand All @@ -27,6 +31,7 @@ impl EVMErrorIntoU256 of Into<EVMError, u256> {
EVMError::InvalidProgramCounter(error_message) => error_message.into(),
EVMError::TypeConversionError(error_message) => error_message.into(),
EVMError::ReturnDataError(error_message) => error_message.into(),
EVMError::JumpError(error_message) => error_message.into(),
}
}
}
2 changes: 1 addition & 1 deletion crates/evm/src/instructions/memory_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl MemoryOperation of MemoryOperationTrait {
/// The new pc target has to be a JUMPDEST opcode.
/// # Specification: https://www.evm.codes/#56?fork=shanghai
fn exec_jump(ref self: ExecutionContext) -> Result<(), EVMError> {
Result::Ok(())
panic_with_felt252('JUMP not implement yet')
}

/// 0x57 - JUMPI operation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use evm::memory::{InternalMemoryTrait, MemoryTrait};
use starknet::EthAddressIntoFelt252;
use utils::helpers::{u256_to_bytes_array};
use utils::traits::{EthAddressIntoU256};
use evm::errors::{EVMError, STACK_UNDERFLOW};
use evm::errors::{EVMError, STACK_UNDERFLOW, INVALID_DESTINATION};
use evm::context::{
ExecutionContext, ExecutionContextTrait, BoxDynamicExecutionContextDestruct, CallContextTrait,
};
Expand Down Expand Up @@ -253,7 +253,6 @@ fn test_exec_mstore8_should_store_last_uint8_offset_63() {
assert(stored == 0xEF, 'mstore8 failed');
}


#[test]
#[available_gas(20000000)]
fn test_msize_initial() {
Expand Down Expand Up @@ -300,3 +299,80 @@ fn test_exec_msize_store_max_offset_1() {
assert(ctx.stack.len() == 1, 'stack should have one element');
assert(ctx.stack.pop().unwrap() == 64, 'should 64 bytes after MSTORE');
}

#[test]
#[available_gas(20000000)]
#[should_panic(expected: ('JUMP not implement yet',))]
fn test_exec_jump_valid() {
// Given
let bytecode: Span<u8> = array![0x01, 0x02, 0x03, 0x5B, 0x04, 0x05].span();
let mut ctx = setup_execution_context_with_bytecode(bytecode);
let counter = 0x03;
ctx.stack.push(counter);

// When
ctx.exec_jump();

// Then
let pc = ctx.program_counter;
assert(pc == 0x03, 'PC should be JUMPDEST');
}


#[test]
#[available_gas(20000000)]
#[should_panic(expected: ('JUMP not implement yet',))]
fn test_exec_jump_invalid() {
// Given
let bytecode: Span<u8> = array![0x01, 0x02, 0x03, 0x5B, 0x04, 0x05].span();
let mut ctx = setup_execution_context_with_bytecode(bytecode);
let counter = 0x02;
ctx.stack.push(counter);

// When
let result = ctx.exec_jump();

// Then
assert(result.is_err(), 'invalid jump dest');
lambda-0x marked this conversation as resolved.
Show resolved Hide resolved
assert(result.unwrap_err() == EVMError::JumpError(INVALID_DESTINATION), 'invalid jump dest');
}

#[test]
#[available_gas(20000000)]
#[should_panic(expected: ('JUMP not implement yet',))]
fn test_exec_jump_out_of_bounds() {
// Given
let bytecode: Span<u8> = array![0x01, 0x02, 0x03, 0x5B, 0x04, 0x05].span();
let mut ctx = setup_execution_context_with_bytecode(bytecode);
let counter = 0xFF;
ctx.stack.push(counter);

// When
let result = ctx.exec_jump();

// Then
assert(result.is_err(), 'invalid jump dest');
assert(result.unwrap_err() == EVMError::JumpError(INVALID_DESTINATION), 'invalid jump dest');
}

// TODO: This is third edge case in which `0x5B` is part of PUSHN instruction and hence
// not a valid opcode to jump to
//
// Remove ignore once its handled
#[test]
#[available_gas(20000000)]
#[ignore]
fn test_exec_jump_inside_pushn() {
// Given
let bytecode: Span<u8> = array![0x60, 0x5B, 0x60, 0x00].span();
let mut ctx = setup_execution_context_with_bytecode(bytecode);
let counter = 0x01;
ctx.stack.push(counter);

// When
let result = ctx.exec_jump();

// Then
assert(result.is_err(), 'invalid jump dest');
assert(result.unwrap_err() == EVMError::JumpError(INVALID_DESTINATION), 'invalid jump dest');
}
Loading