Crates.io | hyperlight-guest-tracing |
lib.rs | hyperlight-guest-tracing |
version | 0.9.0 |
created_at | 2025-08-05 21:43:53.017021+00 |
updated_at | 2025-08-29 03:32:22.68679+00 |
description | Provides the tracing functionality for the hyperlight guest. |
homepage | https://github.com/hyperlight-dev/hyperlight |
repository | https://github.com/hyperlight-dev/hyperlight |
max_upload_size | |
id | 1783116 |
size | 31,472 |
Hyperlight is a lightweight Virtual Machine Manager (VMM) designed to be embedded within applications. It enables safe execution of untrusted code within micro virtual machines with very low latency and minimal overhead.
We are a Cloud Native Computing Foundation sandbox project.
Note: Hyperlight is a nascent project with an evolving API and no guaranteed support. Assistance is provided on a best-effort basis by the developers.
Hyperlight is a library for creating micro virtual machines — or sandboxes — specifically optimized for securely running untrusted code with minimal impact. It supports both Windows and Linux, utilizing Windows Hypervisor Platform on Windows, and either Microsoft Hypervisor (mshv) or KVM on Linux.
These micro VMs operate without a kernel or operating system, keeping overhead low. Instead, guests are built specifically for Hyperlight using the Hyperlight Guest library, which provides a controlled set of APIs that facilitate interaction between host and guest:
By default, Hyperlight restricts guest access to a minimal API. The only host function available by default allows the guest to print messages, which are displayed on the host console or redirected to stdout, as configured. Hosts can choose to expose additional host functions, expanding the guest’s capabilities as needed.
Below is an example demonstrating the use of the Hyperlight host library in Rust to execute a simple guest application. It is followed by an example of a simple guest application using the Hyperlight guest library, also written in Rust.
use std::thread;
use hyperlight_host::{MultiUseSandbox, UninitializedSandbox};
fn main() -> hyperlight_host::Result<()> {
// Create an uninitialized sandbox with a guest binary
let mut uninitialized_sandbox = UninitializedSandbox::new(
hyperlight_host::GuestBinary::FilePath("path/to/your/guest/binary".to_string()),
None // default configuration
)?;
// Registering a host function makes it available to be called by the guest
uninitialized_sandbox.register("Sleep5Secs", || {
thread::sleep(std::time::Duration::from_secs(5));
Ok(())
})?;
// Note: This function is unused by the guest code below, it's just here for demonstration purposes
// Initialize sandbox to be able to call host functions
let mut multi_use_sandbox: MultiUseSandbox = uninitialized_sandbox.evolve()?;
// Call a function in the guest
let message = "Hello, World! I am executing inside of a VM :)\n".to_string();
// in order to call a function it first must be defined in the guest and exposed so that
// the host can call it
multi_use_sandbox.call::<i32>(
"PrintOutput",
message,
)?;
Ok(())
}
#![no_std]
#![no_main]
extern crate alloc;
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::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::host_comm::call_host_function;
fn print_output(function_call: &FunctionCall) -> Result<Vec<u8>> {
if let ParameterValue::String(message) = function_call.parameters.clone().unwrap()[0].clone() {
let result = call_host_function::<i32>(
"HostPrint",
Some(Vec::from(&[ParameterValue::String(message.to_string())])),
ReturnType::Int,
)?;
Ok(get_flatbuffer_result(result))
} else {
Err(HyperlightGuestError::new(
ErrorCode::GuestFunctionParameterTypeMismatch,
"Invalid parameters passed to simple_print_output".to_string(),
))
}
}
#[no_mangle]
pub extern "C" fn hyperlight_main() {
let print_output_def = GuestFunctionDefinition::new(
"PrintOutput".to_string(),
Vec::from(&[ParameterType::String]),
ReturnType::Int,
print_output as usize,
);
register_function(print_output_def);
}
#[no_mangle]
pub fn guest_dispatch_function(function_call: FunctionCall) -> Result<Vec<u8>> {
let function_name = function_call.function_name.clone();
return Err(HyperlightGuestError::new(
ErrorCode::GuestFunctionNotFound,
function_name,
));
}
Note: Guest applications require a specific build configuration. Create a .cargo/config.toml
file in your guest project with the following content:
[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]
panic = "abort"
[profile.dev]
panic = "abort"
For additional examples of using the Hyperlight host Rust library, see the ./src/hyperlight_host/examples directory.
For examples of guest applications, see the ./src/tests/c_guests directory for C guests and the ./src/tests/rust_guests directory for Rust guests.
Note: Hyperlight guests can be written using the Hyperlight Rust or C Guest libraries.
Hyperlight Host Libraries (i.e., the ones that create and manage the VMs)
Hyperlight Guest Libraries (i.e., the ones to make it easier to create guests that run inside the VMs)
outb
), abstractions for calling host functions and receiving return values, and the input/output stacks used for guest-host communication.hyperlight_guest_bin
, exposing its core functionality for use in C programs and other languages via FFI.Hyperlight Common (functionality used by both the host and the guest)
Test Guest Applications:
Tests:
You can run Hyperlight on:
After having an environment with a hypervisor setup, running the example has the following pre-requisites:
sudo apt install build-essential
. For
Azure Linux, run sudo dnf install build-essential
.cargo install just
On Windows you also need pwsh.On Ubuntu, run:
wget https://apt.llvm.org/llvm.sh
chmod +x ./llvm.sh
sudo ./llvm.sh 17 all
sudo ln -s /usr/lib/llvm-17/bin/clang-cl /usr/bin/clang-cl
sudo ln -s /usr/lib/llvm-17/bin/llvm-lib /usr/bin/llvm-lib
sudo ln -s /usr/lib/llvm-17/bin/lld-link /usr/bin/lld-link
sudo ln -s /usr/lib/llvm-17/bin/llvm-ml /usr/bin/llvm-ml
sudo ln -s /usr/lib/llvm-17/bin/ld.lld /usr/bin/ld.lld
sudo ln -s /usr/lib/llvm-17/bin/clang /usr/bin/clang
On Windows, see this.
On Azure Linux, run:
sudo dnf remove clang -y || true
sudo dnf install clang17 -y
sudo dnf install clang17-tools-extra -y
Then, we are ready to build and run the example:
just build # build the Hyperlight library
just rg # build the rust test guest binaries
cargo run --example hello-world
If all worked as expected, you should see the following message in your console:
Hello, World! I am executing inside of a VM :)
If you get the error Error: NoHypervisorFound
and KVM or mshv is set up then this may be a permissions issue. In bash,
you can use ls -l /dev/kvm
or ls -l /dev/mshv
to check which group owns that device and then groups
to make sure
your user is a member of that group.
For more details on how to verify that KVM is correctly installed and permissions are correct, follow the guide here.
If you are interested in contributing to Hyperlight, running the entire test-suite is a good way to get started. To do so, on your console, run the following commands:
just guests # build the c and rust test guests
just build # build the Hyperlight library
just test # runs the tests
Also , please review the CONTRIBUTING.md file for more information on how to contribute to Hyperlight.
Note: For general Hyperlight development, you may also need flatc (Flatbuffer compiler): for instructions, see here. Copyright © contributors to Hyperlight, established as Hyperlight a Series of LF Projects, LLC.
This project holds fortnightly community meetings to discuss the project's progress, roadmap, and any other topics of interest. The meetings are open to everyone, and we encourage you to join us.
The Hyperlight project Slack is hosted in the CNCF Slack #hyperlight. To join the Slack, join the CNCF Slack, and join the #hyperlight channel.
For more information, please refer to our compilation of documents in the docs/
directory.
See the CNCF Code of Conduct.