* programs/sbf: fix invalid_reference_casting errors in tests

* programs/sbf: enable dev-context-only-utils on solana-sdk

* programs/sbf: switch to clippy::arithmetic_side_effects

* solana-program: fix formatting
This commit is contained in:
Alessandro Decina 2023-09-06 01:01:20 +07:00 committed by GitHub
parent 3a91d3cc6a
commit 25d3db0c18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 36 additions and 22 deletions

View File

@ -5753,6 +5753,7 @@ dependencies = [
name = "solana-sbf-rust-invoke"
version = "1.17.0"
dependencies = [
"rustversion",
"solana-program",
"solana-sbf-rust-invoked",
"solana-sbf-rust-realloc",

View File

@ -22,6 +22,7 @@ net2 = "0.2.37"
num-derive = "0.3"
num-traits = "0.2"
rand = "0.8"
rustversion = "1.0.14"
serde = "1.0.112"
serde_json = "1.0.56"
solana_rbpf = "=0.6.0"
@ -101,6 +102,7 @@ solana_rbpf = { workspace = true }
[dev-dependencies]
solana-ledger = { workspace = true }
solana-sdk = { workspace = true, features = ["dev-context-only-utils"] }
[[bench]]
name = "bpf_loader"

View File

@ -14,6 +14,7 @@ default = ["program"]
program = []
[dependencies]
rustversion = { workspace = true }
solana-program = { workspace = true }
solana-sbf-rust-invoked = { workspace = true }
solana-sbf-rust-realloc = { workspace = true }

View File

@ -1,4 +1,3 @@
//! Example Rust-based SBF program that issues a cross-program-invocation
pub mod instructions;
pub mod processor;

View File

@ -2,7 +2,7 @@
#![cfg(feature = "program")]
#![allow(unreachable_code)]
#![allow(clippy::integer_arithmetic)]
#![allow(clippy::arithmetic_side_effects)]
use {
crate::instructions::*,
@ -797,9 +797,10 @@ fn process_instruction(
// AccountDataSizeChanged
let serialized_len_ptr =
unsafe { account.data.borrow_mut().as_mut_ptr().offset(-8) as *mut u64 };
unsafe {
std::ptr::write(
&account.data as *const _ as usize as *mut Rc<RefCell<&mut [u8]>>,
overwrite_account_data(
account,
Rc::from_raw(((rc_box_addr as usize) + mem::size_of::<usize>() * 2) as *mut _),
);
}
@ -836,10 +837,7 @@ fn process_instruction(
// global_deallocator.dealloc(rc_box_addr) which is invalid and
// happens to write a poison value into the account.
unsafe {
std::ptr::write(
&account.data as *const _ as usize as *mut Rc<RefCell<&mut [u8]>>,
Rc::new(RefCell::new(&mut [])),
);
overwrite_account_data(account, Rc::new(RefCell::new(&mut [])));
}
}
TEST_FORBID_LEN_UPDATE_AFTER_OWNERSHIP_CHANGE => {
@ -886,8 +884,8 @@ fn process_instruction(
// allows us to test having CallerAccount::ref_to_len_in_vm in an
// account region.
unsafe {
std::ptr::write(
&account.data as *const _ as usize as *mut Rc<RefCell<&mut [u8]>>,
overwrite_account_data(
account,
Rc::from_raw(((rc_box_addr as usize) + mem::size_of::<usize>() * 2) as *mut _),
);
}
@ -920,10 +918,7 @@ fn process_instruction(
// global_deallocator.dealloc(rc_box_addr) which is invalid and
// happens to write a poison value into the account.
unsafe {
std::ptr::write(
&account.data as *const _ as usize as *mut Rc<RefCell<&mut [u8]>>,
Rc::new(RefCell::new(&mut [])),
);
overwrite_account_data(account, Rc::new(RefCell::new(&mut [])));
}
}
TEST_ALLOW_WRITE_AFTER_OWNERSHIP_CHANGE_TO_CALLER => {
@ -1133,9 +1128,13 @@ fn process_instruction(
let account = &accounts[ARGUMENT_INDEX];
let key = *account.key;
let key = &key as *const _ as usize;
unsafe {
*mem::transmute::<_, *mut *const Pubkey>(&account.key) = key as *const Pubkey;
#[rustversion::attr(since(1.72), allow(invalid_reference_casting))]
fn overwrite_account_key(account: &AccountInfo, key: *const Pubkey) {
unsafe {
*mem::transmute::<_, *mut *const Pubkey>(&account.key) = key;
}
}
overwrite_account_key(account, key as *const Pubkey);
let callee_program_id = accounts[CALLEE_PROGRAM_INDEX].key;
invoke(
@ -1179,9 +1178,13 @@ fn process_instruction(
const CALLEE_PROGRAM_INDEX: usize = 2;
let account = &accounts[ARGUMENT_INDEX];
let owner = account.owner as *const _ as usize + 1;
unsafe {
*mem::transmute::<_, *mut *const Pubkey>(&account.owner) = owner as *const Pubkey;
#[rustversion::attr(since(1.72), allow(invalid_reference_casting))]
fn overwrite_account_owner(account: &AccountInfo, owner: *const Pubkey) {
unsafe {
*mem::transmute::<_, *mut *const Pubkey>(&account.owner) = owner;
}
}
overwrite_account_owner(account, owner as *const Pubkey);
let callee_program_id = accounts[CALLEE_PROGRAM_INDEX].key;
invoke(
@ -1303,3 +1306,11 @@ struct RcBox<T> {
weak: usize,
value: T,
}
#[rustversion::attr(since(1.72), allow(invalid_reference_casting))]
unsafe fn overwrite_account_data(account: &AccountInfo, data: Rc<RefCell<&mut [u8]>>) {
std::ptr::write(
&account.data as *const _ as usize as *mut Rc<RefCell<&mut [u8]>>,
data,
);
}

View File

@ -1,7 +1,7 @@
//! Example Rust-based SBF realloc test program
#![cfg(feature = "program")]
#![allow(clippy::integer_arithmetic)]
#![allow(clippy::arithmetic_side_effects)]
extern crate solana_program;
use {

View File

@ -3,9 +3,9 @@
//! The upgradeable BPF loader is responsible for deploying, upgrading, and
//! executing BPF programs. The upgradeable loader allows a program's authority
//! to update the program at any time. This ability breaks the "code is law"
//! contract that once a program is on-chain it is immutable. Because of this,
//! care should be taken before executing upgradeable programs which still have
//! a functioning authority. For more information refer to the
//! contract that once a program is on-chain it is immutable. Because of this,
//! care should be taken before executing upgradeable programs which still have
//! a functioning authority. For more information refer to the
//! [`loader_upgradeable_instruction`] module.
//!
//! The `solana program deploy` CLI command uses the