Skip to content

Commit 2b06ec1

Browse files
authored
Revert "Use sol_get_sysvar instead of sol_get_<NAME>_sysvar (#257)" (#455)
1 parent 369f979 commit 2b06ec1

File tree

10 files changed

+49
-247
lines changed

10 files changed

+49
-247
lines changed

define-syscall/src/definitions.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ define_syscall!(fn sol_get_return_data(data: *mut u8, length: u64, program_id: *
4949
// - `is_writable` (`u8`): `true` if the instruction requires the account to be writable
5050
define_syscall!(fn sol_get_processed_sibling_instruction(index: u64, meta: *mut u8, program_id: *mut u8, data: *mut u8, accounts: *mut u8) -> u64);
5151

52-
// these are deprecated - use sol_get_sysvar instead
53-
define_syscall!(#[deprecated(since = "3.0.0", note = "Use `sol_get_sysvar` with `Clock` sysvar address instead")] fn sol_get_clock_sysvar(addr: *mut u8) -> u64);
54-
define_syscall!(#[deprecated(since = "3.0.0", note = "Use `sol_get_sysvar` with `EpochSchedule` sysvar address instead")] fn sol_get_epoch_schedule_sysvar(addr: *mut u8) -> u64);
55-
define_syscall!(#[deprecated(since = "3.0.0", note = "Use `sol_get_sysvar` with `Rent` sysvar address instead")] fn sol_get_rent_sysvar(addr: *mut u8) -> u64);
56-
define_syscall!(#[deprecated(since = "3.0.0", note = "Use `sol_get_sysvar` with `LastRestartSlot` sysvar address instead")] fn sol_get_last_restart_slot(addr: *mut u8) -> u64);
57-
define_syscall!(#[deprecated(since = "3.0.0", note = "Use `sol_get_sysvar` with `EpochRewards` sysvar address instead")] fn sol_get_epoch_rewards_sysvar(addr: *mut u8) -> u64);
52+
// these are to be deprecated once they are superceded by sol_get_sysvar
53+
define_syscall!(fn sol_get_clock_sysvar(addr: *mut u8) -> u64);
54+
define_syscall!(fn sol_get_epoch_schedule_sysvar(addr: *mut u8) -> u64);
55+
define_syscall!(fn sol_get_rent_sysvar(addr: *mut u8) -> u64);
56+
define_syscall!(fn sol_get_last_restart_slot(addr: *mut u8) -> u64);
57+
define_syscall!(fn sol_get_epoch_rewards_sysvar(addr: *mut u8) -> u64);
5858

5959
// this cannot go through sol_get_sysvar but can be removed once no longer in use
6060
define_syscall!(fn sol_get_fees_sysvar(addr: *mut u8) -> u64);

define-syscall/src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ pub mod definitions;
99
))]
1010
#[macro_export]
1111
macro_rules! define_syscall {
12-
($(#[$attr:meta])* fn $name:ident($($arg:ident: $typ:ty),*) -> $ret:ty) => {
13-
$(#[$attr])*
12+
(fn $name:ident($($arg:ident: $typ:ty),*) -> $ret:ty) => {
1413
#[inline]
1514
pub unsafe fn $name($($arg: $typ),*) -> $ret {
1615
// this enum is used to force the hash to be computed in a const context
@@ -24,8 +23,8 @@ macro_rules! define_syscall {
2423
}
2524

2625
};
27-
($(#[$attr:meta])* fn $name:ident($($arg:ident: $typ:ty),*)) => {
28-
define_syscall!($(#[$attr])* fn $name($($arg: $typ),*) -> ());
26+
(fn $name:ident($($arg:ident: $typ:ty),*)) => {
27+
define_syscall!(fn $name($($arg: $typ),*) -> ());
2928
}
3029
}
3130

@@ -35,14 +34,13 @@ macro_rules! define_syscall {
3534
)))]
3635
#[macro_export]
3736
macro_rules! define_syscall {
38-
($(#[$attr:meta])* fn $name:ident($($arg:ident: $typ:ty),*) -> $ret:ty) => {
37+
(fn $name:ident($($arg:ident: $typ:ty),*) -> $ret:ty) => {
3938
extern "C" {
40-
$(#[$attr])*
4139
pub fn $name($($arg: $typ),*) -> $ret;
4240
}
4341
};
44-
($(#[$attr:meta])* fn $name:ident($($arg:ident: $typ:ty),*)) => {
45-
define_syscall!($(#[$attr])* fn $name($($arg: $typ),*) -> ());
42+
(fn $name:ident($($arg:ident: $typ:ty),*)) => {
43+
define_syscall!(fn $name($($arg: $typ),*) -> ());
4644
}
4745
}
4846

sysvar/src/clock.rs

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -130,74 +130,8 @@ pub use {
130130
};
131131

132132
impl Sysvar for Clock {
133-
impl_sysvar_get!(id());
133+
impl_sysvar_get!(sol_get_clock_sysvar);
134134
}
135135

136136
#[cfg(feature = "bincode")]
137137
impl SysvarSerialize for Clock {}
138-
139-
#[cfg(test)]
140-
mod tests {
141-
use {super::*, crate::tests::to_bytes, serial_test::serial};
142-
143-
#[test]
144-
#[serial]
145-
fn test_clock_get_uses_sysvar_syscall() {
146-
let expected = Clock {
147-
slot: 1,
148-
epoch_start_timestamp: 2,
149-
epoch: 3,
150-
leader_schedule_epoch: 4,
151-
unix_timestamp: 5,
152-
};
153-
154-
let data = to_bytes(&expected);
155-
crate::tests::mock_get_sysvar_syscall(&data);
156-
157-
let got = Clock::get().unwrap();
158-
assert_eq!(got, expected);
159-
}
160-
161-
struct ValidateIdSyscall {
162-
data: Vec<u8>,
163-
}
164-
165-
impl crate::program_stubs::SyscallStubs for ValidateIdSyscall {
166-
fn sol_get_sysvar(
167-
&self,
168-
sysvar_id_addr: *const u8,
169-
var_addr: *mut u8,
170-
offset: u64,
171-
length: u64,
172-
) -> u64 {
173-
// Validate that the macro passed the correct sysvar id pointer
174-
let passed_id = unsafe { *(sysvar_id_addr as *const solana_pubkey::Pubkey) };
175-
assert_eq!(passed_id, id());
176-
177-
let slice = unsafe { std::slice::from_raw_parts_mut(var_addr, length as usize) };
178-
slice.copy_from_slice(
179-
&self.data[offset as usize..(offset.saturating_add(length)) as usize],
180-
);
181-
solana_program_entrypoint::SUCCESS
182-
}
183-
}
184-
185-
#[test]
186-
#[serial]
187-
fn test_clock_get_passes_correct_sysvar_id() {
188-
let expected = Clock {
189-
slot: 11,
190-
epoch_start_timestamp: 22,
191-
epoch: 33,
192-
leader_schedule_epoch: 44,
193-
unix_timestamp: 55,
194-
};
195-
let data = to_bytes(&expected);
196-
let prev = crate::program_stubs::set_syscall_stubs(Box::new(ValidateIdSyscall { data }));
197-
198-
let got = Clock::get().unwrap();
199-
assert_eq!(got, expected);
200-
201-
let _ = crate::program_stubs::set_syscall_stubs(prev);
202-
}
203-
}

sysvar/src/epoch_rewards.rs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -163,33 +163,8 @@ pub use {
163163
};
164164

165165
impl Sysvar for EpochRewards {
166-
impl_sysvar_get!(id());
166+
impl_sysvar_get!(sol_get_epoch_rewards_sysvar);
167167
}
168168

169169
#[cfg(feature = "bincode")]
170170
impl SysvarSerialize for EpochRewards {}
171-
172-
#[cfg(test)]
173-
mod tests {
174-
use {super::*, crate::tests::to_bytes, serial_test::serial};
175-
176-
#[test]
177-
#[serial]
178-
fn test_epoch_rewards_get_uses_sysvar_syscall() {
179-
let expected = EpochRewards {
180-
distribution_starting_block_height: 42,
181-
num_partitions: 7,
182-
parent_blockhash: solana_hash::Hash::new_unique(),
183-
total_points: 1234567890,
184-
total_rewards: 100,
185-
distributed_rewards: 10,
186-
active: true,
187-
};
188-
189-
let data = to_bytes(&expected);
190-
crate::tests::mock_get_sysvar_syscall(&data);
191-
192-
let got = EpochRewards::get().unwrap();
193-
assert_eq!(got, expected);
194-
}
195-
}

sysvar/src/epoch_schedule.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,24 +128,8 @@ pub use {
128128
};
129129

130130
impl Sysvar for EpochSchedule {
131-
impl_sysvar_get!(id());
131+
impl_sysvar_get!(sol_get_epoch_schedule_sysvar);
132132
}
133133

134134
#[cfg(feature = "bincode")]
135135
impl SysvarSerialize for EpochSchedule {}
136-
137-
#[cfg(test)]
138-
mod tests {
139-
use {super::*, crate::tests::to_bytes, serial_test::serial};
140-
141-
#[test]
142-
#[serial]
143-
fn test_epoch_schedule_get_uses_sysvar_syscall() {
144-
let expected = EpochSchedule::custom(1234, 5678, false);
145-
let data = to_bytes(&expected);
146-
crate::tests::mock_get_sysvar_syscall(&data);
147-
148-
let got = EpochSchedule::get().unwrap();
149-
assert_eq!(got, expected);
150-
}
151-
}

sysvar/src/fees.rs

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl SysvarSerialize for Fees {}
6464

6565
#[cfg(test)]
6666
mod tests {
67-
use {super::*, serial_test::serial};
67+
use super::*;
6868

6969
#[test]
7070
fn test_clone() {
@@ -76,50 +76,4 @@ mod tests {
7676
let cloned_fees = fees.clone();
7777
assert_eq!(cloned_fees, fees);
7878
}
79-
80-
struct MockFeesSyscall;
81-
impl crate::program_stubs::SyscallStubs for MockFeesSyscall {
82-
fn sol_get_fees_sysvar(&self, var_addr: *mut u8) -> u64 {
83-
let fees = Fees {
84-
fee_calculator: FeeCalculator {
85-
lamports_per_signature: 42,
86-
},
87-
};
88-
unsafe {
89-
std::ptr::copy_nonoverlapping(
90-
&fees as *const _ as *const u8,
91-
var_addr,
92-
core::mem::size_of::<Fees>(),
93-
);
94-
}
95-
solana_program_entrypoint::SUCCESS
96-
}
97-
}
98-
99-
#[test]
100-
#[serial]
101-
fn test_fees_get_deprecated_syscall_path() {
102-
let _ = crate::program_stubs::set_syscall_stubs(Box::new(MockFeesSyscall));
103-
let got = Fees::get().unwrap();
104-
assert_eq!(got.fee_calculator.lamports_per_signature, 42);
105-
}
106-
107-
struct FailFeesSyscall;
108-
impl crate::program_stubs::SyscallStubs for FailFeesSyscall {
109-
fn sol_get_fees_sysvar(&self, _var_addr: *mut u8) -> u64 {
110-
9999
111-
}
112-
}
113-
114-
#[test]
115-
#[serial]
116-
fn test_fees_get_deprecated_non_success_maps_to_unsupported() {
117-
let prev = crate::program_stubs::set_syscall_stubs(Box::new(FailFeesSyscall));
118-
let got = Fees::get();
119-
assert_eq!(
120-
got,
121-
Err(solana_program_error::ProgramError::UnsupportedSysvar)
122-
);
123-
let _ = crate::program_stubs::set_syscall_stubs(prev);
124-
}
12579
}

sysvar/src/last_restart_slot.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,8 @@ pub use {
4545
};
4646

4747
impl Sysvar for LastRestartSlot {
48-
impl_sysvar_get!(id());
48+
impl_sysvar_get!(sol_get_last_restart_slot);
4949
}
5050

5151
#[cfg(feature = "bincode")]
5252
impl SysvarSerialize for LastRestartSlot {}
53-
54-
#[cfg(test)]
55-
mod tests {
56-
use {super::*, crate::tests::to_bytes, serial_test::serial};
57-
58-
#[test]
59-
#[serial]
60-
fn test_last_restart_slot_get_uses_sysvar_syscall() {
61-
let expected = LastRestartSlot {
62-
last_restart_slot: 9999,
63-
};
64-
let data = to_bytes(&expected);
65-
crate::tests::mock_get_sysvar_syscall(&data);
66-
67-
let got = LastRestartSlot::get().unwrap();
68-
assert_eq!(got, expected);
69-
}
70-
}

sysvar/src/lib.rs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,6 @@ pub trait SysvarSerialize:
161161
/// Implements the [`Sysvar::get`] method for both SBF and host targets.
162162
#[macro_export]
163163
macro_rules! impl_sysvar_get {
164-
// DEPRECATED: This variant is only for the deprecated Fees sysvar and should be
165-
// removed once Fees is no longer in use. It uses the old-style direct syscall
166-
// approach instead of the new sol_get_sysvar syscall.
167164
($syscall_name:ident) => {
168165
fn get() -> Result<Self, $crate::__private::ProgramError> {
169166
let mut var = Self::default();
@@ -182,24 +179,6 @@ macro_rules! impl_sysvar_get {
182179
}
183180
}
184181
};
185-
($sysvar_id:expr) => {
186-
fn get() -> Result<Self, $crate::__private::ProgramError> {
187-
// Allocate uninitialized memory for the sysvar struct
188-
let mut uninit = core::mem::MaybeUninit::<Self>::uninit();
189-
let size = core::mem::size_of::<Self>() as u64;
190-
// Safety: we build a mutable slice pointing to the uninitialized
191-
// buffer. The `get_sysvar` syscall will fill exactly `size`
192-
// bytes, after which the buffer is fully initialised.
193-
let dst = unsafe {
194-
core::slice::from_raw_parts_mut(uninit.as_mut_ptr() as *mut u8, size as usize)
195-
};
196-
// Attempt to load the sysvar data using the provided sysvar id.
197-
$crate::get_sysvar(dst, &$sysvar_id, 0, size)?;
198-
// Safety: `get_sysvar` succeeded and initialised the buffer.
199-
let var = unsafe { uninit.assume_init() };
200-
Ok(var)
201-
}
202-
};
203182
}
204183

205184
/// Handler for retrieving a slice of sysvar data from the `sol_get_sysvar`
@@ -290,19 +269,6 @@ mod tests {
290269
}));
291270
}
292271

293-
/// Convert a value to its in-memory byte representation.
294-
///
295-
/// Safety: This relies on the type's plain old data layout. Intended for tests.
296-
pub fn to_bytes<T>(value: &T) -> Vec<u8> {
297-
unsafe {
298-
let size = core::mem::size_of::<T>();
299-
let ptr = (value as *const T) as *const u8;
300-
let mut data = vec![0u8; size];
301-
std::ptr::copy_nonoverlapping(ptr, data.as_mut_ptr(), size);
302-
data
303-
}
304-
}
305-
306272
#[test]
307273
fn test_sysvar_account_info_to_from() {
308274
let test_sysvar = TestSysvar::default();

sysvar/src/program_stubs.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,32 @@ pub(crate) fn sol_get_sysvar(
155155
.sol_get_sysvar(sysvar_id_addr, var_addr, offset, length)
156156
}
157157

158+
pub(crate) fn sol_get_clock_sysvar(var_addr: *mut u8) -> u64 {
159+
SYSCALL_STUBS.read().unwrap().sol_get_clock_sysvar(var_addr)
160+
}
161+
162+
pub(crate) fn sol_get_epoch_schedule_sysvar(var_addr: *mut u8) -> u64 {
163+
SYSCALL_STUBS
164+
.read()
165+
.unwrap()
166+
.sol_get_epoch_schedule_sysvar(var_addr)
167+
}
168+
169+
pub(crate) fn sol_get_fees_sysvar(var_addr: *mut u8) -> u64 {
170+
SYSCALL_STUBS.read().unwrap().sol_get_fees_sysvar(var_addr)
171+
}
172+
173+
pub(crate) fn sol_get_rent_sysvar(var_addr: *mut u8) -> u64 {
174+
SYSCALL_STUBS.read().unwrap().sol_get_rent_sysvar(var_addr)
175+
}
176+
177+
pub(crate) fn sol_get_last_restart_slot(var_addr: *mut u8) -> u64 {
178+
SYSCALL_STUBS
179+
.read()
180+
.unwrap()
181+
.sol_get_last_restart_slot(var_addr)
182+
}
183+
158184
pub fn sol_get_epoch_stake(vote_address: *const u8) -> u64 {
159185
SYSCALL_STUBS
160186
.read()
@@ -185,6 +211,9 @@ pub fn sol_get_stack_height() -> u64 {
185211
SYSCALL_STUBS.read().unwrap().sol_get_stack_height()
186212
}
187213

188-
pub(crate) fn sol_get_fees_sysvar(var_addr: *mut u8) -> u64 {
189-
SYSCALL_STUBS.read().unwrap().sol_get_fees_sysvar(var_addr)
214+
pub(crate) fn sol_get_epoch_rewards_sysvar(var_addr: *mut u8) -> u64 {
215+
SYSCALL_STUBS
216+
.read()
217+
.unwrap()
218+
.sol_get_epoch_rewards_sysvar(var_addr)
190219
}

0 commit comments

Comments
 (0)