From 42e9b7023f20536386d31d1b56753f880a7cfe48 Mon Sep 17 00:00:00 2001 From: Tyera Date: Tue, 2 Jul 2024 16:28:44 -0600 Subject: [PATCH 1/2] Handle deprecated Instructions sysvar methods (#1959) * Remove deprecated legacy-message methods (deprecated in v1.9) * Unpub deprecated method load_current_index; dedupe * Unpub deprecated method load_instruction_at; dedupe * Remove allow(deprecated) tags * Make load_instruction_at available to benches (cherry picked from commit adb9d9e3c9ebc1cf5493bff9f8da33e3cf1d7d9c) # Conflicts: # sdk/program/Cargo.toml # sdk/program/src/sysvar/instructions.rs --- Cargo.lock | 1 + sdk/Cargo.toml | 1 + sdk/program/Cargo.toml | 5 ++++ sdk/program/src/sysvar/instructions.rs | 34 +++++++++++++------------- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9d867e1d0462bd..d9fc23cf23236c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6819,6 +6819,7 @@ dependencies = [ "num-derive", "num-traits", "parking_lot 0.12.3", + "qualifier_attr", "rand 0.8.5", "rustc_version 0.4.0", "rustversion", diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index be8560a88a26b5..1f553f66a52272 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -98,6 +98,7 @@ assert_matches = { workspace = true } curve25519-dalek = { workspace = true } hex = { workspace = true } solana-logger = { workspace = true } +solana-program = { workspace = true, features = ["dev-context-only-utils"] } solana-sdk = { path = ".", features = ["dev-context-only-utils"] } static_assertions = { workspace = true } tiny-bip39 = { workspace = true } diff --git a/sdk/program/Cargo.toml b/sdk/program/Cargo.toml index cbfaf3161a8d8b..0d2e4b8dfaa45b 100644 --- a/sdk/program/Cargo.toml +++ b/sdk/program/Cargo.toml @@ -25,7 +25,11 @@ log = { workspace = true } memoffset = { workspace = true } num-derive = { workspace = true } num-traits = { workspace = true, features = ["i128"] } +<<<<<<< HEAD rustversion = { workspace = true } +======= +qualifier_attr = { workspace = true, optional = true } +>>>>>>> adb9d9e3c9 (Handle deprecated Instructions sysvar methods (#1959)) serde = { workspace = true } serde_bytes = { workspace = true } serde_derive = { workspace = true } @@ -92,4 +96,5 @@ crate-type = ["cdylib", "rlib"] [features] default = ["borsh"] borsh = ["dep:borsh", "dep:borsh0-10"] +dev-context-only-utils = ["dep:qualifier_attr"] frozen-abi = ["dep:solana-frozen-abi", "dep:solana-frozen-abi-macro"] diff --git a/sdk/program/src/sysvar/instructions.rs b/sdk/program/src/sysvar/instructions.rs index 249b11b5f9452f..00deb7dc6ed575 100644 --- a/sdk/program/src/sysvar/instructions.rs +++ b/sdk/program/src/sysvar/instructions.rs @@ -29,6 +29,7 @@ #![allow(clippy::arithmetic_side_effects)] +<<<<<<< HEAD use crate::{ account_info::AccountInfo, instruction::{AccountMeta, Instruction}, @@ -37,6 +38,10 @@ use crate::{ sanitize::SanitizeError, serialize_utils::{read_pubkey, read_slice, read_u16, read_u8}, }; +======= +#[cfg(feature = "dev-context-only-utils")] +use qualifier_attr::qualifiers; +>>>>>>> adb9d9e3c9 (Handle deprecated Instructions sysvar methods (#1959)) #[cfg(not(target_os = "solana"))] use { crate::serialize_utils::{append_slice, append_u16, append_u8}, @@ -147,11 +152,10 @@ fn serialize_instructions(instructions: &[BorrowedInstruction]) -> Vec { /// `Transaction`. /// /// `data` is the instructions sysvar account data. -#[deprecated( - since = "1.8.0", - note = "Unsafe because the sysvar accounts address is not checked, please use `load_current_index_checked` instead" -)] -pub fn load_current_index(data: &[u8]) -> u16 { +/// +/// Unsafe because the sysvar accounts address is not checked; only used +/// internally after such a check. +fn load_current_index(data: &[u8]) -> u16 { let mut instr_fixed_data = [0u8; 2]; let len = data.len(); instr_fixed_data.copy_from_slice(&data[len - 2..len]); @@ -172,10 +176,8 @@ pub fn load_current_index_checked( } let instruction_sysvar = instruction_sysvar_account_info.try_borrow_data()?; - let mut instr_fixed_data = [0u8; 2]; - let len = instruction_sysvar.len(); - instr_fixed_data.copy_from_slice(&instruction_sysvar[len - 2..len]); - Ok(u16::from_le_bytes(instr_fixed_data)) + let index = load_current_index(&instruction_sysvar); + Ok(index) } /// Store the current `Instruction`'s index in the instructions sysvar data. @@ -232,11 +234,11 @@ fn deserialize_instruction(index: usize, data: &[u8]) -> Result Result { +/// +/// Unsafe because the sysvar accounts address is not checked; only used +/// internally after such a check. +#[cfg_attr(feature = "dev-context-only-utils", qualifiers(pub))] +fn load_instruction_at(index: usize, data: &[u8]) -> Result { deserialize_instruction(index, data) } @@ -255,7 +257,7 @@ pub fn load_instruction_at_checked( } let instruction_sysvar = instruction_sysvar_account_info.try_borrow_data()?; - deserialize_instruction(index, &instruction_sysvar).map_err(|err| match err { + load_instruction_at(index, &instruction_sysvar).map_err(|err| match err { SanitizeError::IndexOutOfBounds => ProgramError::InvalidArgument, _ => ProgramError::InvalidInstructionData, }) @@ -276,13 +278,11 @@ pub fn get_instruction_relative( } let instruction_sysvar = instruction_sysvar_account_info.data.borrow(); - #[allow(deprecated)] let current_index = load_current_index(&instruction_sysvar) as i64; let index = current_index.saturating_add(index_relative_to_current); if index < 0 { return Err(ProgramError::InvalidArgument); } - #[allow(deprecated)] load_instruction_at( current_index.saturating_add(index_relative_to_current) as usize, &instruction_sysvar, From 33756f699f6a66364e12f27d9779e29b43454dc6 Mon Sep 17 00:00:00 2001 From: Tyera Eulberg Date: Tue, 2 Jul 2024 20:13:06 -0600 Subject: [PATCH 2/2] Fix conflicts --- sdk/program/Cargo.toml | 5 +---- sdk/program/src/sysvar/instructions.rs | 3 --- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/sdk/program/Cargo.toml b/sdk/program/Cargo.toml index 0d2e4b8dfaa45b..c37cd182bbb869 100644 --- a/sdk/program/Cargo.toml +++ b/sdk/program/Cargo.toml @@ -25,11 +25,8 @@ log = { workspace = true } memoffset = { workspace = true } num-derive = { workspace = true } num-traits = { workspace = true, features = ["i128"] } -<<<<<<< HEAD -rustversion = { workspace = true } -======= qualifier_attr = { workspace = true, optional = true } ->>>>>>> adb9d9e3c9 (Handle deprecated Instructions sysvar methods (#1959)) +rustversion = { workspace = true } serde = { workspace = true } serde_bytes = { workspace = true } serde_derive = { workspace = true } diff --git a/sdk/program/src/sysvar/instructions.rs b/sdk/program/src/sysvar/instructions.rs index 00deb7dc6ed575..cf74f3552e59de 100644 --- a/sdk/program/src/sysvar/instructions.rs +++ b/sdk/program/src/sysvar/instructions.rs @@ -29,7 +29,6 @@ #![allow(clippy::arithmetic_side_effects)] -<<<<<<< HEAD use crate::{ account_info::AccountInfo, instruction::{AccountMeta, Instruction}, @@ -38,10 +37,8 @@ use crate::{ sanitize::SanitizeError, serialize_utils::{read_pubkey, read_slice, read_u16, read_u8}, }; -======= #[cfg(feature = "dev-context-only-utils")] use qualifier_attr::qualifiers; ->>>>>>> adb9d9e3c9 (Handle deprecated Instructions sysvar methods (#1959)) #[cfg(not(target_os = "solana"))] use { crate::serialize_utils::{append_slice, append_u16, append_u8},