Skip to content

zephyr: unable to compile with WAMR_BUILD_LIBC_WASI flag. #3311

@lucasAbadFr

Description

@lucasAbadFr

Hello, I'm currently experimenting with sockets in WAMR on Zephyr, and I'm encountering some challenges.

  1. I've adapted a section of the Zephyr socket API.
  2. I'm attempting to run an HTTP client on WAMR (largely inspired by this sample).
  3. I'm working with the stm32 nucleo-h563zi board, but I'm facing compilation issues.

Compilation issues

  1. Plain compilation

    I compilated the http_client to a wasm module and like with the simple sample, I included it in a header file. But when I ran it on the board I had:

    *** Booting Zephyr OS build v3.6.0-961-gddb147d0a45d ***
    [00:00:00:000 - 20001190]: warning: failed to link import function (env, getaddrinfo)
    [00:00:00:000 - 20001190]: warning: failed to link import function (env, socket)
    [00:00:00:000 - 20001190]: warning: failed to link import function (env, connect)
    [00:00:00:000 - 20001190]: warning: failed to link import function (env, send)
    [00:00:00:000 - 20001190]: warning: failed to link import function (env, recv)
    [00:00:00:000 - 20001190]: warning: failed to link import function (env, close)
    [00:00:00:000 - 20001190]: warning: failed to link import function (env, __errno_location)
    Preparing HTTP GET request for http://192.0.2.10:8000/
    Exception: failed to call unlinked import function (env, getaddrinfo)
    elapsed: 72

    📄 Notes: I later adapted the sample to not use getaddrinfo because it add a useless dependency to netdb.h that I did not want.

  2. WAMR_BUILD_LIBC_WASI flag

    After looking at the socket-api sample, I found that I need to use the WAMR_BUILD_LIBC_WASI flag, so I added this flag to the CMakeLists.txt file but it didn't work.

    The output of the compilation is the same described in this issue
    My errors concist mainly of:

    1. libc_errno.h not found.

      /home/user/wasm-micro-runtime/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/random.c:16:10: fatal error: libc_errno.h: No such file or directory
      16 | #include "libc_errno.h"
            |          ^~~~~~~~~~~~~~
      compilation terminated.
    2. nfds_t unknown type.

      /home/user/wasm-micro-runtime/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.h:57:65: error: unknown type name 'nfds_t'
      57 | blocking_op_poll(wasm_exec_env_t exec_env, struct pollfd *pfds, nfds_t nfds,
    3. pthread_condattr_t unknown type & CLOCK_MONOTONIC undeclared.

      # Skip previous logs
      /home/user/wasm-micro-runtime/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/locking.h:137:5: error: unknown type name 'pthread_condattr_t'
      137 |     pthread_condattr_t attr;
            |     ^~~~~~~~~~~~~~~~~~
      /home/user/wasm-micro-runtime/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/locking.h:139:9: warning: implicit declaration of function 'pthread_condattr_init' [-Wimplicit-function-declaration]
      139 |     if (pthread_condattr_init(&attr) != 0)
            |         ^~~~~~~~~~~~~~~~~~~~~
      /home/user/wasm-micro-runtime/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/locking.h:142:9: warning: implicit declaration of function 'pthread_condattr_setclock' [-Wimplicit-function-declaration]
      142 |     if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0)
            |         ^~~~~~~~~~~~~~~~~~~~~~~~~
      /home/user/wasm-micro-runtime/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/locking.h:142:42: error: 'CLOCK_MONOTONIC' undeclared (first use in this function)
      142 |     if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0)
            |                                          ^~~~~~~~~~~~~~~
      /home/user/wasm-micro-runtime/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/locking.h:142:42: note: each undeclared identifier is reported only once for each function it appears in
      /home/user/wasm-micro-runtime/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/locking.h:145:9: warning: implicit declaration of function 'pthread_cond_init'; did you mean 'os_thread_env_init'? [-Wimplicit-function-declaration]
      145 |     if (pthread_cond_init(&cond->object, &attr) != 0)
            |         ^~~~~~~~~~~~~~~~~
            |         os_thread_env_init
      /home/user/wasm-micro-runtime/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/locking.h:150:5: warning: implicit declaration of function 'pthread_condattr_destroy' [-Wimplicit-function-declaration]
      150 |     pthread_condattr_destroy(&attr);
            |     ^~~~~~~~~~~~~~~~~~~~~~~~
      # Skip following logs
  3. Try to resolve

    I browsed the issues and found this Pull Request, but applying the fix lead to more errors and just one error was fixed.

    In core/shared/platform/zephyr/shared_platform.cmake I added the following lines:

    + if (WAMR_BUILD_LIBC_WASI EQUAL 1)
    +   list(APPEND source_all ${PLATFORM_SHARED_DIR}/../common/posix/posix_file.c)
    +   include (${CMAKE_CURRENT_LIST_DIR}/../common/libc-util/platform_common_libc_util.cmake)
    +   set(source_all ${source_all} ${PLATFORM_COMMON_LIBC_UTIL_SOURCE})
    + endif ()

    The error libc_errno.h was fixed but the other errors still persist. I also get all the errors from the /core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c file, mostly linked to the File System API ( e.g errors on struct pollfd).

  4. Test on host

    I reused the socket-api sample to compile and run the http_client module and it worked on my host machine.

    ./iwasm --addr-pool=192.0.2.10 http_get.wasm
    Preparing HTTP GET request for http://127.0.0.1:8000/
    sock = 5
    Response:
    
    HTTP/1.0 200 OK
    Server: SimpleHTTP/0.6 Python/3.10.12
    Date: Thu, 11 Apr 2024 13:41:11 GMT
    Content-type: text/html
    Content-Length: 14054
    Last-Modified: Thu, 30 Mar 2023 09:11:09 GMT
    
    # Skip HTML content

Runtime configuration

  1. Am I missing something ?

    My runtime configuration is the following:

    -- Build Configurations:
          Build as target THUMBV8
          CMAKE_BUILD_TYPE
          WAMR Interpreter enabled
          WAMR AOT enabled
          WAMR Fast JIT disabled
          WAMR LLVM ORC JIT disabled
          Libc builtin enabled
          Libc WASI enabled
          Fast interpreter disabled
          Multiple modules disabled
          Bulk memory feature enabled
          Wakeup of blocking operations enabled
          Reference types disabled
          GC performance profiling disabled
          Global heap pool enabled
          Custom global heap size: 131072
          Module instance context enabled
          Quick AOT/JIT entries enabled
  2. How to use the address pool ?

    I would like to have some help with the libc_wasi_init(wasm_module, argc, argv, &wasi_parse_ctx) function because if I understood it's mandatory to run a network application on WAMR.

    I looked at the product-mini/plateforms/posix/main.c & product-mini/plateforms/common/libc_wasi.c files but didn't fully understand the implementation because it is made to parse arguments from the command line.

    Meanwhile, I'm trying to run the http_client module on the board, so I don't have the possibility to pass arguments to the module.

    I came up with this configuration before wasm_runtime_full_init(&init_args) call in main.c.

    #if WASM_ENABLE_LIBC_WASI != 0
    #define HUMAN_READABLE_ADDRESS "192.0.2.10\\24"
    libc_wasi_parse_context_t wasi_parse_ctx;
    memset(&wasi_parse_ctx, 0, sizeof(wasi_parse_ctx));
    
    libc_wasi_parse_result_t result = libc_wasi_parse(HUMAN_READABLE_ADDRESS, &wasi_parse_ctx);
    switch (result) {
          case LIBC_WASI_PARSE_RESULT_OK:
                continue;
          case LIBC_WASI_PARSE_RESULT_NEED_HELP:
                return;
          case LIBC_WASI_PARSE_RESULT_BAD_PARAM:
                return;
    }
    libc_wasi_init(wasm_module, argc, argv, &wasi_parse_ctx);
    #endif

    But I'm not sure if it's the right way to use the libc_wasi_parse function.

  3. Should I change build policy ?

    I tried to adapt the CMakeLists.txt file from the simple sample. What I changed:

    1. I added the WAMR_BUILD_LIBC_WASI flag.
    2. I added some env conf (path to wamr-sdk, wasi-sdk, python, ...)
    3. I added the wamr-sdk as an external projecxt and compile the libapp_framework.a.
    4. I added a custom target to compile the http_client module.
    5. I added a custom command that use a python script to generate the http_client.h module.
    • .wasm size ~= 1.3kB
    • .h size ~= 7.6kB

Any help will be appreciated. Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementCheck if this issue/PR enhances a feature; scripts will use this info.libc-wasiplatformcore/shared/platform

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions