Skip to content

Commit

Permalink
Merge pull request #100 from blocklessnetwork/fix/the_entry_using_log
Browse files Browse the repository at this point in the history
Fix/the entry using log
  • Loading branch information
Joinhack authored Apr 26, 2024
2 parents 692f4fe + e1e7d48 commit eb2b779
Show file tree
Hide file tree
Showing 36 changed files with 650 additions and 507 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/rust-fmt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: CI

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

on:
pull_request:
branches:
- main
env:
CARGO_TERM_COLOR: always

jobs:
fmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/[email protected]
with:
components: rustfmt
- run: cargo fmt --all --check
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ wasmtime-wasi = "19.0.0"
wiggle-generate = "19.0.0"
wasmtime-wasi-threads = "19.0.0"
wasi-common = { path = "crates/wasi-common", version="19.0.0" }
# witx dependency by wiggle
wiggle = "19.0.0"
witx = "0.9.1"

anyhow = "1.0.22"
cap-std = "3.0.0"
cap-fs-ext = "3.0.0"
Expand Down
29 changes: 8 additions & 21 deletions blockless/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ use wasmtime_wasi_threads::WasiThreadsCtx;
pub(crate) struct BlocklessContext {
pub(crate) preview1_ctx: Option<wasi_common::WasiCtx>,

pub(crate) preview2_ctx: Option<Arc<Mutex<wasmtime_wasi::WasiCtx>>>,

pub(crate) preview2_table: Arc<Mutex<wasmtime::component::ResourceTable>>,

pub(crate) preview2_adapter: Arc<wasmtime_wasi::preview1::WasiPreview1Adapter>,
pub(crate) preview2_ctx: Option<Arc<Mutex<wasmtime_wasi::WasiP1Ctx>>>,

pub(crate) wasi_threads: Option<Arc<WasiThreadsCtx<BlocklessContext>>>,
}
Expand All @@ -21,21 +17,12 @@ impl Default for BlocklessContext {
preview1_ctx: None,
preview2_ctx: None,
wasi_threads: None,
preview2_adapter: Default::default(),
preview2_table: Arc::new(Mutex::new(wasmtime::component::ResourceTable::new())),
}
}
}

impl wasmtime_wasi::WasiView for BlocklessContext {
fn table(&mut self) -> &mut wasmtime::component::ResourceTable {
Arc::get_mut(&mut self.preview2_table)
.expect("wasmtime_wasi was not compatiable threads")
.get_mut()
.unwrap()
}

fn ctx(&mut self) -> &mut wasmtime_wasi::WasiCtx {
impl BlocklessContext {
pub(crate) fn preview2_ctx(&mut self) -> &mut wasmtime_wasi::WasiP1Ctx {
let ctx = self.preview2_ctx.as_mut().unwrap();
Arc::get_mut(ctx)
.expect("wasmtime_wasi was not compatiable threads")
Expand All @@ -44,12 +31,12 @@ impl wasmtime_wasi::WasiView for BlocklessContext {
}
}

impl wasmtime_wasi::preview1::WasiPreview1View for BlocklessContext {
fn adapter(&self) -> &wasmtime_wasi::preview1::WasiPreview1Adapter {
&self.preview2_adapter
impl wasmtime_wasi::WasiView for BlocklessContext {
fn table(&mut self) -> &mut wasmtime::component::ResourceTable {
self.preview2_ctx().table()
}

fn adapter_mut(&mut self) -> &mut wasmtime_wasi::preview1::WasiPreview1Adapter {
Arc::get_mut(&mut self.preview2_adapter).expect("preview2 is not compatible with threads")
fn ctx(&mut self) -> &mut wasmtime_wasi::WasiCtx {
self.preview2_ctx().ctx()
}
}
44 changes: 22 additions & 22 deletions blockless/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ impl BlocklessConfig2Preview1WasiBuilder for BlocklessConfig {
struct BlocklessRunner(BlocklessConfig);

impl BlocklessRunner {

/// blockless run method, it execute the wasm program with configure file.
async fn run(self) -> ExitStatus {
let b_conf = self.0;
Expand Down Expand Up @@ -161,7 +160,7 @@ impl BlocklessRunner {
let mut ctx = BlocklessContext::default();
ctx.preview1_ctx = Some(preview1_ctx);
let mut store = Store::new(&engine, ctx);
Self::preview1_setup_linker(&mut linker);
Self::preview1_setup_linker(&mut linker, support_thread);
let (module, entry) = Self::module_linker(version, entry, &mut store, &mut linker).await;
// support thread.
if support_thread {
Expand All @@ -173,7 +172,7 @@ impl BlocklessRunner {
linker.instantiate_async(&mut store, &module).await.unwrap()
};
let func = inst.get_typed_func::<(), ()>(&mut store, &entry).unwrap();
// if thread multi thread use sync model.
// if thread multi thread use sync model.
// The multi-thread model is used for the cpu intensive program.
let result = if support_thread {
func.call(&mut store, ())
Expand All @@ -194,24 +193,24 @@ impl BlocklessRunner {
}

/// setup functions the preview1 for linker
fn preview1_setup_linker(linker: &mut Linker<BlocklessContext>) {
// define the macro of extends.
macro_rules! add_to_linker {
($method:expr) => {
$method(linker, |s| s.preview1_ctx.as_mut().unwrap()).unwrap()
};
fn preview1_setup_linker(linker: &mut Linker<BlocklessContext>, support_thread: bool) {
if !support_thread {
// define the macro of extends.
macro_rules! add_to_linker {
($method:expr) => {
$method(linker, |s| s.preview1_ctx.as_mut().unwrap()).unwrap()
};
}
add_to_linker!(blockless_env::add_drivers_to_linker);
add_to_linker!(blockless_env::add_http_to_linker);
add_to_linker!(blockless_env::add_ipfs_to_linker);
add_to_linker!(blockless_env::add_s3_to_linker);
add_to_linker!(blockless_env::add_memory_to_linker);
add_to_linker!(blockless_env::add_cgi_to_linker);
add_to_linker!(blockless_env::add_socket_to_linker);
}
add_to_linker!(blockless_env::add_drivers_to_linker);
add_to_linker!(blockless_env::add_http_to_linker);
add_to_linker!(blockless_env::add_ipfs_to_linker);
add_to_linker!(blockless_env::add_s3_to_linker);
add_to_linker!(blockless_env::add_memory_to_linker);
add_to_linker!(blockless_env::add_cgi_to_linker);
add_to_linker!(blockless_env::add_socket_to_linker);
wasi_common::sync::add_to_linker(
linker,
|host| host.preview1_ctx.as_mut().unwrap()
).unwrap();
wasi_common::sync::add_to_linker(linker, |host| host.preview1_ctx.as_mut().unwrap())
.unwrap();
}

fn preview1_setup_thread_support(
Expand All @@ -224,7 +223,8 @@ impl BlocklessRunner {
})
.unwrap();
store.data_mut().wasi_threads = Some(Arc::new(
WasiThreadsCtx::new(module.clone(), Arc::new(linker.clone())).unwrap(),
WasiThreadsCtx::new(module.clone(), Arc::new(linker.clone()))
.expect("wasi thread ctx new fail."),
));
}

Expand All @@ -241,7 +241,7 @@ impl BlocklessRunner {
(module, ENTRY.to_string())
}
BlocklessConfigVersion::Version1 => {
if entry == "" {
if entry.is_empty() {
entry = ENTRY.to_string();
}
let mut module_linker = ModuleLinker::new(linker, store);
Expand Down
2 changes: 1 addition & 1 deletion blockless/tests/blockless_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ fn test_blockless_normal() {
config.set_version(BlocklessConfigVersion::Version0);
let code = run_blockless(config);
assert_eq!(code.code, 0);
}
}
17 changes: 15 additions & 2 deletions blockless/tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use blockless::{ExitStatus, blockless_run};
use blockless::{blockless_run, ExitStatus};
use tokio::runtime::Builder;
use wasi_common::BlocklessConfig;

/// runing environment for test.
#[allow(dead_code)]
pub fn run_blockless(config: BlocklessConfig) -> ExitStatus {
let rt = Builder::new_current_thread()
.enable_io()
Expand All @@ -10,4 +12,15 @@ pub fn run_blockless(config: BlocklessConfig) -> ExitStatus {
.unwrap();

rt.block_on(async { blockless_run(config).await })
}
}

/// multi-thread runing environment for test.
#[allow(dead_code)]
pub fn multi_threads_run_blockless(config: BlocklessConfig) -> ExitStatus {
let rt = Builder::new_multi_thread()
.enable_io()
.enable_time()
.build()
.unwrap();
rt.block_on(async { blockless_run(config).await })
}
9 changes: 5 additions & 4 deletions blockless/tests/modules_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs;

use common::run_blockless;
use tempdir::TempDir;
use wasi_common::{BlocklessModule, ModuleType, BlocklessConfig, BlocklessConfigVersion};
use wasi_common::{BlocklessConfig, BlocklessConfigVersion, BlocklessModule, ModuleType};

#[test]
fn test_linker_module() {
Expand Down Expand Up @@ -95,7 +95,9 @@ fn test_blockless_extension_http_req() {
"#;

let temp_dir = TempDir::new("blockless_run").unwrap();
let guest_path = temp_dir.path().join("test_blockless_extension_http_req.wasm");
let guest_path = temp_dir
.path()
.join("test_blockless_extension_http_req.wasm");

fs::write(&guest_path, guest_wasm).unwrap();

Expand All @@ -112,7 +114,6 @@ fn test_blockless_extension_http_req() {
assert_eq!(code.code, 0);
}


#[test]
fn test_blockless_run_primary_module_can_call_reactor_module() {
let primary_code = r#"
Expand Down Expand Up @@ -446,4 +447,4 @@ fn test_blockless_reactor_module_can_call_reactor_module_with_callback_endless_l
config.set_modules(modules);
let code = run_blockless(config);
assert_eq!(code.code, 0);
}
}
23 changes: 23 additions & 0 deletions blockless/tests/multi_threads.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::fs;

use blockless::BlocklessConfig;
use tempdir::TempDir;

mod common;

#[test]
fn multi_threads() {
let temp_dir = TempDir::new("blockless_run").unwrap();
let code = r#"
(module
(func (export "_start"))
(memory (export "memory") 1)
)
"#;
let file_path = temp_dir.path().join("test_multi_threads.wat");
fs::write(&file_path, &code).unwrap();
let entry = file_path.to_str().unwrap();
let mut cfg = BlocklessConfig::new(&entry);
cfg.set_feature_thread(true);
common::multi_threads_run_blockless(cfg);
}
1 change: 1 addition & 0 deletions bls-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ md5 = { workspace = true }
url = { workspace = true }
clap = { workspace = true, features = ["color", "suggestions", "derive"] }
dlopen = { workspace = true }
once_cell.workspace = true

[dependencies.env_logger]
workspace = true
Loading

0 comments on commit eb2b779

Please sign in to comment.