solana_program/sysvar/
instructions.rs

1//! The serialized instructions of the current transaction.
2//!
3//! The _instructions sysvar_ provides access to the serialized instruction data
4//! for the currently-running transaction. This allows for [instruction
5//! introspection][in], which is required for correctly interoperating with
6//! native programs like the [secp256k1] and [ed25519] programs.
7//!
8//! [in]: https://docs.solanalabs.com/implemented-proposals/instruction_introspection
9//! [secp256k1]: crate::secp256k1_program
10//! [ed25519]: crate::ed25519_program
11//!
12//! Unlike other sysvars, the data in the instructions sysvar is not accessed
13//! through a type that implements the [`Sysvar`] trait. Instead, the
14//! instruction sysvar is accessed through several free functions within this
15//! module.
16//!
17//! [`Sysvar`]: crate::sysvar::Sysvar
18//!
19//! See also the Solana [documentation on the instructions sysvar][sdoc].
20//!
21//! [sdoc]: https://docs.solanalabs.com/runtime/sysvars#instructions
22//!
23//! # Examples
24//!
25//! For a complete example of how the instructions sysvar is used see the
26//! documentation for [`secp256k1_instruction`] in the `solana-sdk` crate.
27//!
28//! [`secp256k1_instruction`]: https://docs.rs/solana-sdk/latest/solana_sdk/secp256k1_instruction/index.html
29
30#![allow(clippy::arithmetic_side_effects)]
31
32#[cfg(feature = "dev-context-only-utils")]
33use qualifier_attr::qualifiers;
34#[cfg(not(target_os = "solana"))]
35use {
36    crate::serialize_utils::{append_slice, append_u16, append_u8},
37    bitflags::bitflags,
38};
39use {
40    crate::{
41        account_info::AccountInfo,
42        instruction::{AccountMeta, Instruction},
43        program_error::ProgramError,
44        pubkey::Pubkey,
45        serialize_utils::{read_pubkey, read_slice, read_u16, read_u8},
46    },
47    solana_sanitize::SanitizeError,
48    solana_sysvar_id::declare_sysvar_id,
49};
50
51/// Instructions sysvar, dummy type.
52///
53/// This type exists for consistency with other sysvar modules, but is a dummy
54/// type that does not contain sysvar data. It implements the [`SysvarId`] trait
55/// but does not implement the [`Sysvar`] trait.
56///
57/// [`SysvarId`]: crate::sysvar::SysvarId
58/// [`Sysvar`]: crate::sysvar::Sysvar
59///
60/// Use the free functions in this module to access the instructions sysvar.
61pub struct Instructions();
62
63declare_sysvar_id!("Sysvar1nstructions1111111111111111111111111", Instructions);
64
65/// Construct the account data for the instructions sysvar.
66///
67/// This function is used by the runtime and not available to Solana programs.
68#[cfg(not(target_os = "solana"))]
69pub fn construct_instructions_data(instructions: &[BorrowedInstruction]) -> Vec<u8> {
70    let mut data = serialize_instructions(instructions);
71    // add room for current instruction index.
72    data.resize(data.len() + 2, 0);
73
74    data
75}
76
77/// Borrowed version of `AccountMeta`.
78///
79/// This struct is used by the runtime when constructing the sysvar. It is not
80/// useful to Solana programs.
81pub struct BorrowedAccountMeta<'a> {
82    pub pubkey: &'a Pubkey,
83    pub is_signer: bool,
84    pub is_writable: bool,
85}
86
87/// Borrowed version of `Instruction`.
88///
89/// This struct is used by the runtime when constructing the sysvar. It is not
90/// useful to Solana programs.
91pub struct BorrowedInstruction<'a> {
92    pub program_id: &'a Pubkey,
93    pub accounts: Vec<BorrowedAccountMeta<'a>>,
94    pub data: &'a [u8],
95}
96
97#[cfg(not(target_os = "solana"))]
98bitflags! {
99    struct InstructionsSysvarAccountMeta: u8 {
100        const IS_SIGNER = 0b00000001;
101        const IS_WRITABLE = 0b00000010;
102    }
103}
104
105// First encode the number of instructions:
106// [0..2 - num_instructions
107//
108// Then a table of offsets of where to find them in the data
109//  3..2 * num_instructions table of instruction offsets
110//
111// Each instruction is then encoded as:
112//   0..2 - num_accounts
113//   2 - meta_byte -> (bit 0 signer, bit 1 is_writable)
114//   3..35 - pubkey - 32 bytes
115//   35..67 - program_id
116//   67..69 - data len - u16
117//   69..data_len - data
118#[cfg(not(target_os = "solana"))]
119fn serialize_instructions(instructions: &[BorrowedInstruction]) -> Vec<u8> {
120    // 64 bytes is a reasonable guess, calculating exactly is slower in benchmarks
121    let mut data = Vec::with_capacity(instructions.len() * (32 * 2));
122    append_u16(&mut data, instructions.len() as u16);
123    for _ in 0..instructions.len() {
124        append_u16(&mut data, 0);
125    }
126
127    for (i, instruction) in instructions.iter().enumerate() {
128        let start_instruction_offset = data.len() as u16;
129        let start = 2 + (2 * i);
130        data[start..start + 2].copy_from_slice(&start_instruction_offset.to_le_bytes());
131        append_u16(&mut data, instruction.accounts.len() as u16);
132        for account_meta in &instruction.accounts {
133            let mut account_meta_flags = InstructionsSysvarAccountMeta::empty();
134            if account_meta.is_signer {
135                account_meta_flags |= InstructionsSysvarAccountMeta::IS_SIGNER;
136            }
137            if account_meta.is_writable {
138                account_meta_flags |= InstructionsSysvarAccountMeta::IS_WRITABLE;
139            }
140            append_u8(&mut data, account_meta_flags.bits());
141            append_slice(&mut data, account_meta.pubkey.as_ref());
142        }
143
144        append_slice(&mut data, instruction.program_id.as_ref());
145        append_u16(&mut data, instruction.data.len() as u16);
146        append_slice(&mut data, instruction.data);
147    }
148    data
149}
150
151/// Load the current `Instruction`'s index in the currently executing
152/// `Transaction`.
153///
154/// `data` is the instructions sysvar account data.
155///
156/// Unsafe because the sysvar accounts address is not checked; only used
157/// internally after such a check.
158fn load_current_index(data: &[u8]) -> u16 {
159    let mut instr_fixed_data = [0u8; 2];
160    let len = data.len();
161    instr_fixed_data.copy_from_slice(&data[len - 2..len]);
162    u16::from_le_bytes(instr_fixed_data)
163}
164
165/// Load the current `Instruction`'s index in the currently executing
166/// `Transaction`.
167///
168/// # Errors
169///
170/// Returns [`ProgramError::UnsupportedSysvar`] if the given account's ID is not equal to [`ID`].
171pub fn load_current_index_checked(
172    instruction_sysvar_account_info: &AccountInfo,
173) -> Result<u16, ProgramError> {
174    if !check_id(instruction_sysvar_account_info.key) {
175        return Err(ProgramError::UnsupportedSysvar);
176    }
177
178    let instruction_sysvar = instruction_sysvar_account_info.try_borrow_data()?;
179    let index = load_current_index(&instruction_sysvar);
180    Ok(index)
181}
182
183/// Store the current `Instruction`'s index in the instructions sysvar data.
184pub fn store_current_index(data: &mut [u8], instruction_index: u16) {
185    let last_index = data.len() - 2;
186    data[last_index..last_index + 2].copy_from_slice(&instruction_index.to_le_bytes());
187}
188
189fn deserialize_instruction(index: usize, data: &[u8]) -> Result<Instruction, SanitizeError> {
190    const IS_SIGNER_BIT: usize = 0;
191    const IS_WRITABLE_BIT: usize = 1;
192
193    let mut current = 0;
194    let num_instructions = read_u16(&mut current, data)?;
195    if index >= num_instructions as usize {
196        return Err(SanitizeError::IndexOutOfBounds);
197    }
198
199    // index into the instruction byte-offset table.
200    current += index * 2;
201    let start = read_u16(&mut current, data)?;
202
203    current = start as usize;
204    let num_accounts = read_u16(&mut current, data)?;
205    let mut accounts = Vec::with_capacity(num_accounts as usize);
206    for _ in 0..num_accounts {
207        let meta_byte = read_u8(&mut current, data)?;
208        let mut is_signer = false;
209        let mut is_writable = false;
210        if meta_byte & (1 << IS_SIGNER_BIT) != 0 {
211            is_signer = true;
212        }
213        if meta_byte & (1 << IS_WRITABLE_BIT) != 0 {
214            is_writable = true;
215        }
216        let pubkey = read_pubkey(&mut current, data)?;
217        accounts.push(AccountMeta {
218            pubkey,
219            is_signer,
220            is_writable,
221        });
222    }
223    let program_id = read_pubkey(&mut current, data)?;
224    let data_len = read_u16(&mut current, data)?;
225    let data = read_slice(&mut current, data, data_len as usize)?;
226    Ok(Instruction {
227        program_id,
228        accounts,
229        data,
230    })
231}
232
233/// Load an `Instruction` in the currently executing `Transaction` at the
234/// specified index.
235///
236/// `data` is the instructions sysvar account data.
237///
238/// Unsafe because the sysvar accounts address is not checked; only used
239/// internally after such a check.
240#[cfg_attr(feature = "dev-context-only-utils", qualifiers(pub))]
241fn load_instruction_at(index: usize, data: &[u8]) -> Result<Instruction, SanitizeError> {
242    deserialize_instruction(index, data)
243}
244
245/// Load an `Instruction` in the currently executing `Transaction` at the
246/// specified index.
247///
248/// # Errors
249///
250/// Returns [`ProgramError::UnsupportedSysvar`] if the given account's ID is not equal to [`ID`].
251pub fn load_instruction_at_checked(
252    index: usize,
253    instruction_sysvar_account_info: &AccountInfo,
254) -> Result<Instruction, ProgramError> {
255    if !check_id(instruction_sysvar_account_info.key) {
256        return Err(ProgramError::UnsupportedSysvar);
257    }
258
259    let instruction_sysvar = instruction_sysvar_account_info.try_borrow_data()?;
260    load_instruction_at(index, &instruction_sysvar).map_err(|err| match err {
261        SanitizeError::IndexOutOfBounds => ProgramError::InvalidArgument,
262        _ => ProgramError::InvalidInstructionData,
263    })
264}
265
266/// Returns the `Instruction` relative to the current `Instruction` in the
267/// currently executing `Transaction`.
268///
269/// # Errors
270///
271/// Returns [`ProgramError::UnsupportedSysvar`] if the given account's ID is not equal to [`ID`].
272pub fn get_instruction_relative(
273    index_relative_to_current: i64,
274    instruction_sysvar_account_info: &AccountInfo,
275) -> Result<Instruction, ProgramError> {
276    if !check_id(instruction_sysvar_account_info.key) {
277        return Err(ProgramError::UnsupportedSysvar);
278    }
279
280    let instruction_sysvar = instruction_sysvar_account_info.data.borrow();
281    let current_index = load_current_index(&instruction_sysvar) as i64;
282    let index = current_index.saturating_add(index_relative_to_current);
283    if index < 0 {
284        return Err(ProgramError::InvalidArgument);
285    }
286    load_instruction_at(
287        current_index.saturating_add(index_relative_to_current) as usize,
288        &instruction_sysvar,
289    )
290    .map_err(|err| match err {
291        SanitizeError::IndexOutOfBounds => ProgramError::InvalidArgument,
292        _ => ProgramError::InvalidInstructionData,
293    })
294}
295
296#[cfg(test)]
297mod tests {
298    use {
299        super::*,
300        crate::{
301            instruction::AccountMeta,
302            message::{Message as LegacyMessage, SanitizedMessage},
303            pubkey::Pubkey,
304        },
305        std::collections::HashSet,
306    };
307
308    fn new_sanitized_message(message: LegacyMessage) -> SanitizedMessage {
309        SanitizedMessage::try_from_legacy_message(message, &HashSet::default()).unwrap()
310    }
311
312    #[test]
313    fn test_load_store_instruction() {
314        let mut data = [4u8; 10];
315        store_current_index(&mut data, 3);
316        #[allow(deprecated)]
317        let index = load_current_index(&data);
318        assert_eq!(index, 3);
319        assert_eq!([4u8; 8], data[0..8]);
320    }
321
322    #[test]
323    fn test_load_instruction_at_checked() {
324        let instruction0 = Instruction::new_with_bincode(
325            Pubkey::new_unique(),
326            &0,
327            vec![AccountMeta::new(Pubkey::new_unique(), false)],
328        );
329        let instruction1 = Instruction::new_with_bincode(
330            Pubkey::new_unique(),
331            &0,
332            vec![AccountMeta::new(Pubkey::new_unique(), false)],
333        );
334        let message = LegacyMessage::new(
335            &[instruction0.clone(), instruction1.clone()],
336            Some(&Pubkey::new_unique()),
337        );
338        let sanitized_message = new_sanitized_message(message);
339
340        let key = id();
341        let mut lamports = 0;
342        let mut data = construct_instructions_data(&sanitized_message.decompile_instructions());
343        let owner = crate::sysvar::id();
344        let mut account_info = AccountInfo::new(
345            &key,
346            false,
347            false,
348            &mut lamports,
349            &mut data,
350            &owner,
351            false,
352            0,
353        );
354
355        assert_eq!(
356            instruction0,
357            load_instruction_at_checked(0, &account_info).unwrap()
358        );
359        assert_eq!(
360            instruction1,
361            load_instruction_at_checked(1, &account_info).unwrap()
362        );
363        assert_eq!(
364            Err(ProgramError::InvalidArgument),
365            load_instruction_at_checked(2, &account_info)
366        );
367
368        let key = Pubkey::new_unique();
369        account_info.key = &key;
370        assert_eq!(
371            Err(ProgramError::UnsupportedSysvar),
372            load_instruction_at_checked(2, &account_info)
373        );
374    }
375
376    #[test]
377    fn test_load_current_index_checked() {
378        let instruction0 = Instruction::new_with_bincode(
379            Pubkey::new_unique(),
380            &0,
381            vec![AccountMeta::new(Pubkey::new_unique(), false)],
382        );
383        let instruction1 = Instruction::new_with_bincode(
384            Pubkey::new_unique(),
385            &0,
386            vec![AccountMeta::new(Pubkey::new_unique(), false)],
387        );
388        let message =
389            LegacyMessage::new(&[instruction0, instruction1], Some(&Pubkey::new_unique()));
390        let sanitized_message = new_sanitized_message(message);
391
392        let key = id();
393        let mut lamports = 0;
394        let mut data = construct_instructions_data(&sanitized_message.decompile_instructions());
395        store_current_index(&mut data, 1);
396        let owner = crate::sysvar::id();
397        let mut account_info = AccountInfo::new(
398            &key,
399            false,
400            false,
401            &mut lamports,
402            &mut data,
403            &owner,
404            false,
405            0,
406        );
407
408        assert_eq!(1, load_current_index_checked(&account_info).unwrap());
409        {
410            let mut data = account_info.try_borrow_mut_data().unwrap();
411            store_current_index(&mut data, 0);
412        }
413        assert_eq!(0, load_current_index_checked(&account_info).unwrap());
414
415        let key = Pubkey::new_unique();
416        account_info.key = &key;
417        assert_eq!(
418            Err(ProgramError::UnsupportedSysvar),
419            load_current_index_checked(&account_info)
420        );
421    }
422
423    #[test]
424    fn test_get_instruction_relative() {
425        let instruction0 = Instruction::new_with_bincode(
426            Pubkey::new_unique(),
427            &0,
428            vec![AccountMeta::new(Pubkey::new_unique(), false)],
429        );
430        let instruction1 = Instruction::new_with_bincode(
431            Pubkey::new_unique(),
432            &0,
433            vec![AccountMeta::new(Pubkey::new_unique(), false)],
434        );
435        let instruction2 = Instruction::new_with_bincode(
436            Pubkey::new_unique(),
437            &0,
438            vec![AccountMeta::new(Pubkey::new_unique(), false)],
439        );
440        let message = LegacyMessage::new(
441            &[
442                instruction0.clone(),
443                instruction1.clone(),
444                instruction2.clone(),
445            ],
446            Some(&Pubkey::new_unique()),
447        );
448        let sanitized_message = new_sanitized_message(message);
449
450        let key = id();
451        let mut lamports = 0;
452        let mut data = construct_instructions_data(&sanitized_message.decompile_instructions());
453        store_current_index(&mut data, 1);
454        let owner = crate::sysvar::id();
455        let mut account_info = AccountInfo::new(
456            &key,
457            false,
458            false,
459            &mut lamports,
460            &mut data,
461            &owner,
462            false,
463            0,
464        );
465
466        assert_eq!(
467            Err(ProgramError::InvalidArgument),
468            get_instruction_relative(-2, &account_info)
469        );
470        assert_eq!(
471            instruction0,
472            get_instruction_relative(-1, &account_info).unwrap()
473        );
474        assert_eq!(
475            instruction1,
476            get_instruction_relative(0, &account_info).unwrap()
477        );
478        assert_eq!(
479            instruction2,
480            get_instruction_relative(1, &account_info).unwrap()
481        );
482        assert_eq!(
483            Err(ProgramError::InvalidArgument),
484            get_instruction_relative(2, &account_info)
485        );
486        {
487            let mut data = account_info.try_borrow_mut_data().unwrap();
488            store_current_index(&mut data, 0);
489        }
490        assert_eq!(
491            Err(ProgramError::InvalidArgument),
492            get_instruction_relative(-1, &account_info)
493        );
494        assert_eq!(
495            instruction0,
496            get_instruction_relative(0, &account_info).unwrap()
497        );
498        assert_eq!(
499            instruction1,
500            get_instruction_relative(1, &account_info).unwrap()
501        );
502        assert_eq!(
503            instruction2,
504            get_instruction_relative(2, &account_info).unwrap()
505        );
506        assert_eq!(
507            Err(ProgramError::InvalidArgument),
508            get_instruction_relative(3, &account_info)
509        );
510
511        let key = Pubkey::new_unique();
512        account_info.key = &key;
513        assert_eq!(
514            Err(ProgramError::UnsupportedSysvar),
515            get_instruction_relative(0, &account_info)
516        );
517    }
518
519    #[test]
520    fn test_serialize_instructions() {
521        let program_id0 = Pubkey::new_unique();
522        let program_id1 = Pubkey::new_unique();
523        let id0 = Pubkey::new_unique();
524        let id1 = Pubkey::new_unique();
525        let id2 = Pubkey::new_unique();
526        let id3 = Pubkey::new_unique();
527        let instructions = vec![
528            Instruction::new_with_bincode(program_id0, &0, vec![AccountMeta::new(id0, false)]),
529            Instruction::new_with_bincode(program_id0, &0, vec![AccountMeta::new(id1, true)]),
530            Instruction::new_with_bincode(
531                program_id1,
532                &0,
533                vec![AccountMeta::new_readonly(id2, false)],
534            ),
535            Instruction::new_with_bincode(
536                program_id1,
537                &0,
538                vec![AccountMeta::new_readonly(id3, true)],
539            ),
540        ];
541
542        let message = LegacyMessage::new(&instructions, Some(&id1));
543        let sanitized_message = new_sanitized_message(message);
544        let serialized = serialize_instructions(&sanitized_message.decompile_instructions());
545
546        // assert that deserialize_instruction is compatible with SanitizedMessage::serialize_instructions
547        for (i, instruction) in instructions.iter().enumerate() {
548            assert_eq!(
549                deserialize_instruction(i, &serialized).unwrap(),
550                *instruction
551            );
552        }
553    }
554
555    #[test]
556    fn test_decompile_instructions_out_of_bounds() {
557        let program_id0 = Pubkey::new_unique();
558        let id0 = Pubkey::new_unique();
559        let id1 = Pubkey::new_unique();
560        let instructions = vec![
561            Instruction::new_with_bincode(program_id0, &0, vec![AccountMeta::new(id0, false)]),
562            Instruction::new_with_bincode(program_id0, &0, vec![AccountMeta::new(id1, true)]),
563        ];
564
565        let message = LegacyMessage::new(&instructions, Some(&id1));
566        let sanitized_message = new_sanitized_message(message);
567        let serialized = serialize_instructions(&sanitized_message.decompile_instructions());
568        assert_eq!(
569            deserialize_instruction(instructions.len(), &serialized).unwrap_err(),
570            SanitizeError::IndexOutOfBounds,
571        );
572    }
573}