From 25d3db0c188aaf43c531e1e067b3511dec9c2ed3 Mon Sep 17 00:00:00 2001 From: Alessandro Decina Date: Wed, 6 Sep 2023 01:01:20 +0700 Subject: [PATCH] Fix CI (#33139) * 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 --- programs/sbf/Cargo.lock | 1 + programs/sbf/Cargo.toml | 2 + programs/sbf/rust/invoke/Cargo.toml | 1 + programs/sbf/rust/invoke/src/lib.rs | 1 - programs/sbf/rust/invoke/src/processor.rs | 45 ++++++++++++++-------- programs/sbf/rust/realloc/src/processor.rs | 2 +- sdk/program/src/bpf_loader_upgradeable.rs | 6 +-- 7 files changed, 36 insertions(+), 22 deletions(-) diff --git a/programs/sbf/Cargo.lock b/programs/sbf/Cargo.lock index 2ab0a3e3d82dae..f1c049055da630 100644 --- a/programs/sbf/Cargo.lock +++ b/programs/sbf/Cargo.lock @@ -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", diff --git a/programs/sbf/Cargo.toml b/programs/sbf/Cargo.toml index d2f3022616e609..04418970594d05 100644 --- a/programs/sbf/Cargo.toml +++ b/programs/sbf/Cargo.toml @@ -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" diff --git a/programs/sbf/rust/invoke/Cargo.toml b/programs/sbf/rust/invoke/Cargo.toml index 66b07e500d897f..3c20ba6cb78f6e 100644 --- a/programs/sbf/rust/invoke/Cargo.toml +++ b/programs/sbf/rust/invoke/Cargo.toml @@ -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 } diff --git a/programs/sbf/rust/invoke/src/lib.rs b/programs/sbf/rust/invoke/src/lib.rs index eb0dde96b85804..f7b0b7bb400e93 100644 --- a/programs/sbf/rust/invoke/src/lib.rs +++ b/programs/sbf/rust/invoke/src/lib.rs @@ -1,4 +1,3 @@ //! Example Rust-based SBF program that issues a cross-program-invocation - pub mod instructions; pub mod processor; diff --git a/programs/sbf/rust/invoke/src/processor.rs b/programs/sbf/rust/invoke/src/processor.rs index 2e1ee8cac9cc42..7c689cfcf860ae 100644 --- a/programs/sbf/rust/invoke/src/processor.rs +++ b/programs/sbf/rust/invoke/src/processor.rs @@ -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>, + overwrite_account_data( + account, Rc::from_raw(((rc_box_addr as usize) + mem::size_of::() * 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>, - 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>, + overwrite_account_data( + account, Rc::from_raw(((rc_box_addr as usize) + mem::size_of::() * 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>, - 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 { weak: usize, value: T, } + +#[rustversion::attr(since(1.72), allow(invalid_reference_casting))] +unsafe fn overwrite_account_data(account: &AccountInfo, data: Rc>) { + std::ptr::write( + &account.data as *const _ as usize as *mut Rc>, + data, + ); +} diff --git a/programs/sbf/rust/realloc/src/processor.rs b/programs/sbf/rust/realloc/src/processor.rs index 172ed7498f88c8..2fb0ca70076d14 100644 --- a/programs/sbf/rust/realloc/src/processor.rs +++ b/programs/sbf/rust/realloc/src/processor.rs @@ -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 { diff --git a/sdk/program/src/bpf_loader_upgradeable.rs b/sdk/program/src/bpf_loader_upgradeable.rs index 870b0006d8d891..907a953d026706 100644 --- a/sdk/program/src/bpf_loader_upgradeable.rs +++ b/sdk/program/src/bpf_loader_upgradeable.rs @@ -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