Skip to content

Commit

Permalink
Merge pull request holochain#1514 from holochain/test-wasm-memory
Browse files Browse the repository at this point in the history
Add test that shows memory failure in wasm
  • Loading branch information
thedavidmeister authored Aug 19, 2022
2 parents 2307a01 + 79367ae commit 8f0a5e2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
9 changes: 8 additions & 1 deletion crates/holochain/src/core/ribosome/real_ribosome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ use std::collections::HashMap;
use std::sync::atomic::AtomicU64;
use std::sync::Arc;

const WASM_METERING_LIMIT: u64 = 10_000_000_000;

/// The only RealRibosome is a Wasm ribosome.
/// note that this is cloned on every invocation so keep clones cheap!
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -380,6 +382,11 @@ impl RealRibosome {
zome_name: &ZomeName,
) -> RibosomeResult<()> {
use holochain_wasmer_host::module::PlruCache;
{
let instance = instance.lock();
wasmer_middlewares::metering::set_remaining_points(&instance, WASM_METERING_LIMIT);
}

// Clear the context as the call is done.
{
CONTEXT_MAP.lock().remove(&context_key);
Expand Down Expand Up @@ -481,7 +488,7 @@ impl RealRibosome {
let cost_function = |_operator: &WasmOperator| -> u64 { 1 };
// @todo 10 giga-ops is totally arbitrary cutoff so we probably
// want to make the limit configurable somehow.
let metering = Arc::new(Metering::new(10_000_000_000, cost_function));
let metering = Arc::new(Metering::new(WASM_METERING_LIMIT, cost_function));
let mut cranelift = Cranelift::default();
cranelift.canonicalize_nans(true).push_middleware(metering);
cranelift
Expand Down
32 changes: 32 additions & 0 deletions crates/holochain/tests/integrity_zome/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ use holochain_types::prelude::DnaWasm;
use holochain_types::prelude::UpdateCoordinatorsPayload;
use holochain_wasm_test_utils::TestCoordinatorWasm;
use holochain_wasm_test_utils::TestIntegrityWasm;
use holochain_wasm_test_utils::TestWasm;
use holochain_zome_types::CoordinatorZome;
use holochain_zome_types::CoordinatorZomeDef;
use holochain_zome_types::IntegrityZome;
use holochain_zome_types::Record;
use holochain_zome_types::Timestamp;
use holochain_zome_types::WasmZome;
use holochain_zome_types::Zome;
use holochain_zome_types::ZomeDef;
use mr_bundle::Bundle;
use serde::Serialize;

#[tokio::test(flavor = "multi_thread")]
async fn test_coordinator_zome_update() {
Expand Down Expand Up @@ -285,3 +288,32 @@ async fn test_update_admin_interface() {

assert!(record.is_some());
}

#[tokio::test(flavor = "multi_thread")]
async fn test_wasm_memory() {
let mut conductor = SweetConductor::from_config(Default::default()).await;
let (dna, _, _) = SweetDnaFile::unique_from_test_wasms(vec![TestWasm::Create])
.await
.unwrap();

let app = conductor.setup_app("app", &[dna]).await.unwrap();
let cells = app.into_cells();

#[derive(Debug, Serialize)]
struct Post(String);

let data = String::from_utf8(vec![0u8; 10_000_000]).unwrap();

let mut cum = 0;
for i in 0..100 {
cum += data.len();
eprintln!("committing {} {} {:?}", i, cum, Timestamp::now());
let _hash: ActionHash = conductor
.call(
&cells[0].zome(TestWasm::Create),
"create_post",
Post(data.clone()),
)
.await;
}
}

0 comments on commit 8f0a5e2

Please sign in to comment.