Skip to content

Commit

Permalink
Implement uvwasi library and improve WASI structure
Browse files Browse the repository at this point in the history
Introduce uvwasi into the build system.
Fix build issues with libuv integration.
Introduce class WasiFunction to enable WASI to acces Instance resources.
Implement further WASI types and fd_write function.

Signed-off-by: Adam Laszlo Kulcsar <[email protected]>
  • Loading branch information
kulcsaradam committed Oct 24, 2023
1 parent 54c0b6a commit 697191a
Show file tree
Hide file tree
Showing 16 changed files with 544 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ jobs:

install: |
apt-get update
apt-get install -y cmake build-essential ninja-build pkg-config python3 clang-12
apt-get install -y cmake build-essential ninja-build pkg-config python3 clang-12 git
#FIXME fix clang version as to 12
ln -s /usr/bin/clang-12 /usr/bin/clang
ln -s /usr/bin/clang++-12 /usr/bin/clang++
Expand Down Expand Up @@ -163,7 +163,7 @@ jobs:

install: |
apt-get update
apt-get install -y cmake build-essential ninja-build pkg-config python3 clang-12
apt-get install -y cmake build-essential ninja-build pkg-config python3 clang-12 git
#FIXME fix clang version as to 12
ln -s /usr/bin/clang-12 /usr/bin/clang
ln -s /usr/bin/clang++-12 /usr/bin/clang++
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
path = third_party/wasm-c-api
url = https://github.com/WebAssembly/wasm-c-api
ignore = untracked
[submodule "third_party/uvwasi"]
path = third_party/uvwasi
url = https://github.com/nodejs/uvwasi.git
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ PROCESSORCOUNT (NPROCS)

# INCLUDE CMAKE FILES
INCLUDE (${PROJECT_SOURCE_DIR}/build/config.cmake)
INCLUDE (${PROJECT_SOURCE_DIR}/build/wasi.cmake)
INCLUDE (${PROJECT_SOURCE_DIR}/build/walrus.cmake)
7 changes: 6 additions & 1 deletion build/walrus.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ ENDIF()
SET (WABT_DEFINITIONS ${WALRUS_DEFINITIONS})
SET (WITH_EXCEPTIONS TRUE)
ADD_SUBDIRECTORY (third_party/wabt)
SET (WALRUS_LIBRARIES ${WALRUS_LIBRARIES} wabt)

# uvwasi
INCLUDE_DIRECTORIES (${WALRUS_INCDIRS} PRIVATE ${WALRUS_THIRD_PARTY_ROOT}/uvwasi/include)
INCLUDE_DIRECTORIES (${WALRUS_INCDIRS} PRIVATE ${CMAKE_BINARY_DIR}/_deps/libuv-src/include)

SET (WALRUS_LIBRARIES ${WALRUS_LIBRARIES} wabt uvwasi_a)

# BUILD
INCLUDE_DIRECTORIES (${WALRUS_INCDIRS})
Expand Down
208 changes: 208 additions & 0 deletions build/wasi.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
cmake_minimum_required(VERSION 3.11)
project (
uvwasi
DESCRIPTION "WASI syscall API built atop libuv"
VERSION 0.0.19
LANGUAGES C
)

# Point CMake at any custom modules we may ship
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")

# This can be a commit hash or tag
set(LIBUV_VERSION v1.44.2)

include(CMakeDependentOption)
cmake_dependent_option(UVWASI_BUILD_TESTS
"Build the unit tests when uvwasi is the root project" ON
"CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR" OFF)

if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU")
list(APPEND uvwasi_cflags -fvisibility=hidden --std=gnu89)
list(APPEND uvwasi_cflags -Wall -Wsign-compare -Wextra -Wstrict-prototypes)
list(APPEND uvwasi_cflags -Wno-unused-parameter)
endif()

set( CMAKE_C_FLAGS -fPIC)

if(DEFINED WALRUS_ARCH)
if(${WALRUS_ARCH} STREQUAL "x86")
set( CMAKE_C_FLAGS "-fPIC -m32")
endif()
endif()

if(APPLE)
set(CMAKE_MACOSX_RPATH ON)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
list(APPEND uvwasi_defines _GNU_SOURCE _POSIX_C_SOURCE=200112)
endif()

# find_package(LIBUV QUIET)
# if(LIBUV_FOUND)
# include_directories(${LIBUV_INCLUDE_DIR})
# else()
include(FetchContent)
## https://libuv.org
FetchContent_Declare(
libuv
GIT_REPOSITORY https://github.com/libuv/libuv.git
GIT_TAG ${LIBUV_VERSION})

FetchContent_GetProperties(libuv)
if(NOT libuv_POPULATED)
FetchContent_Populate(libuv)
include_directories("${libuv_SOURCE_DIR}/include")
add_subdirectory(${libuv_SOURCE_DIR} ${libuv_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
set(LIBUV_INCLUDE_DIR ${libuv_SOURCE_DIR}/include)
set(LIBUV_LIBRARIES uv_a)
# endif()

## uvwasi source code files.
set(uvwasi_sources
third_party/uvwasi/src/clocks.c
third_party/uvwasi/src/fd_table.c
third_party/uvwasi/src/path_resolver.c
third_party/uvwasi/src/poll_oneoff.c
third_party/uvwasi/src/sync_helpers.c
third_party/uvwasi/src/uv_mapping.c
third_party/uvwasi/src/uvwasi.c
third_party/uvwasi/src/wasi_rights.c
third_party/uvwasi/src/wasi_serdes.c
)

option(UVWASI_DEBUG_LOG "Enable debug logging" OFF)
if(UVWASI_DEBUG_LOG)
list(APPEND uvwasi_cflags -DUVWASI_DEBUG_LOG)
endif()

# Code Coverage Configuration
add_library(coverage_config INTERFACE)

option(CODE_COVERAGE "Enable coverage reporting" OFF)
if(CODE_COVERAGE AND CMAKE_C_COMPILER_ID MATCHES "AppleClang|GNU|Clang")
# Add required flags (GCC & LLVM/Clang)
target_compile_options(coverage_config INTERFACE
-O0 # no optimization
-g # generate debug info
--coverage # sets all required flags
)
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.13)
target_link_options(coverage_config INTERFACE --coverage)
else()
target_link_libraries(coverage_config INTERFACE --coverage)
endif()
endif()

# ASAN Support
option(ASAN "Enable code asan" OFF)
if(ASAN AND CMAKE_C_COMPILER_ID MATCHES "AppleClang|GNU|Clang")
set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
endif()

## Static library target.
add_library(uvwasi_a STATIC ${uvwasi_sources})
target_compile_definitions(uvwasi_a PRIVATE ${uvwasi_defines})
target_compile_options(uvwasi_a PRIVATE ${uvwasi_cflags})
target_include_directories(uvwasi_a PRIVATE ${PROJECT_SOURCE_DIR}/include)
if(CODE_COVERAGE)
target_link_libraries(uvwasi_a PUBLIC ${LIBUV_LIBRARIES} coverage_config)
else()
target_link_libraries(uvwasi_a PRIVATE ${LIBUV_LIBRARIES})
endif()

## Shared library target.
add_library(uvwasi SHARED ${uvwasi_sources})
target_compile_definitions(uvwasi PRIVATE ${uvwasi_defines})
target_compile_options(uvwasi PRIVATE ${uvwasi_cflags})
target_include_directories(uvwasi PRIVATE ${PROJECT_SOURCE_DIR}/include)
if(CODE_COVERAGE)
target_link_libraries(uvwasi PUBLIC ${LIBUV_LIBRARIES} coverage_config)
else()
target_link_libraries(uvwasi PRIVATE ${LIBUV_LIBRARIES})
endif()


## Test targets.
if(UVWASI_BUILD_TESTS)
enable_testing()
file(GLOB test_files "test/test-*.c")
foreach(file ${test_files})
get_filename_component(test_name ${file} NAME_WE)
add_executable(${test_name} ${file})
add_test(NAME ${test_name}
COMMAND ${test_name})
target_include_directories(${test_name}
PRIVATE
${PROJECT_SOURCE_DIR}/include)
target_link_libraries(${test_name} PRIVATE ${LIBUV_LIBRARIES} uvwasi_a)
list(APPEND test_list ${test_name})
endforeach()

add_custom_target(check
COMMAND ctest -VV -C Debug -R test-
DEPENDS ${test_list}
)
endif()

option(INSTALL_UVWASI "Enable installation of uvwasi. (Projects embedding uvwasi may want to turn this OFF.)" ON)
if(INSTALL_UVWASI AND NOT CODE_COVERAGE)
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

set(target_export_name ${PROJECT_NAME}Targets CACHE INTERNAL "")
set(cmake_files_install_dir ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})

set(pkg_config ${PROJECT_BINARY_DIR}/uvwasi.pc)
set(version_file ${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake)
set(config_file ${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake)

configure_file(${PROJECT_SOURCE_DIR}/third_party/uvwasi/cmake/uvwasi.pc.in ${pkg_config} @ONLY)
write_basic_package_version_file(${version_file} VERSION ${PROJECT_VERSION} COMPATIBILITY AnyNewerVersion)
configure_package_config_file(${PROJECT_SOURCE_DIR}/third_party/uvwasi/cmake/Config.cmake.in ${config_file} INSTALL_DESTINATION ${cmake_files_install_dir})

install(
TARGETS uvwasi_a uvwasi
EXPORT ${target_export_name}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
install(
DIRECTORY ${PROJECT_SOURCE_DIR}/include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/uvwasi
FILES_MATCHING PATTERN "*.h"
)
install(
FILES ${pkg_config}
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
)
install(
EXPORT ${target_export_name}
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${cmake_files_install_dir}
)
install(
FILES ${version_file} ${config_file}
DESTINATION ${cmake_files_install_dir}
)
endif()

message(STATUS "summary of uvwasi build options:
Install prefix: ${CMAKE_INSTALL_PREFIX}
Target system: ${CMAKE_SYSTEM_NAME}
Compiler:
C compiler: ${CMAKE_C_COMPILER}
CFLAGS: ${CMAKE_C_FLAGS_${_build_type}} ${CMAKE_C_FLAGS}
LibUV libraries: ${LIBUV_LIBRARIES}
LibUV includes: ${LIBUV_INCLUDE_DIR}
Debug logging: ${UVWASI_DEBUG_LOG}
Code coverage: ${CODE_COVERAGE}
ASAN: ${ASAN}
Build tests: ${UVWASI_BUILD_TESTS}
")
45 changes: 45 additions & 0 deletions src/runtime/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,49 @@ void ImportedFunction::call(ExecutionState& state, Value* argv, Value* result)
m_callback(newState, argv, result, m_data);
}

WasiFunction* WasiFunction::createWasiFunction(Store* store,
FunctionType* functionType,
WasiFunctionCallback callback,
Instance* instance)
{
WasiFunction* func = new WasiFunction(functionType,
callback,
instance);
store->appendExtern(func);
return func;
}

void WasiFunction::interpreterCall(ExecutionState& state, uint8_t* bp, ByteCodeStackOffset* offsets,
uint16_t parameterOffsetCount, uint16_t resultOffsetCount)
{
const FunctionType* ft = functionType();
const ValueTypeVector& paramTypeInfo = ft->param();
const ValueTypeVector& resultTypeInfo = ft->result();

ALLOCA(Value, paramVector, sizeof(Value) * paramTypeInfo.size());
ALLOCA(Value, resultVector, sizeof(Value) * resultTypeInfo.size());

size_t offsetIndex = 0;
size_t size = paramTypeInfo.size();
Value* paramVectorStart = paramVector;
for (size_t i = 0; i < size; i++) {
paramVector[i] = Value(paramTypeInfo[i], bp + offsets[offsetIndex]);
offsetIndex += valueFunctionCopyCount(paramTypeInfo[i]);
}

call(state, paramVectorStart, resultVector);

for (size_t i = 0; i < resultTypeInfo.size(); i++) {
resultVector[i].writeToMemory(bp + offsets[offsetIndex]);
offsetIndex += valueFunctionCopyCount(resultTypeInfo[i]);
}
}

void WasiFunction::call(ExecutionState& state, Value* argv, Value* result)
{
ExecutionState newState(state, this);
CHECK_STACK_LIMIT(newState);
m_callback(newState, argv, result, this->m_runningInstance);
}

} // namespace Walrus
48 changes: 48 additions & 0 deletions src/runtime/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class FunctionType;
class ModuleFunction;
class DefinedFunction;
class ImportedFunction;
class WasiFunction;

class Function : public Extern {
public:
Expand Down Expand Up @@ -71,6 +72,10 @@ class Function : public Extern {
{
return false;
}
virtual bool isWasiFunction() const
{
return false;
}

DefinedFunction* asDefinedFunction()
{
Expand All @@ -84,6 +89,12 @@ class Function : public Extern {
return reinterpret_cast<ImportedFunction*>(this);
}

WasiFunction* asWasiFunction()
{
assert(isWasiFunction());
return reinterpret_cast<WasiFunction*>(this);
}

protected:
Function(FunctionType* functionType)
: m_functionType(functionType)
Expand Down Expand Up @@ -168,6 +179,43 @@ class ImportedFunction : public Function {
void* m_data;
};

class WasiFunction : public Function {
public:
typedef std::function<void(ExecutionState& state, Value* argv, Value* result, Instance* instance)> WasiFunctionCallback;

static WasiFunction* createWasiFunction(Store* store,
FunctionType* functionType,
WasiFunctionCallback callback,
Instance* instance);

virtual bool isWasiFunction() const override
{
return true;
}

void setRunningInstance(Instance* instance)
{
m_runningInstance = instance;
}

virtual void call(ExecutionState& state, Value* argv, Value* result) override;
virtual void interpreterCall(ExecutionState& state, uint8_t* bp, ByteCodeStackOffset* offsets,
uint16_t parameterOffsetCount, uint16_t resultOffsetCount) override;

protected:
WasiFunction(FunctionType* functionType,
WasiFunctionCallback callback,
Instance* instance)
: Function(functionType)
, m_callback(callback)
, m_runningInstance(instance)
{
}

WasiFunctionCallback m_callback;
Instance* m_runningInstance;
};

} // namespace Walrus

#endif // __WalrusFunction__
Loading

0 comments on commit 697191a

Please sign in to comment.