Skip to content

Commit

Permalink
flamenco, runtime: fix copying calldests into program cache
Browse files Browse the repository at this point in the history
  • Loading branch information
topointon-jump committed Oct 22, 2024
1 parent 6e4df6a commit b7d6bbf
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/ballet/sbpf/fd_sbpf_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -599,11 +599,11 @@ fd_sbpf_program_new( void * prog_mem,
/* Initialize calldests map */

ulong pc_max = elf_info->rodata_sz / 8UL;
prog->calldests =
fd_sbpf_calldests_join( fd_sbpf_calldests_new(
prog->calldests_shmem = fd_sbpf_calldests_new(
FD_SCRATCH_ALLOC_APPEND( laddr, fd_sbpf_calldests_align(),
fd_sbpf_calldests_footprint( pc_max ) ),
pc_max ) );
pc_max );
prog->calldests = fd_sbpf_calldests_join( prog->calldests_shmem );

return prog;
}
Expand Down
2 changes: 2 additions & 0 deletions src/ballet/sbpf/fd_sbpf_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ struct __attribute__((aligned(32UL))) fd_sbpf_program {
ulong entry_pc; /* entrypoint PC (at text[ entry_pc - start_pc ]) ... FIXME: HMMMM ... CODE SEEMS TO USE TEXT[ ENTRY_PC ] */

/* Bit vector of valid call destinations (bit count is rodata_sz) */
void * calldests_shmem;
/* Local join to bit vector of valid call destinations */
fd_sbpf_calldests_t * calldests;
};
typedef struct fd_sbpf_program fd_sbpf_program_t;
Expand Down
4 changes: 2 additions & 2 deletions src/flamenco/runtime/program/fd_bpf_loader_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,9 @@ execute( fd_exec_instr_ctx_t * instr_ctx, fd_sbpf_validated_program_t * prog, uc
/* instr_ctx */ instr_ctx,
/* heap_max */ heap_max, /* TODO configure heap allocator */
/* entry_cu */ instr_ctx->txn_ctx->compute_meter,
/* rodata */ fd_sbpf_validated_program_rodata( prog ),
/* rodata */ prog->rodata,
/* rodata_sz */ prog->rodata_sz,
/* text */ (ulong *)((ulong)fd_sbpf_validated_program_rodata( prog ) + (ulong)prog->text_off), /* Note: text_off is byte offset */
/* text */ (ulong *)((ulong)prog->rodata + (ulong)prog->text_off), /* Note: text_off is byte offset */
/* text_cnt */ prog->text_cnt,
/* text_off */ prog->text_off,
/* text_sz */ prog->text_sz,
Expand Down
38 changes: 19 additions & 19 deletions src/flamenco/runtime/program/fd_bpf_program_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@
#include <assert.h>

fd_sbpf_validated_program_t *
fd_sbpf_validated_program_new( void * mem ) {
fd_sbpf_validated_program_new( void * mem, fd_sbpf_elf_info_t const * elf_info ) {
fd_sbpf_validated_program_t * validated_prog = (fd_sbpf_validated_program_t *)mem;

ulong l = FD_LAYOUT_INIT;

/* calldests backing memory */
l = FD_LAYOUT_APPEND( l, alignof(fd_sbpf_validated_program_t), sizeof(fd_sbpf_validated_program_t) );
validated_prog->calldests_shmem = (uchar *)mem + l;

/* rodata backing memory */
l = FD_LAYOUT_APPEND( l, fd_sbpf_calldests_align(), fd_sbpf_calldests_footprint(elf_info->rodata_sz/8UL) );
validated_prog->rodata = (uchar *)mem + l;

return (fd_sbpf_validated_program_t *)mem;
}

Expand All @@ -20,23 +32,12 @@ ulong
fd_sbpf_validated_program_footprint( fd_sbpf_elf_info_t const * elf_info ) {
ulong l = FD_LAYOUT_INIT;
l = FD_LAYOUT_APPEND( l, alignof(fd_sbpf_validated_program_t), sizeof(fd_sbpf_validated_program_t) );
assert( l==offsetof(fd_sbpf_validated_program_t, calldests) );
l = FD_LAYOUT_APPEND( l, fd_sbpf_calldests_align(), fd_sbpf_calldests_footprint(elf_info->rodata_sz/8UL) );
l = FD_LAYOUT_APPEND( l, 8UL, elf_info->rodata_footprint );
l = FD_LAYOUT_FINI( l, 128UL );
return l;
}

uchar *
fd_sbpf_validated_program_rodata( fd_sbpf_validated_program_t * prog ) {
ulong l = FD_LAYOUT_INIT;
l = FD_LAYOUT_APPEND( l, alignof(fd_sbpf_validated_program_t), sizeof(fd_sbpf_validated_program_t) );
assert( l==offsetof(fd_sbpf_validated_program_t, calldests) );
l = FD_LAYOUT_APPEND( l, fd_sbpf_calldests_align(), fd_sbpf_calldests_footprint(prog->rodata_sz/8UL) );
l = FD_LAYOUT_FINI( l, FD_SBPF_PROG_RODATA_ALIGN );
return (uchar *)fd_type_pun(prog) + l;
}

static inline fd_funk_rec_key_t
fd_acc_mgr_cache_key( fd_pubkey_t const * pubkey ) {
fd_funk_rec_key_t id;
Expand Down Expand Up @@ -154,14 +155,12 @@ fd_bpf_create_bpf_program_cache_entry( fd_exec_slot_ctx_t * slot_ctx,
return -1;
}

uchar * val = fd_funk_val( rec, fd_funk_wksp( funk ) );
fd_sbpf_validated_program_t * validated_prog = (fd_sbpf_validated_program_t *)val;
validated_prog->rodata_sz = elf_info.rodata_sz;
uchar * rodata = fd_sbpf_validated_program_rodata( validated_prog );

void * val = fd_funk_val( rec, fd_funk_wksp( funk ) );
fd_sbpf_validated_program_t * validated_prog = fd_sbpf_validated_program_new( val, &elf_info );

ulong prog_align = fd_sbpf_program_align();
ulong prog_footprint = fd_sbpf_program_footprint( &elf_info );
fd_sbpf_program_t * prog = fd_sbpf_program_new( fd_scratch_alloc( prog_align, prog_footprint ), &elf_info, rodata );
fd_sbpf_program_t * prog = fd_sbpf_program_new( fd_scratch_alloc( prog_align, prog_footprint ), &elf_info, validated_prog->rodata );
if( FD_UNLIKELY( !prog ) ) {
if( update_program_blacklist ) {
fd_bpf_add_to_program_blacklist( slot_ctx, program_pubkey );
Expand Down Expand Up @@ -241,7 +240,8 @@ fd_bpf_create_bpf_program_cache_entry( fd_exec_slot_ctx_t * slot_ctx,
}
}

fd_memcpy( validated_prog->calldests, prog->calldests, fd_sbpf_calldests_footprint(prog->rodata_sz/8UL) );
fd_memcpy( validated_prog->calldests_shmem, prog->calldests_shmem, fd_sbpf_calldests_footprint(prog->rodata_sz/8UL) );
validated_prog->calldests = fd_sbpf_calldests_join( validated_prog->calldests_shmem );

validated_prog->entry_pc = prog->entry_pc;
validated_prog->last_updated_slot = slot_ctx->slot_bank.slot;
Expand Down
14 changes: 9 additions & 5 deletions src/flamenco/runtime/program/fd_bpf_program_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@ struct fd_sbpf_validated_program {

ulong rodata_sz;

fd_sbpf_calldests_t calldests[];
/* We keep the pointer to the calldests raw memory around, so that we can easily copy the entire
data structures (including the private header) later. */
void * calldests_shmem;
fd_sbpf_calldests_t * calldests;

uchar * rodata;

/* Backing memory for calldests and rodata */
// uchar calldests_shmem[];
// uchar rodata[];
};
typedef struct fd_sbpf_validated_program fd_sbpf_validated_program_t;

FD_PROTOTYPES_BEGIN

fd_sbpf_validated_program_t *
fd_sbpf_validated_program_new( void * mem );
fd_sbpf_validated_program_new( void * mem, fd_sbpf_elf_info_t const * elf_info );


ulong
Expand All @@ -34,9 +41,6 @@ fd_sbpf_validated_program_align( void );
ulong
fd_sbpf_validated_program_footprint( fd_sbpf_elf_info_t const * elf_info );

uchar *
fd_sbpf_validated_program_rodata( fd_sbpf_validated_program_t * prog );

/* FIXME: Implement this (or remove?) */
ulong
fd_sbpf_validated_program_from_sbpf_program( fd_sbpf_program_t const * prog,
Expand Down

0 comments on commit b7d6bbf

Please sign in to comment.