Skip to content

Commit 41021bb

Browse files
committed
runtime: make BPF Loader v3 structs zero-alloc
Removes dynamic allocations in BPF Loader v3 by turning pointer option types into flat option types.
1 parent 6b8ac09 commit 41021bb

File tree

8 files changed

+101
-120
lines changed

8 files changed

+101
-120
lines changed

src/flamenco/runtime/fd_runtime.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,8 +2365,8 @@ fd_new_target_program_data_account( fd_exec_slot_ctx_t * slot_ctx,
23652365

23662366
/* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank/builtins/core_bpf_migration/mod.rs#L118-L125 */
23672367
if( config_upgrade_authority_address!=NULL ) {
2368-
if( FD_UNLIKELY( state->inner.buffer.authority_address==NULL ||
2369-
memcmp( config_upgrade_authority_address, state->inner.buffer.authority_address, sizeof(fd_pubkey_t) ) ) ) {
2368+
if( FD_UNLIKELY( !state->inner.buffer.has_authority_address ||
2369+
!fd_pubkey_eq( config_upgrade_authority_address, &state->inner.buffer.authority_address ) ) ) {
23702370
return -1;
23712371
}
23722372
}
@@ -2386,11 +2386,13 @@ fd_new_target_program_data_account( fd_exec_slot_ctx_t * slot_ctx,
23862386
.discriminant = fd_bpf_upgradeable_loader_state_enum_program_data,
23872387
.inner = {
23882388
.program_data = {
2389-
.slot = slot_ctx->slot_bank.slot,
2390-
.upgrade_authority_address = config_upgrade_authority_address
2389+
.slot = slot_ctx->slot_bank.slot
23912390
}
23922391
}
23932392
};
2393+
fd_types_option_flat_from_nullable(
2394+
programdata_metadata.inner.program_data, upgrade_authority_address,
2395+
config_upgrade_authority_address );
23942396

23952397
/* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank/builtins/core_bpf_migration/mod.rs#L139-L144 */
23962398
new_target_program_data_account->vt->set_lamports( new_target_program_data_account, lamports );

src/flamenco/runtime/program/fd_bpf_loader_program.c

Lines changed: 53 additions & 45 deletions
Large diffs are not rendered by default.

src/flamenco/runtime/program/fd_bpf_program_util.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,17 @@ static int
8282
fd_bpf_get_executable_program_content_for_upgradeable_loader( fd_exec_slot_ctx_t * slot_ctx,
8383
fd_txn_account_t * program_acc,
8484
uchar const ** program_data,
85-
ulong * program_data_len,
86-
fd_spad_t * runtime_spad ) {
85+
ulong * program_data_len ) {
8786
FD_TXN_ACCOUNT_DECL( programdata_acc );
8887

89-
fd_bpf_upgradeable_loader_state_t * program_account_state =
90-
fd_bincode_decode_spad(
91-
bpf_upgradeable_loader_state, runtime_spad,
88+
fd_bpf_upgradeable_loader_state_t program_account_state[1];
89+
if( FD_UNLIKELY( !fd_bincode_decode_flat(
90+
program_account_state,
91+
bpf_upgradeable_loader_state,
9292
program_acc->vt->get_data( program_acc ),
9393
program_acc->vt->get_data_len( program_acc ),
94-
NULL );
95-
if( FD_UNLIKELY( !program_account_state ) ) {
94+
NULL
95+
) ) ) {
9696
return -1;
9797
}
9898
if( !fd_bpf_upgradeable_loader_state_is_program( program_account_state ) ) {
@@ -182,7 +182,7 @@ fd_bpf_create_bpf_program_cache_entry( fd_exec_slot_ctx_t * slot_ctx,
182182

183183
int res;
184184
if( !memcmp( program_acc->vt->get_owner( program_acc ), fd_solana_bpf_loader_upgradeable_program_id.key, sizeof(fd_pubkey_t) ) ) {
185-
res = fd_bpf_get_executable_program_content_for_upgradeable_loader( slot_ctx, program_acc, &program_data, &program_data_len, runtime_spad );
185+
res = fd_bpf_get_executable_program_content_for_upgradeable_loader( slot_ctx, program_acc, &program_data, &program_data_len );
186186
} else if( !memcmp( program_acc->vt->get_owner( program_acc ), fd_solana_bpf_loader_v4_program_id.key, sizeof(fd_pubkey_t) ) ) {
187187
res = fd_bpf_get_executable_program_content_for_v4_loader( program_acc, &program_data, &program_data_len );
188188
} else {

src/flamenco/runtime/program/fd_builtin_programs.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33

44
#include "../../fd_flamenco_base.h"
55
#include "../../runtime/fd_system_ids.h"
6-
#include "../../features/fd_features.h"
7-
#include "../context/fd_exec_epoch_ctx.h"
8-
#include "../context/fd_exec_slot_ctx.h"
96
#include "../fd_system_ids.h"
107
#include "../fd_system_ids_pp.h"
118

src/flamenco/types/fd_fuzz_types.h

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2748,15 +2748,9 @@ void *fd_bpf_upgradeable_loader_state_buffer_generate( void *mem, void **alloc_m
27482748
*alloc_mem = (uchar *) *alloc_mem + sizeof(fd_bpf_upgradeable_loader_state_buffer_t);
27492749
fd_bpf_upgradeable_loader_state_buffer_new(mem);
27502750
{
2751-
uchar is_null = fd_rng_uchar( rng ) % 2;
2752-
if( !is_null ) {
2753-
self->authority_address = (fd_pubkey_t *) *alloc_mem;
2754-
*alloc_mem = (uchar *) *alloc_mem + sizeof(fd_pubkey_t);
2755-
fd_pubkey_new( self->authority_address );
2756-
fd_pubkey_generate( self->authority_address, alloc_mem, rng );
2757-
}
2758-
else {
2759-
self->authority_address = NULL;
2751+
self->has_authority_address = fd_rng_uchar( rng ) % 2;
2752+
if( self->has_authority_address ) {
2753+
fd_pubkey_generate( &self->authority_address, alloc_mem, rng );
27602754
}
27612755
}
27622756
return mem;
@@ -2776,15 +2770,9 @@ void *fd_bpf_upgradeable_loader_state_program_data_generate( void *mem, void **a
27762770
fd_bpf_upgradeable_loader_state_program_data_new(mem);
27772771
self->slot = fd_rng_ulong( rng );
27782772
{
2779-
uchar is_null = fd_rng_uchar( rng ) % 2;
2780-
if( !is_null ) {
2781-
self->upgrade_authority_address = (fd_pubkey_t *) *alloc_mem;
2782-
*alloc_mem = (uchar *) *alloc_mem + sizeof(fd_pubkey_t);
2783-
fd_pubkey_new( self->upgrade_authority_address );
2784-
fd_pubkey_generate( self->upgrade_authority_address, alloc_mem, rng );
2785-
}
2786-
else {
2787-
self->upgrade_authority_address = NULL;
2773+
self->has_upgrade_authority_address = fd_rng_uchar( rng ) % 2;
2774+
if( self->has_upgrade_authority_address ) {
2775+
fd_pubkey_generate( &self->upgrade_authority_address, alloc_mem, rng );
27882776
}
27892777
}
27902778
return mem;

src/flamenco/types/fd_types.c

Lines changed: 22 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/flamenco/types/fd_types.h

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/flamenco/types/fd_types.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,7 +1785,7 @@
17851785
"name": "bpf_upgradeable_loader_state_buffer",
17861786
"type": "struct",
17871787
"fields": [
1788-
{ "name": "authority_address", "type": "option", "element": "pubkey" }
1788+
{ "name": "authority_address", "type": "option", "element": "pubkey", "flat": true }
17891789
]
17901790
},
17911791
{
@@ -1800,7 +1800,7 @@
18001800
"type": "struct",
18011801
"fields": [
18021802
{ "name": "slot", "type": "ulong" },
1803-
{ "name": "upgrade_authority_address", "type": "option", "element": "pubkey" }
1803+
{ "name": "upgrade_authority_address", "type": "option", "element": "pubkey", "flat": true }
18041804
]
18051805
},
18061806
{

0 commit comments

Comments
 (0)