From 660ea02f5876dba8bb4aed7fb1711cde54fad0ce Mon Sep 17 00:00:00 2001 From: Tao Zhu Date: Thu, 21 Sep 2023 14:32:42 +0000 Subject: [PATCH] heap_size type to be consistent with request instruction --- program-runtime/src/compute_budget.rs | 12 ++++++------ programs/bpf_loader/src/lib.rs | 8 ++++---- programs/loader-v4/src/lib.rs | 13 ++++++------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/program-runtime/src/compute_budget.rs b/program-runtime/src/compute_budget.rs index 6fa9cda02b7228..9f88f0e66fe050 100644 --- a/program-runtime/src/compute_budget.rs +++ b/program-runtime/src/compute_budget.rs @@ -102,7 +102,7 @@ pub struct ComputeBudget { /// The total cost is calculated as `msm_base_cost + (length - 1) * msm_incremental_cost`. pub curve25519_ristretto_msm_incremental_cost: u64, /// program heap region size, default: solana_sdk::entrypoint::HEAP_LENGTH - pub heap_size: usize, + pub heap_size: u32, /// Number of compute units per additional 32k heap above the default (~.5 /// us per 32k at 15 units/us rounded up) pub heap_cost: u64, @@ -171,7 +171,7 @@ impl ComputeBudget { curve25519_ristretto_multiply_cost: 2_208, curve25519_ristretto_msm_base_cost: 2303, curve25519_ristretto_msm_incremental_cost: 788, - heap_size: solana_sdk::entrypoint::HEAP_LENGTH, + heap_size: u32::try_from(solana_sdk::entrypoint::HEAP_LENGTH).unwrap(), heap_cost: 8, mem_op_base_cost: 10, alt_bn128_addition_cost: 334, @@ -267,7 +267,7 @@ impl ComputeBudget { InstructionError::InvalidInstructionData, )); } - self.heap_size = bytes as usize; + self.heap_size = bytes; } let compute_unit_limit = updated_compute_unit_limit @@ -512,7 +512,7 @@ mod tests { Ok(PrioritizationFeeDetails::default()), ComputeBudget { compute_unit_limit: DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT as u64, - heap_size: MAX_HEAP_FRAME_BYTES as usize, + heap_size: MAX_HEAP_FRAME_BYTES, ..ComputeBudget::default() } ); @@ -562,7 +562,7 @@ mod tests { )), ComputeBudget { compute_unit_limit: MAX_COMPUTE_UNIT_LIMIT as u64, - heap_size: MAX_HEAP_FRAME_BYTES as usize, + heap_size: MAX_HEAP_FRAME_BYTES, ..ComputeBudget::default() } ); @@ -580,7 +580,7 @@ mod tests { )), ComputeBudget { compute_unit_limit: 1, - heap_size: MAX_HEAP_FRAME_BYTES as usize, + heap_size: MAX_HEAP_FRAME_BYTES, ..ComputeBudget::default() } ); diff --git a/programs/bpf_loader/src/lib.rs b/programs/bpf_loader/src/lib.rs index ae585d8f9582f2..9ca1da413fce26 100644 --- a/programs/bpf_loader/src/lib.rs +++ b/programs/bpf_loader/src/lib.rs @@ -191,10 +191,10 @@ pub fn check_loader_id(id: &Pubkey) -> bool { } /// Only used in macro, do not use directly! -pub fn calculate_heap_cost(heap_size: u64, heap_cost: u64, enable_rounding_fix: bool) -> u64 { +pub fn calculate_heap_cost(heap_size: u32, heap_cost: u64, enable_rounding_fix: bool) -> u64 { const KIBIBYTE: u64 = 1024; const PAGE_SIZE_KB: u64 = 32; - let mut rounded_heap_size = heap_size; + let mut rounded_heap_size = u64::from(heap_size); if enable_rounding_fix { rounded_heap_size = rounded_heap_size .saturating_add(PAGE_SIZE_KB.saturating_mul(KIBIBYTE).saturating_sub(1)); @@ -267,7 +267,7 @@ macro_rules! create_vm { .feature_set .is_active(&solana_sdk::feature_set::round_up_heap_size::id()); let mut heap_cost_result = invoke_context.consume_checked($crate::calculate_heap_cost( - heap_size as u64, + heap_size, invoke_context.get_compute_budget().heap_cost, round_up_heap_size, )); @@ -281,7 +281,7 @@ macro_rules! create_vm { >::zero_filled(stack_size); let mut heap = solana_rbpf::aligned_memory::AlignedMemory::< { solana_rbpf::ebpf::HOST_ALIGN }, - >::zero_filled(heap_size); + >::zero_filled(usize::try_from(heap_size).unwrap()); let vm = $crate::create_vm( $program, $regions, diff --git a/programs/loader-v4/src/lib.rs b/programs/loader-v4/src/lib.rs index c22d95856f157e..4645b33c26a6e0 100644 --- a/programs/loader-v4/src/lib.rs +++ b/programs/loader-v4/src/lib.rs @@ -94,10 +94,10 @@ pub fn create_program_runtime_environment_v2<'a>( BuiltinProgram::new_loader(config, FunctionRegistry::default()) } -fn calculate_heap_cost(heap_size: u64, heap_cost: u64) -> u64 { +fn calculate_heap_cost(heap_size: u32, heap_cost: u64) -> u64 { const KIBIBYTE: u64 = 1024; const PAGE_SIZE_KB: u64 = 32; - heap_size + u64::from(heap_size) .saturating_add(PAGE_SIZE_KB.saturating_mul(KIBIBYTE).saturating_sub(1)) .checked_div(PAGE_SIZE_KB.saturating_mul(KIBIBYTE)) .expect("PAGE_SIZE_KB * KIBIBYTE > 0") @@ -114,12 +114,11 @@ pub fn create_vm<'a, 'b>( let sbpf_version = program.get_sbpf_version(); let compute_budget = invoke_context.get_compute_budget(); let heap_size = compute_budget.heap_size; - invoke_context.consume_checked(calculate_heap_cost( - heap_size as u64, - compute_budget.heap_cost, - ))?; + invoke_context.consume_checked(calculate_heap_cost(heap_size, compute_budget.heap_cost))?; let mut stack = AlignedMemory::<{ ebpf::HOST_ALIGN }>::zero_filled(config.stack_size()); - let mut heap = AlignedMemory::<{ ebpf::HOST_ALIGN }>::zero_filled(compute_budget.heap_size); + let mut heap = AlignedMemory::<{ ebpf::HOST_ALIGN }>::zero_filled( + usize::try_from(compute_budget.heap_size).unwrap(), + ); let stack_len = stack.len(); let regions: Vec = vec![ program.get_ro_region(),