Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
* Add ARM64 Linux MSHV support by @cshung in https://github.com/hyperlight-dev/hyperlight/pull/1626

### Changed
* Sandboxes configured for debugging cannot create snapshots on x86_64 or ARM64, including after debugger detach.
* `hyperlight-host` requires Rust 1.95.
* `Snapshot::save` caches memory-layer hashes and trusts existing same-length
regular blobs by their content-addressed filenames without verifying their
Expand Down Expand Up @@ -40,6 +41,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
* Host-written snapshot memory has instruction-cache coherence before guest execution.
* Fix slow scratch zeroing on restore under KVM in default builds.
* Fix C# component bindings keeping an entry for every resource handle passed to or from the guest.
* GDB memory reads and writes translate each guest page on hosts with larger pages.
* GDB snapshot recovery handles cancellation and crash detach. Restore rejects installed software breakpoints in ready sandboxes on x86_64 and ARM64.

### Removed
* **Breaking:** `SandboxBuilder::init_data`, `GuestEnvironment`, `GuestBlob`,
Expand Down
2 changes: 1 addition & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ test-no-surrogate target=default-target:
# runs tests that exercise gdb debugging
test-rust-gdb-debugging target=default-target features="":
{{ cargo-cmd }} test --profile={{ if target == "debug" { "dev" } else { target } }} {{ target-triple-flag }} --example guest-debugging {{ if features =="" {'--features gdb'} else { "--features gdb," + features } }}
{{ cargo-cmd }} test --profile={{ if target == "debug" { "dev" } else { target } }} {{ target-triple-flag }} -p hyperlight-host --lib {{ if features =="" {'--features gdb'} else { "--features gdb," + features } }} -- hypervisor::gdb::
{{ cargo-cmd }} test --profile={{ if target == "debug" { "dev" } else { target } }} {{ target-triple-flag }} -p hyperlight-host --lib {{ if features =="" {'--features gdb'} else { "--features gdb," + features } }} -- hypervisor::gdb:: hypervisor::hyperlight_vm::debug::tests:: gdb_recovery_tests::

# rust test for crashdump
test-rust-crashdump target=default-target features="":
Expand Down
22 changes: 22 additions & 0 deletions docs/how-to-debug-a-hyperlight-guest.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ session of a guest binary running inside a Hyperlight sandbox.
The debug target will refuse any resume, step actions and write operations to
the guest memory and registers until the gdb client disconnects or the sandbox is stopped.

Sandboxes configured for debugging cannot create snapshots on x86_64 or ARM64,
even after debugger detach. Debugger register and memory edits can violate guest
invariants. This restriction applies regardless of installed breakpoints.

Debugging supports loading and restoring existing snapshots. Restore rejects
installed software breakpoints in a ready sandbox. Recovery of a poisoned
sandbox discards breakpoint bookkeeping before restoring clean memory.

## Example

### Sandbox configuration
Expand Down Expand Up @@ -157,6 +165,20 @@ When the gdb client requests one of the supported features mentioned above, a re
is sent over the communication channel to the main thread for the sandbox
to resolve.

### ARM64 stops and stepping

The host prepares the guest registers before reporting the initial debug stop.
This stop happens once, before the first guest instruction executes. For a
sandbox created from an initialized snapshot, it happens at the first dispatcher
entry because guest initialization has already run.

Software breakpoints replace one instruction with a four-byte `BRK`. GDB
controls breakpoint removal and reinsertion when stepping over that instruction.
Hyperlight handles the breakpoint packets and enables CPU single-step for a
step request. It does not predict the next instruction address.

### Debug command flow

Below is a sequence diagram that shows the interaction between the entities
involved in the gdb debugging of a Hyperlight guest running inside a **KVM** or **MSHV** sandbox.

Expand Down
3 changes: 3 additions & 0 deletions src/hyperlight_common/src/arch/aarch64/exn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

use crate::vmem::bits;

/// HVC immediate for forwarding guest exceptions to the Hyper-V debugger.
pub const DEBUG_EXCEPTION_HVC: u16 = 0x485a;

const ESR_EC_UNKNOWN: u64 = 0b000000; // udf generates this
const ESR_EC_DATA_ABORT_LOWER_EL: u64 = 0b100100;
const ESR_EC_DATA_ABORT_SAME_EL: u64 = 0b100101;
Expand Down
17 changes: 17 additions & 0 deletions src/hyperlight_guest_bin/src/arch/aarch64/exception/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,24 @@ context_restore:\n
q_off = const offset_of!(ExceptionContext, q),
);

// Only synchronous exceptions have a syndrome for debug forwarding.
// The host resumes debug events at ERET and other synchronous exceptions at the prologue.
macro_rules! vbar_entry {
($et:literal, $ef:literal) => {
concat!(
"
.if {ExceptionType_",
$et,
"} == {ExceptionType_Synchronous}\n
stp x0, x1, [sp, #-16]!\n
mrs x0, mdscr_el1\n
tbz x0, #13, 1f\n
ldp x0, x1, [sp], #16\n
hvc #{debug_hvc}\n
eret\n
1:\n
ldp x0, x1, [sp], #16\n
.endif\n
sub sp, sp, #{ctx_size}\n
str x30, [sp, #{x30_off}]\n
bl context_save\n
Expand All @@ -129,6 +143,8 @@ macro_rules! vbar_entry {
global_asm!("
.balign 0x800\n
.global vbar\n
.global hyperlight_debug_vectors\n
hyperlight_debug_vectors:\n
vbar:\n",
vbar_entry!("Synchronous", "CurrentSP0"),
vbar_entry!("IRQ", "CurrentSP0"),
Expand All @@ -148,6 +164,7 @@ vbar:\n",
vbar_entry!("SError", "LowerAArch32"),
ctx_size = const size_of::<ExceptionContext>(),
x30_off = const offset_of!(ExceptionContext, x) + 15 * 0x010,
debug_hvc = const hyperlight_common::arch::exn::DEBUG_EXCEPTION_HVC,
handler = sym super::handle::handle_exception,
ExceptionType_Synchronous = const ExceptionType::Synchronous as u64,
ExceptionType_IRQ = const ExceptionType::Irq as u64,
Expand Down
213 changes: 212 additions & 1 deletion src/hyperlight_host/examples/guest-debugging/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ fn main() -> hyperlight_host::Result<()> {
.host_function("Sleep5Secs", sleep_5_secs)
.build()?;

// Call guest function
#[cfg(target_arch = "x86_64")]
multi_use_sandbox_dbg
.call::<()>("UseSSE2Registers", ())
.unwrap();
#[cfg(target_arch = "aarch64")]
multi_use_sandbox_dbg.call::<()>("NoOp", ()).unwrap();

let message =
"Hello, World! I am executing inside of a VM with debugger attached :)\n".to_string();
Expand Down Expand Up @@ -291,6 +293,215 @@ mod tests {
}

#[test]
#[cfg(target_arch = "aarch64")]
#[serial]
fn test_gdb_bootstrap_step_preserves_live_stack() {
let (out_file_path, cmd_file_path, manifest_dir) =
gdb_test_paths("gdb-aarch64-bootstrap-stack");
let cmd = format!(
"set osabi none
file {manifest_dir}/../tests/rust_guests/bin/debug/simpleguest
target remote :8080
set pagination off
set logging file {out_file_path}
set logging enabled on
set language c
set $entry = $pc
set $instruction = *(unsigned int *)$entry
set *(unsigned int *)$entry = 0xd503201f
set $bootstrap_sp = $sp
set $canary = $sp - 16
set $saved = *(unsigned long long *)$canary
set *(unsigned long long *)$canary = 0x123456789abcdef0
set $sp = $sp - 32
stepi
if *(unsigned long long *)$canary != 0x123456789abcdef0
echo Bootstrap debug step overwrote the live stack\\n
quit 1
end
set *(unsigned long long *)$canary = $saved
set $sp = $bootstrap_sp
set *(unsigned int *)$entry = $instruction
set $pc = $entry
echo Bootstrap debug step preserved the live stack\\n
set logging enabled off
detach
quit
"
);
let result = run_guest_and_gdb(&cmd_file_path, &out_file_path, &cmd, |contents| {
contents.contains("Bootstrap debug step preserved the live stack")
});
cleanup(&out_file_path, &cmd_file_path);
assert!(result.is_ok(), "{}", result.unwrap_err());
}

#[test]
#[cfg(target_arch = "aarch64")]
#[serial]
fn test_gdb_continue_over_sw_breakpoint() {
for step_commands in [
"",
"set $return_pc = $x30
stepi
if $pc != $return_pc
echo Software breakpoint step missed its destination\\n
quit 1
end",
] {
let (out_file_path, cmd_file_path, manifest_dir) =
gdb_test_paths("gdb-aarch64-continue");
let cmd = format!(
"file {manifest_dir}/../tests/rust_guests/bin/debug/simpleguest
target remote :8080
set pagination off
set logging file {out_file_path}
set logging enabled on
break simpleguest::no_op
break simpleguest::print_output
commands 2
echo Continued over ARM64 software breakpoint\\n
set logging enabled off
detach
quit
end
continue
{step_commands}
continue
"
);
#[cfg(windows)]
let cmd = format!("set osabi none\n{cmd}");
let checker =
|contents: String| contents.contains("Continued over ARM64 software breakpoint");
let result = run_guest_and_gdb(&cmd_file_path, &out_file_path, &cmd, checker);

cleanup(&out_file_path, &cmd_file_path);
assert!(result.is_ok(), "{}", result.unwrap_err());
}
}

#[test]
#[cfg(target_arch = "aarch64")]
#[serial]
fn test_gdb_hardware_breakpoint_and_step() {
let (out_file_path, cmd_file_path, manifest_dir) = gdb_test_paths("gdb-aarch64-hardware");
let cmd = format!(
"set osabi none
file {manifest_dir}/../tests/rust_guests/bin/debug/simpleguest
target remote :8080
set pagination off
set logging file {out_file_path}
set logging enabled on
set $entry_pc = $pc
stepi
if $pc != $entry_pc + 4
echo Entry step did not execute one instruction\\n
quit 1
end
hbreak simpleguest::no_op
continue
set $return_pc = $x30
stepi
if $pc != $return_pc
echo Hardware RET step missed its destination\\n
quit 1
end
echo Hardware breakpoint and entry/RET stepping passed\\n
set logging enabled off
detach
quit
"
);
let checker = |contents: String| {
contents.contains("Hardware breakpoint and entry/RET stepping passed")
};
let result = run_guest_and_gdb(&cmd_file_path, &out_file_path, &cmd, checker);
cleanup(&out_file_path, &cmd_file_path);
assert!(result.is_ok(), "{}", result.unwrap_err());
}

#[test]
#[cfg(target_arch = "aarch64")]
#[serial]
fn test_gdb_code_writes_and_self_loop() {
let (out_file_path, cmd_file_path, manifest_dir) =
gdb_test_paths("gdb-aarch64-code-writes");
let cmd = format!(
"set osabi none
file {manifest_dir}/../tests/rust_guests/bin/debug/simpleguest
target remote :8080
set pagination off
set logging file {out_file_path}
set logging enabled on
hbreak simpleguest::no_op
continue
delete 1
set language c
set $loop_pc = $pc
set $instruction = *(unsigned int *)$pc
set *(unsigned int *)$pc = 0x14000000
stepi
if $pc != $loop_pc
echo Self-loop step missed its destination\\n
quit 1
end
set *(unsigned int *)$pc = $instruction
set $return_pc = $x30
stepi
if $pc != $return_pc
echo Restored RET missed its destination\\n
quit 1
end
echo Code writes and self-loop stepping passed\\n
set logging enabled off
detach
quit
"
);
let checker =
|contents: String| contents.contains("Code writes and self-loop stepping passed");
let result = run_guest_and_gdb(&cmd_file_path, &out_file_path, &cmd, checker);
cleanup(&out_file_path, &cmd_file_path);
assert!(result.is_ok(), "{}", result.unwrap_err());
}

#[test]
#[cfg(target_arch = "aarch64")]
#[serial]
fn test_gdb_single_step() {
let (out_file_path, cmd_file_path, manifest_dir) = gdb_test_paths("gdb-aarch64-step");
let cmd = format!(
"file {manifest_dir}/../tests/rust_guests/bin/debug/simpleguest
target remote :8080
set pagination off
set logging file {out_file_path}
set logging enabled on
break simpleguest::no_op
stepi
echo Stepped over ARM64 linear instruction\\n
continue
stepi
echo Stepped over ARM64 RET\\n
set logging enabled off
detach
quit
"
);
#[cfg(windows)]
let cmd = format!("set osabi none\n{cmd}");
let checker = |contents: String| {
contents.contains("Stepped over ARM64 linear instruction")
&& contents.contains("Stepped over ARM64 RET")
};
let result = run_guest_and_gdb(&cmd_file_path, &out_file_path, &cmd, checker);

cleanup(&out_file_path, &cmd_file_path);
assert!(result.is_ok(), "{}", result.unwrap_err());
}

#[test]
#[cfg(target_arch = "x86_64")]
#[serial]
fn test_gdb_sse_check() {
let (out_file_path, cmd_file_path, manifest_dir) = gdb_test_paths("gdb-sse");
Expand Down
Loading
Loading