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