-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(bpf): instruction handling #609
Open
kprotty
wants to merge
19
commits into
main
Choose a base branch
from
king/bpf-loader
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,784
−9
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c5fd353
bpf loader and basic native cpi
yewman 01964d2
Partial implementation of Bpf program + native cpi and serialization
yewman 7dfba76
bpf: executeV3SetAuthority[Checked]
kprotty b9eb219
fixes + failing SetAuthority test
kprotty 60a3dd7
test executeV3SetAuthority
kprotty a0f4f3c
test(executeV3SetAuthorityChecked)
kprotty e2042e8
feat(bpf): executeV3Close wip
kprotty 8b5ba97
fix executeV3Close test accounts_delta
kprotty 6b4dc48
executeV3Upgrade & executeV3ExtendProgram
kprotty ac67ac9
test executeV3Upgrade
kprotty 8a962ff
fix rebase
yewman c424380
Fix tests
yewman f17595f
Fix derived program address for test cases
yewman 580c5af
fix executeV3Close test + refactor feature_set
kprotty e31c4bf
reach cpi call in executeV3ExtendProgram test
kprotty b8cc8a8
fix stylecheck
kprotty 2d1f5e9
zig fmt
kprotty cc749a8
fix: isOwnedByCurrent
kprotty e7113e1
sanitized account indexes
kprotty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
2,387 changes: 2,387 additions & 0 deletions
2,387
src/runtime/program/bpf_loader_program/execute.zig
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const sig = @import("../../../sig.zig"); | ||
|
||
const Pubkey = sig.core.Pubkey; | ||
|
||
pub const v1 = struct { | ||
/// [agave] https://github.com/anza-xyz/agave/blob/c5ed1663a1218e9e088e30c81677bc88059cc62b/sdk/sdk-ids/src/lib.rs#L11 | ||
pub const ID = | ||
Pubkey.parseBase58String("BPFLoader1111111111111111111111111111111111") catch unreachable; | ||
|
||
/// [agave] https://github.com/anza-xyz/agave/blob/faea52f338df8521864ab7ce97b120b2abb5ce13/programs/bpf_loader/src/lib.rs#L56 | ||
pub const COMPUTE_UNITS = 1_140; | ||
}; | ||
|
||
pub const v2 = struct { | ||
/// [agave] https://github.com/anza-xyz/agave/blob/c5ed1663a1218e9e088e30c81677bc88059cc62b/sdk/sdk-ids/src/lib.rs#L7 | ||
pub const ID = | ||
Pubkey.parseBase58String("BPFLoader2111111111111111111111111111111111") catch unreachable; | ||
|
||
/// [agave] https://github.com/anza-xyz/agave/blob/faea52f338df8521864ab7ce97b120b2abb5ce13/programs/bpf_loader/src/lib.rs#L55 | ||
pub const COMPUTE_UNITS = 570; | ||
}; | ||
|
||
pub const v3 = struct { | ||
/// [agave] https://github.com/anza-xyz/agave/blob/c5ed1663a1218e9e088e30c81677bc88059cc62b/sdk/sdk-ids/src/lib.rs#L15-L16 | ||
pub const ID = | ||
Pubkey.parseBase58String("BPFLoaderUpgradeab1e11111111111111111111111") catch unreachable; | ||
|
||
/// [agave] https://github.com/anza-xyz/agave/blob/faea52f338df8521864ab7ce97b120b2abb5ce13/programs/bpf_loader/src/lib.rs#L57 | ||
pub const COMPUTE_UNITS = 2_370; | ||
|
||
pub const instruction = @import("v3_instruction.zig"); | ||
pub const Instruction = instruction.Instruction; | ||
pub const State = @import("v3_state.zig").State; | ||
}; | ||
|
||
pub const execute = @import("execute.zig").execute; |
267 changes: 267 additions & 0 deletions
267
src/runtime/program/bpf_loader_program/v3_instruction.zig
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,267 @@ | ||
pub const InitializeBuffer = struct { | ||
pub const AccountIndex = enum(u1) { | ||
/// `[WRITE]` source account to initialize. | ||
account = 0, | ||
/// `[]` Buffer authority, optional, if omitted then the buffer will be immutable. | ||
authority = 1, | ||
}; | ||
}; | ||
|
||
pub const Write = struct { | ||
/// Offset at which to write the given bytes. | ||
offset: u32, | ||
/// Serialized program data | ||
bytes: []const u8, | ||
|
||
pub const AccountIndex = enum(u1) { | ||
/// `[WRITE]` Buffer account to write program data to. | ||
account = 0, | ||
/// `[SIGNER]` Buffer authority. | ||
authority = 1, | ||
}; | ||
}; | ||
|
||
pub const DeployWithMaxDataLen = struct { | ||
/// Maximum length that the program can be upgraded to. | ||
max_data_len: usize, | ||
|
||
pub const AccountIndex = enum(u3) { | ||
/// `[WRITE, SIGNER]` The payer account that will pay to create the ProgramData account. | ||
payer = 0, | ||
/// `[WRITE]` The uninitialized ProgramData account. | ||
program_data = 1, | ||
/// `[WRITE]` The uninitialized Program account. | ||
program = 2, | ||
/// `[WRITE]` The Buffer account where the program data has been written. | ||
/// The buffer account's authority must match the program's authority. | ||
buffer = 3, | ||
/// `[]` Rent sysvar. | ||
rent = 4, | ||
/// `[]` Clock sysvar. | ||
clock = 5, | ||
/// `[]` System program (`solana_sdk::system_program::id()`). | ||
system_program = 6, | ||
/// `[SIGNER]` The program's authority. | ||
authority = 7, | ||
}; | ||
}; | ||
|
||
pub const Upgrade = struct { | ||
pub const AccountIndex = enum(u7) { | ||
/// `[WRITE]` The ProgramData account. | ||
program_data = 0, | ||
/// `[WRITE]` The Program account. | ||
program = 1, | ||
/// `[WRITE]` The Buffer account where the program data has been written. | ||
/// The buffer account's authority must match the program's authority. | ||
buffer = 2, | ||
/// `[WRITE]` The spill account. | ||
spill = 3, | ||
/// `[]` Rent sysvar. | ||
rent = 4, | ||
/// `[]` Clock sysvar. | ||
clock = 5, | ||
/// `[SIGNER]` The program's authority. | ||
authority = 6, | ||
}; | ||
}; | ||
|
||
pub const SetAuthority = struct { | ||
pub const AccountIndex = enum(u2) { | ||
/// `[WRITE]` The Buffer or ProgramData account to change the authority of. | ||
account = 0, | ||
/// `[SIGNER]` The current authority. | ||
present_authority = 1, | ||
/// `[]` The new authority, optional, if omitted then the program will not be upgradeable. | ||
new_authority = 2, | ||
}; | ||
}; | ||
|
||
pub const SetAuthorityChecked = struct { | ||
pub const AccountIndex = enum(u2) { | ||
/// `[WRITE]` The Buffer or ProgramData account to change the authority of. | ||
account = 0, | ||
/// `[SIGNER]` The current authority. | ||
present_authority = 1, | ||
/// `[SIGNER]` The new authority. | ||
new_authority = 2, | ||
}; | ||
}; | ||
|
||
pub const Close = struct { | ||
pub const AccountIndex = enum(u2) { | ||
/// `[WRITE]` The account to close, if closing a program must be the ProgramData account. | ||
account = 0, | ||
/// `[WRITE]` The account to deposit the closed account's lamports. | ||
recipient = 1, | ||
/// `[SIGNER]` The account's authority, Optional, required for initialized accounts. | ||
authority = 2, | ||
/// `[WRITE]` The associated Program account if the account to close is a ProgramData account. | ||
program = 3, | ||
}; | ||
}; | ||
|
||
pub const ExtendProgram = struct { | ||
/// Number of bytes to extend the program data. | ||
additional_bytes: u32, | ||
|
||
pub const AccountIndex = enum(u2) { | ||
/// `[WRITE]` The ProgramData account. | ||
program_data = 0, | ||
/// `[WRITE]` The ProgramData account's associated Program account. | ||
program = 1, | ||
/// `[]` System program (`solana_sdk::system_program::id()`), | ||
/// optional used to transfer lamports from the payer to the ProgramData account. | ||
system_program = 2, | ||
/// `[WRITE, SIGNER]` The payer account, optional, that will pay necessary rent exemption | ||
/// costs for the increased storage size. | ||
payer = 3, | ||
}; | ||
}; | ||
|
||
/// [agave] https://github.com/anza-xyz/agave/blob/master/sdk/program/src/loader_upgradeable_instruction.rs#L7 | ||
pub const Instruction = union(enum) { | ||
/// Initialize a Buffer account. | ||
/// | ||
/// A Buffer account is an intermediary that once fully populated is used | ||
/// with the `DeployWithMaxDataLen` instruction to populate the program's | ||
/// ProgramData account. | ||
/// | ||
/// The `InitializeBuffer` instruction requires no signers and MUST be | ||
/// included within the same Transaction as the system program's | ||
/// `CreateAccount` instruction that creates the account being initialized. | ||
/// Otherwise another party may initialize the account. | ||
/// | ||
/// # Account references | ||
/// 0. `[writable]` source account to initialize. | ||
/// 1. `[]` Buffer authority, optional, if omitted then the buffer will be | ||
/// immutable. | ||
initialize_buffer: InitializeBuffer, | ||
|
||
/// Write program data into a Buffer account. | ||
/// | ||
/// # Account references | ||
/// 0. `[writable]` Buffer account to write program data to. | ||
/// 1. `[signer]` Buffer authority | ||
write: Write, | ||
|
||
/// Deploy an executable program. | ||
/// | ||
/// A program consists of a Program and ProgramData account pair. | ||
/// - The Program account's address will serve as the program id for any | ||
/// instructions that execute this program. | ||
/// - The ProgramData account will remain mutable by the loader only and | ||
/// holds the program data and authority information. The ProgramData | ||
/// account's address is derived from the Program account's address and | ||
/// created by the DeployWithMaxDataLen instruction. | ||
/// | ||
/// The ProgramData address is derived from the Program account's address as | ||
/// follows: | ||
/// | ||
/// ``` | ||
/// # use solana_program::pubkey::Pubkey; | ||
/// # use solana_program::bpf_loader_upgradeable; | ||
/// # let program_address = &[]; | ||
/// let (program_data_address, _) = Pubkey::find_program_address( | ||
/// &[program_address], | ||
/// &bpf_loader_upgradeable::id() | ||
/// ); | ||
/// ``` | ||
/// | ||
/// The `DeployWithMaxDataLen` instruction does not require the ProgramData | ||
/// account be a signer and therefore MUST be included within the same | ||
/// Transaction as the system program's `CreateAccount` instruction that | ||
/// creates the Program account. Otherwise another party may initialize the | ||
/// account. | ||
/// | ||
/// # Account references | ||
/// 0. `[writable, signer]` The payer account that will pay to create the | ||
/// ProgramData account. | ||
/// 1. `[writable]` The uninitialized ProgramData account. | ||
/// 2. `[writable]` The uninitialized Program account. | ||
/// 3. `[writable]` The Buffer account where the program data has been | ||
/// written. The buffer account's authority must match the program's | ||
/// authority | ||
/// 4. `[]` Rent sysvar. | ||
/// 5. `[]` Clock sysvar. | ||
/// 6. `[]` System program (`solana_sdk::system_program::id()`). | ||
/// 7. `[signer]` The program's authority | ||
deploy_with_max_data_len: DeployWithMaxDataLen, | ||
|
||
/// Upgrade a program. | ||
/// | ||
/// A program can be updated as long as the program's authority has not been | ||
/// set to `None`. | ||
/// | ||
/// The Buffer account must contain sufficient lamports to fund the | ||
/// ProgramData account to be rent-exempt, any additional lamports left over | ||
/// will be transferred to the spill account, leaving the Buffer account | ||
/// balance at zero. | ||
/// | ||
/// # Account references | ||
/// 0. `[writable]` The ProgramData account. | ||
/// 1. `[writable]` The Program account. | ||
/// 2. `[writable]` The Buffer account where the program data has been | ||
/// written. The buffer account's authority must match the program's | ||
/// authority | ||
/// 3. `[writable]` The spill account. | ||
/// 4. `[]` Rent sysvar. | ||
/// 5. `[]` Clock sysvar. | ||
/// 6. `[signer]` The program's authority. | ||
upgrade: Upgrade, | ||
|
||
/// Set a new authority that is allowed to write the buffer or upgrade the | ||
/// program. To permanently make the buffer immutable or disable program | ||
/// updates omit the new authority. | ||
/// | ||
/// # Account references | ||
/// 0. `[writable]` The Buffer or ProgramData account to change the | ||
/// authority of. | ||
/// 1. `[signer]` The current authority. | ||
/// 2. `[]` The new authority, optional, if omitted then the program will | ||
/// not be upgradeable. | ||
set_authority: SetAuthority, | ||
|
||
/// Closes an account owned by the upgradeable loader of all lamports and | ||
/// withdraws all the lamports | ||
/// | ||
/// # Account references | ||
/// 0. `[writable]` The account to close, if closing a program must be the | ||
/// ProgramData account. | ||
/// 1. `[writable]` The account to deposit the closed account's lamports. | ||
/// 2. `[signer]` The account's authority, Optional, required for | ||
/// initialized accounts. | ||
/// 3. `[writable]` The associated Program account if the account to close | ||
/// is a ProgramData account. | ||
close: Close, | ||
|
||
/// Extend a program's ProgramData account by the specified number of bytes. | ||
/// Only upgradeable program's can be extended. | ||
/// | ||
/// The payer account must contain sufficient lamports to fund the | ||
/// ProgramData account to be rent-exempt. If the ProgramData account | ||
/// balance is already sufficient to cover the rent exemption cost | ||
/// for the extended bytes, the payer account is not required. | ||
/// | ||
/// # Account references | ||
/// 0. `[writable]` The ProgramData account. | ||
/// 1. `[writable]` The ProgramData account's associated Program account. | ||
/// 2. `[]` System program (`solana_sdk::system_program::id()`), optional, used to transfer | ||
/// lamports from the payer to the ProgramData account. | ||
/// 3. `[writable, signer]` The payer account, optional, that will pay | ||
/// necessary rent exemption costs for the increased storage size. | ||
extend_program: ExtendProgram, | ||
|
||
/// Set a new authority that is allowed to write the buffer or upgrade the | ||
/// program. | ||
/// | ||
/// This instruction differs from SetAuthority in that the new authority is a | ||
/// required signer. | ||
/// | ||
/// # Account references | ||
/// 0. `[writable]` The Buffer or ProgramData account to change the | ||
/// authority of. | ||
/// 1. `[signer]` The current authority. | ||
/// 2. `[signer]` The new authority. | ||
set_authority_checked: SetAuthorityChecked, | ||
}; |
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,54 @@ | ||
const sig = @import("../../../sig.zig"); | ||
|
||
const Pubkey = sig.core.Pubkey; | ||
|
||
/// [agave] https://github.com/anza-xyz/agave/blob/5fb000f27e476add032e08a1de9e89310b0eab4b/sdk/program/src/bpf_loader_upgradeable.rs#L29 | ||
pub const State = union(enum) { | ||
/// Account is not initialized. | ||
uninitialized, | ||
/// A Buffer account. | ||
buffer: struct { | ||
/// Authority address | ||
authority_address: ?Pubkey, | ||
// The raw program data follows this serialized structure in the | ||
// account's data. | ||
}, | ||
/// An Program account. | ||
program: struct { | ||
/// Address of the ProgramData account. | ||
programdata_address: Pubkey, | ||
}, | ||
// A ProgramData account. | ||
program_data: struct { | ||
/// Slot that the program was last modified. | ||
slot: u64, | ||
/// Address of the Program's upgrade authority. | ||
upgrade_authority_address: ?Pubkey, | ||
// The raw program data follows this serialized structure in the | ||
// account's data. | ||
}, | ||
|
||
pub const UNINITIALIZED_SIZE: usize = 4; | ||
pub const BUFFER_METADATA_SIZE: usize = 37; | ||
pub const PROGRAM_SIZE: usize = 36; | ||
pub const PROGRAM_DATA_METADATA_SIZE: usize = 45; | ||
|
||
pub fn serializedSize(self: State) !usize { | ||
return switch (self) { | ||
.uninitialized => UNINITIALIZED_SIZE, | ||
.buffer => BUFFER_METADATA_SIZE, | ||
.program => PROGRAM_SIZE, | ||
.program_data => PROGRAM_DATA_METADATA_SIZE, | ||
}; | ||
} | ||
|
||
/// [agave] https://github.com/anza-xyz/solana-sdk/blob/c07f692e41d757057c8700211a9300cdcd6d33b1/loader-v3-interface/src/state.rs#L57 | ||
pub fn sizeOfBuffer(program_len: usize) usize { | ||
return BUFFER_METADATA_SIZE +| program_len; | ||
} | ||
|
||
/// [agave] https://github.com/anza-xyz/solana-sdk/blob/c07f692e41d757057c8700211a9300cdcd6d33b1/loader-v3-interface/src/state.rs#L62 | ||
pub fn sizeOfProgramData(program_len: usize) usize { | ||
return PROGRAM_DATA_METADATA_SIZE +| program_len; | ||
} | ||
}; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we change the instruction definition to use the typed + index enum approach similar to
sig/src/runtime/program/vote/instruction.zig
Line 6 in ef431d3