From 60e1186697f804d1849c526d63f4ce3951de2fb3 Mon Sep 17 00:00:00 2001 From: Lucas Steuernagel Date: Sat, 21 Dec 2024 13:19:18 -0300 Subject: [PATCH] Use syscall numbers in definitions --- Cargo.lock | 3 +- programs/bpf_loader/Cargo.toml | 3 +- programs/bpf_loader/src/syscalls/mod.rs | 129 ++++++++++++++++-------- programs/sbf/Cargo.lock | 2 +- sdk/cpi/src/syscalls.rs | 4 +- sdk/define-syscall/src/codes.rs | 51 ++++++++++ sdk/define-syscall/src/definitions.rs | 74 +++++++------- sdk/define-syscall/src/lib.rs | 72 ++----------- sdk/instruction/src/syscalls.rs | 4 +- sdk/program/src/syscalls/definitions.rs | 2 - svm/examples/Cargo.lock | 2 +- 11 files changed, 196 insertions(+), 150 deletions(-) create mode 100644 sdk/define-syscall/src/codes.rs diff --git a/Cargo.lock b/Cargo.lock index 9a856b3f1a31bb..b545f9fae2bef0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6229,13 +6229,12 @@ dependencies = [ "bincode", "byteorder", "libsecp256k1", - "log", - "memoffset 0.9.1", "rand 0.8.5", "scopeguard", "solana-bn254", "solana-compute-budget", "solana-curve25519", + "solana-define-syscall", "solana-feature-set", "solana-log-collector", "solana-measure", diff --git a/programs/bpf_loader/Cargo.toml b/programs/bpf_loader/Cargo.toml index fc99553d3fefe4..bc9af37619c168 100644 --- a/programs/bpf_loader/Cargo.toml +++ b/programs/bpf_loader/Cargo.toml @@ -13,11 +13,11 @@ edition = { workspace = true } bincode = { workspace = true } byteorder = { workspace = true } libsecp256k1 = { workspace = true } -log = { workspace = true } scopeguard = { workspace = true } solana-bn254 = { workspace = true } solana-compute-budget = { workspace = true } solana-curve25519 = { workspace = true } +solana-define-syscall = { workspace = true } solana-feature-set = { workspace = true } solana-log-collector = { workspace = true } solana-measure = { workspace = true } @@ -32,7 +32,6 @@ thiserror = { workspace = true } [dev-dependencies] assert_matches = { workspace = true } -memoffset = { workspace = true } rand = { workspace = true } solana-sdk = { workspace = true, features = ["dev-context-only-utils"] } test-case = { workspace = true } diff --git a/programs/bpf_loader/src/syscalls/mod.rs b/programs/bpf_loader/src/syscalls/mod.rs index c1f60fb67e2894..b9593c031c7922 100644 --- a/programs/bpf_loader/src/syscalls/mod.rs +++ b/programs/bpf_loader/src/syscalls/mod.rs @@ -18,6 +18,7 @@ use { ALT_BN128_PAIRING_ELEMENT_LEN, ALT_BN128_PAIRING_OUTPUT_LEN, }, solana_compute_budget::compute_budget::ComputeBudget, + solana_define_syscall::codes, solana_feature_set::{ self as feature_set, abort_on_invalid_curve, blake3_syscall_enabled, bpf_account_data_direct_mapping, curve25519_syscall_enabled, @@ -328,44 +329,64 @@ pub fn create_program_runtime_environment_v1<'a>( let mut result = BuiltinProgram::new_loader_with_dense_registration(config); // Abort - result.register_function("abort", 1, SyscallAbort::vm)?; + result.register_function("abort", codes::ABORT, SyscallAbort::vm)?; // Panic - result.register_function("sol_panic_", 2, SyscallPanic::vm)?; + result.register_function("sol_panic_", codes::SOL_PANIC, SyscallPanic::vm)?; // Logging - result.register_function("sol_log_", 7, SyscallLog::vm)?; - result.register_function("sol_log_64_", 8, SyscallLogU64::vm)?; - result.register_function("sol_log_pubkey", 9, SyscallLogPubkey::vm)?; - result.register_function("sol_log_compute_units_", 10, SyscallLogBpfComputeUnits::vm)?; + result.register_function("sol_log_", codes::SOL_LOG_, SyscallLog::vm)?; + result.register_function("sol_log_64_", codes::SOL_LOG_64_, SyscallLogU64::vm)?; + result.register_function( + "sol_log_pubkey", + codes::SOL_LOG_PUBKEY, + SyscallLogPubkey::vm, + )?; + result.register_function( + "sol_log_compute_units_", + codes::SOL_LOG_COMPUTE_UNITS_, + SyscallLogBpfComputeUnits::vm, + )?; // Program defined addresses (PDA) result.register_function( "sol_create_program_address", - 32, + codes::SOL_CREATE_PROGRAM_ADDRESS, SyscallCreateProgramAddress::vm, )?; result.register_function( "sol_try_find_program_address", - 33, + codes::SOL_TRY_FIND_PROGRAM_ADDRESS, SyscallTryFindProgramAddress::vm, )?; // Sha256 - result.register_function("sol_sha256", 17, SyscallHash::vm::)?; + result.register_function( + "sol_sha256", + codes::SOL_SHA256, + SyscallHash::vm::, + )?; // Keccak256 - result.register_function("sol_keccak256", 18, SyscallHash::vm::)?; + result.register_function( + "sol_keccak256", + codes::SOL_KECCAK256, + SyscallHash::vm::, + )?; // Secp256k1 Recover - result.register_function("sol_secp256k1_recover", 19, SyscallSecp256k1Recover::vm)?; + result.register_function( + "sol_secp256k1_recover", + codes::SOL_SECP256K1_RECOVER, + SyscallSecp256k1Recover::vm, + )?; // Blake3 register_feature_gated_function!( result, blake3_syscall_enabled, "sol_blake3", - 20, + codes::SOL_BLAKE3, SyscallHash::vm::, )?; @@ -374,45 +395,53 @@ pub fn create_program_runtime_environment_v1<'a>( result, curve25519_syscall_enabled, "sol_curve_validate_point", - 24, + codes::SOL_CURVE_VALIDATE_POINT, SyscallCurvePointValidation::vm, )?; register_feature_gated_function!( result, curve25519_syscall_enabled, "sol_curve_group_op", - 25, + codes::SOL_CURVE_GROUP_OP, SyscallCurveGroupOps::vm, )?; register_feature_gated_function!( result, curve25519_syscall_enabled, "sol_curve_multiscalar_mul", - 26, + codes::SOL_CURVE_MULTISCALAR_MUL, SyscallCurveMultiscalarMultiplication::vm, )?; // Sysvars - result.register_function("sol_get_clock_sysvar", 36, SyscallGetClockSysvar::vm)?; + result.register_function( + "sol_get_clock_sysvar", + codes::SOL_GET_CLOCK_SYSVAR, + SyscallGetClockSysvar::vm, + )?; result.register_function( "sol_get_epoch_schedule_sysvar", - 37, + codes::SOL_GET_EPOCH_SCHEDULE_SYSVAR, SyscallGetEpochScheduleSysvar::vm, )?; register_feature_gated_function!( result, !disable_fees_sysvar, "sol_get_fees_sysvar", - 40, + codes::SOL_GET_FEES_SYSVAR, SyscallGetFeesSysvar::vm, )?; - result.register_function("sol_get_rent_sysvar", 41, SyscallGetRentSysvar::vm)?; + result.register_function( + "sol_get_rent_sysvar", + codes::SOL_GET_RENT_SYSVAR, + SyscallGetRentSysvar::vm, + )?; register_feature_gated_function!( result, last_restart_slot_syscall_enabled, "sol_get_last_restart_slot", - 38, + codes::SOL_GET_LAST_RESTART_SLOT, SyscallGetLastRestartSlotSysvar::vm, )?; @@ -420,40 +449,60 @@ pub fn create_program_runtime_environment_v1<'a>( result, epoch_rewards_syscall_enabled, "sol_get_epoch_rewards_sysvar", - 39, + codes::SOL_GET_EPOCH_REWARDS_SYSVAR, SyscallGetEpochRewardsSysvar::vm, )?; // Memory ops - result.register_function("sol_memcpy_", 3, SyscallMemcpy::vm)?; - result.register_function("sol_memmove_", 4, SyscallMemmove::vm)?; - result.register_function("sol_memset_", 5, SyscallMemset::vm)?; - result.register_function("sol_memcmp_", 6, SyscallMemcmp::vm)?; + result.register_function("sol_memcpy_", codes::SOL_MEMCPY_, SyscallMemcpy::vm)?; + result.register_function("sol_memmove_", codes::SOL_MEMMOVE_, SyscallMemmove::vm)?; + result.register_function("sol_memset_", codes::SOL_MEMSET_, SyscallMemset::vm)?; + result.register_function("sol_memcmp_", codes::SOL_MEMCMP_, SyscallMemcmp::vm)?; // Processed sibling instructions result.register_function( "sol_get_processed_sibling_instruction", - 22, + codes::SOL_GET_PROCESSED_SIBLING_INSTRUCTION, SyscallGetProcessedSiblingInstruction::vm, )?; // Stack height - result.register_function("sol_get_stack_height", 23, SyscallGetStackHeight::vm)?; + result.register_function( + "sol_get_stack_height", + codes::SOL_GET_STACK_HEIGHT, + SyscallGetStackHeight::vm, + )?; // Return data - result.register_function("sol_set_return_data", 14, SyscallSetReturnData::vm)?; - result.register_function("sol_get_return_data", 15, SyscallGetReturnData::vm)?; + result.register_function( + "sol_set_return_data", + codes::SOL_SET_RETURN_DATA, + SyscallSetReturnData::vm, + )?; + result.register_function( + "sol_get_return_data", + codes::SOL_GET_RETURN_DATA, + SyscallGetReturnData::vm, + )?; // Cross-program invocation - result.register_function("sol_invoke_signed_c", 12, SyscallInvokeSignedC::vm)?; - result.register_function("sol_invoke_signed_rust", 13, SyscallInvokeSignedRust::vm)?; + result.register_function( + "sol_invoke_signed_c", + codes::SOL_INVOKE_SIGNED_C, + SyscallInvokeSignedC::vm, + )?; + result.register_function( + "sol_invoke_signed_rust", + codes::SOL_INVOKE_SIGNED_RUST, + SyscallInvokeSignedRust::vm, + )?; // Memory allocator register_feature_gated_function!( result, !disable_deploy_of_alloc_free_syscall, "sol_alloc_free_", - 11, + codes::SOL_ALLOC_FREE_, SyscallAllocFree::vm, )?; @@ -462,7 +511,7 @@ pub fn create_program_runtime_environment_v1<'a>( result, enable_alt_bn128_syscall, "sol_alt_bn128_group_op", - 28, + codes::SOL_ALT_BN128_GROUP_OP, SyscallAltBn128::vm, )?; @@ -471,7 +520,7 @@ pub fn create_program_runtime_environment_v1<'a>( result, enable_big_mod_exp_syscall, "sol_big_mod_exp", - 30, + codes::SOL_BIG_MOD_EXP, SyscallBigModExp::vm, )?; @@ -480,7 +529,7 @@ pub fn create_program_runtime_environment_v1<'a>( result, enable_poseidon_syscall, "sol_poseidon", - 21, + codes::SOL_POSEIDON, SyscallPoseidon::vm, )?; @@ -489,7 +538,7 @@ pub fn create_program_runtime_environment_v1<'a>( result, remaining_compute_units_syscall_enabled, "sol_remaining_compute_units", - 31, + codes::SOL_REMAINING_COMPUTE_UNITS, SyscallRemainingComputeUnits::vm )?; @@ -498,7 +547,7 @@ pub fn create_program_runtime_environment_v1<'a>( result, enable_alt_bn128_compression_syscall, "sol_alt_bn128_compression", - 29, + codes::SOL_ALT_BN128_COMPRESSION, SyscallAltBn128Compression::vm, )?; @@ -507,7 +556,7 @@ pub fn create_program_runtime_environment_v1<'a>( result, get_sysvar_syscall_enabled, "sol_get_sysvar", - 34, + codes::SOL_GET_SYSVAR, SyscallGetSysvar::vm, )?; @@ -516,12 +565,12 @@ pub fn create_program_runtime_environment_v1<'a>( result, enable_get_epoch_stake_syscall, "sol_get_epoch_stake", - 35, + codes::SOL_GET_EPOCH_STAKE, SyscallGetEpochStake::vm, )?; // Log data - result.register_function("sol_log_data", 16, SyscallLogData::vm)?; + result.register_function("sol_log_data", codes::SOL_LOG_DATA, SyscallLogData::vm)?; Ok(result) } diff --git a/programs/sbf/Cargo.lock b/programs/sbf/Cargo.lock index b2d05ea2c66871..cdc7cb33ed2374 100644 --- a/programs/sbf/Cargo.lock +++ b/programs/sbf/Cargo.lock @@ -5115,11 +5115,11 @@ dependencies = [ "bincode", "byteorder 1.5.0", "libsecp256k1 0.6.0", - "log", "scopeguard", "solana-bn254", "solana-compute-budget", "solana-curve25519", + "solana-define-syscall", "solana-feature-set", "solana-log-collector", "solana-measure", diff --git a/sdk/cpi/src/syscalls.rs b/sdk/cpi/src/syscalls.rs index 7175f53852987c..9f9dfb301e5808 100644 --- a/sdk/cpi/src/syscalls.rs +++ b/sdk/cpi/src/syscalls.rs @@ -1,7 +1,9 @@ +#[allow(unused)] +use solana_define_syscall::codes::SOL_GET_RETURN_DATA; /// Syscall definitions used by `solana_cpi`. pub use solana_define_syscall::definitions::{ sol_invoke_signed_c, sol_invoke_signed_rust, sol_set_return_data, }; use {solana_define_syscall::define_syscall, solana_pubkey::Pubkey}; -define_syscall!(fn sol_get_return_data(data: *mut u8, length: u64, program_id: *mut Pubkey) -> u64); +define_syscall!(fn sol_get_return_data(data: *mut u8, length: u64, program_id: *mut Pubkey) -> u64, SOL_GET_RETURN_DATA); diff --git a/sdk/define-syscall/src/codes.rs b/sdk/define-syscall/src/codes.rs new file mode 100644 index 00000000000000..d55e3b350eaa37 --- /dev/null +++ b/sdk/define-syscall/src/codes.rs @@ -0,0 +1,51 @@ +/// These are syscall codes specified in SIMD-0178. +/// If a new syscall is to be included, add a new number constant +/// for correct registration. + +macro_rules! define_code { + ($name:ident, $code:literal) => { + pub const $name: u32 = $code; + }; +} + +define_code!(ABORT, 1); +define_code!(SOL_PANIC, 2); +define_code!(SOL_MEMCPY_, 3); +define_code!(SOL_MEMMOVE_, 4); +define_code!(SOL_MEMSET_, 5); +define_code!(SOL_MEMCMP_, 6); +define_code!(SOL_LOG_, 7); +define_code!(SOL_LOG_64_, 8); +define_code!(SOL_LOG_PUBKEY, 9); +define_code!(SOL_LOG_COMPUTE_UNITS_, 10); +define_code!(SOL_ALLOC_FREE_, 11); +define_code!(SOL_INVOKE_SIGNED_C, 12); +define_code!(SOL_INVOKE_SIGNED_RUST, 13); +define_code!(SOL_SET_RETURN_DATA, 14); +define_code!(SOL_GET_RETURN_DATA, 15); +define_code!(SOL_LOG_DATA, 16); +define_code!(SOL_SHA256, 17); +define_code!(SOL_KECCAK256, 18); +define_code!(SOL_SECP256K1_RECOVER, 19); +define_code!(SOL_BLAKE3, 20); +define_code!(SOL_POSEIDON, 21); +define_code!(SOL_GET_PROCESSED_SIBLING_INSTRUCTION, 22); +define_code!(SOL_GET_STACK_HEIGHT, 23); +define_code!(SOL_CURVE_VALIDATE_POINT, 24); +define_code!(SOL_CURVE_GROUP_OP, 25); +define_code!(SOL_CURVE_MULTISCALAR_MUL, 26); +define_code!(SOL_CURVE_PAIRING_MAP, 27); +define_code!(SOL_ALT_BN128_GROUP_OP, 28); +define_code!(SOL_ALT_BN128_COMPRESSION, 29); +define_code!(SOL_BIG_MOD_EXP, 30); +define_code!(SOL_REMAINING_COMPUTE_UNITS, 31); +define_code!(SOL_CREATE_PROGRAM_ADDRESS, 32); +define_code!(SOL_TRY_FIND_PROGRAM_ADDRESS, 33); +define_code!(SOL_GET_SYSVAR, 34); +define_code!(SOL_GET_EPOCH_STAKE, 35); +define_code!(SOL_GET_CLOCK_SYSVAR, 36); +define_code!(SOL_GET_EPOCH_SCHEDULE_SYSVAR, 37); +define_code!(SOL_GET_LAST_RESTART_SLOT, 38); +define_code!(SOL_GET_EPOCH_REWARDS_SYSVAR, 39); +define_code!(SOL_GET_FEES_SYSVAR, 40); +define_code!(SOL_GET_RENT_SYSVAR, 41); diff --git a/sdk/define-syscall/src/definitions.rs b/sdk/define-syscall/src/definitions.rs index 0f75edb1333b7a..1846ed769fda82 100644 --- a/sdk/define-syscall/src/definitions.rs +++ b/sdk/define-syscall/src/definitions.rs @@ -1,43 +1,45 @@ //! This module is only for syscall definitions that bring in no extra dependencies. +#[allow(unused)] +use crate::codes::*; use crate::define_syscall; -define_syscall!(fn sol_secp256k1_recover(hash: *const u8, recovery_id: u64, signature: *const u8, result: *mut u8) -> u64); -define_syscall!(fn sol_poseidon(parameters: u64, endianness: u64, vals: *const u8, val_len: u64, hash_result: *mut u8) -> u64); -define_syscall!(fn sol_invoke_signed_c(instruction_addr: *const u8, account_infos_addr: *const u8, account_infos_len: u64, signers_seeds_addr: *const u8, signers_seeds_len: u64) -> u64); -define_syscall!(fn sol_invoke_signed_rust(instruction_addr: *const u8, account_infos_addr: *const u8, account_infos_len: u64, signers_seeds_addr: *const u8, signers_seeds_len: u64) -> u64); -define_syscall!(fn sol_set_return_data(data: *const u8, length: u64)); -define_syscall!(fn sol_get_stack_height() -> u64); -define_syscall!(fn sol_log_(message: *const u8, len: u64)); -define_syscall!(fn sol_log_64_(arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64)); -define_syscall!(fn sol_log_compute_units_()); -define_syscall!(fn sol_log_data(data: *const u8, data_len: u64)); -define_syscall!(fn sol_memcpy_(dst: *mut u8, src: *const u8, n: u64)); -define_syscall!(fn sol_memmove_(dst: *mut u8, src: *const u8, n: u64)); -define_syscall!(fn sol_memcmp_(s1: *const u8, s2: *const u8, n: u64, result: *mut i32)); -define_syscall!(fn sol_memset_(s: *mut u8, c: u8, n: u64)); -define_syscall!(fn sol_log_pubkey(pubkey_addr: *const u8)); -define_syscall!(fn sol_create_program_address(seeds_addr: *const u8, seeds_len: u64, program_id_addr: *const u8, address_bytes_addr: *const u8) -> u64); -define_syscall!(fn sol_try_find_program_address(seeds_addr: *const u8, seeds_len: u64, program_id_addr: *const u8, address_bytes_addr: *const u8, bump_seed_addr: *const u8) -> u64); -define_syscall!(fn sol_sha256(vals: *const u8, val_len: u64, hash_result: *mut u8) -> u64); -define_syscall!(fn sol_keccak256(vals: *const u8, val_len: u64, hash_result: *mut u8) -> u64); -define_syscall!(fn sol_blake3(vals: *const u8, val_len: u64, hash_result: *mut u8) -> u64); -define_syscall!(fn sol_curve_validate_point(curve_id: u64, point_addr: *const u8, result: *mut u8) -> u64); -define_syscall!(fn sol_curve_group_op(curve_id: u64, group_op: u64, left_input_addr: *const u8, right_input_addr: *const u8, result_point_addr: *mut u8) -> u64); -define_syscall!(fn sol_curve_multiscalar_mul(curve_id: u64, scalars_addr: *const u8, points_addr: *const u8, points_len: u64, result_point_addr: *mut u8) -> u64); -define_syscall!(fn sol_curve_pairing_map(curve_id: u64, point: *const u8, result: *mut u8) -> u64); -define_syscall!(fn sol_alt_bn128_group_op(group_op: u64, input: *const u8, input_size: u64, result: *mut u8) -> u64); -define_syscall!(fn sol_big_mod_exp(params: *const u8, result: *mut u8) -> u64); -define_syscall!(fn sol_remaining_compute_units() -> u64); -define_syscall!(fn sol_alt_bn128_compression(op: u64, input: *const u8, input_size: u64, result: *mut u8) -> u64); -define_syscall!(fn sol_get_sysvar(sysvar_id_addr: *const u8, result: *mut u8, offset: u64, length: u64) -> u64); -define_syscall!(fn sol_get_epoch_stake(vote_address: *const u8) -> u64); +define_syscall!(fn sol_secp256k1_recover(hash: *const u8, recovery_id: u64, signature: *const u8, result: *mut u8) -> u64, SOL_SECP256K1_RECOVER); +define_syscall!(fn sol_poseidon(parameters: u64, endianness: u64, vals: *const u8, val_len: u64, hash_result: *mut u8) -> u64, SOL_POSEIDON); +define_syscall!(fn sol_invoke_signed_c(instruction_addr: *const u8, account_infos_addr: *const u8, account_infos_len: u64, signers_seeds_addr: *const u8, signers_seeds_len: u64) -> u64, SOL_INVOKE_SIGNED_C); +define_syscall!(fn sol_invoke_signed_rust(instruction_addr: *const u8, account_infos_addr: *const u8, account_infos_len: u64, signers_seeds_addr: *const u8, signers_seeds_len: u64) -> u64, SOL_INVOKE_SIGNED_RUST); +define_syscall!(fn sol_set_return_data(data: *const u8, length: u64), SOL_SET_RETURN_DATA); +define_syscall!(fn sol_get_stack_height() -> u64, SOL_GET_STACK_HEIGHT); +define_syscall!(fn sol_log_(message: *const u8, len: u64), SOL_LOG_); +define_syscall!(fn sol_log_64_(arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64), SOL_LOG_64_); +define_syscall!(fn sol_log_compute_units_(), SOL_LOG_COMPUTE_UNITS_); +define_syscall!(fn sol_log_data(data: *const u8, data_len: u64), SOL_LOG_DATA); +define_syscall!(fn sol_memcpy_(dst: *mut u8, src: *const u8, n: u64), SOL_MEMCPY_); +define_syscall!(fn sol_memmove_(dst: *mut u8, src: *const u8, n: u64), SOL_MEMMOVE_); +define_syscall!(fn sol_memcmp_(s1: *const u8, s2: *const u8, n: u64, result: *mut i32), SOL_MEMCMP_); +define_syscall!(fn sol_memset_(s: *mut u8, c: u8, n: u64), SOL_MEMSET_); +define_syscall!(fn sol_log_pubkey(pubkey_addr: *const u8), SOL_LOG_PUBKEY); +define_syscall!(fn sol_create_program_address(seeds_addr: *const u8, seeds_len: u64, program_id_addr: *const u8, address_bytes_addr: *const u8) -> u64, SOL_CREATE_PROGRAM_ADDRESS); +define_syscall!(fn sol_try_find_program_address(seeds_addr: *const u8, seeds_len: u64, program_id_addr: *const u8, address_bytes_addr: *const u8, bump_seed_addr: *const u8) -> u64, SOL_TRY_FIND_PROGRAM_ADDRESS); +define_syscall!(fn sol_sha256(vals: *const u8, val_len: u64, hash_result: *mut u8) -> u64, SOL_SHA256); +define_syscall!(fn sol_keccak256(vals: *const u8, val_len: u64, hash_result: *mut u8) -> u64, SOL_KECCAK256); +define_syscall!(fn sol_blake3(vals: *const u8, val_len: u64, hash_result: *mut u8) -> u64, SOL_BLAKE3); +define_syscall!(fn sol_curve_validate_point(curve_id: u64, point_addr: *const u8, result: *mut u8) -> u64, SOL_CURVE_VALIDATE_POINT); +define_syscall!(fn sol_curve_group_op(curve_id: u64, group_op: u64, left_input_addr: *const u8, right_input_addr: *const u8, result_point_addr: *mut u8) -> u64, SOL_CURVE_GROUP_OP); +define_syscall!(fn sol_curve_multiscalar_mul(curve_id: u64, scalars_addr: *const u8, points_addr: *const u8, points_len: u64, result_point_addr: *mut u8) -> u64, SOL_CURVE_MULTISCALAR_MUL); +define_syscall!(fn sol_curve_pairing_map(curve_id: u64, point: *const u8, result: *mut u8) -> u64, SOL_CURVE_PAIRING_MAP); +define_syscall!(fn sol_alt_bn128_group_op(group_op: u64, input: *const u8, input_size: u64, result: *mut u8) -> u64, SOL_ALT_BN128_GROUP_OP); +define_syscall!(fn sol_big_mod_exp(params: *const u8, result: *mut u8) -> u64, SOL_BIG_MOD_EXP); +define_syscall!(fn sol_remaining_compute_units() -> u64, SOL_REMAINING_COMPUTE_UNITS); +define_syscall!(fn sol_alt_bn128_compression(op: u64, input: *const u8, input_size: u64, result: *mut u8) -> u64, SOL_ALT_BN128_COMPRESSION); +define_syscall!(fn sol_get_sysvar(sysvar_id_addr: *const u8, result: *mut u8, offset: u64, length: u64) -> u64, SOL_GET_SYSVAR); +define_syscall!(fn sol_get_epoch_stake(vote_address: *const u8) -> u64, SOL_GET_EPOCH_STAKE); // these are to be deprecated once they are superceded by sol_get_sysvar -define_syscall!(fn sol_get_clock_sysvar(addr: *mut u8) -> u64); -define_syscall!(fn sol_get_epoch_schedule_sysvar(addr: *mut u8) -> u64); -define_syscall!(fn sol_get_rent_sysvar(addr: *mut u8) -> u64); -define_syscall!(fn sol_get_last_restart_slot(addr: *mut u8) -> u64); -define_syscall!(fn sol_get_epoch_rewards_sysvar(addr: *mut u8) -> u64); +define_syscall!(fn sol_get_clock_sysvar(addr: *mut u8) -> u64, SOL_GET_CLOCK_SYSVAR); +define_syscall!(fn sol_get_epoch_schedule_sysvar(addr: *mut u8) -> u64, SOL_GET_EPOCH_SCHEDULE_SYSVAR); +define_syscall!(fn sol_get_rent_sysvar(addr: *mut u8) -> u64, SOL_GET_RENT_SYSVAR); +define_syscall!(fn sol_get_last_restart_slot(addr: *mut u8) -> u64, SOL_GET_LAST_RESTART_SLOT); +define_syscall!(fn sol_get_epoch_rewards_sysvar(addr: *mut u8) -> u64, SOL_GET_EPOCH_REWARDS_SYSVAR); // this cannot go through sol_get_sysvar but can be removed once no longer in use -define_syscall!(fn sol_get_fees_sysvar(addr: *mut u8) -> u64); +define_syscall!(fn sol_get_fees_sysvar(addr: *mut u8) -> u64, SOL_GET_FEES_SYSVAR); diff --git a/sdk/define-syscall/src/lib.rs b/sdk/define-syscall/src/lib.rs index 119afea0ec7188..e9dae7f50338fe 100644 --- a/sdk/define-syscall/src/lib.rs +++ b/sdk/define-syscall/src/lib.rs @@ -1,86 +1,30 @@ +pub mod codes; pub mod definitions; #[cfg(target_feature = "static-syscalls")] #[macro_export] macro_rules! define_syscall { - (fn $name:ident($($arg:ident: $typ:ty),*) -> $ret:ty) => { + (fn $name:ident($($arg:ident: $typ:ty),*) -> $ret:ty, $code:ident) => { #[inline] pub unsafe fn $name($($arg: $typ),*) -> $ret { - // this enum is used to force the hash to be computed in a const context - #[repr(usize)] - enum Syscall { - Code = $crate::sys_hash(stringify!($name)), - } - - let syscall: extern "C" fn($($arg: $typ),*) -> $ret = core::mem::transmute(Syscall::Code); + let syscall: extern "C" fn($($arg: $typ),*) -> $ret = core::mem::transmute($code as usize); syscall($($arg),*) } - }; - (fn $name:ident($($arg:ident: $typ:ty),*)) => { - define_syscall!(fn $name($($arg: $typ),*) -> ()); + (fn $name:ident($($arg:ident: $typ:ty),*), $code:ident) => { + define_syscall!(fn $name($($arg: $typ),*) -> (), $code); } } #[cfg(not(target_feature = "static-syscalls"))] #[macro_export] macro_rules! define_syscall { - (fn $name:ident($($arg:ident: $typ:ty),*) -> $ret:ty) => { + (fn $name:ident($($arg:ident: $typ:ty),*) -> $ret:ty, $code:ident) => { extern "C" { pub fn $name($($arg: $typ),*) -> $ret; } }; - (fn $name:ident($($arg:ident: $typ:ty),*)) => { - define_syscall!(fn $name($($arg: $typ),*) -> ()); - } -} - -#[cfg(target_feature = "static-syscalls")] -pub const fn sys_hash(name: &str) -> usize { - murmur3_32(name.as_bytes(), 0) as usize -} - -#[cfg(target_feature = "static-syscalls")] -const fn murmur3_32(buf: &[u8], seed: u32) -> u32 { - const fn pre_mix(buf: [u8; 4]) -> u32 { - u32::from_le_bytes(buf) - .wrapping_mul(0xcc9e2d51) - .rotate_left(15) - .wrapping_mul(0x1b873593) - } - - let mut hash = seed; - - let mut i = 0; - while i < buf.len() / 4 { - let buf = [buf[i * 4], buf[i * 4 + 1], buf[i * 4 + 2], buf[i * 4 + 3]]; - hash ^= pre_mix(buf); - hash = hash.rotate_left(13); - hash = hash.wrapping_mul(5).wrapping_add(0xe6546b64); - - i += 1; - } - - match buf.len() % 4 { - 0 => {} - 1 => { - hash = hash ^ pre_mix([buf[i * 4], 0, 0, 0]); - } - 2 => { - hash = hash ^ pre_mix([buf[i * 4], buf[i * 4 + 1], 0, 0]); - } - 3 => { - hash = hash ^ pre_mix([buf[i * 4], buf[i * 4 + 1], buf[i * 4 + 2], 0]); - } - _ => { /* unreachable!() */ } + (fn $name:ident($($arg:ident: $typ:ty),*), $code:ident) => { + define_syscall!(fn $name($($arg: $typ),*) -> (), $code); } - - hash = hash ^ buf.len() as u32; - hash = hash ^ (hash.wrapping_shr(16)); - hash = hash.wrapping_mul(0x85ebca6b); - hash = hash ^ (hash.wrapping_shr(13)); - hash = hash.wrapping_mul(0xc2b2ae35); - hash = hash ^ (hash.wrapping_shr(16)); - - hash } diff --git a/sdk/instruction/src/syscalls.rs b/sdk/instruction/src/syscalls.rs index f811f00d8a1ada..5395d17a0a2f7d 100644 --- a/sdk/instruction/src/syscalls.rs +++ b/sdk/instruction/src/syscalls.rs @@ -1,3 +1,5 @@ +#[allow(unused)] +use solana_define_syscall::codes::SOL_GET_PROCESSED_SIBLING_INSTRUCTION; pub use solana_define_syscall::definitions::sol_get_stack_height; use { crate::{AccountMeta, ProcessedSiblingInstruction}, @@ -5,4 +7,4 @@ use { solana_pubkey::Pubkey, }; -define_syscall!(fn sol_get_processed_sibling_instruction(index: u64, meta: *mut ProcessedSiblingInstruction, program_id: *mut Pubkey, data: *mut u8, accounts: *mut AccountMeta) -> u64); +define_syscall!(fn sol_get_processed_sibling_instruction(index: u64, meta: *mut ProcessedSiblingInstruction, program_id: *mut Pubkey, data: *mut u8, accounts: *mut AccountMeta) -> u64, SOL_GET_PROCESSED_SIBLING_INSTRUCTION); diff --git a/sdk/program/src/syscalls/definitions.rs b/sdk/program/src/syscalls/definitions.rs index 1eba11116cdd0d..1efdf0a211e990 100644 --- a/sdk/program/src/syscalls/definitions.rs +++ b/sdk/program/src/syscalls/definitions.rs @@ -13,8 +13,6 @@ pub use solana_define_syscall::definitions::{ sol_get_epoch_stake, sol_get_fees_sysvar, sol_get_last_restart_slot, sol_get_rent_sysvar, sol_get_sysvar, sol_keccak256, sol_remaining_compute_units, }; -#[cfg(target_feature = "static-syscalls")] -pub use solana_define_syscall::sys_hash; #[deprecated(since = "2.1.0", note = "Use `solana_instruction::syscalls` instead")] pub use solana_instruction::syscalls::{ sol_get_processed_sibling_instruction, sol_get_stack_height, diff --git a/svm/examples/Cargo.lock b/svm/examples/Cargo.lock index c1f6c445e421fd..7510566726abdd 100644 --- a/svm/examples/Cargo.lock +++ b/svm/examples/Cargo.lock @@ -4966,11 +4966,11 @@ dependencies = [ "bincode", "byteorder", "libsecp256k1", - "log", "scopeguard", "solana-bn254", "solana-compute-budget", "solana-curve25519", + "solana-define-syscall", "solana-feature-set", "solana-log-collector", "solana-measure",