wasm-micro-runtime icon indicating copy to clipboard operation
wasm-micro-runtime copied to clipboard

Error when running wasm_runtime_create_exec_env in platform Linux-SGX

Open dzobbe opened this issue 3 years ago • 3 comments
trafficstars

Hello All,

I am facing an issue while trying to run the wasm_runtime_create_exec_env function in a SGX enclave. I have noticed from the enclave-sample app that the command for creating exec env is not implemented. Is it supported this function in the enclave? Here is the code.

 os_set_print_function(enclave_print);
    
    char *buffer;
    std::array<char, 128> error_buf;
    uint32_t size, stack_size = 8092, heap_size = 8092;

    auto wasm_initialized = wasm_runtime_init();

    if (!wasm_initialized) {
        xeprintf("Failed to init wasm! \n" );
        return;
    }


    auto module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
                                          error_buf.data(), error_buf.size());

   
    if (!module) {
        xeprintf("Failed to load bytecode! \n" );
        return;
    }

    auto module_inst = wasm_runtime_instantiate(module, stack_size, heap_size, error_buf.data(), error_buf.size());

    if (!module_inst) {
        xeprintf("Failed to instantiate module! \n" );
        return;
    }

    auto exec_env = wasm_runtime_create_exec_env(module_inst, stack_size);

    if (!exec_env) {
        xeprintf("Failed to create env! \n" );
        return;
    }

    auto func = wasm_runtime_lookup_function(module_inst, "hello", nullptr);

    if (!func) {
        xeprintf("Failed to lookup function! \n" );
        return;
    }

    std::string arg("C++");
    auto wasm_buf = wasm_runtime_module_dup_data(module_inst, arg.data(), arg.size());
    std::array<uint32_t, 2> func_args = {wasm_buf, arg.size()};

    auto call_succeeded = wasm_runtime_call_wasm(exec_env, func, func_args.size(), func_args.data());

    if (!call_succeeded) {
        xeprintf("Failed to call function! \n" );
        return;
    }

    auto func_rv = wasm_runtime_addr_app_to_native(module_inst, func_args[0]);

    xprintf("WASM function returned: %s\n\n",std::string(static_cast<char*>(func_rv)).c_str());

dzobbe avatar Mar 08 '22 22:03 dzobbe

Hi, the wasm_runtime_create_exec_env calls wasm_exec_env_create to create the exec_env, normally we should be able to call them in the enclave. Although in enclave-sample app the command for creating exec env hasn't been implemented, but the command CMD_EXEC_APP_FUNC/CMD_EXEC_APP_MAIN will call wasm_application_execute_main/wasm_application_execute_main, which eventually call wasm_exec_env_create.

Could you please add logs (e.g. os_printf(...)) in wasm_exec_env_create to check the details?

wenyongh avatar Mar 09 '22 12:03 wenyongh

I added some more logs and discovered that the error is on this function "wasm_cluster_create" internal to wasm_exec_env_create. I also added some prints in wasm_cluster_create and discovered that it fails on this call "wasm_exec_env_set_aux_stack".

Actually, I have noticed that "wasm_cluster_create" falls into a WASM_ENABLE_THREAD_MGR section, which I thought must be disabled in linux-sgx platform.

I tried to run wasm_application_execute_main/wasm_application_execute_func and noticed that also these function go through the WASM_ENABLE_THREAD_MGR block. But they work fine.

When disabling WASM_ENABLE_THREAD_MGR, it seems the application to work fine.

dzobbe avatar Mar 09 '22 15:03 dzobbe

Yes, by default the building of linux-sgx enables the lib-pthread support, which might need to set aux stack while creating exec env. Do you use wasi-sdk to compile the wasm app? Maybe you can try adding -Wl,--export=__heap_base,--export=__data_end when building wasm app and run it again wit iwasm of default built (without disable WASM_ENABLE_THREAD_MGR).

wenyongh avatar Mar 10 '22 06:03 wenyongh