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

embed to C failed #2273

Open
hhm152800 opened this issue Jun 7, 2023 · 5 comments
Open

embed to C failed #2273

hhm152800 opened this issue Jun 7, 2023 · 5 comments

Comments

@hhm152800
Copy link

os: vmware ubuntu
toolchain: wasi-sdk-20
wasm-app src:

int add (int a, int b)
{
	return a+b;
}

command:
$CC test.c -o test.wasm -O0 -Wl,--export=add -Wl,--strip-all,--no-entry -nostdlib --target=wasm32-wasi -O0 -z stack-size=4096 -Wl,--initial-memory=65536 -Wl,--allow-undefined -Wl,--allow-undefined-file=${WAMR_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt

cmakelist flag:

 project (wasmtest C ASM)
enable_language (ASM_MASM)
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
set (WAMR_BUILD_PLATFORM "linux")
set (WAMR_BUILD_TARGET "X86_64")
set (WAMR_BUILD_INTERP 1)
set (WAMR_BUILD_FAST_INTERP 1)
set (WAMR_BUILD_AOT 1)
set (WAMR_BUILD_JIT 0)
set (WAMR_BUILD_LIBC_BUILTIN 1)
set (WAMR_BUILD_LIBC_WASI 1)
set (WAMR_BUILD_SIMD 0)
set (WAMR_DISABLE_HW_BOUND_CHECK 1)
set (WAMR_DISABLE_STACK_HW_BOUND_CHECK 1)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
set(WAMR_ROOT_DIR /home/ubuntu/workspace/wasm-micro-runtime)
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
add_executable(wasmtest main.c ${UNCOMMON_SHARED_SOURCE})
target_link_libraries (wasmtest vmlib -lm -ldl -lpthread -lrt)

c src:

wasm_runtime_init();
  buffer = bh_read_file_to_buffer("test.wasm", &buf_size);
  module = wasm_runtime_load((unsigned char*)buffer, buf_size, error_buf, sizeof(error_buf));
  if (!module)
      return -1;
  module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,
                                             error_buf, sizeof(error_buf));
  if (!module_inst) {
      return -1;
  }
  exec_env = wasm_runtime_create_exec_env(module_inst, stack_size);
  if (!exec_env) {
      return -1;
  }
  if (!(func = wasm_runtime_lookup_function(module_inst, "add",
                                            NULL))) {
      return -1;
  }
  wasm_val_t results[1];
  wasm_val_t params[2];
  params[0].kind = WASM_I32;
  params[0].of.i32 = 4;
  params[1].kind = WASM_I32;
  params[1].of.i32 = 4;
  wasm_runtime_call_wasm_a(exec_env, func, 1, results, 2, params);

exception :

#if defined(os_writegsbase)
    {
        WASMMemoryInstance *memory_inst = wasm_get_default_memory(module_inst);
        if (memory_inst)
            /* write base addr of linear memory to GS segment register */
            os_writegsbase(memory_inst->memory_data);
    }
#endif

when try to do , always exception on os_writegsbase, why, how to deal with it

@wenyongh
Copy link
Contributor

wenyongh commented Jun 7, 2023

Hi, you can manually remove these lines:
https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/core/shared/platform/linux/platform_internal.h#L67-L71

This is to write the linear memory base address to the x86 segment register GS so that it may speed up the linear memory access for AOT/JIT. I found that your os is vmware ubuntu, writing that register may be disabled by your platform so you encountered the error. Maybe we can add control for that, e.g. add cmake -DWAMR_DISABLE_WRITE_GS_BASE=1 to disable it (like cmake -DWAMR_DISABLE_HW_BOUND_CHECK).

@hhm152800
Copy link
Author

hhm152800 commented Jun 8, 2023

thanks, it work when i remove code, i think will be more easy to resolve that add some note in os_writegsbase

@wenyongh
Copy link
Contributor

wenyongh commented Jun 8, 2023

Yes, do you know whether there is some compiler macro related to vmware ubuntu so we can use it to disable the os_writegsbase in the platform_internal.h?

@hhm152800
Copy link
Author

Yes, do you know whether there is some compiler macro related to vmware ubuntu so we can use it to disable the os_writegsbase in the platform_internal.h?

sorry , i don't know that, i try it on ubuntu only a week...

@hhm152800
Copy link
Author

Yes, do you know whether there is some compiler macro related to vmware ubuntu so we can use it to disable the os_writegsbase in the platform_internal.h?

may vmware cause , i try ubuntu18, ubuntu20, get the same result

wenyongh added a commit that referenced this issue Jun 13, 2023
Support to disable writing x86-64 GS segment register by
  `cmake -DWAMR_DISABLE_WRITE_GS_BASE=1`
and update document. Issue was reported in #2273.
victoryang00 pushed a commit to victoryang00/wamr-aot-gc-checkpoint-restore that referenced this issue May 27, 2024
)

Support to disable writing x86-64 GS segment register by
  `cmake -DWAMR_DISABLE_WRITE_GS_BASE=1`
and update document. Issue was reported in bytecodealliance#2273.
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

No branches or pull requests

2 participants