Skip to content

Commit b63d425

Browse files
committed
Allow compiling debug-builtins in no_std environment to enable gdb jit
- Make jit-debug crate #![no_std] when perf_jitdebug is not enabled - Make gdb_jit_int module build in no_std environment - Use std when perf_jitdebug is enabled because the perf_jitdebug module does not run in no_std environment - Split debug-builtins feature into debug-builtins and perf-builtins to individually toggle: - the no_std gdb_jit_int module by using debug-builtins - the std perf_jitdebug module using perf-builtins. NOTE: They can be both enabled, but it won't support no_std. Signed-off-by: Doru Blânzeanu <dblnz@pm.me>
1 parent c34eb3f commit b63d425

10 files changed

Lines changed: 53 additions & 12 deletions

File tree

Cargo.lock

Lines changed: 27 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ default = [
460460
"coredump",
461461
"addr2line",
462462
"debug-builtins",
463+
"perf-builtins",
463464
"component-model",
464465
"threads",
465466
"gc",
@@ -519,6 +520,7 @@ profiling = ["wasmtime/profiling", "wasmtime/call-hook"]
519520
coredump = ["wasmtime-cli-flags/coredump"]
520521
addr2line = ["wasmtime/addr2line"]
521522
debug-builtins = ["wasmtime/debug-builtins"]
523+
perf-builtins = ["wasmtime/perf-builtins"]
522524
threads = ["wasmtime-cli-flags/threads"]
523525
gc = ["wasmtime-cli-flags/gc", "wasmtime/gc"]
524526
gc-drc = ["gc", "wasmtime/gc-drc", "wasmtime-cli-flags/gc-drc"]

crates/jit-debug/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ wasmtime-versioned-export-macros = { workspace = true }
2020

2121
[dependencies]
2222
object = { workspace = true, optional = true }
23+
spin = "0.10.0"
2324
wasmtime-versioned-export-macros = { workspace = true }
2425

2526
[target.'cfg(target_os = "linux")'.dependencies]

crates/jit-debug/src/gdb_jit_int.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
//! the __jit_debug_register_code() and __jit_debug_descriptor to register
33
//! or unregister generated object images with debuggers.
44
5-
use std::pin::Pin;
6-
use std::ptr;
7-
use std::sync::Mutex;
5+
use alloc::boxed::Box;
6+
use alloc::vec::Vec;
7+
use core::pin::Pin;
8+
use core::ptr;
9+
use spin::mutex::Mutex;
810
use wasmtime_versioned_export_macros::versioned_link;
911

1012
#[repr(C)]
@@ -87,7 +89,7 @@ unsafe impl Sync for GdbJitImageRegistration {}
8789

8890
unsafe fn register_gdb_jit_image(entry: *mut JITCodeEntry) {
8991
unsafe {
90-
let _lock = GDB_REGISTRATION.lock().unwrap();
92+
let _lock = GDB_REGISTRATION.lock();
9193
let desc = &mut *wasmtime_jit_debug_descriptor();
9294

9395
// Add it to the linked list in the JIT descriptor.
@@ -109,7 +111,7 @@ unsafe fn register_gdb_jit_image(entry: *mut JITCodeEntry) {
109111

110112
unsafe fn unregister_gdb_jit_image(entry: *mut JITCodeEntry) {
111113
unsafe {
112-
let _lock = GDB_REGISTRATION.lock().unwrap();
114+
let _lock = GDB_REGISTRATION.lock();
113115
let desc = &mut *wasmtime_jit_debug_descriptor();
114116

115117
// Remove the code entry corresponding to the code from the linked list.

crates/jit-debug/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
//! > you're interested in using this feel free to file an issue on the
55
//! > Wasmtime repository to start a discussion about doing so, but otherwise
66
//! > be aware that your usage of this crate is not supported.
7+
#![no_std]
8+
9+
#[cfg(feature = "gdb_jit_int")]
10+
extern crate alloc;
11+
#[cfg(all(feature = "perf_jitdump", target_os = "linux"))]
12+
extern crate std;
713

814
#[cfg(feature = "gdb_jit_int")]
915
pub mod gdb_jit_int;

crates/jit-debug/src/perf_jitdump.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use std::io;
1616
use std::io::Write;
1717
use std::path::Path;
1818
use std::ptr;
19+
use std::string::String;
20+
use std::vec::Vec;
1921
use std::{mem, process};
2022

2123
/// Defines jitdump record types

crates/wasmtime/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ default = [
139139
'addr2line',
140140
'coredump',
141141
'debug-builtins',
142+
'perf-builtins',
142143
'runtime',
143144
'component-model',
144145
'threads',
@@ -243,7 +244,8 @@ coredump = ["dep:wasm-encoder", "runtime", "std"]
243244

244245
# Export some symbols from the final binary to assist in debugging
245246
# Cranelift-generated code with native debuggers like GDB and LLDB.
246-
debug-builtins = ["dep:wasmtime-jit-debug", "std"]
247+
debug-builtins = ["wasmtime-jit-debug/gdb_jit_int"]
248+
perf-builtins = ["wasmtime-jit-debug/perf_jitdump"]
247249

248250
# Enable support for executing compiled Wasm modules.
249251
runtime = [

crates/wasmtime/build.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ fn main() {
2727
custom_cfg("has_virtual_memory", has_virtual_memory);
2828
custom_cfg("has_host_compiler_backend", has_host_compiler_backend);
2929

30-
// If this OS isn't supported or if Cranelift doesn't support the host then
31-
// there's no need to build these helpers.
30+
// If this OS isn't supported or if Cranelift doesn't support the host or
31+
// if no debug-builtins enabled then there's no need to build these helpers.
3232
#[cfg(feature = "runtime")]
33-
if supported_os && has_host_compiler_backend {
33+
if (supported_os && has_host_compiler_backend) || cfg!(feature = "debug-builtins") {
3434
build_c_helpers();
3535
}
3636

crates/wasmtime/src/engine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl Engine {
109109
#[cfg(has_native_signals)]
110110
crate::runtime::vm::init_traps(config.macos_use_mach_ports);
111111
if !cfg!(miri) {
112-
#[cfg(all(has_host_compiler_backend, feature = "debug-builtins"))]
112+
#[cfg(feature = "debug-builtins")]
113113
crate::runtime::vm::debug_builtins::init();
114114
}
115115
}

crates/wasmtime/src/runtime/vm/debug_builtins.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static mut VMCTX_AND_MEMORY: (NonNull<VMContext>, usize) = (NonNull::dangling(),
1313

1414
#[versioned_export]
1515
pub unsafe extern "C" fn resolve_vmctx_memory_ptr(p: *const u32) -> *const u8 {
16-
let ptr = std::ptr::read(p);
16+
let ptr = core::ptr::read(p);
1717
assert!(
1818
VMCTX_AND_MEMORY.0 != NonNull::dangling(),
1919
"must call `__vmctx->set()` before resolving Wasm pointers"

0 commit comments

Comments
 (0)