From 21e066ef263df86ff260ccc69285c64ddfba0dac Mon Sep 17 00:00:00 2001 From: Jack May Date: Mon, 16 May 2022 12:30:37 -0700 Subject: [PATCH] Merge pull request from GHSA-wxrh-ff9f-fc6w --- account-decoder/src/parse_sysvar.rs | 2 +- client/src/blockhash_query.rs | 10 +-- programs/bpf_loader/src/syscalls.rs | 96 +++++++++++++++++++++-------- runtime/src/bank.rs | 10 +-- sdk/program/src/clock.rs | 40 +++++++++++- sdk/program/src/epoch_schedule.rs | 35 ++++++++++- sdk/program/src/fee_calculator.rs | 2 +- sdk/program/src/lib.rs | 19 ++++++ sdk/program/src/rent.rs | 34 +++++++++- sdk/program/src/sysvar/fees.rs | 40 ++++++++++-- 10 files changed, 244 insertions(+), 44 deletions(-) diff --git a/account-decoder/src/parse_sysvar.rs b/account-decoder/src/parse_sysvar.rs index e5896f9977..b52e0e0476 100644 --- a/account-decoder/src/parse_sysvar.rs +++ b/account-decoder/src/parse_sysvar.rs @@ -40,7 +40,7 @@ pub fn parse_sysvar(data: &[u8], pubkey: &Pubkey) -> Result(zero_init: bool) -> T { + let mut val = T::default(); + let p = &mut val as *mut _ as *mut u8; + for i in 0..(size_of::() as isize) { + unsafe { + *p.offset(i) = if zero_init { 0 } else { i as u8 }; + } + } + val + } + + fn are_bytes_equal(first: &T, second: &T) -> bool { + let p_first = first as *const _ as *const u8; + let p_second = second as *const _ as *const u8; + for i in 0..(size_of::() as isize) { + unsafe { + if *p_first.offset(i) != *p_second.offset(i) { + return false; + } + } + } + true + } + #[test] #[allow(deprecated)] fn test_syscall_get_sysvar() { let config = Config::default(); - let src_clock = Clock { - slot: 1, - epoch_start_timestamp: 2, - epoch: 3, - leader_schedule_epoch: 4, - unix_timestamp: 5, - }; - let src_epochschedule = EpochSchedule { - slots_per_epoch: 1, - leader_schedule_slot_offset: 2, - warmup: false, - first_normal_epoch: 3, - first_normal_slot: 4, - }; - let src_fees = Fees { - fee_calculator: FeeCalculator { - lamports_per_signature: 1, - }, - }; - let src_rent = Rent { - lamports_per_byte_year: 1, - exemption_threshold: 2.0, - burn_percent: 3, + + let mut src_clock = create_filled_type::(false); + src_clock.slot = 1; + src_clock.epoch_start_timestamp = 2; + src_clock.epoch = 3; + src_clock.leader_schedule_epoch = 4; + src_clock.unix_timestamp = 5; + let mut src_epochschedule = create_filled_type::(false); + src_epochschedule.slots_per_epoch = 1; + src_epochschedule.leader_schedule_slot_offset = 2; + src_epochschedule.warmup = false; + src_epochschedule.first_normal_epoch = 3; + src_epochschedule.first_normal_slot = 4; + let mut src_fees = create_filled_type::(false); + src_fees.fee_calculator = FeeCalculator { + lamports_per_signature: 1, }; + let mut src_rent = create_filled_type::(false); + src_rent.lamports_per_byte_year = 1; + src_rent.exemption_threshold = 2.0; + src_rent.burn_percent = 3; let mut sysvar_cache = SysvarCache::default(); sysvar_cache.set_clock(src_clock.clone()); @@ -4680,6 +4701,14 @@ mod tests { syscall.call(got_clock_va, 0, 0, 0, 0, &memory_mapping, &mut result); result.unwrap(); assert_eq!(got_clock, src_clock); + + let mut clean_clock = create_filled_type::(true); + clean_clock.slot = src_clock.slot; + clean_clock.epoch_start_timestamp = src_clock.epoch_start_timestamp; + clean_clock.epoch = src_clock.epoch; + clean_clock.leader_schedule_epoch = src_clock.leader_schedule_epoch; + clean_clock.unix_timestamp = src_clock.unix_timestamp; + assert!(are_bytes_equal(&got_clock, &clean_clock)); } // Test epoch_schedule sysvar @@ -4717,6 +4746,15 @@ mod tests { ); result.unwrap(); assert_eq!(got_epochschedule, src_epochschedule); + + let mut clean_epochschedule = create_filled_type::(true); + clean_epochschedule.slots_per_epoch = src_epochschedule.slots_per_epoch; + clean_epochschedule.leader_schedule_slot_offset = + src_epochschedule.leader_schedule_slot_offset; + clean_epochschedule.warmup = src_epochschedule.warmup; + clean_epochschedule.first_normal_epoch = src_epochschedule.first_normal_epoch; + clean_epochschedule.first_normal_slot = src_epochschedule.first_normal_slot; + assert!(are_bytes_equal(&got_epochschedule, &clean_epochschedule)); } // Test fees sysvar @@ -4746,11 +4784,15 @@ mod tests { syscall.call(got_fees_va, 0, 0, 0, 0, &memory_mapping, &mut result); result.unwrap(); assert_eq!(got_fees, src_fees); + + let mut clean_fees = create_filled_type::(true); + clean_fees.fee_calculator = src_fees.fee_calculator; + assert!(are_bytes_equal(&got_fees, &clean_fees)); } // Test rent sysvar { - let got_rent = Rent::default(); + let got_rent = create_filled_type::(true); let got_rent_va = 0x100000000; let memory_mapping = MemoryMapping::new::( @@ -4775,6 +4817,12 @@ mod tests { syscall.call(got_rent_va, 0, 0, 0, 0, &memory_mapping, &mut result); result.unwrap(); assert_eq!(got_rent, src_rent); + + let mut clean_rent = create_filled_type::(true); + clean_rent.lamports_per_byte_year = src_rent.lamports_per_byte_year; + clean_rent.exemption_threshold = src_rent.exemption_threshold; + clean_rent.burn_percent = src_rent.burn_percent; + assert!(are_bytes_equal(&got_rent, &clean_rent)); } } diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index d1935a97fe..23d2063d76 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -2266,7 +2266,7 @@ impl Bank { block_height: self.block_height, collector_id: self.collector_id, collector_fees: self.collector_fees.load(Relaxed), - fee_calculator: self.fee_calculator.clone(), + fee_calculator: self.fee_calculator, fee_rate_governor: self.fee_rate_governor.clone(), collected_rent: self.collected_rent.load(Relaxed), rent_collector: self.rent_collector.clone(), @@ -12751,7 +12751,7 @@ pub(crate) mod tests { StateMut::::state(&acc).map(|v| v.convert_to_current()); match state { Ok(nonce::State::Initialized(ref data)) => { - Some((data.blockhash, data.fee_calculator.clone())) + Some((data.blockhash, data.fee_calculator)) } _ => None, } @@ -12788,7 +12788,7 @@ pub(crate) mod tests { StateMut::::state(&acc).map(|v| v.convert_to_current()); match state { Ok(nonce::State::Initialized(ref data)) => { - Some((data.blockhash, data.fee_calculator.clone())) + Some((data.blockhash, data.fee_calculator)) } _ => None, } @@ -12821,7 +12821,7 @@ pub(crate) mod tests { StateMut::::state(&acc).map(|v| v.convert_to_current()); match state { Ok(nonce::State::Initialized(ref data)) => { - Some((data.blockhash, data.fee_calculator.clone())) + Some((data.blockhash, data.fee_calculator)) } _ => None, } @@ -12858,7 +12858,7 @@ pub(crate) mod tests { StateMut::::state(&acc).map(|v| v.convert_to_current()); match state { Ok(nonce::State::Initialized(ref data)) => { - Some((data.blockhash, data.fee_calculator.clone())) + Some((data.blockhash, data.fee_calculator)) } _ => None, } diff --git a/sdk/program/src/clock.rs b/sdk/program/src/clock.rs index cc15900028..2ef2538f62 100644 --- a/sdk/program/src/clock.rs +++ b/sdk/program/src/clock.rs @@ -1,5 +1,10 @@ //! Information about the network's clock, ticks, slots, etc. +use { + crate::{clone_zeroed, copy_field}, + std::mem::MaybeUninit, +}; + // The default tick rate that the cluster attempts to achieve. Note that the actual tick // rate at any given time should be expected to drift pub const DEFAULT_TICKS_PER_SECOND: u64 = 160; @@ -105,7 +110,7 @@ pub type UnixTimestamp = i64; /// as the network progresses). /// #[repr(C)] -#[derive(Serialize, Clone, Deserialize, Debug, Default, PartialEq)] +#[derive(Serialize, Deserialize, Debug, Default, PartialEq)] pub struct Clock { /// the current network/bank Slot pub slot: Slot, @@ -121,3 +126,36 @@ pub struct Clock { /// timestamp_correction and timestamp_bounding features pub unix_timestamp: UnixTimestamp, } + +impl Clone for Clock { + fn clone(&self) -> Self { + clone_zeroed(|cloned: &mut MaybeUninit| { + let ptr = cloned.as_mut_ptr(); + unsafe { + copy_field!(ptr, self, slot); + copy_field!(ptr, self, epoch_start_timestamp); + copy_field!(ptr, self, epoch); + copy_field!(ptr, self, leader_schedule_epoch); + copy_field!(ptr, self, unix_timestamp); + } + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_clone() { + let clock = Clock { + slot: 1, + epoch_start_timestamp: 2, + epoch: 3, + leader_schedule_epoch: 4, + unix_timestamp: 5, + }; + let cloned_clock = clock.clone(); + assert_eq!(cloned_clock, clock); + } +} diff --git a/sdk/program/src/epoch_schedule.rs b/sdk/program/src/epoch_schedule.rs index a8e6456bbc..2810e66d73 100644 --- a/sdk/program/src/epoch_schedule.rs +++ b/sdk/program/src/epoch_schedule.rs @@ -2,6 +2,10 @@ /// 1 Epoch = 400 * 8192 ms ~= 55 minutes pub use crate::clock::{Epoch, Slot, DEFAULT_SLOTS_PER_EPOCH}; +use { + crate::{clone_zeroed, copy_field}, + std::mem::MaybeUninit, +}; /// The number of slots before an epoch starts to calculate the leader schedule. /// Default is an entire epoch, i.e. leader schedule for epoch X is calculated at @@ -17,7 +21,7 @@ pub const MAX_LEADER_SCHEDULE_EPOCH_OFFSET: u64 = 3; pub const MINIMUM_SLOTS_PER_EPOCH: u64 = 32; #[repr(C)] -#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize, AbiExample)] +#[derive(Debug, Copy, PartialEq, Deserialize, Serialize, AbiExample)] #[serde(rename_all = "camelCase")] pub struct EpochSchedule { /// The maximum number of slots in each epoch. @@ -47,6 +51,21 @@ impl Default for EpochSchedule { } } +impl Clone for EpochSchedule { + fn clone(&self) -> Self { + clone_zeroed(|cloned: &mut MaybeUninit| { + let ptr = cloned.as_mut_ptr(); + unsafe { + copy_field!(ptr, self, slots_per_epoch); + copy_field!(ptr, self, leader_schedule_slot_offset); + copy_field!(ptr, self, warmup); + copy_field!(ptr, self, first_normal_epoch); + copy_field!(ptr, self, first_normal_slot); + } + }) + } +} + impl EpochSchedule { pub fn new(slots_per_epoch: u64) -> Self { Self::custom(slots_per_epoch, slots_per_epoch, true) @@ -229,4 +248,18 @@ mod tests { assert!(last_slots_in_epoch == slots_per_epoch); } } + + #[test] + fn test_clone() { + let epoch_schedule = EpochSchedule { + slots_per_epoch: 1, + leader_schedule_slot_offset: 2, + warmup: true, + first_normal_epoch: 4, + first_normal_slot: 5, + }; + #[allow(clippy::clone_on_copy)] + let cloned_epoch_schedule = epoch_schedule.clone(); + assert_eq!(cloned_epoch_schedule, epoch_schedule); + } } diff --git a/sdk/program/src/fee_calculator.rs b/sdk/program/src/fee_calculator.rs index 3dd8078bf1..5c91cd94c0 100644 --- a/sdk/program/src/fee_calculator.rs +++ b/sdk/program/src/fee_calculator.rs @@ -6,7 +6,7 @@ use { log::*, }; -#[derive(Serialize, Deserialize, Default, PartialEq, Eq, Clone, Debug, AbiExample)] +#[derive(Serialize, Deserialize, Default, PartialEq, Eq, Clone, Copy, Debug, AbiExample)] #[serde(rename_all = "camelCase")] pub struct FeeCalculator { /// The current cost of a signature. diff --git a/sdk/program/src/lib.rs b/sdk/program/src/lib.rs index aa33948436..15437da9c7 100644 --- a/sdk/program/src/lib.rs +++ b/sdk/program/src/lib.rs @@ -798,6 +798,25 @@ macro_rules! unchecked_div_by_const { }}; } +use std::{mem::MaybeUninit, ptr::write_bytes}; + +#[macro_export] +macro_rules! copy_field { + ($ptr:expr, $self:ident, $field:ident) => { + std::ptr::addr_of_mut!((*$ptr).$field).write($self.$field) + }; +} + +pub fn clone_zeroed(clone: F) -> T +where + F: Fn(&mut MaybeUninit), +{ + let mut value = MaybeUninit::::uninit(); + unsafe { write_bytes(&mut value, 0, 1) } + clone(&mut value); + unsafe { value.assume_init() } +} + // This module is purposefully listed after all other exports: because of an // interaction within rustdoc between the reexports inside this module of // `solana_program`'s top-level modules, and `solana_sdk`'s glob re-export of diff --git a/sdk/program/src/rent.rs b/sdk/program/src/rent.rs index 773da85a20..3a9ef33fd9 100644 --- a/sdk/program/src/rent.rs +++ b/sdk/program/src/rent.rs @@ -3,10 +3,15 @@ //! [rent]: https://docs.solana.com/implemented-proposals/rent #![allow(clippy::integer_arithmetic)] -use crate::clock::DEFAULT_SLOTS_PER_EPOCH; +//! configuration for network rent + +use { + crate::{clock::DEFAULT_SLOTS_PER_EPOCH, clone_zeroed, copy_field}, + std::mem::MaybeUninit, +}; #[repr(C)] -#[derive(Serialize, Deserialize, PartialEq, Clone, Copy, Debug, AbiExample)] +#[derive(Serialize, Deserialize, PartialEq, Copy, Debug, AbiExample)] pub struct Rent { /// Rental rate pub lamports_per_byte_year: u64, @@ -44,6 +49,19 @@ impl Default for Rent { } } +impl Clone for Rent { + fn clone(&self) -> Self { + clone_zeroed(|cloned: &mut MaybeUninit| { + let ptr = cloned.as_mut_ptr(); + unsafe { + copy_field!(ptr, self, lamports_per_byte_year); + copy_field!(ptr, self, exemption_threshold); + copy_field!(ptr, self, burn_percent); + } + }) + } +} + impl Rent { /// calculate how much rent to burn from the collected rent pub fn calculate_burn(&self, rent_collected: u64) -> (u64, u64) { @@ -191,4 +209,16 @@ mod tests { assert!(RentDue::Exempt.is_exempt()); assert!(!RentDue::Paying(0).is_exempt()); } + + #[test] + fn test_clone() { + let rent = Rent { + lamports_per_byte_year: 1, + exemption_threshold: 2.2, + burn_percent: 3, + }; + #[allow(clippy::clone_on_copy)] + let cloned_rent = rent.clone(); + assert_eq!(cloned_rent, rent); + } } diff --git a/sdk/program/src/sysvar/fees.rs b/sdk/program/src/sysvar/fees.rs index 24a8823953..5675901f7a 100644 --- a/sdk/program/src/sysvar/fees.rs +++ b/sdk/program/src/sysvar/fees.rs @@ -2,8 +2,12 @@ //! #![allow(deprecated)] -use crate::{ - fee_calculator::FeeCalculator, impl_sysvar_get, program_error::ProgramError, sysvar::Sysvar, +use { + crate::{ + clone_zeroed, copy_field, fee_calculator::FeeCalculator, impl_sysvar_get, + program_error::ProgramError, sysvar::Sysvar, + }, + std::mem::MaybeUninit, }; crate::declare_deprecated_sysvar_id!("SysvarFees111111111111111111111111111111111", Fees); @@ -13,15 +17,27 @@ crate::declare_deprecated_sysvar_id!("SysvarFees11111111111111111111111111111111 note = "Please do not use, will no longer be available in the future" )] #[repr(C)] -#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)] +#[derive(Serialize, Deserialize, Debug, Default, PartialEq)] pub struct Fees { pub fee_calculator: FeeCalculator, } + +impl Clone for Fees { + fn clone(&self) -> Self { + clone_zeroed(|cloned: &mut MaybeUninit| { + let ptr = cloned.as_mut_ptr(); + unsafe { + copy_field!(ptr, self, fee_calculator); + } + }) + } +} + impl Fees { pub fn new(fee_calculator: &FeeCalculator) -> Self { #[allow(deprecated)] Self { - fee_calculator: fee_calculator.clone(), + fee_calculator: *fee_calculator, } } } @@ -29,3 +45,19 @@ impl Fees { impl Sysvar for Fees { impl_sysvar_get!(sol_get_fees_sysvar); } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_clone() { + let fees = Fees { + fee_calculator: FeeCalculator { + lamports_per_signature: 1, + }, + }; + let cloned_fees = fees.clone(); + assert_eq!(cloned_fees, fees); + } +}