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

Simplify check for data Rc in account info data and improve tesing #4046

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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: 1 addition & 6 deletions programs/bpf_loader/src/syscalls/cpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,7 @@ impl<'a, 'b> CallerAccount<'a, 'b> {
let ref_to_len_in_vm = if direct_mapping {
let vm_addr = (account_info.data.as_ptr() as *const u64 as u64)
.saturating_add(size_of::<u64>() as u64);
// In the same vein as the other check_account_info_pointer() checks, we don't lock
// this pointer to a specific address but we don't want it to be inside accounts, or
// callees might be able to write to the pointed memory.
if vm_addr >= ebpf::MM_INPUT_START {
return Err(SyscallError::InvalidPointer.into());
}

VmValue::VmAddress {
vm_addr,
memory_mapping,
Expand Down
1 change: 1 addition & 0 deletions programs/sbf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ SBF_SDK_PATH := ../../sdk/sbf
SRC_DIR := c/src
OUT_DIR := target/sbf-solana-solana/release


test: rust all
SBF_OUT_DIR=$(OUT_DIR) cargo test --features="sbf_rust,sbf_c" $(TEST_ARGS)

Expand Down
40 changes: 21 additions & 19 deletions programs/sbf/rust/invoke/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1523,23 +1523,25 @@ fn process_instruction<'a>(
.unwrap();
}
TEST_ACCOUNT_INFO_LAMPORTS_RC => {
msg!("TEST_ACCOUNT_INFO_LAMPORTS_RC_IN_ACCOUNT");
msg!("TEST_ACCOUNT_INFO_LAMPORTS_RC");

let mut account0 = accounts[0].clone();
let account1 = accounts[1].clone();
let account0 = accounts[0].clone();
let mut account1 = accounts[1].clone();
let account2 = accounts[2].clone();

account0.lamports = unsafe {
let dst = account1.data.borrow_mut().as_mut_ptr();
// 32 = size_of::<RcBox>()
account1.lamports = unsafe {
let dst = account0.data.borrow_mut().as_mut_ptr();
// 32 = size_of::<Rc<RefCell<&mut u64>()
std::ptr::copy(
std::mem::transmute::<Rc<RefCell<&mut u64>>, *const u8>(account0.lamports),
std::mem::transmute::<Rc<RefCell<&mut u64>>, *const u8>(account1.lamports),
dst,
32,
);
std::mem::transmute::<*mut u8, Rc<RefCell<&mut u64>>>(dst)
};

account0.realloc(account1.data_len() + 102, false)?;

let mut instruction_data = vec![TEST_WRITE_ACCOUNT, 1];
instruction_data.extend_from_slice(&1u64.to_le_bytes());
instruction_data.push(1);
Expand All @@ -1549,8 +1551,8 @@ fn process_instruction<'a>(
*program_id,
&[
(program_id, false, false),
(accounts[1].key, true, false),
(accounts[0].key, false, false),
(accounts[0].key, true, false),
(accounts[1].key, false, false),
],
instruction_data.to_vec(),
),
Expand All @@ -1559,19 +1561,19 @@ fn process_instruction<'a>(
.unwrap();
}
TEST_ACCOUNT_INFO_DATA_RC => {
msg!("TEST_ACCOUNT_INFO_DATA_RC_IN_ACCOUNT");
msg!("TEST_ACCOUNT_INFO_DATA_RC");

let mut account0 = accounts[0].clone();
let account1 = accounts[1].clone();
let account0 = accounts[0].clone();
let mut account1 = accounts[1].clone();
let account2 = accounts[2].clone();

account0.data = unsafe {
let dst = account1.data.borrow_mut().as_mut_ptr();
// 32 = size_of::<RcBox>()
account1.data = unsafe {
let dst = account0.data.borrow_mut().as_mut_ptr();
// 40 = size_of::<Rc<RefCell<&[u8]>()
std::ptr::copy(
std::mem::transmute::<Rc<RefCell<&mut [u8]>>, *const u8>(account0.data),
std::mem::transmute::<Rc<RefCell<&mut [u8]>>, *const u8>(account1.data),
dst,
32,
40,
);
std::mem::transmute::<*mut u8, Rc<RefCell<&mut [u8]>>>(dst)
};
Expand All @@ -1585,8 +1587,8 @@ fn process_instruction<'a>(
*program_id,
&[
(program_id, false, false),
(accounts[1].key, true, false),
(accounts[0].key, false, false),
(accounts[0].key, true, false),
(accounts[1].key, false, false),
],
instruction_data.to_vec(),
),
Expand Down
2 changes: 1 addition & 1 deletion programs/sbf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5197,8 +5197,8 @@ fn test_account_info_rc_in_account() {
let mint_pubkey = mint_keypair.pubkey();

let account_metas = vec![
AccountMeta::new(mint_pubkey, true),
AccountMeta::new(account_keypair.pubkey(), false),
AccountMeta::new(mint_pubkey, true),
AccountMeta::new_readonly(invoke_program_id, false),
];

Expand Down
Loading