Skip to content

Commit

Permalink
Add spawn test case
Browse files Browse the repository at this point in the history
  • Loading branch information
XuJiandong committed Apr 16, 2024
1 parent 0e653fb commit 38d73a9
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 3 deletions.
4 changes: 2 additions & 2 deletions script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ tokio = { version = "1.35.0", features = ["rt-multi-thread"] }

[dev-dependencies]
proptest = "1.0"
ckb-db = { path = "../db", version = "= 0.115.0-pre" }
ckb-store = { path = "../store", version = "= 0.115.0-pre" }
ckb-db = { path = "../db", version = "= 0.115.0-pre", features = ["portable"] }
ckb-store = { path = "../store", version = "= 0.115.0-pre", features = ["portable"] }
ckb-test-chain-utils = { path = "../util/test-chain-utils", version = "= 0.115.0-pre" }
tiny-keccak = { version = "2.0", features = ["sha3"] }
ckb-crypto = { path = "../util/crypto", version = "= 0.115.0-pre" }
Expand Down
11 changes: 11 additions & 0 deletions script/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ where
// MessageBox is expected to be empty before returning from `run`
// function, there is no need to persist messages.
message_box: Arc<Mutex<Vec<Message>>>,
resume_suspend_count: usize,
}

impl<DL> Scheduler<DL>
Expand Down Expand Up @@ -95,6 +96,7 @@ where
suspended: BTreeMap::default(),
message_box,
terminated_vms: BTreeMap::default(),
resume_suspend_count: 0,
}
}

Expand Down Expand Up @@ -133,6 +135,7 @@ where
.collect(),
message_box,
terminated_vms: full.terminated_vms.into_iter().collect(),
resume_suspend_count: 0,
}
}

Expand Down Expand Up @@ -764,6 +767,10 @@ where
{
let mut sc = context.snapshot2_context().lock().expect("lock");
sc.resume(&mut machine.machine, snapshot)?;
self.resume_suspend_count += 1;
if self.resume_suspend_count % 1000 == 0 {
println!("resume_suspend_count = {}", self.resume_suspend_count);
}
}
self.instantiated.insert(*id, (context, machine));
self.suspended.remove(id);
Expand All @@ -786,6 +793,10 @@ where
let sc = context.snapshot2_context().lock().expect("lock");
sc.make_snapshot(&mut machine.machine)?
};
self.resume_suspend_count += 1;
if self.resume_suspend_count % 1000 == 0 {
println!("resume_suspend_count = {}", self.resume_suspend_count);
}
self.suspended.insert(*id, snapshot);
self.instantiated.remove(id);
Ok(())
Expand Down
36 changes: 36 additions & 0 deletions script/src/verify/tests/ckb_latest/features_since_v2023.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1185,3 +1185,39 @@ fn check_spawn_length_out_of_bound() {
let result = simple_spawn_test("testdata/spawn_cases", &[16]);
assert_eq!(result.is_ok(), SCRIPT_VERSION == ScriptVersion::V2);
}

#[test]
fn check_spawn_huge_swap() {
let script_version = SCRIPT_VERSION;

let (spawn_caller_cell, spawn_caller_data_hash) =
load_cell_from_path("testdata/spawn_huge_swap");

let spawn_caller_script = Script::new_builder()
.hash_type(script_version.data_hash_type().into())
.code_hash(spawn_caller_data_hash)
.build();
let output = CellOutputBuilder::default()
.capacity(capacity_bytes!(100).pack())
.lock(spawn_caller_script)
.build();
let input = CellInput::new(OutPoint::null(), 0);

let transaction = TransactionBuilder::default().input(input).build();
let dummy_cell = create_dummy_cell(output);

let rtx = ResolvedTransaction {
transaction,
resolved_cell_deps: vec![spawn_caller_cell],
resolved_inputs: vec![dummy_cell],
resolved_dep_groups: vec![],
};
let verifier = TransactionScriptsVerifierWithEnv::new();
let result = verifier.verify(script_version, &rtx, 70_000_000);
if script_version >= ScriptVersion::V2 {
let msg = result.unwrap_err().to_string();
assert!(msg.contains("ExceededMaximumCycles"))
} else {
assert!(result.is_err())
}
}
4 changes: 3 additions & 1 deletion script/testdata/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ ALL_BINS := jalr_zero \
spawn_configurable_caller \
spawn_configurable_callee \
spawn_dag \
spawn_fuzzing
spawn_fuzzing \
spawn_huge_swap

ALL_LIBS := is_even.lib \
add1.lib sub1.lib mul2.lib div2.lib
Expand Down Expand Up @@ -156,3 +157,4 @@ spawn_configurable_caller: spawn_configurable_caller.c spawn_utils.h
spawn_configurable_callee: spawn_configurable_callee.c spawn_utils.h
spawn_dag: spawn_dag.c spawn_dag.h spawn_dag_escape_encoding.h
spawn_fuzzing: spawn_fuzzing.c spawn_utils.h
spawn_huge_swap: spawn_huge_swap.c spawn_utils.h
Binary file modified script/testdata/add1.lib
Binary file not shown.
Binary file modified script/testdata/div2.lib
Binary file not shown.
Binary file modified script/testdata/mul2.lib
Binary file not shown.
Binary file modified script/testdata/spawn_cases
Binary file not shown.
Binary file added script/testdata/spawn_huge_swap
Binary file not shown.
65 changes: 65 additions & 0 deletions script/testdata/spawn_huge_swap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "spawn_utils.h"

// 2.4 M bytes
static uint64_t g_data[300 * 1024];

int main() {
int err = 0;
uint64_t fds[2] = {0};
uint64_t pid = 0;
uint64_t current_pid = ckb_process_id();
size_t argc = 1;
const char* argv[2] = {"", 0};
int8_t exit_code = 0;

printf("current pid = %d", current_pid);
for (size_t i = 0; i < sizeof(g_data) / sizeof(uint64_t); i++) {
g_data[i] = current_pid;
}

if (current_pid == 7) {
// wait forever
ckb_wait(0, &exit_code);
} else {
err = full_spawn(0, argc, argv, fds, &pid);
CHECK(err);
if (current_pid == 0) {
uint8_t buf[1] = {0};
while (true) {
size_t len = 1;
ckb_read(fds[CKB_STDIN], buf, &len);
}
} else if (current_pid == 1) {
uint64_t inherited_fds[3];
size_t fds_len = 3;
err = ckb_inherited_file_descriptors(inherited_fds, &fds_len);
CHECK(err);
uint8_t buf[1] = {0};
while (true) {
size_t len = 1;
ckb_write(inherited_fds[CKB_STDOUT], buf, &len);
ckb_read(fds[CKB_STDIN], buf, &len);
}
} else if (current_pid == 2) {
uint64_t inherited_fds[3];
size_t fds_len = 3;
err = ckb_inherited_file_descriptors(inherited_fds, &fds_len);
CHECK(err);
uint8_t buf[1] = {0};
while (true) {
size_t len = 1;
ckb_write(inherited_fds[CKB_STDOUT], buf, &len);
}
} else {
// wait forever
ckb_wait(0, &exit_code);
}
}
// avoid g_data to be optimized
for (size_t i = 0; i < sizeof(g_data) / sizeof(uint64_t); i++) {
err += (int8_t)g_data[i];
}

exit:
return err;
}
Binary file modified script/testdata/sub1.lib
Binary file not shown.

0 comments on commit 38d73a9

Please sign in to comment.