Skip to content

Commit 5d97b88

Browse files
committed
Remove dep on hostprint and make mmaps linux only
Signed-off-by: James Sturtevant <jsturtevant@gmail.com>
1 parent 5c4f47a commit 5d97b88

4 files changed

Lines changed: 27 additions & 34 deletions

File tree

src/hyperlight_wasm/src/sandbox/wasm_sandbox.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
use std::path::Path;
1818
use std::sync::Arc;
1919

20+
#[cfg(target_os = "linux")]
2021
use hyperlight_host::mem::memory_region::{MemoryRegion, MemoryRegionFlags, MemoryRegionType};
2122
use hyperlight_host::sandbox::snapshot::Snapshot;
2223
use hyperlight_host::{MultiUseSandbox, Result, new_error};
@@ -107,6 +108,7 @@ impl WasmSandbox {
107108
/// It is the caller's responsibility to ensure that the host side
108109
/// of the region remains intact and is not written to until the
109110
/// produced LoadedWasmSandbox is discarded or devolved.
111+
#[cfg(target_os = "linux")]
110112
pub unsafe fn load_module_by_mapping(
111113
mut self,
112114
base: *mut libc::c_void,

src/hyperlight_wasm_runtime/src/module.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -157,23 +157,6 @@ fn init_wasm_runtime(function_call: FunctionCall) -> Result<Vec<u8>> {
157157
)?;
158158
}
159159

160-
161-
let host_print_def = hostfuncs::HostFunctionDefinition {
162-
function_name: "HostPrint".to_string(),
163-
parameter_types: Some(alloc::vec![ParameterType::String]),
164-
return_type: ReturnType::Int,
165-
};
166-
let captured = host_print_def.clone();
167-
linker.func_new(
168-
"env",
169-
"HostPrint",
170-
hostfuncs::hostfunc_type(&host_print_def, &engine)?,
171-
move |c, ps, rs| {
172-
hostfuncs::call(&captured, c, ps, rs)
173-
.map_err(|e| wasmtime::Error::msg(format!("{:?}", e)))
174-
},
175-
)?;
176-
177160
*CUR_ENGINE.lock() = Some(engine);
178161
*CUR_LINKER.lock() = Some(linker);
179162
Ok(get_flatbuffer_result::<i32>(0))

src/rust_wasm_samples/src/lib.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,8 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
use std::ffi::CString;
1817
mod hostfuncs {
1918

20-
use std::os::raw::c_char;
21-
extern "C" {
22-
pub fn HostPrint(s: *const c_char) -> i32;
23-
}
24-
2519
mod host {
2620
extern "C" {
2721
#[link_name = "TestHostFunc"]
@@ -33,15 +27,31 @@ mod hostfuncs {
3327
}
3428
}
3529

30+
#[link(wasm_import_module = "wasi_snapshot_preview1")]
31+
extern "C" {
32+
fn fd_write(fd: i32, iovs: i32, iovs_len: i32, retptr: i32) -> i32;
33+
}
34+
35+
fn wasi_print(s: &str) -> i32 {
36+
let buf = s.as_ptr();
37+
let len = s.len();
38+
let iov: [u32; 2] = [buf as u32, len as u32];
39+
let mut written: u32 = 0;
40+
unsafe {
41+
fd_write(
42+
1, // stdout
43+
iov.as_ptr() as i32,
44+
1, // one iovec
45+
&mut written as *mut u32 as i32,
46+
);
47+
}
48+
written as i32
49+
}
50+
3651
macro_rules! hlprint {
3752
($($arg:tt)*) => {{
3853
let f = format!($($arg)*);
39-
let s = CString::new(f).unwrap();
40-
let r;
41-
unsafe {
42-
r = hostfuncs::HostPrint(s.as_ptr());
43-
}
44-
r
54+
wasi_print(&f)
4555
}}
4656
}
4757

src/wasmsamples/RunWasm.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ limitations under the License.
2222
#include <ctype.h>
2323
#include <math.h>
2424

25-
int HostPrint(char* msg); // Implementation of this will be available in the native host
26-
2725
int64_t GetTimeSinceBootMicrosecond();
2826

2927
__attribute__((export_name("CalcFib")))
@@ -53,7 +51,7 @@ void* ReceiveByteArray(void* data, int length) {
5351
__attribute__((export_name("WasmPrintUsingHostPrint")))
5452
int WasmPrintUsingHostPrint(char* msg)
5553
{
56-
HostPrint(msg); // Host borrows msg
54+
printf("%s", msg);
5755
int len = strlen(msg);
5856
free(msg); // Free the param since we own
5957
return len;
@@ -69,7 +67,7 @@ void PrintHelloWorld()
6967
__attribute__((export_name("Print")))
7068
void Print(char* msg)
7169
{
72-
HostPrint(msg);
70+
printf("%s", msg);
7371
free(msg); // Free the msg since we own it
7472
}
7573

@@ -99,7 +97,7 @@ char* ToUpper(char* msg)
9997
__attribute__((export_name("PrintUpper"), optnone))
10098
void PrintUpper(char* msg)
10199
{
102-
HostPrint(ToUpper(msg));
100+
printf("%s", ToUpper(msg));
103101
free(msg);
104102
}
105103

0 commit comments

Comments
 (0)