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

Question: Segmentation fault (Zero Byte Expected) #97

Open
Sieluna opened this issue Jan 18, 2025 · 4 comments · May be fixed by #103
Open

Question: Segmentation fault (Zero Byte Expected) #97

Sieluna opened this issue Jan 18, 2025 · 4 comments · May be fixed by #103

Comments

@Sieluna
Copy link

Sieluna commented Jan 18, 2025

I got a error in console: Segmentation fault (core dumped)

A fast reproduce: https://github.com/LoongBuns/wamr-rust-sdk/tree/ohno/examples/wasm-wild

Run with this wasm https://github.com/wasm3/wasm-coremark/blob/main/coremark-minimal.wasm

I'm not quite sure what I need to enable to get iwasm to understand my needs, as this doesn't fail with other runtimes coremark .

use core::error::Error;
use core::ffi::c_void;
use core::result::Result;

use wamr_rust_sdk::{
    function::Function, instance::Instance, module::Module, runtime::Runtime, value::WasmValue,
};

extern "C" fn clock_ms_host() -> i64 {
    use std::time::{SystemTime, UNIX_EPOCH};
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("Clock may have gone backwards")
        .as_millis() as i64
}

impl Runtime {
    pub fn builder_with_module_name(name: &str) -> RuntimeBuilder {
        let args = RuntimeInitArgs::default();
        RuntimeBuilder {
            args,
            host_functions: HostFunctionList::new(name),
        }
    }
}

pub fn wamr_coremark() -> Result<f32, Box<dyn Error>> {
    let coremark_wasm = include_bytes!("https://github.com/wasm3/wasm-coremark/blob/main/coremark-minimal.wasm");
    let runtime = Runtime::builder_with_module_name("env")
        .use_system_allocator()
        .run_as_interpreter()
        .register_host_function("clock_ms", clock_ms_host as *mut c_void)
        .build()?;

    let module = Module::from_vec(&runtime, Vec::from(&coremark_wasm), "")?;

    let instance = Instance::new(&runtime, &module, 2 * 1024)?;

    let function = Function::find_export_func(&instance, "run")?;

    if let WasmValue::F32(res) = function.call(&instance, &vec![])? {
        Ok(res)
    } else {
        panic!("Failed running coremark in wasmr");
    }
}
@Sieluna Sieluna changed the title Question: Segmentation fault (core dumped) Question: Segmentation fault (Zero Byte Expected) Jan 21, 2025
@Sieluna
Copy link
Author

Sieluna commented Jan 22, 2025

@lum1n0us I tried to simplify the code but error still there.

(module
  (type (;0;) (func (result i64)))
  (type (;1;) (func (result f32)))
  (import "env" "clock_ms" (func (;0;) (type 0)))
  (table (;0;) 1 1 funcref)
  (memory (;0;) 1)
  (global (;0;) (mut i32) i32.const 8192)
  (global (;1;) i32 i32.const 8192)
  (global (;2;) i32 i32.const 8192)
  (export "memory" (memory 0))
  (export "run" (func 1))
  (export "__data_end" (global 1))
  (export "__heap_base" (global 2))
  (func (;1;) (type 1) (result f32)
    call 0
    f32.convert_i64_s
  )
)

If load the function in c, it works:

static NativeSymbol native_symbols[] = { { "clock_ms", clock_ms, "()I", NULL } };
init_args = (RuntimeInitArgs){ .mem_alloc_type = Alloc_With_Pool, 
    .mem_alloc_option.pool = { global_heap_buf, sizeof(global_heap_buf) }, 
    .native_module_name = "env", 
    .native_symbols = native_symbols, 
    .n_native_symbols = 1 };

if (!wasm_runtime_full_init(&init_args) || 
    !(buffer = bh_read_file_to_buffer(wasm_path, &buf_size)) ||
    !(module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf, sizeof(error_buf))) ||
    !(module_inst = wasm_runtime_instantiate(module, stack_size, heap_size, error_buf, sizeof(error_buf))) ||
    !(exec_env = wasm_runtime_create_exec_env(module_inst, stack_size)) ||
    !(func = wasm_runtime_lookup_function(module_inst, "run"))) {
    printf("Error: %s\n", error_buf);
    return -1;
}

wasm_val_t results[1] = { { .kind = WASM_F32 } };
if (!wasm_runtime_call_wasm_a(exec_env, func, 1, results, 0, NULL)) {
    printf("Call failed: %s\n", wasm_runtime_get_exception(module_inst));
    return -1;
}
printf("Returned: %f\n", results[0].of.f32);

Image

However, when running the equivalent Rust implementation, the following error was encountered:

Image

Could you please provide some suggestions to solve the above problem?

The code stop here: Stop reason: signal SIGSEGV: address not mapped to object (fault address: 0x4)

https://github.com/bytecodealliance/wasm-micro-runtime/blob/b6dea221a609606ce6c3dcaa0f4d0399ea5bc4dc/core/iwasm/interpreter/wasm_interp_fast.c#L6280-L6284

Sieluna added a commit to LoongBuns/wamr-rust-sdk that referenced this issue Jan 22, 2025
Sieluna added a commit to LoongBuns/wamr-rust-sdk that referenced this issue Jan 22, 2025
Sieluna added a commit to LoongBuns/wamr-rust-sdk that referenced this issue Jan 22, 2025
@lum1n0us
Copy link
Collaborator

Thank you for the update. I will respond as soon as possible.

@lum1n0us lum1n0us linked a pull request Jan 23, 2025 that will close this issue
@lum1n0us
Copy link
Collaborator

#103 should be capable of resolving this issue. Please give it a try.

@Sieluna
Copy link
Author

Sieluna commented Jan 23, 2025

wasm_func_get_result_count might not be a good method. We need calculate the actual capacity instead. For example (func (;2;) (type 0) (result i64), returns a result count of 1. However, argv size need to take account for two cases: i32/f32 should return 1, i64/f64 should return 2.

Submit: lum1n0us#18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants