Skip to content

Commit

Permalink
Fix CI (#33139)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alessandrod authored Sep 5, 2023
1 parent 3a91d3c commit 25d3db0
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 22 deletions.
1 change: 1 addition & 0 deletions programs/sbf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions programs/sbf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions programs/sbf/rust/invoke/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
1 change: 0 additions & 1 deletion programs/sbf/rust/invoke/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//! Example Rust-based SBF program that issues a cross-program-invocation
pub mod instructions;
pub mod processor;
45 changes: 28 additions & 17 deletions programs/sbf/rust/invoke/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![cfg(feature = "program")]
#![allow(unreachable_code)]
#![allow(clippy::integer_arithmetic)]
#![allow(clippy::arithmetic_side_effects)]

use {
crate::instructions::*,
Expand Down Expand Up @@ -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 _),
);
}
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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 _),
);
}
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
);
}
2 changes: 1 addition & 1 deletion programs/sbf/rust/realloc/src/processor.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions sdk/program/src/bpf_loader_upgradeable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 25d3db0

Please sign in to comment.