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

Abi bail instead of expect in as_abort | targetting main #344

Merged
merged 6 commits into from
Nov 21, 2024
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
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,11 @@ jobs:
unit_tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: "recursive"
- name: setup_tests
shell: bash
run: |
cargo install cargo-script && cargo script tools/setup_test.rs && git diff --no-ext-diff --quiet
cargo install rust-script && rust-script tools/setup_test.rs && git diff --no-ext-diff --quiet

14 changes: 5 additions & 9 deletions src/as_execution/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1544,18 +1544,14 @@ pub fn assembly_script_abort(
line: i32,
col: i32,
) -> ABIResult<()> {
let memory = ctx
.data()
.get_ffi_env()
.memory
.as_ref()
.expect("Failed to get memory on env")
.clone();
let env = ctx.data();
let memory = get_memory!(env);

let message_ = message
.read(&memory, &ctx)
.read(memory, &ctx)
.map_err(|e| wasmer::RuntimeError::new(e.to_string()));
let filename_ = filename
.read(&memory, &ctx)
.read(memory, &ctx)
.map_err(|e| wasmer::RuntimeError::new(e.to_string()));

if message_.is_err() || filename_.is_err() {
Expand Down
37 changes: 37 additions & 0 deletions src/tests/tests_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,43 @@ fn test_class_id() {
assert_eq!(array_class_id, 4);
}

#[test]
#[serial]
fn test_abort_does_not_panic() {
// AS source code for reference
// ```ts
//import { u256 } from 'as-bignum/assembly';
//const test = u256.from<string>('1982982');
//export function main(_args: StaticArray<u8>): void {
//}
// ```
// The above code fails in the start method, causing wasmer to abort the
// initialization of the module, hence returning memory as None that caused
// a panic

let interface: Box<dyn Interface> = Box::new(TestInterface);
let bytecode = include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/wasm/abort_crash.wasm"
));

let module = ASModule::new(bytecode, 100_000, GasCosts::default(), Compiler::SP).unwrap();
let mut store = Store::new(module._engine);
let mut context = ASContext::new(&*interface, module.binary_module, GasCosts::default());
let create_vm_instance_and_init_env = context.create_vm_instance_and_init_env(&mut store);
match create_vm_instance_and_init_env {
Ok(_) => {
panic!("Expected an error")
}
Err(e) => {
assert_eq!(
e.to_string(),
"RuntimeError: Runtime error: AssemblyScript memory is missing from the environment"
)
}
}
}

#[cfg(feature = "execution-trace")]
#[test]
#[serial]
Expand Down
Binary file added wasm/abort_crash.wasm
Binary file not shown.
Loading