Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
Cleanup feature: sycall base costs (#26304)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmay authored Jun 29, 2022
1 parent e8ed7c1 commit 265f36c
Showing 1 changed file with 24 additions and 110 deletions.
134 changes: 24 additions & 110 deletions programs/bpf_loader/src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use {
curve25519_syscall_enabled, disable_fees_sysvar, executables_incur_cpi_data_cost,
libsecp256k1_0_5_upgrade_enabled, limit_secp256k1_recovery_id,
prevent_calling_precompiles_as_programs, quick_bail_on_panic, syscall_saturated_math,
update_syscall_base_costs, zk_token_sdk_enabled,
zk_token_sdk_enabled,
},
hash::{Hasher, HASH_BYTES},
instruction::{
Expand Down Expand Up @@ -577,12 +577,9 @@ declare_syscall!(
.map_err(|_| SyscallError::InvokeContextBorrowFailed),
result
);
if !invoke_context
if invoke_context
.feature_set
.is_active(&update_syscall_base_costs::id())
|| invoke_context
.feature_set
.is_active(&quick_bail_on_panic::id())
.is_active(&quick_bail_on_panic::id())
{
question_mark!(invoke_context.get_compute_meter().consume(len), result);
}
Expand Down Expand Up @@ -617,17 +614,10 @@ declare_syscall!(
.map_err(|_| SyscallError::InvokeContextBorrowFailed),
result
);
let cost = if invoke_context
.feature_set
.is_active(&update_syscall_base_costs::id())
{
invoke_context
.get_compute_budget()
.syscall_base_cost
.max(len)
} else {
len
};
let cost = invoke_context
.get_compute_budget()
.syscall_base_cost
.max(len);
question_mark!(invoke_context.get_compute_meter().consume(cost), result);

question_mark!(
Expand Down Expand Up @@ -700,14 +690,7 @@ declare_syscall!(
.map_err(|_| SyscallError::InvokeContextBorrowFailed),
result
);
let cost = if invoke_context
.feature_set
.is_active(&update_syscall_base_costs::id())
{
invoke_context.get_compute_budget().syscall_base_cost
} else {
0
};
let cost = invoke_context.get_compute_budget().syscall_base_cost;
question_mark!(invoke_context.get_compute_meter().consume(cost), result);

ic_logger_msg!(
Expand Down Expand Up @@ -1006,11 +989,7 @@ declare_syscall!(
result
);
let compute_budget = invoke_context.get_compute_budget();
if invoke_context
.feature_set
.is_active(&update_syscall_base_costs::id())
&& compute_budget.sha256_max_slices < vals_len
{
if compute_budget.sha256_max_slices < vals_len {
ic_msg!(
invoke_context,
"Sha256 hashing {} sequences in one syscall is over the limit {}",
Expand Down Expand Up @@ -1060,20 +1039,11 @@ declare_syscall!(
),
result
);
let cost = if invoke_context
.feature_set
.is_active(&update_syscall_base_costs::id())
{
compute_budget.mem_op_base_cost.max(
compute_budget
.sha256_byte_cost
.saturating_mul((val.len() as u64).saturating_div(2)),
)
} else {
let cost = compute_budget.mem_op_base_cost.max(
compute_budget
.sha256_byte_cost
.saturating_mul((val.len() as u64).saturating_div(2))
};
.saturating_mul((val.len() as u64).saturating_div(2)),
);
question_mark!(invoke_context.get_compute_meter().consume(cost), result);
hasher.hash(bytes);
}
Expand Down Expand Up @@ -1243,11 +1213,7 @@ declare_syscall!(
result
);
let compute_budget = invoke_context.get_compute_budget();
if invoke_context
.feature_set
.is_active(&update_syscall_base_costs::id())
&& compute_budget.sha256_max_slices < vals_len
{
if compute_budget.sha256_max_slices < vals_len {
ic_msg!(
invoke_context,
"Keccak256 hashing {} sequences in one syscall is over the limit {}",
Expand Down Expand Up @@ -1297,20 +1263,11 @@ declare_syscall!(
),
result
);
let cost = if invoke_context
.feature_set
.is_active(&update_syscall_base_costs::id())
{
compute_budget.mem_op_base_cost.max(
compute_budget
.sha256_byte_cost
.saturating_mul((val.len() as u64).saturating_div(2)),
)
} else {
let cost = compute_budget.mem_op_base_cost.max(
compute_budget
.sha256_byte_cost
.saturating_mul((val.len() as u64).saturating_div(2))
};
.saturating_mul((val.len() as u64).saturating_div(2)),
);
question_mark!(invoke_context.get_compute_meter().consume(cost), result);
hasher.hash(bytes);
}
Expand All @@ -1325,16 +1282,9 @@ fn mem_op_consume<'a, 'b>(
n: u64,
) -> Result<(), EbpfError<BpfError>> {
let compute_budget = invoke_context.get_compute_budget();
let cost = if invoke_context
.feature_set
.is_active(&update_syscall_base_costs::id())
{
compute_budget
.mem_op_base_cost
.max(n.saturating_div(compute_budget.cpi_bytes_per_unit))
} else {
n.saturating_div(compute_budget.cpi_bytes_per_unit)
};
let cost = compute_budget
.mem_op_base_cost
.max(n.saturating_div(compute_budget.cpi_bytes_per_unit));
invoke_context.get_compute_meter().consume(cost)
}

Expand All @@ -1357,17 +1307,7 @@ declare_syscall!(
.map_err(|_| SyscallError::InvokeContextBorrowFailed),
result
);
// When deprecating `update_syscall_base_costs` switch to `mem_op_consume`
let compute_budget = invoke_context.get_compute_budget();
let update_syscall_base_costs = invoke_context
.feature_set
.is_active(&update_syscall_base_costs::id());
if update_syscall_base_costs {
let cost = compute_budget
.mem_op_base_cost
.max(n.saturating_div(compute_budget.cpi_bytes_per_unit));
question_mark!(invoke_context.get_compute_meter().consume(cost), result);
}
question_mark!(mem_op_consume(&invoke_context, n), result);

let do_check_physical_overlapping = invoke_context
.feature_set
Expand All @@ -1378,11 +1318,6 @@ declare_syscall!(
return;
}

if !update_syscall_base_costs {
let cost = n.saturating_div(compute_budget.cpi_bytes_per_unit);
question_mark!(invoke_context.get_compute_meter().consume(cost), result);
};

let dst_ptr = question_mark!(
translate_slice_mut::<u8>(
memory_mapping,
Expand Down Expand Up @@ -2224,11 +2159,7 @@ declare_syscall!(
result
);
let compute_budget = invoke_context.get_compute_budget();
if invoke_context
.feature_set
.is_active(&update_syscall_base_costs::id())
&& compute_budget.sha256_max_slices < vals_len
{
if compute_budget.sha256_max_slices < vals_len {
ic_msg!(
invoke_context,
"Blake3 hashing {} sequences in one syscall is over the limit {}",
Expand Down Expand Up @@ -2278,28 +2209,11 @@ declare_syscall!(
),
result
);
let cost = if invoke_context
.feature_set
.is_active(&update_syscall_base_costs::id())
{
compute_budget.mem_op_base_cost.max(
compute_budget
.sha256_byte_cost
.saturating_mul((val.len() as u64).saturating_div(2)),
)
} else if invoke_context
.feature_set
.is_active(&syscall_saturated_math::id())
{
let cost = compute_budget.mem_op_base_cost.max(
compute_budget
.sha256_byte_cost
.saturating_mul((val.len() as u64).saturating_div(2))
} else {
#[allow(clippy::integer_arithmetic)]
{
compute_budget.sha256_byte_cost * (val.len() as u64 / 2)
}
};
.saturating_mul((val.len() as u64).saturating_div(2)),
);
question_mark!(invoke_context.get_compute_meter().consume(cost), result);
hasher.hash(bytes);
}
Expand Down

0 comments on commit 265f36c

Please sign in to comment.