Skip to content

Commit

Permalink
feat: deplete compute meter for vm errors (#3751)
Browse files Browse the repository at this point in the history
* feat: deplete compute meter for vm errors

* add tests

* rekey apply_cost_tracker_during_replay

(cherry picked from commit b120fc1)
  • Loading branch information
jstarry authored and bw-solana committed Dec 11, 2024
1 parent d8f33ca commit 98ce35e
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 21 deletions.
18 changes: 18 additions & 0 deletions programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,24 @@ pub fn execute<'a, 'b: 'a>(
Err(Box::new(error) as Box<dyn std::error::Error>)
}
ProgramResult::Err(mut error) => {
if invoke_context
.get_feature_set()
.is_active(&solana_feature_set::apply_cost_tracker_during_replay::id())
&& !matches!(error, EbpfError::SyscallError(_))
{
// when an exception is thrown during the execution of a
// Basic Block (e.g., a null memory dereference or other
// faults), determining the exact number of CUs consumed
// up to the point of failure requires additional effort
// and is unnecessary since these cases are rare.
//
// In order to simplify CU tracking, simply consume all
// remaining compute units so that the block cost
// tracker uses the full requested compute unit cost for
// this failed transaction.
invoke_context.consume(invoke_context.get_remaining());
}

if direct_mapping {
if let EbpfError::AccessViolation(
AccessType::Store,
Expand Down
7 changes: 7 additions & 0 deletions programs/sbf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions programs/sbf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ members = [
"rust/custom_heap",
"rust/dep_crate",
"rust/deprecated_loader",
"rust/divide_by_zero",
"rust/dup_accounts",
"rust/error_handling",
"rust/external_spend",
Expand Down
15 changes: 15 additions & 0 deletions programs/sbf/rust/divide_by_zero/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "solana-sbf-rust-divide-by-zero"
version = { workspace = true }
description = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[dependencies]
solana-program = { workspace = true }

[lib]
crate-type = ["cdylib"]
27 changes: 27 additions & 0 deletions programs/sbf/rust/divide_by_zero/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! Example/test program to trigger vm error by dividing by zero
#![feature(asm_experimental_arch)]

extern crate solana_program;
use {
solana_program::{account_info::AccountInfo, entrypoint::ProgramResult, pubkey::Pubkey},
std::arch::asm,
};

solana_program::entrypoint_no_alloc!(process_instruction);
fn process_instruction(
_program_id: &Pubkey,
_accounts: &[AccountInfo],
_instruction_data: &[u8],
) -> ProgramResult {
unsafe {
asm!(
"
mov64 r0, 0
mov64 r1, 0
div64 r0, r1
"
);
}
Ok(())
}
Loading

0 comments on commit 98ce35e

Please sign in to comment.