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

There is an issue using sockets in wasm.

Open kamylee opened this issue 1 year ago • 11 comments

code in wasm: ` #include "socket_utils.h" #include <arpa/inet.h> #include <netinet/in.h>

#include <sys/socket.h> #include <unistd.h> #ifdef wasi #include <wasi_socket_ext.h> #endif

... int testTcpClient() { int socket_fd, ret, total_size = 0, af; char buffer[1024] = { 0 }; char ip_string[64] = { 0 }; socklen_t len; struct sockaddr_storage server_address = { 0 }; struct sockaddr_storage local_address = { 0 };

af = AF_INET;
len = sizeof(struct sockaddr_in);
init_sockaddr_inet((struct sockaddr_in*)&server_address);


printf("[Client] Create socket\n");
socket_fd = socket(af, SOCK_STREAM, 0);
if (socket_fd == -1) {
    perror("Create socket failed");
    return EXIT_FAILURE;
}

printf("[Client] Connect socket\n");
if (connect(socket_fd, (struct sockaddr*)&server_address, len) == -1) {
    perror("Connect failed");
    close(socket_fd);
    return EXIT_FAILURE;
}

len = sizeof(local_address);
ret = getsockname(socket_fd, (struct sockaddr*)&local_address, &len);
if (ret == -1) {
    perror("Failed to retrieve socket address");
    close(socket_fd);
    return EXIT_FAILURE;
}

if (sockaddr_to_string((struct sockaddr*)&local_address, ip_string,
    sizeof(ip_string) / sizeof(ip_string[0]))
    != 0) {
    printf("[Client] failed to parse local address\n");
    close(socket_fd);
    return EXIT_FAILURE;
}

printf("[Client] Local address is: %s\n", ip_string);

printf("[Client] Client receive\n");
while (1) {
    ret = recv(socket_fd, buffer + total_size, sizeof(buffer) - total_size,
        0);
    if (ret <= 0)
        break;
    total_size += ret;
}

printf("[Client] %d bytes received:\n", total_size);
if (total_size > 0) {
    printf("Buffer recieved:\n%s\n", buffer);
}

close(socket_fd);
printf("[Client] BYE \n");

return 1;

}`

CmakeLists.txt: ` ... add_executable (demoS7.wasm "demoS7.cpp"

)

TARGET_LINK_LIBRARIES(demoS7.wasm cjson socket_wasi_ext )

set_target_properties(demoS7.wasm PROPERTIES LINK_FLAGS " -O3
-Wl,--export=init -Wl,--export=onMessage,--export=onDataRecv
-Wl,--export=__heap_base -Wl,--export=__data_end
-Wl,--no-entry -Wl,--strip-all -Wl,--allow-undefined
") ... ` Some error occurred during runtime?

image

[07:02:11:630 - 7FFB3DC9D720]: warning: failed to link import function (wasi_snapshot_preview1, sock_connect) [07:02:11:010 - 7FFB3DC9D720]: warning: failed to link import function (wasi_snapshot_preview1, sock_open) [07:02:11:375 - 7FFB3DC9D720]: warning: failed to link import function (wasi_snapshot_preview1, sock_addr_local)

kamylee avatar May 06 '24 07:05 kamylee

please refer to https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/samples/socket-api/wasm-src/CMakeLists.txt.

include(${CMAKE_CURRENT_SOURCE_DIR}/../../../core/iwasm/libraries/lib-socket/lib_socket_wasi.cmake)

lum1n0us avatar May 06 '24 08:05 lum1n0us

image

The problem still persists after I added it.

build iwasm: cmake .. -DWAMR_BUILD_LIB_WASI_THREADS=1 -DWAMR_BUILD_PLATFORM=windows cmake --build . --config Release

kamylee avatar May 06 '24 10:05 kamylee

🤔 it's weird. Would u mind uploading full content of your code(including CMakeLists.txt)?

lum1n0us avatar May 06 '24 11:05 lum1n0us

🤔 it's weird. Would u mind uploading full content of your code(including CMakeLists.txt)?

Uploaded file: DemoS7.zip

Command to compile to wasm file: cmake -G "Unix Makefiles" -DWASI_SDK_PREFIX=E:/WorkSpace/DownLoads/wasi-sdk-21.0.m-mingw/wasi-sdk-21.0+m -DCMAKE_TOOLCHAIN_FILE=E:/WorkSpace/DownLoads/wasi-sdk-21.0.m-mingw/wasi-sdk-21.0+m/share/cmake/wasi-sdk.cmake -DCMAKE_SYSROOT=E:/WorkSpace/DownLoads/wasi-sdk-21.0.m-mingw/wasi-sdk-21.0+m/share/wasi-sysroot ..

there are also some issue with thread: image

kamylee avatar May 07 '24 01:05 kamylee

the same error occurs when usring iwasm.exe: image

kamylee avatar May 07 '24 01:05 kamylee

here is a minimum version of CMakeLists.txt:

cmake_minimum_required (VERSION 3.8)

project ("demoS7.wasm")

#已经在官方socket-api项目中生成了libsocket_wasi_ext.a,这里直接链接
include(/workspaces/wasm-micro-runtime/core/iwasm/libraries/lib-socket/lib_socket_wasi.cmake)

add_subdirectory(thirdparty/cJSON)

# 将源代码添加到此项目的可执行文件。
add_executable(demoS7.wasm demoS7.cpp)

target_compile_options(demoS7.wasm PRIVATE -pthread)

target_link_libraries(demoS7.wasm
  cjson
  socket_wasi_ext
  pthread
)

target_link_options(demoS7.wasm PRIVATE
  LINKER:--export=__heap_base
  LINKER:--export=__data_end
  LINKER:--export=malloc
  LINKER:--export=free
  LINKER:--shared-memory,--max-memory=10485760
  LINKER:--no-check-features
  LINKER:--allow-undefined
)

And for iwasm building, please use -DWAMR_BUILD_LIB_PTHREAD=1 instead of -DWAMR_BUILD_LIB_WASI_THREADS=1 unless using -DCMAKE_TOOLCHAIN_FILE=wasi-sdk-pthread.cmake when compiling wasm applications.

BTW, for wasm applications, please use latest wasi-sdk which allows a shorter command like cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=/opt/wasi-sdk-21.0/share/cmake/wasi-sdk.cmake

lum1n0us avatar May 07 '24 03:05 lum1n0us

thank you very much for your patient response! Today wamr has been updated to the lastest version。 wasi-sdk:wasi-sdk-21.0+m thread run ok after use -DWAMR_BUILD_LIB_PTHREAD=1。 image

But the issue with the socket still persists. win10: image ubuntu22.04: image

this is my current CMakeLists.txt: CMakeLists.txt

kamylee avatar May 07 '24 09:05 kamylee

you might want to try this

lum1n0us avatar May 07 '24 11:05 lum1n0us

The example socket-api has been successfully run: image

However, the example socket API under Windows always fails to compile. `cmake -DWASI_SDK_DIR=E:/WorkSpace/DownLoads/wasi-sdk-21.0.m-mingw/wasi-sdk-21.0+m -DWABT_DIR=E:/WorkSpace/wamr_source/wabt-1.0.34 ..

cmake --build . --config Release ` image image image

Where is the problem?

kamylee avatar May 08 '24 05:05 kamylee

It's a POSIX-like socket. I am not confident about its compatibility with Windows. There might be some requirement.

@eloparco Please help us on socket APIs on Windows.

lum1n0us avatar May 08 '24 08:05 lum1n0us

I temporarily resolved this problem using the native socket API, for example, invoking native_connect(), native_send(), and native_recv() instead of the system socket API.

thank you for your help!

kamylee avatar May 08 '24 09:05 kamylee