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