From d275a95e95794e9b2024937dcdcb4980e5af3ed5 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Tue, 8 Mar 2022 20:44:20 -0600 Subject: [PATCH] added experimental cptr feature --- core/onyx/cptr.onyx | 42 +++++++++++++++++++++++ core/{os/onyx_fs.onyx => onyx/fs.onyx} | 0 core/std.onyx | 4 ++- modules/onyx_runtime/onyx_runtime.c | 46 ++++++++++++++++++++++++++ tests/struct_use_pointer_member | 2 +- 5 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 core/onyx/cptr.onyx rename core/{os/onyx_fs.onyx => onyx/fs.onyx} (100%) diff --git a/core/onyx/cptr.onyx b/core/onyx/cptr.onyx new file mode 100644 index 00000000..d5e97df0 --- /dev/null +++ b/core/onyx/cptr.onyx @@ -0,0 +1,42 @@ +package core + +cptr :: struct (T: type_expr) { + data: u64; + + make :: (ptr: ^$T) -> cptr(T) { + return .{ __cptr_make(ptr) }; + } + + read :: (this: cptr($T)) -> T { + buf: [sizeof T] u8; + __cptr_read(this.data, ~~buf, sizeof T); + return *cast(^T) buf; + } + + read_u8 :: (this: cptr(u8)) => __cptr_read_u8(this.data); + read_u16 :: (this: cptr(u16)) => __cptr_read_u16(this.data); + read_u32 :: (this: cptr(u32)) => __cptr_read_u32(this.data); + read_u64 :: (this: cptr(u64)) => __cptr_read_u64(this.data); + read_i8 :: (this: cptr(i8)) => cast(i8) __cptr_read_u8(this.data); + read_i16 :: (this: cptr(i16)) => cast(i16) __cptr_read_u16(this.data); + read_i32 :: (this: cptr(i32)) => cast(i32) __cptr_read_u32(this.data); + read_i64 :: (this: cptr(i64)) => cast(i64) __cptr_read_u64(this.data); +} + +#local { + #foreign "onyx_runtime" { + __cptr_make :: (x: rawptr) -> u64 --- + __cptr_read :: (x: u64, dest: rawptr, size: u32) -> void --- + __cptr_read_u8 :: (x: u64) -> u8 --- + __cptr_read_u16 :: (x: u64) -> u16 --- + __cptr_read_u32 :: (x: u64) -> u32 --- + __cptr_read_u64 :: (x: u64) -> u64 --- + + // + // The equivalent write instructions are pusposefully left out. + // Until a VERY CONVINCING REASON as to why the must be included + // arises, having them in is more of a security vulnerability than + // I want to have. + // + } +} diff --git a/core/os/onyx_fs.onyx b/core/onyx/fs.onyx similarity index 100% rename from core/os/onyx_fs.onyx rename to core/onyx/fs.onyx diff --git a/core/std.onyx b/core/std.onyx index 4c672e32..83d6aaf9 100644 --- a/core/std.onyx +++ b/core/std.onyx @@ -49,10 +49,12 @@ package core #if runtime.runtime == .Onyx { #load "./runtime/onyx_run" #load "./os/process" - #load "./os/onyx_fs" #load "./net/net" #load "./net/tcp" + + #load "./onyx/fs" + #load "./onyx/cptr" } #if runtime.runtime == .Wasi { #load "./wasi/wasi" diff --git a/modules/onyx_runtime/onyx_runtime.c b/modules/onyx_runtime/onyx_runtime.c index 9e348dc1..f3527c35 100644 --- a/modules/onyx_runtime/onyx_runtime.c +++ b/modules/onyx_runtime/onyx_runtime.c @@ -1018,6 +1018,45 @@ ONYX_DEF(__net_address_get_port, (WASM_I64), (WASM_I32)) { } + + +// +// C-Pointers +// +// These are wildly unsafe and break the core principles of the security +// of WebAssembly, so there should be a way to turn them off! +// +ONYX_DEF(__cptr_make, (WASM_I32), (WASM_I64)) { + wasm_val_init_ptr(&results->data[0], ONYX_PTR(params->data[0].of.i32)); + return NULL; +} + +ONYX_DEF(__cptr_read, (WASM_I64, WASM_I32, WASM_I32), ()) { + memcpy(ONYX_PTR(params->data[1].of.i32), (void *) params->data[0].of.i64, params->data[2].of.i32); + return NULL; +} + +ONYX_DEF(__cptr_read_u8, (WASM_I64), (WASM_I32)) { + results->data[0] = WASM_I32_VAL(*(u8 *) params->data[0].of.i64); + return NULL; +} + +ONYX_DEF(__cptr_read_u16, (WASM_I64), (WASM_I32)) { + results->data[0] = WASM_I32_VAL(*(u16 *) params->data[0].of.i64); + return NULL; +} + +ONYX_DEF(__cptr_read_u32, (WASM_I64), (WASM_I32)) { + results->data[0] = WASM_I32_VAL(*(u32 *) params->data[0].of.i64); + return NULL; +} + +ONYX_DEF(__cptr_read_u64, (WASM_I64), (WASM_I64)) { + results->data[0] = WASM_I64_VAL(*(u64 *) params->data[0].of.i64); + return NULL; +} + + ONYX_LIBRARY { ONYX_FUNC(__file_open_impl) ONYX_FUNC(__file_close) @@ -1064,5 +1103,12 @@ ONYX_LIBRARY { ONYX_FUNC(__net_address_get_address) ONYX_FUNC(__net_address_get_port) + ONYX_FUNC(__cptr_make) + ONYX_FUNC(__cptr_read) + ONYX_FUNC(__cptr_read_u8) + ONYX_FUNC(__cptr_read_u16) + ONYX_FUNC(__cptr_read_u32) + ONYX_FUNC(__cptr_read_u64) + NULL }; diff --git a/tests/struct_use_pointer_member b/tests/struct_use_pointer_member index a8fbd546..6d70f1cf 100644 --- a/tests/struct_use_pointer_member +++ b/tests/struct_use_pointer_member @@ -1,2 +1,2 @@ Hello, I am Billy! -Go away!! func[9] +Go away!! func[17] -- 2.25.1