Skip to content

Commit

Permalink
[Misc] Refactor the WASI-Crypto to plugin.
Browse files Browse the repository at this point in the history
Signed-off-by: YiYing He <[email protected]>
  • Loading branch information
q82419 authored and hydai committed Jul 26, 2022
1 parent 8b29d41 commit 92ac2a7
Show file tree
Hide file tree
Showing 109 changed files with 1,287 additions and 1,275 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ jobs:
- name: Install requirements
run: |
dnf update -y
dnf install -y cmake ninja-build boost llvm llvm-devel lld-devel clang git file rpm-build dpkg-dev openssl-devel-1.1
dnf install -y cmake ninja-build boost llvm llvm-devel lld-devel clang git file rpm-build dpkg-dev openssl-devel
- uses: actions/checkout@v2
with:
fetch-depth: 0
Expand All @@ -122,7 +122,7 @@ jobs:
cmake --build build
- name: Build WasmEdge with WasiCrypto
run: |
cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=Release -DWASMEDGE_BUILD_TESTS=ON -DWASMEDGE_BUILD_PACKAGE="TGZ;DEB;RPM" -DWASMEDGE_BUILD_WASI_CRYPTO=ON .
cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=Release -DWASMEDGE_BUILD_TESTS=ON -DWASMEDGE_BUILD_PACKAGE="TGZ;DEB;RPM" -DWASMEDGE_PLUGIN_WASI_CRYPTO=ON .
cmake --build build
- name: Test WasmEdge
run: |
Expand Down Expand Up @@ -233,7 +233,7 @@ jobs:
run: |
apt update
apt install -y libssl-dev
cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE -DWASMEDGE_BUILD_TESTS=ON -DOPENSSL_ROOT_DIR=/usr/local -DWASMEDGE_BUILD_WASI_CRYPTO=ON .
cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE -DWASMEDGE_BUILD_TESTS=ON -DOPENSSL_ROOT_DIR=/usr/local -DWASMEDGE_PLUGIN_WASI_CRYPTO=ON .
cmake --build build
- name: Test WasmEdge
if: ${{ ! matrix.coverage }}
Expand All @@ -250,7 +250,7 @@ jobs:
apt install -y gcovr libssl-dev
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --target wasm32-wasi
git config --global --add safe.directory $(pwd)
cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=Debug -DWASMEDGE_BUILD_TESTS=ON -DWASMEDGE_BUILD_COVERAGE=ON -DWASMEDGE_BUILD_WASI_CRYPTO=ON -DWASMEDGE_BUILD_COVERAGE=ON .
cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=Debug -DWASMEDGE_BUILD_TESTS=ON -DWASMEDGE_BUILD_COVERAGE=ON -DWASMEDGE_PLUGIN_WASI_CRYPTO=ON -DWASMEDGE_BUILD_COVERAGE=ON .
cmake --build build
LD_LIBRARY_PATH=$(pwd)/build/lib/api cmake --build build --target codecov
- name: Create and upload coverage report to Codecov
Expand Down Expand Up @@ -301,7 +301,7 @@ jobs:
./build/tools/wasmedge/wasmedge -v
- name: Build ${{ matrix.name }} package with wasi-crypto
run: |
bash utils/docker/build-manylinux.sh -enable-wasi-crypto
bash utils/docker/build-manylinux.sh -DWASMEDGE_PLUGIN_WASI_CRYPTO=On
./build/tools/wasmedge/wasmedge -v
- name: Upload artifact (v1)
uses: actions/upload-artifact@v1
Expand Down Expand Up @@ -349,7 +349,7 @@ jobs:
- name: Build WasmEdge with wasi-crypto
run: |
brew install [email protected]
cmake -Bbuild -GNinja -DWASMEDGE_BUILD_TESTS=ON -DWASMEDGE_BUILD_PACKAGE="TGZ" -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl -DWASMEDGE_BUILD_WASI_CRYPTO=ON .
cmake -Bbuild -GNinja -DWASMEDGE_BUILD_TESTS=ON -DWASMEDGE_BUILD_PACKAGE="TGZ" -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl -DWASMEDGE_PLUGIN_WASI_CRYPTO=ON .
cmake --build build
- name: Test WasmEdge
continue-on-error: true
Expand Down Expand Up @@ -406,7 +406,7 @@ jobs:
$Env:CC = "clang-cl"
$Env:CXX = "clang-cl"
$cmake_sys_version = "10.0.19041.0"
cmake -Bbuild -GNinja "-DCMAKE_SYSTEM_VERSION=$cmake_sys_version" -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDLL "-DLLVM_DIR=$llvm_dir" -DWASMEDGE_BUILD_TESTS=ON -DWASMEDGE_BUILD_PACKAGE="ZIP" -DWASMEDGE_BUILD_WASI_CRYPTO=ON .
cmake -Bbuild -GNinja "-DCMAKE_SYSTEM_VERSION=$cmake_sys_version" -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDLL "-DLLVM_DIR=$llvm_dir" -DWASMEDGE_BUILD_TESTS=ON -DWASMEDGE_BUILD_PACKAGE="ZIP" -DWASMEDGE_PLUGIN_WASI_CRYPTO=ON .
cmake --build build
- name: Test WasmEdge
continue-on-error: true
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
run: |
apt update
apt install -y libssl-dev
cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=Debug -DWASMEDGE_BUILD_WASI_CRYPTO=ON .
cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=Debug -DWASMEDGE_PLUGIN_WASI_CRYPTO=ON .
cmake --build build
- name: Perform CodeQL Analysis
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ jobs:
uses: actions/checkout@v2
- name: Build ${{ matrix.name }} package
run: |
bash utils/docker/build-manylinux.sh -enable-wasi-crypto
bash utils/docker/build-manylinux.sh -DWASMEDGE_PLUGIN_WASI_CRYPTO=On
- name: Upload ${{ matrix.name }} rpm package
uses: actions/upload-release-asset@v1
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/static-code-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
run: |
mkdir build
cd build
infer compile -- cmake -DCMAKE_BUILD_TYPE=Debug -DLLVM_DIR=/usr/lib/llvm-11/lib/cmake/llvm -DWASMEDGE_BUILD_WASI_CRYPTO=ON ..
infer compile -- cmake -DCMAKE_BUILD_TYPE=Debug -DLLVM_DIR=/usr/lib/llvm-11/lib/cmake/llvm -DWASMEDGE_PLUGIN_WASI_CRYPTO=ON ..
- name: Run Infer Static Code Analysis
run: |
Expand Down
6 changes: 1 addition & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,11 @@ option(WASMEDGE_BUILD_EXAMPLE "Generate examples." OFF)
option(WASMEDGE_FORCE_DISABLE_LTO "Forcibly disable link time optimization when linking even in Release/RelWithDeb build." OFF)
set(WASMEDGE_PLUGIN_WASI_NN_BACKEND "" CACHE STRING "Enable WasmEdge Wasi-NN plugin with backends.")
# Currently supported WASI-NN backend: "OpenVINO" on Linux x86_64
option(WASMEDGE_BUILD_WASI_CRYPTO "Build wasi-crypto for WasmEdge" OFF)
option(WASMEDGE_PLUGIN_WASI_CRYPTO "Enable WasmEdge Wasi-crypto plugin." OFF)

set(WASMEDGE_BUILD_PACKAGE "DEB;RPM" CACHE STRING "Package generate types")
set(CPACK_PROJECT_CONFIG_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cmake/cpack_config.cmake)

if(WASMEDGE_BUILD_WASI_CRYPTO)
add_compile_definitions(WASMEDGE_BUILD_WASI_CRYPTO)
endif()

if(WASMEDGE_BUILD_COVERAGE)
set(GCOVR_ADDITIONAL_ARGS "--exclude-unreachable-branches;--exclude-throw-branches")
include(CodeCoverage)
Expand Down
2 changes: 1 addition & 1 deletion docs/book/en/src/dev/rust/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Current not support android.

## Getting started with wasi-crypto

1. Build wasi-crypto with `-DWASMEDGE_BUILD_WASI_CRYPTO=ON`.
1. Build wasi-crypto with `-DWASMEDGE_PLUGIN_WASI_CRYPTO=ON`.
2. Using [wasi-crypto binding](https://github.com/WebAssembly/wasi-crypto/tree/main/implementations/bindings/rust) in your cargo.toml

```toml
Expand Down
3 changes: 0 additions & 3 deletions lib/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ if(WASMEDGE_BUILD_STATIC_LIB)
wasmedge_add_static_lib_component_command(wasmedgeExecutor)
wasmedge_add_static_lib_component_command(wasmedgeHostModuleWasi)
wasmedge_add_static_lib_component_command(wasmedgePlugin)
if(WASMEDGE_BUILD_WASI_CRYPTO)
wasmedge_add_static_lib_component_command(wasmedgeHostModuleWasiCrypto)
endif()
wasmedge_add_static_lib_component_command(wasmedgeVM)

if (WASMEDGE_BUILD_AOT_RUNTIME)
Expand Down
40 changes: 12 additions & 28 deletions lib/api/wasmedge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
#include "driver/compiler.h"
#include "driver/tool.h"
#include "host/wasi/wasimodule.h"
#ifdef WASMEDGE_BUILD_WASI_CRYPTO
#include "host/wasi_crypto/module.h"
#endif
#include "plugin/plugin.h"
#include "vm/vm.h"

Expand Down Expand Up @@ -1673,6 +1670,18 @@ WasmEdge_ModuleInstanceCreateWasiNN(void) {
return nullptr;
}

WASMEDGE_CAPI_EXPORT WasmEdge_ModuleInstanceContext *
WasmEdge_ModuleInstanceCreateWasiCrypto(void) {
using namespace std::literals::string_view_literals;
if (const auto *Plugin = WasmEdge::Plugin::Plugin::find("wasi_crypto"sv)) {
if (const auto *Module = Plugin->findModule("wasi_crypto"sv)) {
auto *ProcMod = toModCxt(Module->create().release());
return ProcMod;
}
}
return nullptr;
}

WASMEDGE_CAPI_EXPORT WasmEdge_ModuleInstanceContext *
WasmEdge_ModuleInstanceCreateWasmEdgeProcess(const char *const *AllowedCmds,
const uint32_t CmdsLen,
Expand Down Expand Up @@ -1706,31 +1715,6 @@ WasmEdge_ModuleInstanceInitWasmEdgeProcess(const char *const *AllowedCmds,
}
}

WASMEDGE_CAPI_EXPORT WasmEdge_ModuleInstanceContext *
WasmEdge_ModuleInstanceCreateWasiCrypto() {
#ifdef WASMEDGE_BUILD_WASI_CRYPTO
auto *WASICryptoMod = new WasmEdge::Host::WasiCryptoModule();
WasmEdge_ModuleInstanceInitWasiCrypto(toModCxt(WASICryptoMod));
return toModCxt(WASICryptoMod);
#else
return nullptr;
#endif
}

WASMEDGE_CAPI_EXPORT void
WasmEdge_ModuleInstanceInitWasiCrypto(WasmEdge_ModuleInstanceContext *Cxt) {
if (!Cxt) {
return;
}
#ifdef WASMEDGE_BUILD_WASI_CRYPTO
auto *WASICryptoMod =
dynamic_cast<WasmEdge::Host::WasiCryptoModule *>(fromModCxt(Cxt));
if (!WASICryptoMod) {
return;
}
#endif
}

WASMEDGE_CAPI_EXPORT WasmEdge_String WasmEdge_ModuleInstanceGetModuleName(
const WasmEdge_ModuleInstanceContext *Cxt) {
if (Cxt) {
Expand Down
3 changes: 0 additions & 3 deletions lib/host/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,3 @@
# SPDX-FileCopyrightText: 2019-2022 Second State INC

add_subdirectory(wasi)
if (WASMEDGE_BUILD_WASI_CRYPTO)
add_subdirectory(wasi_crypto)
endif()
7 changes: 0 additions & 7 deletions lib/vm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,3 @@ target_link_libraries(wasmedgeVM
wasmedgeExecutor
wasmedgeHostModuleWasi
)

if(WASMEDGE_BUILD_WASI_CRYPTO)
target_link_libraries(wasmedgeVM
PUBLIC
wasmedgeHostModuleWasiCrypto
)
endif()
60 changes: 22 additions & 38 deletions lib/vm/vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
#include "vm/async.h"

#include "host/wasi/wasimodule.h"
#ifdef WASMEDGE_BUILD_WASI_CRYPTO
#include "host/wasi_crypto/module.h"
#endif
#include "plugin/plugin.h"

namespace WasmEdge {
Expand Down Expand Up @@ -37,41 +34,36 @@ void VM::unsafeInitVM() {
ExecutorEngine.registerModule(StoreRef, *WasiMod.get());
ImpObjs.insert({HostRegistration::Wasi, std::move(WasiMod)});
}
if (Conf.hasHostRegistration(HostRegistration::WasmEdge_Process)) {
bool Founded = false;
if (const auto *Plugin = Plugin::Plugin::find("wasmedge_process"sv)) {
if (const auto *Module = Plugin->findModule("wasmedge_process"sv)) {
auto ProcMod = Module->create();
ExecutorEngine.registerModule(StoreRef, *ProcMod);
ImpObjs.emplace(HostRegistration::WasmEdge_Process, std::move(ProcMod));
Founded = true;

// Load the plugins.
auto loadPlugin = [=](std::string_view Name, HostRegistration Host) {
if (Conf.hasHostRegistration(Host)) {
bool Founded = false;
if (const auto *Plugin = Plugin::Plugin::find(Name)) {
if (const auto *Module = Plugin->findModule(Name)) {
auto ProcMod = Module->create();
ExecutorEngine.registerModule(StoreRef, *ProcMod);
ImpObjs.emplace(Host, std::move(ProcMod));
Founded = true;
}
}
}
if (!Founded) {
spdlog::debug("wasmedge_process module not founded."sv);
}
}
if (Conf.hasHostRegistration(HostRegistration::WasiNN)) {
bool Founded = false;
if (const auto *Plugin = Plugin::Plugin::find("wasi_nn"sv)) {
if (const auto *Module = Plugin->findModule("wasi_nn"sv)) {
auto ProcMod = Module->create();
ExecutorEngine.registerModule(StoreRef, *ProcMod);
ImpObjs.emplace(HostRegistration::WasiNN, std::move(ProcMod));
Founded = true;
if (!Founded) {
spdlog::debug(Name, "module not founded."sv);
}
}
if (!Founded) {
spdlog::debug("wasi_nn module not founded."sv);
}
}
};
loadPlugin("wasmedge_process"sv, HostRegistration::WasmEdge_Process);
loadPlugin("wasi_nn"sv, HostRegistration::WasiNN);
loadPlugin("wasi_crypto"sv, HostRegistration::WasiCrypto);

uint8_t Index = static_cast<uint8_t>(HostRegistration::Max);
for (const auto &Plugin : Plugin::Plugin::plugins()) {
if (Conf.isForbiddenPlugins(Plugin.name())) {
continue;
}
// skip WasmEdge_Process and wasi_nn
if (Plugin.name() == "wasmedge_process"sv || Plugin.name() == "wasi_nn"sv) {
// skip WasmEdge_Process, wasi_nn, ans wasi_crypto.
if (Plugin.name() == "wasmedge_process"sv || Plugin.name() == "wasi_nn"sv ||
Plugin.name() == "wasi_crypto"sv) {
continue;
}
for (const auto &Module : Plugin.modules()) {
Expand All @@ -81,14 +73,6 @@ void VM::unsafeInitVM() {
std::move(ModObj));
}
}
#ifdef WASMEDGE_BUILD_WASI_CRYPTO
if (Conf.hasHostRegistration(HostRegistration::WasiCrypto)) {
std::unique_ptr<Runtime::Instance::ModuleInstance> WasiCryptoMod =
std::make_unique<Host::WasiCryptoModule>();
ExecutorEngine.registerModule(StoreRef, *WasiCryptoMod.get());
ImpObjs.insert({HostRegistration::WasiCrypto, std::move(WasiCryptoMod)});
}
#endif
}

Expect<void> VM::unsafeRegisterModule(std::string_view Name,
Expand Down
8 changes: 6 additions & 2 deletions plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
# SPDX-FileCopyrightText: 2019-2022 Second State INC

# Only Linux systems support wasmedge_process now.
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
add_subdirectory(wasmedge_process)
endif()

if(WASMEDGE_PLUGIN_WASI_NN_BACKEND)
if (WASMEDGE_PLUGIN_WASI_NN_BACKEND)
add_subdirectory(wasi_nn)
endif()

if (WASMEDGE_PLUGIN_WASI_CRYPTO)
add_subdirectory(wasi_crypto)
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

find_package(OpenSSL REQUIRED)

wasmedge_add_library(wasmedgeHostModuleWasiCrypto
wasmedge_add_library(wasmedgePluginWasiCrypto
SHARED
module.cpp
ctx.cpp
asymmetric_common/ctx.cpp
asymmetric_common/func.cpp
asymmetric_common/keypair.cpp
Expand All @@ -19,7 +22,6 @@ wasmedge_add_library(wasmedgeHostModuleWasiCrypto
kx/func.cpp
kx/kx.cpp
kx/options.cpp
module.cpp
signatures/ctx.cpp
signatures/ecdsa.cpp
signatures/eddsa.cpp
Expand All @@ -43,13 +45,31 @@ wasmedge_add_library(wasmedgeHostModuleWasiCrypto
utils/hostfunction.cpp
)

target_include_directories(wasmedgeHostModuleWasiCrypto
target_compile_options(wasmedgePluginWasiCrypto
PUBLIC
-DWASMEDGE_PLUGIN
)

if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
target_link_options(wasmedgePluginWasiCrypto
PUBLIC
-Wl,-U,__ZN8WasmEdge6Plugin14PluginRegisterC1EPKNS0_6Plugin16PluginDescriptorE
-Wl,-U,__ZN8WasmEdge6Plugin14PluginRegisterD1Ev
)
endif()

target_include_directories(wasmedgePluginWasiCrypto
PUBLIC
$<TARGET_PROPERTY:wasmedgePlugin,INCLUDE_DIRECTORIES>
${CMAKE_CURRENT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/thirdparty
)

target_link_libraries(wasmedgeHostModuleWasiCrypto
target_link_libraries(wasmedgePluginWasiCrypto
PUBLIC
OpenSSL::Crypto
wasmedgeCommon
wasmedgeSystem
)

install(TARGETS wasmedgePluginWasiCrypto DESTINATION ${CMAKE_INSTALL_LIBDIR}/wasmedge)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2019-2022 Second State INC

#include "host/wasi_crypto/ctx.h"
#include "ctx.h"

namespace WasmEdge {
namespace Host {
Expand Down
Loading

0 comments on commit 92ac2a7

Please sign in to comment.