-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow programs to realloc their accounts within limits
- Loading branch information
Showing
15 changed files
with
1,030 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "solana-bpf-rust-realloc" | ||
version = "1.8.0" | ||
description = "Solana BPF test program written in Rust" | ||
authors = ["Solana Maintainers <[email protected]>"] | ||
repository = "https://github.com/solana-labs/solana" | ||
license = "Apache-2.0" | ||
homepage = "https://solana.com/" | ||
documentation = "https://docs.rs/solana-bpf-rust-realloc" | ||
edition = "2018" | ||
|
||
[features] | ||
custom-heap = [] | ||
|
||
[dependencies] | ||
solana-program = { path = "../../../../sdk/program", version = "=1.8.0" } | ||
|
||
[lib] | ||
crate-type = ["lib", "cdylib"] | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//! @brief Example Rust-based BPF realloc test program | ||
pub const REALLOC_ZERO: u8 = 0; | ||
pub const REALLOC_TO: u8 = 1; | ||
pub const REALLOC_MAX_PLUS_ONE: u8 = 2; | ||
pub const REALLOC_MAX: u8 = 3; | ||
pub const REALLOC_AND_ASSIGN: u8 = 4; | ||
pub const REALLOC_AND_ASSIGN_TO_SELF: u8 = 5; | ||
pub const ASSIGN_TO_SELF_AND_REALLOC: u8 = 6; | ||
pub const CHECK: u8 = 7; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
//! @brief Example Rust-based BPF realloc test program | ||
pub mod instructions; | ||
|
||
extern crate solana_program; | ||
use crate::instructions::*; | ||
use solana_program::{ | ||
account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, | ||
entrypoint::MAX_PERMITTED_DATA_INCREASE, msg, program::invoke, pubkey::Pubkey, | ||
system_instruction, system_program, | ||
}; | ||
use std::convert::TryInto; | ||
|
||
entrypoint!(process_instruction); | ||
#[allow(clippy::unnecessary_wraps)] | ||
fn process_instruction( | ||
program_id: &Pubkey, | ||
accounts: &[AccountInfo], | ||
instruction_data: &[u8], | ||
) -> ProgramResult { | ||
let account = &accounts[0]; | ||
let pre_len = account.data_len(); | ||
|
||
match instruction_data[0] { | ||
REALLOC_ZERO => { | ||
msg!("realloc to zero"); | ||
account.realloc(0)?; | ||
assert_eq!(0, account.data_len()); | ||
} | ||
REALLOC_TO => { | ||
let (bytes, _) = instruction_data[2..].split_at(std::mem::size_of::<usize>()); | ||
let new_len = usize::from_le_bytes(bytes.try_into().unwrap()); | ||
msg!("realloc to {}", new_len); | ||
account.realloc(new_len)?; | ||
assert_eq!(new_len, account.data_len()); | ||
} | ||
REALLOC_MAX_PLUS_ONE => { | ||
msg!("realloc to max + 1"); | ||
account.realloc(pre_len + MAX_PERMITTED_DATA_INCREASE + 1)?; | ||
} | ||
REALLOC_MAX => { | ||
msg!("realloc to max"); | ||
account.realloc(pre_len + MAX_PERMITTED_DATA_INCREASE)?; | ||
assert_eq!(pre_len + MAX_PERMITTED_DATA_INCREASE, account.data_len()); | ||
account.try_borrow_mut_data()?[pre_len..].fill(instruction_data[1]); | ||
} | ||
REALLOC_AND_ASSIGN => { | ||
msg!("realloc and assign"); | ||
account.realloc(pre_len + MAX_PERMITTED_DATA_INCREASE)?; | ||
assert_eq!(pre_len + MAX_PERMITTED_DATA_INCREASE, account.data_len()); | ||
account.assign(&system_program::id()); | ||
assert_eq!(*account.owner, system_program::id()); | ||
} | ||
REALLOC_AND_ASSIGN_TO_SELF => { | ||
msg!("realloc and assign to self via cpi"); | ||
account.realloc(pre_len + MAX_PERMITTED_DATA_INCREASE)?; | ||
assert_eq!(pre_len + MAX_PERMITTED_DATA_INCREASE, account.data_len()); | ||
invoke( | ||
&system_instruction::assign(account.key, program_id), | ||
accounts, | ||
)?; | ||
assert_eq!(pre_len + MAX_PERMITTED_DATA_INCREASE, account.data_len()); | ||
} | ||
ASSIGN_TO_SELF_AND_REALLOC => { | ||
msg!("assign to self via cpi and realloc"); | ||
invoke( | ||
&system_instruction::assign(account.key, program_id), | ||
accounts, | ||
)?; | ||
account.realloc(pre_len + MAX_PERMITTED_DATA_INCREASE)?; | ||
assert_eq!(pre_len + MAX_PERMITTED_DATA_INCREASE, account.data_len()); | ||
assert_eq!(account.owner, program_id); | ||
} | ||
CHECK => { | ||
msg!("check"); | ||
let (bytes, _) = instruction_data[2..].split_at(std::mem::size_of::<usize>()); | ||
let new_len = usize::from_le_bytes(bytes.try_into().unwrap()); | ||
assert_eq!(new_len, account.data_len()); | ||
let data = account.try_borrow_mut_data()?; | ||
for x in data[0..5].iter() { | ||
assert_eq!(0, *x); | ||
} | ||
for x in data[5..].iter() { | ||
assert_eq!(instruction_data[1], *x); | ||
} | ||
} | ||
_ => panic!(), | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
name = "solana-bpf-rust-realloc-invoke" | ||
version = "1.8.0" | ||
description = "Solana BPF test program written in Rust" | ||
authors = ["Solana Maintainers <[email protected]>"] | ||
repository = "https://github.com/solana-labs/solana" | ||
license = "Apache-2.0" | ||
homepage = "https://solana.com/" | ||
documentation = "https://docs.rs/solana-bpf-rust-realloc-invoke" | ||
edition = "2018" | ||
|
||
[features] | ||
custom-heap = [] | ||
|
||
[dependencies] | ||
solana-program = { path = "../../../../sdk/program", version = "=1.8.0" } | ||
solana-bpf-rust-realloc = { path = "../realloc", version = "=1.8.0", features = ["custom-heap"]} | ||
|
||
[lib] | ||
crate-type = ["lib", "cdylib"] | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//! @brief Example Rust-based BPF realloc test program | ||
pub const INVOKE_REALLOC_ZERO_RO: u8 = 0; | ||
pub const INVOKE_REALLOC_ZERO: u8 = 1; | ||
pub const INVOKE_REALLOC_MAX_PLUS_ONE: u8 = 2; | ||
pub const INVOKE_REALLOC_MAX: u8 = 3; | ||
pub const INVOKE_REALLOC_AND_ASSIGN: u8 = 4; | ||
pub const INVOKE_REALLOC_AND_ASSIGN_TO_SELF: u8 = 5; | ||
pub const INVOKE_ASSIGN_TO_SELF_AND_REALLOC: u8 = 6; | ||
pub const INVOKE_REALLOC_INVOKE_CHECK: u8 = 7; | ||
pub const INVOKE_OVERFLOW: u8 = 8; | ||
pub const INVOKE_REALLOC_TO: u8 = 9; | ||
pub const INVOKE_REALLOC_RECURSIVE: u8 = 10; | ||
pub const INVOKE_CREATE_ACCOUNT_CHECK: u8 = 11; |
Oops, something went wrong.