Skip to content

Commit

Permalink
add negative test
Browse files Browse the repository at this point in the history
  • Loading branch information
seanyoung committed Dec 4, 2024
1 parent 0f2e93f commit e65e7ab
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
27 changes: 26 additions & 1 deletion programs/sbf/rust/account_mem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn process_instruction(
sol_memcpy(&mut data[data_len.saturating_sub(3)..], &buf, 10);
}
11 => {
// memmov dst overlaps end of account
// memmove dst overlaps end of account
unsafe {
sol_memmove(
data[data_len.saturating_sub(3)..].as_mut_ptr(),
Expand All @@ -109,6 +109,31 @@ pub fn process_instruction(
// memmov dst overlaps begin of account
unsafe { sol_memmove(too_early(3).as_mut_ptr(), buf.as_ptr(), 10) };
}
14 => {
// hash overlaps end of account
use solana_program::hash::{hashv, Hasher};

let mut hasher = Hasher::default();
let data = too_early(9);

hasher.hashv(&[data]);
assert_eq!(hashv(&[data]), hasher.result());
}
15 => {
// hash overlaps end of account
use solana_program::keccak::{hashv, Hasher};

let mut hasher = Hasher::default();
let data = unsafe { std::slice::from_raw_parts_mut(data_ptr, data_len + 11) };

hasher.hashv(&[data]);

assert_eq!(hashv(&[data]), hasher.result());
}
16 => {
// hash overlaps end of account
let _hash = blake3::hash(too_early(7));
}

20 => {
use solana_program::hash::{hashv, Hasher};
Expand Down
10 changes: 8 additions & 2 deletions programs/sbf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5485,12 +5485,18 @@ fn test_mem_syscalls_overlap_account_begin_or_end() {
let account = AccountSharedData::new(42, 1024, &program_id);
bank.store_account(&account_keypair.pubkey(), &account);

for instr in 0..=13 {
for instr in 0..=16 {
println!("Testing direct_mapping:{direct_mapping} instruction:{instr}");
let instruction =
Instruction::new_with_bytes(program_id, &[instr], account_metas.clone());

let message = Message::new(&[instruction], Some(&mint_pubkey));
let message = Message::new(
&[
instruction,
ComputeBudgetInstruction::set_compute_unit_limit(1_000_000_000),
],
Some(&mint_pubkey),
);
let tx = Transaction::new(&[&mint_keypair], message.clone(), bank.last_blockhash());
let (result, _, logs) = process_transaction_and_record_inner(&bank, tx);

Expand Down

0 comments on commit e65e7ab

Please sign in to comment.