Skip to content

WAMR application can access the whole File System at Zephyr #4399

Open
@banana-sun

Description

@banana-sun

OS: Zephyr v3.7.1
WAMR brance: https://github.com/bytecodealliance/wasm-micro-runtime/tree/dev/zephyr_file_socket
Hardware: NUCLEO-H563ZI
Zephyr File System: lfs/test.txt
WAMR FS interaction folder: --dir=/lfs

There is a test.txt file at /lfs. Then in the WAMR application, if need to access test.txt file, it should be /test.txt instead of /lfs/test.txt. This is my understanding. I'm not sure if it's correct.

Runtime implement:

    const char * wasi_dir = "/lfs";

    /* load WASM module */
    wasm_module = wasm_runtime_load(wasm_file_buf, entry.size, error_buf, sizeof(error_buf));

    wasm_runtime_set_wasi_args_ex(wasm_module, &wasi_dir, 1, NULL, 0, NULL, 0, NULL, 0, 0, 1, 2);

    /* instantiate the module */
    wasm_module_inst =
              wasm_runtime_instantiate(wasm_module, CONFIG_APP_STACK_SIZE, CONFIG_APP_HEAP_SIZE, error_buf, sizeof(error_buf));
    wasm_application_execute_main(module_inst, app_argc, app_argv);

Application code:

#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>

static int file_test(void)
{
    int     fd;
    uint8_t buf[32] = { 0 };

    fd = open("/lfs/test.txt", O_CREAT | O_RDWR);
    if (fd < 0)
    {
        printf("Error opening file [%d]\n", fd);
        printf("%s\r\n", strerror(errno));
        return fd;
    }
    printf("File opened successfully\n");

    uint32_t len = read(fd, buf, sizeof(buf));
    if (len < 0)
    {
        printf("Error reading file [%d]\n", len);
        return len;
    }
    printf("Read: %s\n", buf);

    close(fd);
    return 0;
}

It will output:

Hello world!
File opened successfully
Read: hello

But if I modify it to: fd = open("/test.txt", O_CREAT | O_RDWR);, it will output Error opening file [-1].

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions