Skip to content
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
47 changes: 43 additions & 4 deletions programs/bpf_loader/src/syscalls/mem_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ struct MemoryChunkIterator<'a> {
// exclusive end index (start + len, so one past the last valid address)
vm_addr_end: u64,
len: u64,
account_index: usize,
account_index: Option<usize>,
is_account: Option<bool>,
}

Expand All @@ -446,7 +446,7 @@ impl<'a> MemoryChunkIterator<'a> {
len,
vm_addr_start: vm_addr,
vm_addr_end,
account_index: 0,
account_index: None,
is_account: None,
})
}
Expand Down Expand Up @@ -490,14 +490,18 @@ impl<'a> Iterator for MemoryChunkIterator<'a> {

let region_is_account;

let mut account_index = self.account_index.unwrap_or_default();
self.account_index = Some(account_index);

loop {
if let Some(account) = self.accounts.get(self.account_index) {
if let Some(account) = self.accounts.get(account_index) {
let account_addr = account.vm_data_addr;
let resize_addr = account_addr.saturating_add(account.original_data_len as u64);

if resize_addr < region.vm_addr {
// region is after this account, move on next one
self.account_index = self.account_index.saturating_add(1);
account_index = account_index.saturating_add(1);
self.account_index = Some(account_index);
} else {
region_is_account =
region.vm_addr == account_addr || region.vm_addr == resize_addr;
Expand Down Expand Up @@ -550,6 +554,41 @@ impl<'a> DoubleEndedIterator for MemoryChunkIterator<'a> {
}
};

let region_is_account;

let mut account_index = self
.account_index
.unwrap_or_else(|| self.accounts.len().saturating_sub(1));
self.account_index = Some(account_index);
Comment on lines +559 to +562
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_or_insert_with()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, this is nicer. Thanks! I'll create a PR on master


loop {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop could use some comments, it took me half an hour to understand what
was going on 😅

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true for a lot of runtime code. I'll review and add some comments

let Some(account) = self.accounts.get(account_index) else {
// address is after all the accounts
region_is_account = false;
break;
};

let account_addr = account.vm_data_addr;
let resize_addr = account_addr.saturating_add(account.original_data_len as u64);

if account_index > 0 && account_addr > region.vm_addr {
account_index = account_index.saturating_sub(1);

self.account_index = Some(account_index);
} else {
region_is_account = region.vm_addr == account_addr || region.vm_addr == resize_addr;
break;
}
}

if let Some(is_account) = self.is_account {
if is_account != region_is_account {
return Some(Err(SyscallError::InvalidLength.into()));
}
} else {
self.is_account = Some(region_is_account);
}

let chunk_len = if region.vm_addr >= self.vm_addr_start {
// consume the whole region
let len = self.vm_addr_end.saturating_sub(region.vm_addr);
Expand Down
15 changes: 14 additions & 1 deletion programs/sbf/rust/account_mem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,20 @@ pub fn process_instruction(
// memmov dst overlaps begin of account
unsafe { sol_memmove(too_early(3).as_mut_ptr(), buf.as_ptr(), 10) };
}

14 => {
// memmove dst overlaps begin of account, reverse order
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad for not reviewing the master PR, but shouldn't these tests all test:

  • success in the boundary condition: dst=too_early(0), src=too_early(0)
  • failure at boundary-1: dst=too_early(0), src=too_early(1)
  • failure at boundary-N: dst=too_early(0), src=too_early(N)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add tests on master

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #4650

unsafe { sol_memmove(too_early(0).as_mut_ptr(), too_early(3).as_ptr(), 10) };
}
15 => {
// memmove dst overlaps end of account, reverse order
unsafe {
sol_memmove(
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which pointer here is causing the failure? I think they are both wrong? what
about the case where dst is ok but src is wrong? and vice versa?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the destination falls within src + length

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data[data_len..].as_mut_ptr() + 10 <- isn't this outside the account region?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I misread your comment. I've added a bunch of tests: #4650

data[data_len..].as_mut_ptr(),
data[data_len.saturating_sub(3)..].as_mut_ptr(),
10,
)
};
}
_ => {}
}

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 @@ -5646,7 +5646,7 @@ 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..=15 {
println!("Testing direct_mapping:{direct_mapping} instruction:{instr}");
let instruction =
Instruction::new_with_bytes(program_id, &[instr], account_metas.clone());
Expand Down
2 changes: 1 addition & 1 deletion sdk/feature-set/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ pub mod apply_cost_tracker_during_replay {
solana_pubkey::declare_id!("2ry7ygxiYURULZCrypHhveanvP5tzZ4toRwVp89oCNSj");
}
pub mod bpf_account_data_direct_mapping {
solana_pubkey::declare_id!("GJVDwRkUPNdk9QaK4VsU4g1N41QNxhy1hevjf8kz45Mq");
solana_pubkey::declare_id!("FNPWmNbHbYy1R8JWVZgCPqsoRBcRu4F6ezSnq5o97Px");
}

pub mod add_set_tx_loaded_accounts_data_size_instruction {
Expand Down