diff --git a/programs/bpf/c/src/ser/ser.c b/programs/bpf/c/src/ser/ser.c new file mode 100644 index 00000000000000..50900fbaf098e3 --- /dev/null +++ b/programs/bpf/c/src/ser/ser.c @@ -0,0 +1,34 @@ +/** + * @brief Example C-based BPF sanity rogram that prints out the parameters + * passed to it + */ +#include + +extern uint64_t entrypoint(const uint8_t *input) { + SolAccountInfo ka[1]; + SolParameters params = (SolParameters){.ka = ka}; + + sol_log(__FILE__); + + if (!sol_deserialize(input, ¶ms, SOL_ARRAY_SIZE(ka))) { + return ERROR_INVALID_ARGUMENT; + } + + char ka_data[] = {1, 2, 3}; + SolPubkey ka_owner; + sol_assert(params.ka_num == 2); + for (int i = 0; i < 2; i++) { + sol_assert(*params.ka[i].lamports == 42); + sol_assert(!sol_memcmp(params.ka[i].data, ka_data, 4)); + sol_assert(SolPubkey_same(params.ka[i].owner, &ka_owner)); + sol_assert(params.ka[i].is_signer == false); + sol_assert(params.ka[i].is_writable == false); + sol_assert(params.ka[i].executable == false); + } + + char data[] = {4, 5, 6, 7}; + sol_assert(params.data_len = 4); + sol_assert(!sol_memcmp(params.data, data, 4)); + + return SUCCESS; +} diff --git a/programs/bpf/tests/programs.rs b/programs/bpf/tests/programs.rs index 162204ef7c0583..58937b5d638257 100644 --- a/programs/bpf/tests/programs.rs +++ b/programs/bpf/tests/programs.rs @@ -1912,6 +1912,38 @@ fn test_program_bpf_disguised_as_bpf_loader() { } } +#[test] +#[cfg(feature = "bpf_c")] +fn test_program_bpf_c_dup() { + solana_logger::setup(); + + let GenesisConfigInfo { + genesis_config, + mint_keypair, + .. + } = create_genesis_config(50); + let mut bank = Bank::new(&genesis_config); + let (name, id, entrypoint) = solana_bpf_loader_program!(); + bank.add_builtin(&name, id, entrypoint); + + let account_address = Pubkey::new_unique(); + let account = + AccountSharedData::new_data(42, &[1_u8, 2, 3], &solana_sdk::system_program::id()).unwrap(); + bank.store_account(&account_address, &account); + + let bank_client = BankClient::new(bank); + + let program_id = load_bpf_program(&bank_client, &bpf_loader::id(), &mint_keypair, "ser"); + let account_metas = vec![ + AccountMeta::new_readonly(account_address, false), + AccountMeta::new_readonly(account_address, false), + ]; + let instruction = Instruction::new_with_bytes(program_id, &[4, 5, 6, 7], account_metas); + bank_client + .send_and_confirm_instruction(&mint_keypair, instruction) + .unwrap(); +} + #[cfg(feature = "bpf_rust")] #[test] fn test_program_bpf_upgrade_via_cpi() {