diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 088efd8a8..51e8bb207 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -21,16 +21,6 @@ updates: - dependency-name: "mshv-bindings" versions: [ ">=0.2.1" ] open-pull-requests-limit: 20 - # Excluded workspace members - guests with custom linker flags - - package-ecosystem: "cargo" - directory: "/src/tests/rust_guests/callbackguest" - schedule: - interval: "daily" - time: "03:00" - labels: - - "kind/dependencies" - - "area/guest" - open-pull-requests-limit: 5 - package-ecosystem: "cargo" directory: "/src/tests/rust_guests/dummyguest" schedule: diff --git a/.github/workflows/Benchmarks.yml b/.github/workflows/Benchmarks.yml index 45b782025..048b788cb 100644 --- a/.github/workflows/Benchmarks.yml +++ b/.github/workflows/Benchmarks.yml @@ -48,10 +48,8 @@ jobs: - name: Copy Guest Binaries run: | - cp ./downloaded-rust-guest-binaries-release/callbackguest ./src/tests/rust_guests/bin/release/callbackguest cp ./downloaded-rust-guest-binaries-release/simpleguest ./src/tests/rust_guests/bin/release/simpleguest cp ./downloaded-rust-guest-binaries-release/dummyguest ./src/tests/rust_guests/bin/release/dummyguest - cp ./downloaded-c-guest-binaries-release/callbackguest ./src/tests/c_guests/bin/release/callbackguest cp ./downloaded-c-guest-binaries-release/simpleguest ./src/tests/c_guests/bin/release/simpleguest ### Benchmarks ### diff --git a/.github/workflows/dep_build_guest_binaries.yml b/.github/workflows/dep_build_guest_binaries.yml index 89ebf4dca..2088d0ae5 100644 --- a/.github/workflows/dep_build_guest_binaries.yml +++ b/.github/workflows/dep_build_guest_binaries.yml @@ -45,7 +45,6 @@ jobs: with: name: rust-guest-binaries-${{ matrix.config }} path: | - src\tests\rust_guests\bin\${{ matrix.config }}\callbackguest src\tests\rust_guests\bin\${{ matrix.config }}\dummyguest src\tests\rust_guests\bin\${{ matrix.config }}\simpleguest if-no-files-found: error @@ -55,6 +54,5 @@ jobs: with: name: c-guest-binaries-${{ matrix.config }} path: | - src\tests\c_guests\bin\${{ matrix.config }}\callbackguest src\tests\c_guests\bin\${{ matrix.config }}\simpleguest if-no-files-found: error diff --git a/.vscode/settings.json b/.vscode/settings.json index 1ec1edf0e..3d6d12789 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,6 +3,5 @@ "Cargo.toml", // guest crates for testing, not part of the workspace "src/tests/rust_guests/simpleguest/Cargo.toml", - "src/tests/rust_guests/callbackguest/Cargo.toml" ] } \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index d5f9a4092..8bccc53d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,6 @@ members = [ ] # Guests have custom linker flags, so we need to exclude them from the workspace exclude = [ - "src/tests/rust_guests/callbackguest", "src/tests/rust_guests/dummyguest", "src/tests/rust_guests/simpleguest", "src/tests/rust_guests/witguest", diff --git a/Justfile b/Justfile index aed82725d..6e8218958 100644 --- a/Justfile +++ b/Justfile @@ -11,7 +11,6 @@ root := justfile_directory() default-target := "debug" simpleguest_source := "src/tests/rust_guests/simpleguest/target/x86_64-unknown-none" dummyguest_source := "src/tests/rust_guests/dummyguest/target/x86_64-unknown-none" -callbackguest_source := "src/tests/rust_guests/callbackguest/target/x86_64-unknown-none" witguest_source := "src/tests/rust_guests/witguest/target/x86_64-unknown-none" rust_guests_bin_dir := "src/tests/rust_guests/bin" @@ -39,13 +38,11 @@ witguest-wit: cd src/tests/rust_guests/witguest && wasm-tools component wit guest.wit -w -o interface.wasm build-rust-guests target=default-target features="": (witguest-wit) - cd src/tests/rust_guests/callbackguest && cargo build {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} cd src/tests/rust_guests/simpleguest && cargo build {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} cd src/tests/rust_guests/dummyguest && cargo build {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} cd src/tests/rust_guests/witguest && cargo build {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} @move-rust-guests target=default-target: - cp {{ callbackguest_source }}/{{ target }}/callbackguest* {{ rust_guests_bin_dir }}/{{ target }}/ cp {{ simpleguest_source }}/{{ target }}/simpleguest* {{ rust_guests_bin_dir }}/{{ target }}/ cp {{ dummyguest_source }}/{{ target }}/dummyguest* {{ rust_guests_bin_dir }}/{{ target }}/ cp {{ witguest_source }}/{{ target }}/witguest* {{ rust_guests_bin_dir }}/{{ target }}/ @@ -59,7 +56,6 @@ clean-rust: cargo clean cd src/tests/rust_guests/simpleguest && cargo clean cd src/tests/rust_guests/dummyguest && cargo clean - cd src/tests/rust_guests/callbackguest && cargo clean {{ if os() == "windows" { "cd src/tests/rust_guests/witguest -ErrorAction SilentlyContinue; cargo clean" } else { "[ -d src/tests/rust_guests/witguest ] && cd src/tests/rust_guests/witguest && cargo clean || true" } }} {{ if os() == "windows" { "Remove-Item src/tests/rust_guests/witguest/interface.wasm -Force -ErrorAction SilentlyContinue" } else { "rm -f src/tests/rust_guests/witguest/interface.wasm" } }} git clean -fdx src/tests/c_guests/bin src/tests/rust_guests/bin @@ -229,7 +225,6 @@ check: fmt-check: cargo +nightly fmt --all -- --check - cargo +nightly fmt --manifest-path src/tests/rust_guests/callbackguest/Cargo.toml -- --check cargo +nightly fmt --manifest-path src/tests/rust_guests/simpleguest/Cargo.toml -- --check cargo +nightly fmt --manifest-path src/tests/rust_guests/dummyguest/Cargo.toml -- --check cargo +nightly fmt --manifest-path src/tests/rust_guests/witguest/Cargo.toml -- --check @@ -240,7 +235,6 @@ check-license-headers: fmt-apply: cargo +nightly fmt --all - cargo +nightly fmt --manifest-path src/tests/rust_guests/callbackguest/Cargo.toml cargo +nightly fmt --manifest-path src/tests/rust_guests/simpleguest/Cargo.toml cargo +nightly fmt --manifest-path src/tests/rust_guests/dummyguest/Cargo.toml cargo +nightly fmt --manifest-path src/tests/rust_guests/witguest/Cargo.toml @@ -251,7 +245,6 @@ clippy target=default-target: (witguest-wit) clippy-guests target=default-target: (witguest-wit) cd src/tests/rust_guests/simpleguest && cargo clippy --profile={{ if target == "debug" { "dev" } else { target } }} -- -D warnings - cd src/tests/rust_guests/callbackguest && cargo clippy --profile={{ if target == "debug" { "dev" } else { target } }} -- -D warnings cd src/tests/rust_guests/witguest && cargo clippy --profile={{ if target == "debug" { "dev" } else { target } }} -- -D warnings clippy-apply-fix-unix: diff --git a/c.just b/c.just index d83776cc5..aeed36808 100644 --- a/c.just +++ b/c.just @@ -16,14 +16,11 @@ build-rust-capi target=default-target: compile-c-guest target=default-target: # elf cd src/tests/c_guests/c_simpleguest && {{ mkdir }} "./out/{{target}}" && clang -c {{ c-compile-options-elf }} {{ if target == "debug" { c-flags-debug-elf } else { c-flags-release-elf } }} main.c {{c-include-flags-elf}} -o "out/{{ target }}/main.o" - cd src/tests/c_guests/c_callbackguest && {{ mkdir }} "./out/{{target}}" && clang -c {{ c-compile-options-elf }} {{ if target == "debug" { c-flags-debug-elf } else { c-flags-release-elf } }} main.c {{c-include-flags-elf}} -o "out/{{ target }}/main.o" link-c-guest target=default-target: # elf cd src/tests/c_guests/c_simpleguest && ld.lld -o out/{{target}}/simpleguest {{c-linker-options-elf}} out/{{target}}/main.o -l hyperlight_guest_capi -L "{{justfile_directory()}}/target/x86_64-unknown-none/{{target}}" - cd src/tests/c_guests/c_callbackguest && ld.lld -o out/{{target}}/callbackguest {{c-linker-options-elf}} out/{{target}}/main.o -l hyperlight_guest_capi -L "{{justfile_directory()}}/target/x86_64-unknown-none/{{target}}" move-c-guests target=default-target: # elf cp src/tests/c_guests/c_simpleguest/out/{{target}}/simpleguest src/tests/c_guests/bin/{{target}}/ - cp src/tests/c_guests/c_callbackguest/out/{{target}}/callbackguest src/tests/c_guests/bin/{{target}}/ diff --git a/src/hyperlight_host/src/lib.rs b/src/hyperlight_host/src/lib.rs index 9a32a9ecc..e1cc2e5a2 100644 --- a/src/hyperlight_host/src/lib.rs +++ b/src/hyperlight_host/src/lib.rs @@ -81,8 +81,7 @@ pub(crate) mod seccomp; /// Signal handling for Linux #[cfg(target_os = "linux")] pub(crate) mod signal_handlers; -/// Utilities for testing including interacting with `simpleguest.exe` -/// and `callbackguest.exe`, our two most basic guest binaries for testing +/// Utilities for testing including interacting with `simpleguest` testing guest binary #[cfg(test)] pub(crate) mod testing; diff --git a/src/hyperlight_host/src/sandbox/uninitialized_evolve.rs b/src/hyperlight_host/src/sandbox/uninitialized_evolve.rs index dc84c97a3..e3334fb3e 100644 --- a/src/hyperlight_host/src/sandbox/uninitialized_evolve.rs +++ b/src/hyperlight_host/src/sandbox/uninitialized_evolve.rs @@ -271,7 +271,7 @@ pub(crate) fn set_up_hypervisor_partition( #[cfg(test)] mod tests { - use hyperlight_testing::{callback_guest_as_string, simple_guest_as_string}; + use hyperlight_testing::simple_guest_as_string; use super::evolve_impl_multi_use; use crate::UninitializedSandbox; @@ -279,10 +279,7 @@ mod tests { #[test] fn test_evolve() { - let guest_bin_paths = vec![ - simple_guest_as_string().unwrap(), - callback_guest_as_string().unwrap(), - ]; + let guest_bin_paths = vec![simple_guest_as_string().unwrap()]; for guest_bin_path in guest_bin_paths { let u_sbox = UninitializedSandbox::new(GuestBinary::FilePath(guest_bin_path.clone()), None) diff --git a/src/hyperlight_host/tests/common/mod.rs b/src/hyperlight_host/tests/common/mod.rs index 39d08a3e5..ab1a97b71 100644 --- a/src/hyperlight_host/tests/common/mod.rs +++ b/src/hyperlight_host/tests/common/mod.rs @@ -15,10 +15,7 @@ limitations under the License. */ use hyperlight_host::func::HostFunction; use hyperlight_host::{GuestBinary, MultiUseSandbox, Result, UninitializedSandbox}; -use hyperlight_testing::{ - c_callback_guest_as_string, c_simple_guest_as_string, callback_guest_as_string, - simple_guest_as_string, -}; +use hyperlight_testing::{c_simple_guest_as_string, simple_guest_as_string}; /// Returns a rust/c simpleguest depending on environment variable GUEST. /// Uses rust guest by default. Run test with environment variable GUEST="c" to use the c version @@ -59,10 +56,10 @@ pub fn get_simpleguest_sandboxes( .collect() } -pub fn get_callbackguest_uninit_sandboxes( +pub fn get_uninit_simpleguest_sandboxes( writer: Option>, // An optional writer to make sure correct info is passed to the host printer ) -> Vec { - let elf_path = get_c_or_rust_callbackguest_path(); + let elf_path = get_c_or_rust_simpleguest_path(); let sandboxes = [ // in hypervisor elf @@ -89,13 +86,3 @@ pub(crate) fn get_c_or_rust_simpleguest_path() -> String { _ => panic!("Unknown guest type '{guest_type}', use either 'rust' or 'c'"), } } - -// returns the the path of callbackguest binary. Picks rust/ version depending on environment variable GUEST (or rust by default if unset) -fn get_c_or_rust_callbackguest_path() -> String { - let guest_type = std::env::var("GUEST").unwrap_or("rust".to_string()); - match guest_type.as_str() { - "rust" => callback_guest_as_string().unwrap(), - "c" => c_callback_guest_as_string().unwrap(), - _ => panic!("Unknown guest type '{guest_type}', use either 'rust' or 'c'"), - } -} diff --git a/src/hyperlight_host/tests/integration_test.rs b/src/hyperlight_host/tests/integration_test.rs index eceabd479..7d98532d3 100644 --- a/src/hyperlight_host/tests/integration_test.rs +++ b/src/hyperlight_host/tests/integration_test.rs @@ -24,9 +24,7 @@ use hyperlight_common::mem::PAGE_SIZE; use hyperlight_host::sandbox::SandboxConfiguration; use hyperlight_host::{GuestBinary, HyperlightError, MultiUseSandbox, UninitializedSandbox}; use hyperlight_testing::simplelogger::{LOGGER, SimpleLogger}; -use hyperlight_testing::{ - c_simple_guest_as_string, callback_guest_as_string, simple_guest_as_string, -}; +use hyperlight_testing::{c_simple_guest_as_string, simple_guest_as_string}; use log::LevelFilter; pub mod common; // pub to disable dead_code warning @@ -40,7 +38,7 @@ fn interrupt_host_call() { let barrier2 = barrier.clone(); let mut usbox = UninitializedSandbox::new( - GuestBinary::FilePath(callback_guest_as_string().expect("Guest Binary Missing")), + GuestBinary::FilePath(simple_guest_as_string().expect("Guest Binary Missing")), None, ) .unwrap(); @@ -312,7 +310,7 @@ fn interrupt_custom_signal_no_and_retry_delay() { #[test] fn interrupt_spamming_host_call() { let mut uninit = UninitializedSandbox::new( - GuestBinary::FilePath(callback_guest_as_string().unwrap()), + GuestBinary::FilePath(simple_guest_as_string().unwrap()), None, ) .unwrap(); diff --git a/src/hyperlight_host/tests/sandbox_host_tests.rs b/src/hyperlight_host/tests/sandbox_host_tests.rs index 32047653c..6c0247cf9 100644 --- a/src/hyperlight_host/tests/sandbox_host_tests.rs +++ b/src/hyperlight_host/tests/sandbox_host_tests.rs @@ -18,7 +18,7 @@ use core::f64; use std::sync::mpsc::channel; use std::sync::{Arc, Mutex}; -use common::new_uninit; +use common::{get_uninit_simpleguest_sandboxes, new_uninit}; use hyperlight_host::sandbox::SandboxConfiguration; use hyperlight_host::{ GuestBinary, HyperlightError, MultiUseSandbox, Result, UninitializedSandbox, new_error, @@ -28,7 +28,7 @@ use hyperlight_testing::simple_guest_as_string; use serial_test::serial; // using LoadLibrary requires serial tests pub mod common; // pub to disable dead_code warning -use crate::common::{get_callbackguest_uninit_sandboxes, get_simpleguest_sandboxes}; +use crate::common::get_simpleguest_sandboxes; #[test] #[cfg_attr(target_os = "windows", serial)] // using LoadLibrary requires serial tests @@ -312,7 +312,7 @@ fn simple_test_parallel() { } fn callback_test_helper() -> Result<()> { - for mut sandbox in get_callbackguest_uninit_sandboxes(None).into_iter() { + for mut sandbox in get_uninit_simpleguest_sandboxes(None).into_iter() { // create host function let (tx, rx) = channel(); sandbox.register("HostMethod1", move |msg: String| { @@ -357,7 +357,7 @@ fn callback_test_parallel() { #[test] #[cfg_attr(target_os = "windows", serial)] // using LoadLibrary requires serial tests fn host_function_error() -> Result<()> { - for mut sandbox in get_callbackguest_uninit_sandboxes(None).into_iter() { + for mut sandbox in get_uninit_simpleguest_sandboxes(None).into_iter() { // create host function sandbox.register("HostMethod1", |_: String| -> Result { Err(new_error!("Host function error!")) diff --git a/src/hyperlight_testing/src/lib.rs b/src/hyperlight_testing/src/lib.rs index eb48e203a..c47b6c797 100644 --- a/src/hyperlight_testing/src/lib.rs +++ b/src/hyperlight_testing/src/lib.rs @@ -72,20 +72,12 @@ pub fn simple_guest_as_string() -> Result { .ok_or_else(|| anyhow!("couldn't convert simple guest PathBuf to string")) } -/// Get a fully qualified OS-specific path to the callbackguest elf binary -pub fn callback_guest_as_string() -> Result { - let buf = rust_guest_as_pathbuf("callbackguest"); - buf.to_str() - .map(|s| s.to_string()) - .ok_or_else(|| anyhow!("couldn't convert callback guest PathBuf to string")) -} - /// Get a fully-qualified OS-specific path to the witguest elf binary pub fn wit_guest_as_string() -> Result { let buf = rust_guest_as_pathbuf("witguest"); buf.to_str() .map(|s| s.to_string()) - .ok_or_else(|| anyhow!("couldn't convert callback guest PathBuf to string")) + .ok_or_else(|| anyhow!("couldn't convert wit guest PathBuf to string")) } /// Get a fully qualified OS-specific path to the dummyguest elf binary @@ -116,13 +108,6 @@ pub fn c_simple_guest_as_string() -> Result { .ok_or_else(|| anyhow!("couldn't convert simple guest PathBuf to string")) } -pub fn c_callback_guest_as_string() -> Result { - let buf = c_guest_as_pathbuf("callbackguest"); - buf.to_str() - .map(|s| s.to_string()) - .ok_or_else(|| anyhow!("couldn't convert callback guest PathBuf to string")) -} - /// Get a fully qualified path to a simple guest binary preferring a binary /// in the same directory as the parent executable. This will be used in /// fuzzing scenarios where pre-built binaries will be built and submitted to diff --git a/src/tests/c_guests/c_callbackguest/main.c b/src/tests/c_guests/c_callbackguest/main.c deleted file mode 100644 index e4fa2a678..000000000 --- a/src/tests/c_guests/c_callbackguest/main.c +++ /dev/null @@ -1,51 +0,0 @@ -// Included from hyperlight_guest_capi/include -#include "hyperlight_guest.h" -// Included from hyperlight_guest_bin/third_party/libc -#include "stdint.h" -#include "stdio.h" -#include "string.h" -// Included from hyperlight_guest_bin/third_party/printf -#include "printf.h" - -int print_output(const char *message) { - int res = printf("%s", message); - - return res; -} - -int guest_function(const char *from_host) { - char guest_message[256] = "Hello from GuestFunction1, "; - int len = strlen(from_host); - strncat(guest_message, from_host, len); - - hl_Parameter params = {.tag = hl_ParameterType_String, - .value = {.String = guest_message}}; - const hl_FunctionCall host_call = {.function_name = "HostMethod1", - .parameters = ¶ms, - .parameters_len = 1, - .return_type = hl_ReturnType_Int}; - hl_call_host_function(&host_call); - hl_get_host_return_value_as_Int(); - - return 0; -} - -HYPERLIGHT_WRAP_FUNCTION(print_output, Int, 1, String); -HYPERLIGHT_WRAP_FUNCTION(guest_function, Int, 1, String); - -void hyperlight_main(void) { - HYPERLIGHT_REGISTER_FUNCTION("PrintOutput", print_output); - HYPERLIGHT_REGISTER_FUNCTION("GuestMethod1", guest_function); -} - -// This dispatch function is only used when the host dispatches a guest function -// call but there is no registered guest function with the given name. -hl_Vec *c_guest_dispatch_function(const hl_FunctionCall *function_call) { - const char *func_name = function_call->function_name; - if (strcmp(func_name, "ThisIsNotARealFunctionButTheNameIsImportant") == 0) { - // This is special case for test `iostack_is_working - return hl_flatbuffer_result_from_Int(99); - } - - return NULL; -} diff --git a/src/tests/c_guests/c_simpleguest/main.c b/src/tests/c_guests/c_simpleguest/main.c index b7a9a596f..2822b108d 100644 --- a/src/tests/c_guests/c_simpleguest/main.c +++ b/src/tests/c_guests/c_simpleguest/main.c @@ -239,8 +239,26 @@ hl_Vec *twenty_four_k_in_eight_k_out(const hl_FunctionCall* params) { return hl_flatbuffer_result_from_Bytes(input.data, 8 * 1024); } +int guest_function(const char *from_host) { + char guest_message[256] = "Hello from GuestFunction1, "; + int len = strlen(from_host); + strncat(guest_message, from_host, len); + + hl_Parameter params = {.tag = hl_ParameterType_String, + .value = {.String = guest_message}}; + const hl_FunctionCall host_call = {.function_name = "HostMethod1", + .parameters = ¶ms, + .parameters_len = 1, + .return_type = hl_ReturnType_Int}; + hl_call_host_function(&host_call); + hl_get_host_return_value_as_Int(); + + return 0; +} + HYPERLIGHT_WRAP_FUNCTION(echo, String, 1, String) // HYPERLIGHT_WRAP_FUNCTION(set_byte_array_to_zero, 1, VecBytes) is not valid for functions that return VecBytes +HYPERLIGHT_WRAP_FUNCTION(guest_function, Int, 1, String) HYPERLIGHT_WRAP_FUNCTION(print_output, Int, 1, String) HYPERLIGHT_WRAP_FUNCTION(stack_allocate, Int, 1, Int) HYPERLIGHT_WRAP_FUNCTION(stack_overflow, Int, 1, Int) @@ -275,6 +293,7 @@ void hyperlight_main(void) // HYPERLIGHT_REGISTER_FUNCTION macro does not work for functions that return VecBytes, // so we use hl_register_function_definition directly hl_register_function_definition("SetByteArrayToZero", set_byte_array_to_zero, 1, (hl_ParameterType[]){hl_ParameterType_VecBytes}, hl_ReturnType_VecBytes); + HYPERLIGHT_REGISTER_FUNCTION("GuestMethod1", guest_function); HYPERLIGHT_REGISTER_FUNCTION("PrintOutput", print_output); HYPERLIGHT_REGISTER_FUNCTION("StackAllocate", stack_allocate); HYPERLIGHT_REGISTER_FUNCTION("StackOverflow", stack_overflow); diff --git a/src/tests/rust_guests/callbackguest/.cargo/config.toml b/src/tests/rust_guests/callbackguest/.cargo/config.toml deleted file mode 100644 index f977e97ab..000000000 --- a/src/tests/rust_guests/callbackguest/.cargo/config.toml +++ /dev/null @@ -1,19 +0,0 @@ -[build] -target = "x86_64-unknown-none" - -[target.x86_64-unknown-none] -rustflags = [ - "-C", - "code-model=small", - "-C", - "link-args=-e entrypoint", -] -linker = "rust-lld" - -[profile.release] -opt-level = 0 -panic = "abort" - -[profile.dev] -opt-level = 0 -panic = "abort" \ No newline at end of file diff --git a/src/tests/rust_guests/callbackguest/Cargo.lock b/src/tests/rust_guests/callbackguest/Cargo.lock deleted file mode 100644 index c899d36c5..000000000 --- a/src/tests/rust_guests/callbackguest/Cargo.lock +++ /dev/null @@ -1,271 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 4 - -[[package]] -name = "anyhow" -version = "1.0.99" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0674a1ddeecb70197781e945de4b3b8ffb61fa939a5597bcf48503737663100" - -[[package]] -name = "autocfg" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" - -[[package]] -name = "bitflags" -version = "2.9.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34efbcccd345379ca2868b2b2c9d3782e9cc58ba87bc7d79d5b53d9c9ae6f25d" - -[[package]] -name = "buddy_system_allocator" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1a0108968a3a2dab95b089c0fc3f1afa7759aa5ebe6f1d86d206d6f7ba726eb" -dependencies = [ - "spin 0.9.8", -] - -[[package]] -name = "callbackguest" -version = "0.9.0" -dependencies = [ - "hyperlight-common", - "hyperlight-guest", - "hyperlight-guest-bin", - "hyperlight-guest-tracing", -] - -[[package]] -name = "cc" -version = "1.2.34" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42bc4aea80032b7bf409b0bc7ccad88853858911b7713a8062fdc0623867bedc" -dependencies = [ - "shlex", -] - -[[package]] -name = "cfg-if" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9" - -[[package]] -name = "flatbuffers" -version = "25.2.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1045398c1bfd89168b5fd3f1fc11f6e70b34f6f66300c87d44d3de849463abf1" -dependencies = [ - "bitflags", - "rustc_version", -] - -[[package]] -name = "glob" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280" - -[[package]] -name = "hyperlight-common" -version = "0.9.0" -dependencies = [ - "anyhow", - "flatbuffers", - "log", - "spin 0.10.0", -] - -[[package]] -name = "hyperlight-guest" -version = "0.9.0" -dependencies = [ - "anyhow", - "flatbuffers", - "hyperlight-common", - "hyperlight-guest-tracing", - "serde_json", -] - -[[package]] -name = "hyperlight-guest-bin" -version = "0.9.0" -dependencies = [ - "buddy_system_allocator", - "cc", - "cfg-if", - "glob", - "hyperlight-common", - "hyperlight-guest", - "hyperlight-guest-tracing", - "log", - "spin 0.10.0", -] - -[[package]] -name = "hyperlight-guest-tracing" -version = "0.9.0" -dependencies = [ - "hyperlight-common", - "hyperlight-guest-tracing-macro", - "spin 0.10.0", -] - -[[package]] -name = "hyperlight-guest-tracing-macro" -version = "0.9.0" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "itoa" -version = "1.0.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" - -[[package]] -name = "lock_api" -version = "0.4.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96936507f153605bddfcda068dd804796c84324ed2510809e5b2a624c81da765" -dependencies = [ - "autocfg", - "scopeguard", -] - -[[package]] -name = "log" -version = "0.4.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" - -[[package]] -name = "memchr" -version = "2.7.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0" - -[[package]] -name = "proc-macro2" -version = "1.0.101" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "rustc_version" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" -dependencies = [ - "semver", -] - -[[package]] -name = "ryu" -version = "1.0.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" - -[[package]] -name = "scopeguard" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" - -[[package]] -name = "semver" -version = "1.0.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56e6fa9c48d24d85fb3de5ad847117517440f6beceb7798af16b4a87d616b8d0" - -[[package]] -name = "serde" -version = "1.0.219" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.219" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "serde_json" -version = "1.0.143" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d401abef1d108fbd9cbaebc3e46611f4b1021f714a0597a71f41ee463f5f4a5a" -dependencies = [ - "itoa", - "memchr", - "ryu", - "serde", -] - -[[package]] -name = "shlex" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" - -[[package]] -name = "spin" -version = "0.9.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" -dependencies = [ - "lock_api", -] - -[[package]] -name = "spin" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5fe4ccb98d9c292d56fec89a5e07da7fc4cf0dc11e156b41793132775d3e591" -dependencies = [ - "lock_api", -] - -[[package]] -name = "syn" -version = "2.0.106" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ede7c438028d4436d71104916910f5bb611972c5cfd7f89b8300a8186e6fada6" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "unicode-ident" -version = "1.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" diff --git a/src/tests/rust_guests/callbackguest/Cargo.toml b/src/tests/rust_guests/callbackguest/Cargo.toml deleted file mode 100644 index fdb6ddbb2..000000000 --- a/src/tests/rust_guests/callbackguest/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "callbackguest" -version = "0.9.0" -edition = "2021" - -[dependencies] -hyperlight-guest = { path = "../../../hyperlight_guest" } -hyperlight-guest-bin = { path = "../../../hyperlight_guest_bin" } -hyperlight-common = { path = "../../../hyperlight_common", default-features = false } -hyperlight-guest-tracing = { path = "../../../hyperlight_guest_tracing" } - -[features] -default = [] -trace_guest = ["hyperlight-guest-bin/trace_guest", "hyperlight-guest/trace_guest", "hyperlight-guest-tracing/trace"] -unwind_guest = ["hyperlight-common/unwind_guest"] -mem_profile = ["hyperlight-common/mem_profile", "hyperlight-guest-bin/mem_profile"] diff --git a/src/tests/rust_guests/callbackguest/src/main.rs b/src/tests/rust_guests/callbackguest/src/main.rs deleted file mode 100644 index d217ccd88..000000000 --- a/src/tests/rust_guests/callbackguest/src/main.rs +++ /dev/null @@ -1,278 +0,0 @@ -/* -Copyright 2025 The Hyperlight Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -#![no_std] -#![no_main] - -extern crate alloc; -extern crate hyperlight_guest; - -use alloc::format; -use alloc::string::ToString; -use alloc::vec::Vec; - -use hyperlight_common::flatbuffer_wrappers::function_call::FunctionCall; -use hyperlight_common::flatbuffer_wrappers::function_types::{ - ParameterType, ParameterValue, ReturnType, -}; -use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode; -use hyperlight_common::flatbuffer_wrappers::guest_log_level::LogLevel; -use hyperlight_common::flatbuffer_wrappers::util::get_flatbuffer_result; -use hyperlight_guest::error::{HyperlightGuestError, Result}; -use hyperlight_guest_bin::guest_function::definition::GuestFunctionDefinition; -use hyperlight_guest_bin::guest_function::register::register_function; -use hyperlight_guest_bin::guest_logger::log_message; -use hyperlight_guest_bin::host_comm::{call_host_function, print_output_with_host_print}; - -#[hyperlight_guest_tracing::trace_function] -fn send_message_to_host_method( - method_name: &str, - guest_message: &str, - message: &str, -) -> Result> { - let message = format!("{}{}", guest_message, message); - let res = call_host_function::( - method_name, - Some(Vec::from(&[ParameterValue::String(message.to_string())])), - ReturnType::Int, - )?; - - Ok(get_flatbuffer_result(res)) -} - -#[hyperlight_guest_tracing::trace_function] -fn guest_function(function_call: &FunctionCall) -> Result> { - if let ParameterValue::String(message) = &function_call.parameters.as_ref().unwrap()[0] { - send_message_to_host_method("HostMethod", "Hello from GuestFunction, ", message) - } else { - Err(HyperlightGuestError::new( - ErrorCode::GuestFunctionParameterTypeMismatch, - "Invalid parameters passed to guest_function".to_string(), - )) - } -} - -#[hyperlight_guest_tracing::trace_function] -fn guest_function1(function_call: &FunctionCall) -> Result> { - if let ParameterValue::String(message) = &function_call.parameters.as_ref().unwrap()[0] { - send_message_to_host_method("HostMethod1", "Hello from GuestFunction1, ", message) - } else { - Err(HyperlightGuestError::new( - ErrorCode::GuestFunctionParameterTypeMismatch, - "Invalid parameters passed to guest_function1".to_string(), - )) - } -} - -#[hyperlight_guest_tracing::trace_function] -fn guest_function2(function_call: &FunctionCall) -> Result> { - if let ParameterValue::String(message) = &function_call.parameters.as_ref().unwrap()[0] { - send_message_to_host_method("HostMethod1", "Hello from GuestFunction2, ", message) - } else { - Err(HyperlightGuestError::new( - ErrorCode::GuestFunctionParameterTypeMismatch, - "Invalid parameters passed to guest_function2".to_string(), - )) - } -} - -#[hyperlight_guest_tracing::trace_function] -fn guest_function3(function_call: &FunctionCall) -> Result> { - if let ParameterValue::String(message) = &function_call.parameters.as_ref().unwrap()[0] { - send_message_to_host_method("HostMethod1", "Hello from GuestFunction3, ", message) - } else { - Err(HyperlightGuestError::new( - ErrorCode::GuestFunctionParameterTypeMismatch, - "Invalid parameters passed to guest_function3".to_string(), - )) - } -} - -#[hyperlight_guest_tracing::trace_function] -fn guest_function4(_: &FunctionCall) -> Result> { - call_host_function::<()>( - "HostMethod4", - Some(Vec::from(&[ParameterValue::String( - "Hello from GuestFunction4".to_string(), - )])), - ReturnType::Void, - )?; - - Ok(get_flatbuffer_result(())) -} - -#[hyperlight_guest_tracing::trace_function] -fn guest_log_message(function_call: &FunctionCall) -> Result> { - if let ( - ParameterValue::String(message), - ParameterValue::String(source), - ParameterValue::Int(level), - ) = ( - &function_call.parameters.as_ref().unwrap()[0], - &function_call.parameters.as_ref().unwrap()[1], - &function_call.parameters.as_ref().unwrap()[2], - ) { - let mut log_level = *level; - if !(0..=6).contains(&log_level) { - log_level = 0; - } - - log_message( - LogLevel::from(log_level as u8), - message, - source, - "guest_log_message", - file!(), - line!(), - ); - - Ok(get_flatbuffer_result(message.len() as i32)) - } else { - Err(HyperlightGuestError::new( - ErrorCode::GuestFunctionParameterTypeMismatch, - "Invalid parameters passed to guest_log_message".to_string(), - )) - } -} - -#[hyperlight_guest_tracing::trace_function] -fn call_error_method(function_call: &FunctionCall) -> Result> { - if let ParameterValue::String(message) = &function_call.parameters.as_ref().unwrap()[0] { - send_message_to_host_method("ErrorMethod", "Error From Host: ", message) - } else { - Err(HyperlightGuestError::new( - ErrorCode::GuestFunctionParameterTypeMismatch, - "Invalid parameters passed to call_error_method".to_string(), - )) - } -} - -#[hyperlight_guest_tracing::trace_function] -fn call_host_spin(_: &FunctionCall) -> Result> { - call_host_function::<()>("Spin", None, ReturnType::Void)?; - Ok(get_flatbuffer_result(())) -} - -#[hyperlight_guest_tracing::trace_function] -fn host_call_loop(function_call: &FunctionCall) -> Result> { - if let ParameterValue::String(message) = &function_call.parameters.as_ref().unwrap()[0] { - loop { - call_host_function::<()>(message, None, ReturnType::Void).unwrap(); - } - } else { - Err(HyperlightGuestError::new( - ErrorCode::GuestFunctionParameterTypeMismatch, - "Invalid parameters passed to host_call_loop".to_string(), - )) - } -} - -#[no_mangle] -#[hyperlight_guest_tracing::trace_function] -pub extern "C" fn hyperlight_main() { - let print_output_def = GuestFunctionDefinition::new( - "PrintOutput".to_string(), - Vec::from(&[ParameterType::String]), - ReturnType::Int, - print_output_with_host_print as usize, - ); - register_function(print_output_def); - - let guest_function_def = GuestFunctionDefinition::new( - "GuestMethod".to_string(), - Vec::from(&[ParameterType::String]), - ReturnType::Int, - guest_function as usize, - ); - register_function(guest_function_def); - - let guest_function1_def = GuestFunctionDefinition::new( - "GuestMethod1".to_string(), - Vec::from(&[ParameterType::String]), - ReturnType::Int, - guest_function1 as usize, - ); - register_function(guest_function1_def); - - let guest_function2_def = GuestFunctionDefinition::new( - "GuestMethod2".to_string(), - Vec::from(&[ParameterType::String]), - ReturnType::Int, - guest_function2 as usize, - ); - register_function(guest_function2_def); - - let guest_function3_def = GuestFunctionDefinition::new( - "GuestMethod3".to_string(), - Vec::from(&[ParameterType::String]), - ReturnType::Int, - guest_function3 as usize, - ); - register_function(guest_function3_def); - - let guest_function4_def = GuestFunctionDefinition::new( - "GuestMethod4".to_string(), - Vec::new(), - ReturnType::Int, - guest_function4 as usize, - ); - register_function(guest_function4_def); - - let guest_log_message_def = GuestFunctionDefinition::new( - "LogMessage".to_string(), - Vec::from(&[ - ParameterType::String, - ParameterType::String, - ParameterType::Int, - ]), - ReturnType::Int, - guest_log_message as usize, - ); - register_function(guest_log_message_def); - - let call_error_method_def = GuestFunctionDefinition::new( - "CallErrorMethod".to_string(), - Vec::from(&[ParameterType::String]), - ReturnType::Int, - call_error_method as usize, - ); - register_function(call_error_method_def); - - let call_host_spin_def = GuestFunctionDefinition::new( - "CallHostSpin".to_string(), - Vec::new(), - ReturnType::Int, - call_host_spin as usize, - ); - register_function(call_host_spin_def); - - let host_call_loop_def = GuestFunctionDefinition::new( - "HostCallLoop".to_string(), - Vec::from(&[ParameterType::String]), - ReturnType::Void, - host_call_loop as usize, - ); - register_function(host_call_loop_def); -} - -#[no_mangle] -#[hyperlight_guest_tracing::trace_function] -pub fn guest_dispatch_function(function_call: FunctionCall) -> Result> { - Err(HyperlightGuestError::new( - ErrorCode::GuestFunctionNotFound, - function_call.function_name.clone(), - )) -} diff --git a/src/tests/rust_guests/dummyguest/Cargo.lock b/src/tests/rust_guests/dummyguest/Cargo.lock index e93ad50ef..fa3de189b 100644 --- a/src/tests/rust_guests/dummyguest/Cargo.lock +++ b/src/tests/rust_guests/dummyguest/Cargo.lock @@ -140,9 +140,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.27" +version = "0.4.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" +checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" [[package]] name = "memchr" diff --git a/src/tests/rust_guests/simpleguest/src/main.rs b/src/tests/rust_guests/simpleguest/src/main.rs index c3b17ad58..d4d81d57f 100644 --- a/src/tests/rust_guests/simpleguest/src/main.rs +++ b/src/tests/rust_guests/simpleguest/src/main.rs @@ -46,7 +46,8 @@ use hyperlight_guest::exit::{abort_with_code, abort_with_code_and_message}; use hyperlight_guest_bin::guest_function::definition::GuestFunctionDefinition; use hyperlight_guest_bin::guest_function::register::register_function; use hyperlight_guest_bin::host_comm::{ - call_host_function, call_host_function_without_returning_result, read_n_bytes_from_user_memory, + call_host_function, call_host_function_without_returning_result, print_output_with_host_print, + read_n_bytes_from_user_memory, }; use hyperlight_guest_bin::memory::malloc; use hyperlight_guest_bin::{MIN_STACK_ADDRESS, guest_logger}; @@ -975,6 +976,90 @@ pub extern "C" fn hyperlight_main() { ); register_function(simple_print_output_def); + let print_output_def = GuestFunctionDefinition::new( + "PrintOutputWithHostPrint".to_string(), + Vec::from(&[ParameterType::String]), + ReturnType::Int, + print_output_with_host_print as usize, + ); + register_function(print_output_def); + + let guest_function_def = GuestFunctionDefinition::new( + "GuestMethod".to_string(), + Vec::from(&[ParameterType::String]), + ReturnType::Int, + guest_function as usize, + ); + register_function(guest_function_def); + + let guest_function1_def = GuestFunctionDefinition::new( + "GuestMethod1".to_string(), + Vec::from(&[ParameterType::String]), + ReturnType::Int, + guest_function1 as usize, + ); + register_function(guest_function1_def); + + let guest_function2_def = GuestFunctionDefinition::new( + "GuestMethod2".to_string(), + Vec::from(&[ParameterType::String]), + ReturnType::Int, + guest_function2 as usize, + ); + register_function(guest_function2_def); + + let guest_function3_def = GuestFunctionDefinition::new( + "GuestMethod3".to_string(), + Vec::from(&[ParameterType::String]), + ReturnType::Int, + guest_function3 as usize, + ); + register_function(guest_function3_def); + + let guest_function4_def = GuestFunctionDefinition::new( + "GuestMethod4".to_string(), + Vec::new(), + ReturnType::Int, + guest_function4 as usize, + ); + register_function(guest_function4_def); + + let guest_log_message_def = GuestFunctionDefinition::new( + "LogMessageWithSource".to_string(), + Vec::from(&[ + ParameterType::String, + ParameterType::String, + ParameterType::Int, + ]), + ReturnType::Int, + guest_log_message as usize, + ); + register_function(guest_log_message_def); + + let call_error_method_def = GuestFunctionDefinition::new( + "CallErrorMethod".to_string(), + Vec::from(&[ParameterType::String]), + ReturnType::Int, + call_error_method as usize, + ); + register_function(call_error_method_def); + + let call_host_spin_def = GuestFunctionDefinition::new( + "CallHostSpin".to_string(), + Vec::new(), + ReturnType::Int, + call_host_spin as usize, + ); + register_function(call_host_spin_def); + + let host_call_loop_def = GuestFunctionDefinition::new( + "HostCallLoop".to_string(), + Vec::from(&[ParameterType::String]), + ReturnType::Void, + host_call_loop as usize, + ); + register_function(host_call_loop_def); + let print_using_printf_def = GuestFunctionDefinition::new( "PrintUsingPrintf".to_string(), Vec::from(&[ParameterType::String]), @@ -1368,6 +1453,179 @@ pub extern "C" fn hyperlight_main() { register_function(call_given_hostfunc_def); } +#[hyperlight_guest_tracing::trace_function] +fn send_message_to_host_method( + method_name: &str, + guest_message: &str, + message: &str, +) -> Result> { + let message = format!("{}{}", guest_message, message); + let res = call_host_function::( + method_name, + Some(Vec::from(&[ParameterValue::String(message.to_string())])), + ReturnType::Int, + )?; + + Ok(get_flatbuffer_result(res)) +} + +#[hyperlight_guest_tracing::trace_function] +fn guest_function(function_call: &FunctionCall) -> Result> { + if let ParameterValue::String(message) = &function_call.parameters.as_ref().unwrap()[0] { + send_message_to_host_method("HostMethod", "Hello from GuestFunction, ", message) + } else { + Err(HyperlightGuestError::new( + ErrorCode::GuestFunctionParameterTypeMismatch, + "Invalid parameters passed to guest_function".to_string(), + )) + } +} + +#[hyperlight_guest_tracing::trace_function] +fn guest_function1(function_call: &FunctionCall) -> Result> { + if let ParameterValue::String(message) = &function_call.parameters.as_ref().unwrap()[0] { + send_message_to_host_method("HostMethod1", "Hello from GuestFunction1, ", message) + } else { + Err(HyperlightGuestError::new( + ErrorCode::GuestFunctionParameterTypeMismatch, + "Invalid parameters passed to guest_function1".to_string(), + )) + } +} + +#[hyperlight_guest_tracing::trace_function] +fn guest_function2(function_call: &FunctionCall) -> Result> { + if let ParameterValue::String(message) = &function_call.parameters.as_ref().unwrap()[0] { + send_message_to_host_method("HostMethod1", "Hello from GuestFunction2, ", message) + } else { + Err(HyperlightGuestError::new( + ErrorCode::GuestFunctionParameterTypeMismatch, + "Invalid parameters passed to guest_function2".to_string(), + )) + } +} + +#[hyperlight_guest_tracing::trace_function] +fn guest_function3(function_call: &FunctionCall) -> Result> { + if let ParameterValue::String(message) = &function_call.parameters.as_ref().unwrap()[0] { + send_message_to_host_method("HostMethod1", "Hello from GuestFunction3, ", message) + } else { + Err(HyperlightGuestError::new( + ErrorCode::GuestFunctionParameterTypeMismatch, + "Invalid parameters passed to guest_function3".to_string(), + )) + } +} + +#[hyperlight_guest_tracing::trace_function] +fn guest_function4(_: &FunctionCall) -> Result> { + call_host_function::<()>( + "HostMethod4", + Some(Vec::from(&[ParameterValue::String( + "Hello from GuestFunction4".to_string(), + )])), + ReturnType::Void, + )?; + + Ok(get_flatbuffer_result(())) +} + +#[hyperlight_guest_tracing::trace_function] +fn guest_log_message(function_call: &FunctionCall) -> Result> { + if let ( + ParameterValue::String(message), + ParameterValue::String(source), + ParameterValue::Int(level), + ) = ( + &function_call.parameters.as_ref().unwrap()[0], + &function_call.parameters.as_ref().unwrap()[1], + &function_call.parameters.as_ref().unwrap()[2], + ) { + let mut log_level = *level; + if !(0..=6).contains(&log_level) { + log_level = 0; + } + + guest_logger::log_message( + LogLevel::from(log_level as u8), + message, + source, + "guest_log_message", + file!(), + line!(), + ); + + Ok(get_flatbuffer_result(message.len() as i32)) + } else { + Err(HyperlightGuestError::new( + ErrorCode::GuestFunctionParameterTypeMismatch, + "Invalid parameters passed to guest_log_message".to_string(), + )) + } +} + +#[hyperlight_guest_tracing::trace_function] +fn call_error_method(function_call: &FunctionCall) -> Result> { + if let ParameterValue::String(message) = &function_call.parameters.as_ref().unwrap()[0] { + send_message_to_host_method("ErrorMethod", "Error From Host: ", message) + } else { + Err(HyperlightGuestError::new( + ErrorCode::GuestFunctionParameterTypeMismatch, + "Invalid parameters passed to call_error_method".to_string(), + )) + } +} + +#[hyperlight_guest_tracing::trace_function] +fn call_host_spin(_: &FunctionCall) -> Result> { + call_host_function::<()>("Spin", None, ReturnType::Void)?; + Ok(get_flatbuffer_result(())) +} + +#[hyperlight_guest_tracing::trace_function] +fn host_call_loop(function_call: &FunctionCall) -> Result> { + if let ParameterValue::String(message) = &function_call.parameters.as_ref().unwrap()[0] { + loop { + call_host_function::<()>(message, None, ReturnType::Void).unwrap(); + } + } else { + Err(HyperlightGuestError::new( + ErrorCode::GuestFunctionParameterTypeMismatch, + "Invalid parameters passed to host_call_loop".to_string(), + )) + } +} + +// Interprets the given guest function call as a host function call and dispatches it to the host. +#[hyperlight_guest_tracing::trace_function] +fn fuzz_host_function(func: FunctionCall) -> Result> { + let mut params = func.parameters.unwrap(); + // first parameter must be string (the name of the host function to call) + let host_func_name = match params.remove(0) { + // TODO use `swap_remove` instead of `remove` if performance is an issue, but left out + // to avoid confusion for replicating failure cases + ParameterValue::String(name) => name, + _ => { + return Err(HyperlightGuestError::new( + ErrorCode::GuestFunctionParameterTypeMismatch, + "Invalid parameters passed to fuzz_host_function".to_string(), + )); + } + }; + + // Because we do not know at compile time the actual return type of the host function to be called + // we cannot use the `call_host_function` generic function. + // We need to use the `call_host_function_without_returning_result` function that does not retrieve the return + // value + call_host_function_without_returning_result( + &host_func_name, + Some(params), + func.expected_return_type, + ) + .expect("failed to call host function"); + Ok(get_flatbuffer_result(())) +} + #[no_mangle] #[hyperlight_guest_tracing::trace_function] pub fn guest_dispatch_function(function_call: FunctionCall) -> Result> { @@ -1413,33 +1671,3 @@ pub fn guest_dispatch_function(function_call: FunctionCall) -> Result> { Ok(get_flatbuffer_result(99)) } - -// Interprets the given guest function call as a host function call and dispatches it to the host. -#[hyperlight_guest_tracing::trace_function] -fn fuzz_host_function(func: FunctionCall) -> Result> { - let mut params = func.parameters.unwrap(); - // first parameter must be string (the name of the host function to call) - let host_func_name = match params.remove(0) { - // TODO use `swap_remove` instead of `remove` if performance is an issue, but left out - // to avoid confusion for replicating failure cases - ParameterValue::String(name) => name, - _ => { - return Err(HyperlightGuestError::new( - ErrorCode::GuestFunctionParameterTypeMismatch, - "Invalid parameters passed to fuzz_host_function".to_string(), - )); - } - }; - - // Because we do not know at compile time the actual return type of the host function to be called - // we cannot use the `call_host_function` generic function. - // We need to use the `call_host_function_without_returning_result` function that does not retrieve the return - // value - call_host_function_without_returning_result( - &host_func_name, - Some(params), - func.expected_return_type, - ) - .expect("failed to call host function"); - Ok(get_flatbuffer_result(())) -} diff --git a/src/tests/rust_guests/witguest/Cargo.lock b/src/tests/rust_guests/witguest/Cargo.lock index 8bf176fa6..762a25e97 100644 --- a/src/tests/rust_guests/witguest/Cargo.lock +++ b/src/tests/rust_guests/witguest/Cargo.lock @@ -325,9 +325,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.27" +version = "0.4.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" +checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" [[package]] name = "memchr" @@ -521,9 +521,9 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "wasmparser" -version = "0.238.0" +version = "0.238.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0ad4ca2ecb86b79ea410cd970985665de1d05774b7107b214bc5852b1bcbad7" +checksum = "3fa99c8328024423875ae4a55345cfde8f0371327fb2d0f33b0f52a06fc44408" dependencies = [ "bitflags", "hashbrown",