Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New framework for detecting OOM issues #586

Merged
merged 14 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions clar2wasm/src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use crate::initialize::initialize_contract;
#[derive(Clone)]
pub struct TestEnvironment {
contract_contexts: HashMap<String, ContractContext>,
epoch: StacksEpochId,
version: ClarityVersion,
pub epoch: StacksEpochId,
pub version: ClarityVersion,
datastore: Datastore,
burn_datastore: BurnDatastore,
cost_tracker: LimitedCostTracker,
Expand Down Expand Up @@ -373,7 +373,7 @@ pub fn interpret(snippet: &str) -> Result<Option<Value>, Error> {
interpret_at(snippet, StacksEpochId::latest(), ClarityVersion::latest())
}

struct TestConfig;
pub struct TestConfig;

impl TestConfig {
/// Select a Clarity version based on enabled features.
Expand Down
132 changes: 132 additions & 0 deletions clar2wasm/tests/oom-checker/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#![cfg(test)]
pub mod unit_tests;

use clar2wasm::compile;
use clar2wasm::datastore::Datastore;
use clar2wasm::tools::{crosscheck, crosscheck_with_env, TestConfig, TestEnvironment};
use clar2wasm::wasm_utils::get_type_in_memory_size;
use clarity::types::StacksEpochId;
use clarity::vm::costs::LimitedCostTracker;
use clarity::vm::errors::{CheckErrors, Error};
use clarity::vm::types::{
ListTypeData, QualifiedContractIdentifier, StandardPrincipalData, TypeSignature,
};
use clarity::vm::{ClarityVersion, Value};

/// Name of the buffer that will fill the empty space.
const IGNORE_BUFFER_NAME: &str = "ignore";
/// Size in memory for the buffer that will fill the empty space's (offset, len).
const IGNORE_BUFFER_SIZE: usize = 8;
/// Minimum size needed in memory to create a filling buffer
const IGNORE_BUFFER_MIN_SIZE_NEEDED: usize = IGNORE_BUFFER_SIZE + IGNORE_BUFFER_NAME.len();

/// Size of a page in Wasm
const WASM_PAGE_SIZE: usize = 65536;

#[allow(clippy::expect_used)]
fn as_oom_check_snippet(
snippet: &str,
args_types: &[TypeSignature],
epoch: StacksEpochId,
version: ClarityVersion,
) -> String {
let compiled_module = Datastore::new()
.as_analysis_db()
.execute(|analysis_db| {
compile(
snippet,
&QualifiedContractIdentifier::new(
StandardPrincipalData::transient(),
("foo").into(),
),
LimitedCostTracker::new_free(),
version,
epoch,
analysis_db,
)
.map_err(|e| CheckErrors::Expects(format!("Compilation failure {e:?}")))
})
.expect("Could not compile snippet")
.module;

// we look for the total number of pages that were allocated for the module.
let memory_pages = compiled_module
.memories
.iter()
.next()
.expect("Couldn't find a memory")
.initial as usize;
// we look for the first byte in memory which doesn't contain useful data.
let stack_pointer_value = match compiled_module
.globals
.iter()
.find(|g| g.name.as_ref().is_some_and(|name| name == "stack-pointer"))
.expect("Couldn't find stack-pointer global")
.kind
{
walrus::GlobalKind::Local(walrus::InitExpr::Value(walrus::ir::Value::I32(val))) => {
val as usize
}
_ => unreachable!("stack-pointer should be a locally declared global with a i32 value"),
};

// WORKAROUND: this is to ignore arguments that are computed at runtime and should be removed after fixing
// [issue #587](https://github.com/stacks-network/clarity-wasm/issues/587)
let args_space_needed = args_types
.iter()
.map(|ty| get_type_in_memory_size(ty, false))
.sum::<i32>() as usize;

// the free space on the last page that we want to fill is the substraction of the total number of bytes
// for all the available pages and the last byte which will contain useful data.
let mut free_space_on_memory_page = memory_pages * WASM_PAGE_SIZE - stack_pointer_value;

let total_space_needed = IGNORE_BUFFER_MIN_SIZE_NEEDED + args_space_needed;
if free_space_on_memory_page < total_space_needed {
free_space_on_memory_page += WASM_PAGE_SIZE;
}

Acaccia marked this conversation as resolved.
Show resolved Hide resolved
format!(
"(define-constant {IGNORE_BUFFER_NAME} 0x{})\n{snippet}",
"00".repeat(free_space_on_memory_page - total_space_needed)
)
}

// TODO: deprecate after fixing [issue #587](https://github.com/stacks-network/clarity-wasm/issues/587)
pub fn crosscheck_oom_with_non_literal_args(
snippet: &str,
args_types: &[TypeSignature],
expected: Result<Option<Value>, Error>,
) {
crosscheck(
&as_oom_check_snippet(
snippet,
args_types,
TestConfig::latest_epoch(),
TestConfig::clarity_version(),
),
expected,
);
}

pub fn crosscheck_oom(snippet: &str, expected: Result<Option<Value>, Error>) {
crosscheck_oom_with_non_literal_args(snippet, &[], expected)
}

pub fn crosscheck_oom_with_env(
snippet: &str,
expected: Result<Option<Value>, Error>,
env: TestEnvironment,
) {
crosscheck_with_env(
&as_oom_check_snippet(snippet, &[], env.epoch, env.version),
expected,
env,
);
}

pub(crate) fn list_of(elem: TypeSignature, max_len: u32) -> TypeSignature {
TypeSignature::SequenceType(clarity::vm::types::SequenceSubtype::ListType(
ListTypeData::new_list(elem, max_len).unwrap(),
))
}
Loading
Loading