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

Implement WASI file functions and improve WASI structure #160

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -164,7 +164,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 @@ -6,3 +6,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 @@ -65,7 +65,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
86 changes: 86 additions & 0 deletions build/wasi.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# This can be a commit hash or tag
set(LIBUV_VERSION v1.44.2)

include(CMakeDependentOption)

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 "${CMAKE_C_FLAGS} -m32")
endif()
endif()

if(DEFINED WALRUS_MODE)
if(${WALRUS_MODE} STREQUAL "debug")
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g")
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()

set(CMAKE_MESSAGE_LOG_LEVEL "WARNING")

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)

## 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()

## 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)
target_link_libraries(uvwasi_a PRIVATE ${LIBUV_LIBRARIES})

set(CMAKE_MESSAGE_LOG_LEVEL "DEFAULT")

message(STATUS "summary of uvwasi build options:
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}
")
43 changes: 43 additions & 0 deletions src/runtime/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,47 @@ 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)
{
WasiFunction* func = new WasiFunction(functionType,
callback);
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
47 changes: 47 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 @@ -72,6 +73,11 @@ class Function : public Extern {
return false;
}

virtual bool isWasiFunction() const
kulcsaradam marked this conversation as resolved.
Show resolved Hide resolved
{
return false;
}

DefinedFunction* asDefinedFunction()
{
ASSERT(isDefinedFunction());
Expand All @@ -84,6 +90,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 +180,41 @@ 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);

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)
: Function(functionType)
, m_callback(callback)
, m_runningInstance(nullptr)
{
}

WasiFunctionCallback m_callback;
Instance* m_runningInstance;
};

} // namespace Walrus

#endif // __WalrusFunction__
7 changes: 6 additions & 1 deletion src/runtime/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "interpreter/ByteCode.h"
#include "interpreter/Interpreter.h"
#include "parser/WASMParser.h"
#include "wasi/Wasi.h"

namespace Walrus {

Expand Down Expand Up @@ -151,7 +152,11 @@ Instance* Module::instantiate(ExecutionState& state, const ExternVector& imports
if (!imports[i]->asFunction()->functionType()->equals(m_imports[i]->functionType())) {
Trap::throwException(state, "imported function type mismatch");
}
instance->m_functions[funcIndex++] = imports[i]->asFunction();
instance->m_functions[funcIndex] = imports[i]->asFunction();
if (imports[i]->asFunction()->isWasiFunction()) {
instance->m_functions[funcIndex]->asWasiFunction()->setRunningInstance(instance);
}
funcIndex++;
break;
}
case ImportType::Global: {
Expand Down
21 changes: 21 additions & 0 deletions src/runtime/SpecTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class SpecTestFunctionTypes {
// The R is meant to represent the results, after R are the result types.
NONE = 0,
I32R,
I32_RI32,
I32I32I32I32_RI32,
RI32,
I64R,
F32R,
Expand Down Expand Up @@ -57,6 +59,25 @@ class SpecTestFunctionTypes {
param->push_back(Value::Type::I32);
m_vector[index++] = new FunctionType(param, result);
}
{
// I32_RI32
param = new ValueTypeVector();
result = new ValueTypeVector();
param->push_back(Value::Type::I32);
result->push_back(Value::Type::I32);
m_vector[index++] = new FunctionType(param, result);
}
{
// I32I32I32I32_RI32
param = new ValueTypeVector();
result = new ValueTypeVector();
param->push_back(Value::Type::I32);
param->push_back(Value::Type::I32);
param->push_back(Value::Type::I32);
param->push_back(Value::Type::I32);
result->push_back(Value::Type::I32);
m_vector[index++] = new FunctionType(param, result);
}
{
// RI32
param = new ValueTypeVector();
Expand Down
Loading