diff --git a/Makefile b/Makefile index 30c21ec..2b0e1e2 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,7 @@ SRC_WASM_MEAN_INT= get-mean-int-wasm/ SRC_WASM_MEAN_FLOAT= get-mean-float-wasm/ SRC_WASM_SD_INT= get-sd-int-wasm/ SRC_WASM_SD_FLOAT= get-sd-float-wasm/ +SRC_WASM_APPEND= wasm-append/ # Compilation process, will call the appropriate makefiles. @@ -32,6 +33,7 @@ wasm: @make -C $(SRC_WASM_MEAN_FLOAT) @make -C $(SRC_WASM_SD_INT) @make -C $(SRC_WASM_SD_FLOAT) + @make -C $(SRC_WASM_APPEND) clean: @make -C $(SRC_U) clean @@ -42,6 +44,7 @@ clean: @make -C $(SRC_WASM_MEAN_FLOAT) clean @make -C $(SRC_WASM_SD_INT) clean @make -C $(SRC_WASM_SD_FLOAT) clean + @make -C $(SRC_WASM_APPEND) clean fclean: @make -C $(SRC_U) fclean diff --git a/app/Cargo.lock b/app/Cargo.lock new file mode 100644 index 0000000..2e3d6c5 --- /dev/null +++ b/app/Cargo.lock @@ -0,0 +1,151 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "app" +version = "1.0.0" +dependencies = [ + "sgx_types", + "sgx_urts", + "wabt", +] + +[[package]] +name = "cc" +version = "1.0.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee" + +[[package]] +name = "cmake" +version = "0.1.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8ad8cef104ac57b68b89df3208164d228503abbdce70f6880ffa3d970e7443a" +dependencies = [ + "cc", +] + +[[package]] +name = "glob" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" + +[[package]] +name = "itoa" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" + +[[package]] +name = "libc" +version = "0.2.112" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" + +[[package]] +name = "proc-macro2" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47aa80447ce4daf1717500037052af176af5d38cc3e571d9ec1c7353fc10c87d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" + +[[package]] +name = "serde" +version = "1.0.133" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97565067517b60e2d1ea8b268e59ce036de907ac523ad83a0475da04e818989a" + +[[package]] +name = "serde_derive" +version = "1.0.133" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed201699328568d8d08208fdd080e3ff594e6c422e438b6705905da01005d537" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee2bb9cd061c5865d345bb02ca49fcef1391741b672b54a0bf7b679badec3142" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sgx_types" +version = "1.1.4" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git#565960cd7b4b36d1188459d75652619971c43f7e" + +[[package]] +name = "sgx_urts" +version = "1.1.4" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git#565960cd7b4b36d1188459d75652619971c43f7e" +dependencies = [ + "libc", + "sgx_types", +] + +[[package]] +name = "syn" +version = "1.0.85" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a684ac3dcd8913827e18cd09a68384ee66c1de24157e3c556c9ab16d85695fb7" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" + +[[package]] +name = "wabt" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00bef93d5e6c81a293bccf107cf43aa47239382f455ba14869d36695d8963b9c" +dependencies = [ + "serde", + "serde_derive", + "serde_json", + "wabt-sys", +] + +[[package]] +name = "wabt-sys" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a4e043159f63e16986e713e9b5e1c06043df4848565bf672e27c523864c7791" +dependencies = [ + "cc", + "cmake", + "glob", +] \ No newline at end of file diff --git a/app/src/main.rs b/app/src/main.rs index 849be2f..93cfcf5 100644 --- a/app/src/main.rs +++ b/app/src/main.rs @@ -31,6 +31,8 @@ static WASM_FILE_MEAN_FLOAT: &str = "get_mean_float.wasm"; static WASM_FILE_SD_INT: &str = "get_sd_int.wasm"; static WASM_FILE_SD_FLOAT: &str = "get_sd_float.wasm"; +static WASM_FILE_APPEND: &str = "wasm_append.wasm"; + static ENCLAVE_FILE: &str = "enclave.signed.so"; extern "C" { @@ -83,6 +85,14 @@ extern "C" { result_out: *mut f32, ) -> sgx_status_t; + fn exec_wasm_append( + eid: sgx_enclave_id_t, + retval: *mut sgx_status_t, + binary: *const u8, + binary_len: usize, + result_out: *mut i32, + ) -> sgx_status_t; + } fn init_enclave() -> SgxResult { @@ -127,6 +137,8 @@ fn main() { let binary_sd_int = fs::read(WASM_FILE_SD_INT).unwrap(); let binary_sd_float = fs::read(WASM_FILE_SD_FLOAT).unwrap(); + let binary_wasm_append = fs::read(WASM_FILE_APPEND).unwrap(); + let mut result_out_median_int = 0i32; let mut result_out_median_float = 0f32; @@ -136,6 +148,8 @@ fn main() { let mut result_out_sd_int = 0f32; let mut result_out_sd_float = 0f32; + let mut result_append_out = 0i32; + let result = unsafe { exec_wasm_median_int( enclave.geteid(), @@ -183,6 +197,14 @@ fn main() { binary_sd_float.as_ptr(), binary_sd_float.len(), &mut result_out_sd_float, + ); + + exec_wasm_append( + enclave.geteid(), + &mut retval, + binary_wasm_append.as_ptr(), + binary_wasm_append.len(), + &mut result_append_out, ) }; @@ -202,6 +224,8 @@ fn main() { println!(); println!("[+] ecall_test success, SD Int result - {:?}", result_out_sd_int); println!("[+] ecall_test success, SD Float result - {:?}", result_out_sd_float); + println!(); + println!("[+] ecall_test success, WASM Append successful - {:?}", result_append_out); enclave.destroy(); } diff --git a/enclave/Cargo.lock b/enclave/Cargo.lock new file mode 100644 index 0000000..5a565be --- /dev/null +++ b/enclave/Cargo.lock @@ -0,0 +1,248 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "cc" +version = "1.0.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9f73505338f7d905b19d18738976aae232eb46b8efc15554ffc56deb5d9ebe4" + +[[package]] +name = "downcast-rs" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" + +[[package]] +name = "hashbrown_tstd" +version = "0.11.2" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" + +[[package]] +name = "libm" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "348108ab3fba42ec82ff6e9564fc4ca0247bdccdc68dd8af9764bbc79c3c8ffb" + +[[package]] +name = "memory_units" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d96e3f3c0b6325d8ccd83c33b28acb183edcb6c67938ba104ec546854b0882" + +[[package]] +name = "num-bigint" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + +[[package]] +name = "parity-wasm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be5e13c266502aadf83426d87d81a0f5d1ef45b8027f5a471c360abfe4bfae92" + +[[package]] +name = "sgx_alloc" +version = "1.1.4" +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?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" +dependencies = [ + "cc", + "sgx_build_helper", + "sgx_libc", +] + +[[package]] +name = "sgx_build_helper" +version = "1.1.4" +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?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" + +[[package]] +name = "sgx_libc" +version = "1.1.4" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" +dependencies = [ + "sgx_types", +] + +[[package]] +name = "sgx_serialize" +version = "1.1.4" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" +dependencies = [ + "sgx_tstd", +] + +[[package]] +name = "sgx_tcrypto" +version = "1.1.4" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" +dependencies = [ + "sgx_types", +] + +[[package]] +name = "sgx_tprotected_fs" +version = "1.1.4" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" +dependencies = [ + "sgx_trts", + "sgx_types", +] + +[[package]] +name = "sgx_trts" +version = "1.1.4" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" +dependencies = [ + "sgx_libc", + "sgx_types", +] + +[[package]] +name = "sgx_tse" +version = "1.1.4" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" +dependencies = [ + "sgx_types", +] + +[[package]] +name = "sgx_tseal" +version = "1.1.4" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" +dependencies = [ + "sgx_tcrypto", + "sgx_trts", + "sgx_tse", + "sgx_types", +] + +[[package]] +name = "sgx_tstd" +version = "1.1.4" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" +dependencies = [ + "hashbrown_tstd", + "sgx_alloc", + "sgx_backtrace_sys", + "sgx_demangle", + "sgx_libc", + "sgx_tprotected_fs", + "sgx_trts", + "sgx_types", + "sgx_unwind", +] + +[[package]] +name = "sgx_types" +version = "1.1.4" +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?rev=e8a9fc22939befa27ff67f5509b2c2dfe8499945#e8a9fc22939befa27ff67f5509b2c2dfe8499945" +dependencies = [ + "sgx_build_helper", +] + +[[package]] +name = "template" +version = "1.0.0" +dependencies = [ + "sgx_alloc", + "sgx_libc", + "sgx_serialize", + "sgx_tcrypto", + "sgx_trts", + "sgx_tseal", + "sgx_tstd", + "sgx_types", + "wasmi", + "wasmi-impl", +] + +[[package]] +name = "wasmi" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a3cb58f98e4d6c944af18c2c9002f22d0b928dfbb8b6c2b7d78a8573a5216bf" +dependencies = [ + "downcast-rs", + "libm", + "memory_units", + "num-rational", + "num-traits", + "parity-wasm", + "wasmi-validation", +] + +[[package]] +name = "wasmi-impl" +version = "0.1.0" +dependencies = [ + "wasmi", +] + +[[package]] +name = "wasmi-validation" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "165343ecd6c018fc09ebcae280752702c9a2ef3e6f8d02f1cfcbdb53ef6d7937" +dependencies = [ + "parity-wasm", +] diff --git a/enclave/Enclave.edl b/enclave/Enclave.edl index 03b9fa0..26f6343 100644 --- a/enclave/Enclave.edl +++ b/enclave/Enclave.edl @@ -32,6 +32,7 @@ enclave { public sgx_status_t exec_wasm_mean_float([in, count=binary_len] const uint8_t* binary_mean_float, size_t binary_len, [out] float* result_out_mean_float); public sgx_status_t exec_wasm_sd_int([in, count=binary_len] const uint8_t* binary_sd_int, size_t binary_len, [out] float* result_out_sd_int); public sgx_status_t exec_wasm_sd_float([in, count=binary_len] const uint8_t* binary_sd_float, size_t binary_len, [out] float* result_out_sd_float); + public sgx_status_t exec_wasm_append([in, count=binary_len] const uint8_t* binary_wasm_append, size_t binary_len, [out] int32_t* result_wasm_append); }; untrusted { diff --git a/enclave/src/lib.rs b/enclave/src/lib.rs index 7e64bf6..4adffe3 100644 --- a/enclave/src/lib.rs +++ b/enclave/src/lib.rs @@ -175,4 +175,29 @@ pub unsafe extern "C" fn exec_wasm_sd_float( } }; sgx_status_t::SGX_SUCCESS +} + +/// # Safety +/// The caller needs to ensure that `binary` is a valid pointer to a slice valid for `binary_len` items +/// and that `result_out` is a valid pointer. +#[no_mangle] +pub unsafe extern "C" fn exec_wasm_append( + binary: *const u8, + binary_len: usize, + result_out: *mut i32, +) -> 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]"; + let data_2 = b"[6,7,8,9,10]"; + unsafe { + *result_out = match wasmi_impl::exec_wasm_append_data(binary_slice, data, data_2) { + Ok(Some(wasmi::RuntimeValue::I32(ret))) => ret, + Ok(_) | Err(_) => return sgx_status_t::SGX_ERROR_UNEXPECTED, + } + }; + sgx_status_t::SGX_SUCCESS } \ No newline at end of file diff --git a/wasm-append/.cargo/config.toml b/wasm-append/.cargo/config.toml new file mode 100644 index 0000000..bc11ae2 --- /dev/null +++ b/wasm-append/.cargo/config.toml @@ -0,0 +1,4 @@ +[target.wasm32-unknown-unknown] +rustflags = [ + "-C", "link-args=--import-memory", +] \ No newline at end of file diff --git a/wasm-append/Cargo.lock b/wasm-append/Cargo.lock new file mode 100644 index 0000000..a7e0990 --- /dev/null +++ b/wasm-append/Cargo.lock @@ -0,0 +1,318 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4f55bd91a0978cbfd91c457a164bab8b4001c833b7f323132c0a4e1922dd44e" +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 = "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.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" +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.14", +] + +[[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.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e25dfac463d778e353db5be2449d1cce89bd6fd23c9f1ea21310ce6e5a1b29c4" + +[[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" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97565067517b60e2d1ea8b268e59ce036de907ac523ad83a0475da04e818989a" + +[[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" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +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", +] + +[[package]] +name = "wasm_append" +version = "0.1.0" +dependencies = [ + "serde-json-core", + "serde-json-wasm", +] diff --git a/wasm-append/Cargo.toml b/wasm-append/Cargo.toml new file mode 100644 index 0000000..e32f4f2 --- /dev/null +++ b/wasm-append/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "wasm_append" +version = "0.1.0" +edition = "2018" + +[lib] +crate-type = ['cdylib'] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[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 } + +[profile.release] +lto = true +opt-level = 's' diff --git a/wasm-append/Makefile b/wasm-append/Makefile new file mode 100644 index 0000000..44b3477 --- /dev/null +++ b/wasm-append/Makefile @@ -0,0 +1,18 @@ + +SRC_WASM = ./ +SRC_RST = ./src/ +BIN = ../bin/ +APP_WASM = wasm_append.wasm +BIN_WASM = $(addprefix $(BIN), $(APP_WASM)) +FILES_RUST_F= $(wildcard $(SRC_RST)*.rs) # Wildcard function used, no need to specify the rust files. Safe as we don't compile the rust files with the makefile. +TOML = Cargo.toml +all: $(BIN_WASM) + +$(BIN_WASM): $(FILES_RUST_F) $(TOML) + @echo "\033[32mStarting cargo to build the append wasm binary...\033[0m" + @cd $(SRC_WASM) && cargo build --target wasm32-unknown-unknown --release + @cp $(SRC_WASM)/target/wasm32-unknown-unknown/release/wasm_append.wasm $(BIN)/$(APP_WASM) + +clean: + @cargo clean + @rm -f $(BIN_WASM) diff --git a/wasm-append/rust-toolchain.toml b/wasm-append/rust-toolchain.toml new file mode 100644 index 0000000..35d7ded --- /dev/null +++ b/wasm-append/rust-toolchain.toml @@ -0,0 +1,3 @@ +[toolchain] +channel = "nightly-2021-11-01" +targets = [ "wasm32-unknown-unknown" ] diff --git a/wasm-append/src/lib.rs b/wasm-append/src/lib.rs new file mode 100644 index 0000000..a065886 --- /dev/null +++ b/wasm-append/src/lib.rs @@ -0,0 +1,40 @@ +#![deny(clippy::mem_forget)] +#![deny(unsafe_op_in_unsafe_fn)] +use core::slice; + +/// Calculates the mean 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_append(msg_1: *const u8, msg_len_1: u32, msg_2: *const u8, msg_len_2: u32) -> i32 { + let x = unsafe { slice::from_raw_parts(msg_1, msg_len_1 as usize) }; + let y = unsafe { slice::from_raw_parts(msg_2, msg_len_2 as usize) }; + + let mut val_1: Vec = serde_json_wasm::from_slice(x).unwrap(); + let mut val_2: Vec = serde_json_wasm::from_slice(y).unwrap(); + + val_1.append(&mut val_2); + + let total = val_1.iter().sum(); + + // Export val_1 to CosmosDB + + // Update this to return 1 (Bool) when successful + return total +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn wasm_append_works() { + let data_1 = b"[1, 2, 3, 4, 5]"; + let data_2 = b"[6, 7, 8, 9, 10]"; + + let res = unsafe { exec_append(data_1.as_ptr(), data_1.len() as u32, data_2.as_ptr(), data_2.len() as u32) }; + assert_eq!(res, 55); + } + +} diff --git a/wasmi-impl/src/lib.rs b/wasmi-impl/src/lib.rs index 81b331c..e67ca88 100644 --- a/wasmi-impl/src/lib.rs +++ b/wasmi-impl/src/lib.rs @@ -5,6 +5,7 @@ use wasmi::{ }; static ENTRYPOINT: &str = "exec"; +static ENTRYPOINT_APPEND: &str = "exec_append"; #[derive(Debug)] pub enum ExecWasmError { @@ -17,6 +18,32 @@ impl From for ExecWasmError { } } +pub fn exec_wasm_append_data( + binary: &[u8], + data: &[u8], + data_2: &[u8], +) -> Result, ExecWasmError> { + let module = wasmi::Module::from_buffer(binary)?; + + // TODO: Calculate the memory size always be larger than `data` + let mem_instance = MemoryInstance::alloc(Pages(1000), None)?; + + // TODO: Error Handling + mem_instance.set(0, data).unwrap(); + mem_instance.set(data.len() as u32, data_2).unwrap(); + + let imports = [ExternVal::Memory(mem_instance)]; + + // TODO: This panics. We probably want to run start functions + let instance = ModuleInstance::with_externvals(&module, imports.iter())?.assert_no_start(); + + Ok(instance.invoke_export( + ENTRYPOINT_APPEND, + &[RuntimeValue::I32(0), RuntimeValue::I32(data.len() as i32), RuntimeValue::I32(data.len() as i32), RuntimeValue::I32(data_2.len() as i32)], + &mut NopExternals, + )?) +} + pub fn exec_wasm_with_data( binary: &[u8], data: &[u8],