From c2310a3c99459f1d7edde69c9479d2ab492870c4 Mon Sep 17 00:00:00 2001 From: Bingle Date: Wed, 17 Aug 2022 10:02:21 +0000 Subject: [PATCH] refactor: Increase enclave memory; allocate arbitrary memory to WASM; import data --- app/Cargo.lock | 58 ++++---- app/src/main.rs | 4 +- enclave/Cargo.lock | 94 ++++++++---- enclave/Cargo.toml | 18 ++- enclave/Enclave.config.xml | 4 +- enclave/Enclave.edl | 2 +- enclave/src/lib.rs | 19 ++- get-median-wasm/Cargo.lock | 297 ++++++++++++++++++++++++++++++++++++- get-median-wasm/Cargo.toml | 1 + get-median-wasm/src/lib.rs | 20 +-- wasmi-impl/src/lib.rs | 4 +- 11 files changed, 433 insertions(+), 88 deletions(-) diff --git a/app/Cargo.lock b/app/Cargo.lock index e96ba72..3b0a8f3 100644 --- a/app/Cargo.lock +++ b/app/Cargo.lock @@ -13,9 +13,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.72" +version = "1.0.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee" +checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" [[package]] name = "cmake" @@ -34,51 +34,51 @@ checksum = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" [[package]] name = "itoa" -version = "1.0.1" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" +checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754" [[package]] name = "libc" -version = "0.2.112" +version = "0.2.132" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" +checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" [[package]] name = "proc-macro2" -version = "1.0.36" +version = "1.0.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" +checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" dependencies = [ - "unicode-xid", + "unicode-ident", ] [[package]] name = "quote" -version = "1.0.14" +version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47aa80447ce4daf1717500037052af176af5d38cc3e571d9ec1c7353fc10c87d" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" dependencies = [ "proc-macro2", ] [[package]] name = "ryu" -version = "1.0.9" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" +checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" [[package]] name = "serde" -version = "1.0.133" +version = "1.0.144" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97565067517b60e2d1ea8b268e59ce036de907ac523ad83a0475da04e818989a" +checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860" [[package]] name = "serde_derive" -version = "1.0.133" +version = "1.0.144" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed201699328568d8d08208fdd080e3ff594e6c422e438b6705905da01005d537" +checksum = "94ed3a816fb1d101812f83e789f888322c34e291f894f19590dc310963e87a00" dependencies = [ "proc-macro2", "quote", @@ -87,9 +87,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.74" +version = "1.0.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee2bb9cd061c5865d345bb02ca49fcef1391741b672b54a0bf7b679badec3142" +checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44" dependencies = [ "itoa", "ryu", @@ -98,13 +98,13 @@ dependencies = [ [[package]] name = "sgx_types" -version = "1.1.4" -source = "git+https://github.com/apache/teaclave-sgx-sdk.git#565960cd7b4b36d1188459d75652619971c43f7e" +version = "1.1.5" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git#c70a82f708fd20e9fd0377990dde097d14024f7a" [[package]] name = "sgx_urts" -version = "1.1.4" -source = "git+https://github.com/apache/teaclave-sgx-sdk.git#565960cd7b4b36d1188459d75652619971c43f7e" +version = "1.1.5" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git#c70a82f708fd20e9fd0377990dde097d14024f7a" dependencies = [ "libc", "sgx_types", @@ -112,20 +112,20 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.85" +version = "1.0.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a684ac3dcd8913827e18cd09a68384ee66c1de24157e3c556c9ab16d85695fb7" +checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" dependencies = [ "proc-macro2", "quote", - "unicode-xid", + "unicode-ident", ] [[package]] -name = "unicode-xid" -version = "0.2.2" +name = "unicode-ident" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" +checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" [[package]] name = "wabt" diff --git a/app/src/main.rs b/app/src/main.rs index 09e4c7b..0c55319 100644 --- a/app/src/main.rs +++ b/app/src/main.rs @@ -33,7 +33,7 @@ extern "C" { retval: *mut sgx_status_t, binary: *const u8, binary_len: usize, - result_out: *mut i32, + result_out: *mut f64, ) -> sgx_status_t; } @@ -72,7 +72,7 @@ fn main() { let binary = fs::read(WASM_FILE).unwrap(); - let mut result_out = 0i32; + let mut result_out = 0f64; let result = unsafe { exec_wasm_test( diff --git a/enclave/Cargo.lock b/enclave/Cargo.lock index 77d287d..a0d48ea 100644 --- a/enclave/Cargo.lock +++ b/enclave/Cargo.lock @@ -4,15 +4,15 @@ version = 3 [[package]] name = "autocfg" -version = "1.0.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "cc" -version = "1.0.72" +version = "1.0.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee" +checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" [[package]] name = "downcast-rs" @@ -23,13 +23,19 @@ checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" [[package]] name = "hashbrown_tstd" version = "0.11.2" -source = "git+https://github.com/apache/teaclave-sgx-sdk.git#565960cd7b4b36d1188459d75652619971c43f7e" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" + +[[package]] +name = "itoa" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754" [[package]] name = "libm" -version = "0.2.1" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a" +checksum = "292a948cd991e376cf75541fe5b97a1081d713c618b4f1b9500f8844e49eb565" [[package]] name = "memory_units" @@ -50,9 +56,9 @@ dependencies = [ [[package]] name = "num-integer" -version = "0.1.44" +version = "0.1.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" dependencies = [ "autocfg", "num-traits", @@ -60,9 +66,9 @@ dependencies = [ [[package]] name = "num-rational" -version = "0.4.0" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d41702bd167c2df5520b384281bc111a4b5efcf7fbc4c9c222c815b07e0a6a6a" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" dependencies = [ "autocfg", "num-bigint", @@ -72,9 +78,9 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.14" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" dependencies = [ "autocfg", ] @@ -85,15 +91,47 @@ version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be5e13c266502aadf83426d87d81a0f5d1ef45b8027f5a471c360abfe4bfae92" +[[package]] +name = "ryu" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" + +[[package]] +name = "serde" +version = "1.0.144" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860" + +[[package]] +name = "serde-json-wasm" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "042ac496d97e5885149d34139bad1d617192770d7eb8f1866da2317ff4501853" +dependencies = [ + "serde", +] + +[[package]] +name = "serde_json" +version = "1.0.85" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "sgx_alloc" version = "1.1.4" -source = "git+https://github.com/apache/teaclave-sgx-sdk.git#565960cd7b4b36d1188459d75652619971c43f7e" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" [[package]] name = "sgx_backtrace_sys" version = "1.1.4" -source = "git+https://github.com/apache/teaclave-sgx-sdk.git#565960cd7b4b36d1188459d75652619971c43f7e" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" dependencies = [ "cc", "sgx_build_helper", @@ -103,17 +141,17 @@ dependencies = [ [[package]] name = "sgx_build_helper" version = "1.1.4" -source = "git+https://github.com/apache/teaclave-sgx-sdk.git#565960cd7b4b36d1188459d75652619971c43f7e" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" [[package]] name = "sgx_demangle" version = "1.1.4" -source = "git+https://github.com/apache/teaclave-sgx-sdk.git#565960cd7b4b36d1188459d75652619971c43f7e" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" [[package]] name = "sgx_libc" version = "1.1.4" -source = "git+https://github.com/apache/teaclave-sgx-sdk.git#565960cd7b4b36d1188459d75652619971c43f7e" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" dependencies = [ "sgx_types", ] @@ -121,7 +159,7 @@ dependencies = [ [[package]] name = "sgx_serialize" version = "1.1.4" -source = "git+https://github.com/apache/teaclave-sgx-sdk.git#565960cd7b4b36d1188459d75652619971c43f7e" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" dependencies = [ "sgx_tstd", ] @@ -129,7 +167,7 @@ dependencies = [ [[package]] name = "sgx_tcrypto" version = "1.1.4" -source = "git+https://github.com/apache/teaclave-sgx-sdk.git#565960cd7b4b36d1188459d75652619971c43f7e" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" dependencies = [ "sgx_types", ] @@ -137,7 +175,7 @@ dependencies = [ [[package]] name = "sgx_tprotected_fs" version = "1.1.4" -source = "git+https://github.com/apache/teaclave-sgx-sdk.git#565960cd7b4b36d1188459d75652619971c43f7e" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" dependencies = [ "sgx_trts", "sgx_types", @@ -146,7 +184,7 @@ dependencies = [ [[package]] name = "sgx_trts" version = "1.1.4" -source = "git+https://github.com/apache/teaclave-sgx-sdk.git#565960cd7b4b36d1188459d75652619971c43f7e" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" dependencies = [ "sgx_libc", "sgx_types", @@ -155,7 +193,7 @@ dependencies = [ [[package]] name = "sgx_tse" version = "1.1.4" -source = "git+https://github.com/apache/teaclave-sgx-sdk.git#565960cd7b4b36d1188459d75652619971c43f7e" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" dependencies = [ "sgx_types", ] @@ -163,7 +201,7 @@ dependencies = [ [[package]] name = "sgx_tseal" version = "1.1.4" -source = "git+https://github.com/apache/teaclave-sgx-sdk.git#565960cd7b4b36d1188459d75652619971c43f7e" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" dependencies = [ "sgx_tcrypto", "sgx_trts", @@ -174,7 +212,7 @@ dependencies = [ [[package]] name = "sgx_tstd" version = "1.1.4" -source = "git+https://github.com/apache/teaclave-sgx-sdk.git#565960cd7b4b36d1188459d75652619971c43f7e" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" dependencies = [ "hashbrown_tstd", "sgx_alloc", @@ -190,12 +228,12 @@ dependencies = [ [[package]] name = "sgx_types" version = "1.1.4" -source = "git+https://github.com/apache/teaclave-sgx-sdk.git#565960cd7b4b36d1188459d75652619971c43f7e" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" [[package]] name = "sgx_unwind" version = "1.1.4" -source = "git+https://github.com/apache/teaclave-sgx-sdk.git#565960cd7b4b36d1188459d75652619971c43f7e" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" dependencies = [ "sgx_build_helper", ] @@ -204,6 +242,8 @@ dependencies = [ name = "template" version = "1.0.0" dependencies = [ + "serde-json-wasm", + "serde_json", "sgx_alloc", "sgx_libc", "sgx_serialize", diff --git a/enclave/Cargo.toml b/enclave/Cargo.toml index 5b9cbd7..34e6852 100644 --- a/enclave/Cargo.toml +++ b/enclave/Cargo.toml @@ -13,15 +13,17 @@ default = [] [dependencies] wasmi-impl = { path = "../wasmi-impl" } wasmi = { version = "0.11.0", default-features = false } +serde-json-wasm = "0.3.2" +serde_json = { version = "1.0", default-features = false, features = ["alloc"] } [target.'cfg(not(target_env = "sgx"))'.dependencies] # You can remove what you don't need, except types and tstd -sgx_types = { git = "https://github.com/apache/teaclave-sgx-sdk.git" } -sgx_tstd = { git = "https://github.com/apache/teaclave-sgx-sdk.git", features = ["backtrace"] } -sgx_tcrypto = { git = "https://github.com/apache/teaclave-sgx-sdk.git" } -sgx_trts = { git = "https://github.com/apache/teaclave-sgx-sdk.git" } -sgx_tseal = { git = "https://github.com/apache/teaclave-sgx-sdk.git" } -sgx_serialize = { git = "https://github.com/apache/teaclave-sgx-sdk.git" } -sgx_alloc = { git = "https://github.com/apache/teaclave-sgx-sdk.git" } -sgx_libc = { git = "https://github.com/apache/teaclave-sgx-sdk.git" } +sgx_types = { git = "https://github.com/apache/teaclave-sgx-sdk.git" , rev = "e8a9fc22939befa27ff67f5509b2c2dfe8499945" } +sgx_tstd = { git = "https://github.com/apache/teaclave-sgx-sdk.git", features = ["backtrace"], rev = "e8a9fc22939befa27ff67f5509b2c2dfe8499945" } +sgx_tcrypto = { git = "https://github.com/apache/teaclave-sgx-sdk.git" , rev = "e8a9fc22939befa27ff67f5509b2c2dfe8499945" } +sgx_trts = { git = "https://github.com/apache/teaclave-sgx-sdk.git" , rev = "e8a9fc22939befa27ff67f5509b2c2dfe8499945" } +sgx_tseal = { git = "https://github.com/apache/teaclave-sgx-sdk.git" , rev = "e8a9fc22939befa27ff67f5509b2c2dfe8499945" } +sgx_serialize = { git = "https://github.com/apache/teaclave-sgx-sdk.git" , rev = "e8a9fc22939befa27ff67f5509b2c2dfe8499945" } +sgx_alloc = { git = "https://github.com/apache/teaclave-sgx-sdk.git" , rev = "e8a9fc22939befa27ff67f5509b2c2dfe8499945" } +sgx_libc = { git = "https://github.com/apache/teaclave-sgx-sdk.git" , rev = "e8a9fc22939befa27ff67f5509b2c2dfe8499945" } [patch.'https://github.com/apache/teaclave-sgx-sdk.git'] \ No newline at end of file diff --git a/enclave/Enclave.config.xml b/enclave/Enclave.config.xml index 67acf14..9f3b182 100644 --- a/enclave/Enclave.config.xml +++ b/enclave/Enclave.config.xml @@ -2,8 +2,8 @@ 0 0 - 0x640000 - 0x6400000 + 0x19999000 + 0x99990000 1 1 0 diff --git a/enclave/Enclave.edl b/enclave/Enclave.edl index 0963254..34b7947 100644 --- a/enclave/Enclave.edl +++ b/enclave/Enclave.edl @@ -26,7 +26,7 @@ enclave { trusted { /* ECALLs */ - public sgx_status_t exec_wasm_test([in, count=binary_len] const uint8_t* binary, size_t binary_len, [out] int32_t* result_out); + public sgx_status_t exec_wasm_test([in, count=binary_len] const uint8_t* binary, size_t binary_len, [out] double* result_out); }; untrusted { diff --git a/enclave/src/lib.rs b/enclave/src/lib.rs index 27fd797..ccea472 100644 --- a/enclave/src/lib.rs +++ b/enclave/src/lib.rs @@ -24,14 +24,15 @@ extern crate sgx_types; #[cfg(not(target_env = "sgx"))] -#[macro_use] extern crate sgx_tstd as std; extern crate wasmi; extern crate wasmi_impl; +extern crate serde_json; use sgx_types::*; -use std::io::{self, Write}; use std::slice; +use std::vec; +use std::{format}; /// # Safety /// The caller needs to ensure that `binary` is a valid pointer to a slice valid for `binary_len` items @@ -40,17 +41,23 @@ use std::slice; pub unsafe extern "C" fn exec_wasm_test( binary: *const u8, binary_len: usize, - result_out: *mut i32, + result_out: *mut f64, ) -> sgx_status_t { if binary.is_null() { return sgx_status_t::SGX_ERROR_INVALID_PARAMETER; } // Safety: SGX generated code will check that the pointer is valid. let binary_slice = unsafe { slice::from_raw_parts(binary, binary_len) }; - let data = b"[1,2,3,4,5]"; + + // TODO: Unsealing operation - waiting on sealing functionality + + let import_data: vec::Vec = vec![1.24,2.0,3.4,4.7,5.5,10.5]; + let data_string = format!("{:?}",import_data); + let data_slice = data_string.as_str().as_bytes(); + unsafe { - *result_out = match wasmi_impl::exec_wasm_with_data(binary_slice, data) { - Ok(Some(wasmi::RuntimeValue::I32(ret))) => ret, + *result_out = match wasmi_impl::exec_wasm_with_data(binary_slice, data_slice) { + Ok(Some(wasmi::RuntimeValue::F64(ret))) => ret.to_float(), Ok(_) | Err(_) => return sgx_status_t::SGX_ERROR_UNEXPECTED, } }; diff --git a/get-median-wasm/Cargo.lock b/get-median-wasm/Cargo.lock index 789a7b9..c4c4c1f 100644 --- a/get-median-wasm/Cargo.lock +++ b/get-median-wasm/Cargo.lock @@ -2,18 +2,275 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +dependencies = [ + "memchr", +] + +[[package]] +name = "atomic-polyfill" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c041a8d9751a520ee19656232a18971f18946a7900f1520ee4400002244dd89" +dependencies = [ + "critical-section", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bare-metal" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5deb64efa5bd81e31fcd1938615a6d98c82eafcbcd787162b6f63b91d6bac5b3" +dependencies = [ + "rustc_version 0.2.3", +] + +[[package]] +name = "bare-metal" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fe8f5a8a398345e52358e18ff07cc17a568fbca5c6f73873d3a62056309603" + +[[package]] +name = "bit_field" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcb6dd1c2376d2e096796e234a70e17e94cc2d5d54ff8ce42b28cef1d0d359a4" + +[[package]] +name = "bitfield" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719" + +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cortex-m" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70858629a458fdfd39f9675c4dc309411f2a3f83bede76988d81bf1a0ecee9e0" +dependencies = [ + "bare-metal 0.2.5", + "bitfield", + "embedded-hal", + "volatile-register", +] + +[[package]] +name = "critical-section" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95da181745b56d4bd339530ec393508910c909c784e8962d15d722bacf0bcbcd" +dependencies = [ + "bare-metal 1.0.0", + "cfg-if", + "cortex-m", + "riscv", +] + +[[package]] +name = "embedded-hal" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35949884794ad573cf46071e41c9b60efb0cb311e3ca01f7af807af1debc66ff" +dependencies = [ + "nb 0.1.3", + "void", +] + [[package]] name = "get-median-wasm" version = "0.1.0" dependencies = [ + "serde-json-core", "serde-json-wasm", ] +[[package]] +name = "hash32" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67" +dependencies = [ + "byteorder", +] + +[[package]] +name = "heapless" +version = "0.7.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db04bc24a18b9ea980628ecf00e6c0264f3c1426dac36c00cb49b6fbad8b0743" +dependencies = [ + "atomic-polyfill", + "hash32", + "rustc_version 0.4.0", + "spin", + "stable_deref_trait", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "lock_api" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327fa5b6a6940e4699ec49a9beae1ea4845c6bab9314e4f84ac68742139d8c53" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "nb" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "801d31da0513b6ec5214e9bf433a77966320625a37860f910be265be6e18d06f" +dependencies = [ + "nb 1.0.0", +] + +[[package]] +name = "nb" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "546c37ac5d9e56f55e73b677106873d9d9f5190605e41a856503623648488cae" + +[[package]] +name = "regex" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" + +[[package]] +name = "riscv" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6907ccdd7a31012b70faf2af85cd9e5ba97657cc3987c4f13f8e4d2c2a088aba" +dependencies = [ + "bare-metal 1.0.0", + "bit_field", + "riscv-target", +] + +[[package]] +name = "riscv-target" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88aa938cda42a0cf62a20cfe8d139ff1af20c2e681212b5b34adb5a58333f222" +dependencies = [ + "lazy_static", + "regex", +] + +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +dependencies = [ + "semver 0.9.0", +] + +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver 1.0.13", +] + +[[package]] +name = "ryu" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" + +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +dependencies = [ + "semver-parser", +] + +[[package]] +name = "semver" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93f6841e709003d68bb2deee8c343572bf446003ec20a583e76f7b15cebf3711" + +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" + [[package]] name = "serde" -version = "1.0.133" +version = "1.0.143" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97565067517b60e2d1ea8b268e59ce036de907ac523ad83a0475da04e818989a" +checksum = "53e8e5d5b70924f74ff5c6d64d9a5acd91422117c60f48c4e07855238a254553" + +[[package]] +name = "serde-json-core" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8014aeea272bca0f0779778d43253f2f3375b414185b30e6ecc4d3e4a9994781" +dependencies = [ + "heapless", + "ryu", + "serde", +] [[package]] name = "serde-json-wasm" @@ -23,3 +280,39 @@ checksum = "042ac496d97e5885149d34139bad1d617192770d7eb8f1866da2317ff4501853" dependencies = [ "serde", ] + +[[package]] +name = "spin" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f6002a767bff9e83f8eeecf883ecb8011875a21ae8da43bffb817a57e78cc09" +dependencies = [ + "lock_api", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + +[[package]] +name = "vcell" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77439c1b53d2303b20d9459b1ade71a83c716e3f9c34f3228c00e6f185d6c002" + +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" + +[[package]] +name = "volatile-register" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ee8f19f9d74293faf70901bc20ad067dc1ad390d2cbf1e3f75f721ffee908b6" +dependencies = [ + "vcell", +] diff --git a/get-median-wasm/Cargo.toml b/get-median-wasm/Cargo.toml index 9c3bc11..cac94d0 100644 --- a/get-median-wasm/Cargo.toml +++ b/get-median-wasm/Cargo.toml @@ -10,6 +10,7 @@ crate-type = ['cdylib'] [dependencies] serde-json-wasm = "0.3.2" +serde-json-core = "0.4.0" # serde_derive = { version = "1.0.133", default-features = false } # serde = { version = "1.0.133", default-features = false } diff --git a/get-median-wasm/src/lib.rs b/get-median-wasm/src/lib.rs index dbc7bd5..b341b03 100644 --- a/get-median-wasm/src/lib.rs +++ b/get-median-wasm/src/lib.rs @@ -2,21 +2,23 @@ #![deny(unsafe_op_in_unsafe_fn)] use core::slice; -/// Calculates the median of an array of integers passed in as a JSON array. Return value will be rounded down. +/// Calculates the median of an array of floats passed in as a JSON array. /// /// # Safety /// The caller needs to ensure that `msg` is a valid pointer, and points to a slice with `msg_len` items #[no_mangle] -pub unsafe extern "C" fn exec(msg: *const u8, msg_len: u32) -> i32 { +pub unsafe extern "C" fn exec(msg: *const u8, msg_len: u32) -> f64 { let x = unsafe { slice::from_raw_parts(msg, msg_len as usize) }; - let mut val: Vec = serde_json_wasm::from_slice(x).unwrap(); - val.sort_unstable(); + //TODO - Fix error handling + let mut val: Vec = serde_json_core::from_slice(&x).unwrap().0; + + val.sort_by(|a, b| a.partial_cmp(b).unwrap()); let val_len = val.len(); if val_len % 2 == 0 { let mid = val_len / 2; - (val[mid - 1] + val[mid]) / 2 + (val[mid - 1] + val[mid]) / 2.0 } else { let mid = (val_len + 1) / 2; val[mid - 1] @@ -29,17 +31,17 @@ mod tests { #[test] fn it_works_odd() { - let data = b"[8, 6, 3, 3, 7, 1, 9]"; + let data = b"[8.0, 6.0, 3.0, 3.0, 7.0, 1.0, 9.0]"; let res = unsafe { exec(data.as_ptr(), data.len() as u32) }; - assert_eq!(res, 6); + assert_eq!(res, 6.0); } #[test] fn it_works_even() { - let data = b"[5, 2, 3, 1, 7, 6, 4, 9]"; + let data = b"[5.0, 2.0, 3.0, 1.0, 7.0, 6.0, 4.0, 9.0]"; let res = unsafe { exec(data.as_ptr(), data.len() as u32) }; - assert_eq!(res, 4); + assert_eq!(res, 4.5); } } diff --git a/wasmi-impl/src/lib.rs b/wasmi-impl/src/lib.rs index 9b598a7..7b28fd5 100644 --- a/wasmi-impl/src/lib.rs +++ b/wasmi-impl/src/lib.rs @@ -1,6 +1,6 @@ #![no_std] use wasmi::{ - self, memory_units::Pages, ExternVal, ImportsBuilder, MemoryInstance, ModuleInstance, + self, memory_units::{Pages}, ExternVal, ImportsBuilder, MemoryInstance, ModuleInstance, NopExternals, RuntimeValue, }; @@ -24,7 +24,7 @@ pub fn exec_wasm_with_data( let module = wasmi::Module::from_buffer(binary)?; // TODO: Calculate the memory size always be larger than `data` - let mem_instance = MemoryInstance::alloc(Pages(200), None)?; + let mem_instance = MemoryInstance::alloc(Pages(13000), None)?; // TODO: Error Handling mem_instance.set(0, data).unwrap();