Skip to content

Commit

Permalink
bank: do not remove trailing 0 bytes from return data (solana-labs#33639
Browse files Browse the repository at this point in the history
)

This is creating havoc for Solang, as the return data is borsh encoded
and therefore `u64` values like 0x100 get truncated.
  • Loading branch information
seanyoung authored Oct 13, 2023
1 parent 923d5b5 commit 4751199
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
12 changes: 3 additions & 9 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4955,7 +4955,7 @@ impl Bank {

let ExecutionRecord {
accounts,
mut return_data,
return_data,
touched_account_count,
accounts_resize_delta,
} = transaction_context.into();
Expand All @@ -4977,14 +4977,8 @@ impl Bank {
saturating_add_assign!(timings.details.changed_account_count, touched_account_count);
let accounts_data_len_delta = status.as_ref().map_or(0, |_| accounts_resize_delta);

let return_data = if enable_return_data_recording {
if let Some(end_index) = return_data.data.iter().rposition(|&x| x != 0) {
let end_index = end_index.saturating_add(1);
return_data.data.truncate(end_index);
Some(return_data)
} else {
None
}
let return_data = if enable_return_data_recording && !return_data.data.is_empty() {
Some(return_data)
} else {
None
};
Expand Down
9 changes: 5 additions & 4 deletions runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9713,9 +9713,9 @@ fn test_tx_return_data() {
let mut return_data = [0u8; MAX_RETURN_DATA];
if !instruction_data.is_empty() {
let index = usize::from_le_bytes(instruction_data.try_into().unwrap());
return_data[index] = 1;
return_data[index / 2] = 1;
transaction_context
.set_return_data(mock_program_id, return_data.to_vec())
.set_return_data(mock_program_id, return_data[..index + 1].to_vec())
.unwrap();
}
Ok(())
Expand Down Expand Up @@ -9767,8 +9767,9 @@ fn test_tx_return_data() {
if let Some(index) = index {
let return_data = return_data.unwrap();
assert_eq!(return_data.program_id, mock_program_id);
let mut expected_data = vec![0u8; index];
expected_data.push(1u8);
let mut expected_data = vec![0u8; index + 1];
// include some trailing zeros
expected_data[index / 2] = 1;
assert_eq!(return_data.data, expected_data);
} else {
assert!(return_data.is_none());
Expand Down

0 comments on commit 4751199

Please sign in to comment.