Skip to content

Commit 9dfe34e

Browse files
Fix UEFI compilation for cranelift-jit
1 parent f641800 commit 9dfe34e

7 files changed

Lines changed: 34 additions & 9 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cranelift/jit/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ cranelift-entity = { workspace = true }
2121
cranelift-control = { workspace = true }
2222
wasmtime-unwinder = { workspace = true, optional = true, features = ["cranelift"] }
2323
anyhow = { workspace = true }
24-
region = "3.0.2"
24+
region = { git = "https://github.com/RossComputerGuy/region-rs", branch = "fix/uefi" }
2525
libc = { workspace = true }
2626
target-lexicon = { workspace = true }
2727
memmap2 = { version = "0.2.1", optional = true }

cranelift/jit/src/backend.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ impl JITBuilder {
8888
libcall_names: Box<dyn Fn(ir::LibCall) -> String + Send + Sync>,
8989
) -> Self {
9090
let symbols = HashMap::new();
91+
92+
#[cfg(not(target_os = "uefi"))]
9193
let lookup_symbols = vec![Box::new(lookup_with_dlsym) as Box<_>];
94+
95+
#[cfg(target_os = "uefi")]
96+
let lookup_symbols = vec![];
97+
9298
Self {
9399
isa,
94100
symbols,
@@ -656,7 +662,7 @@ impl Module for JITModule {
656662
}
657663
}
658664

659-
#[cfg(not(windows))]
665+
#[cfg(all(not(windows), not(target_os = "uefi")))]
660666
fn lookup_with_dlsym(name: &str) -> Option<*const u8> {
661667
let c_str = CString::new(name).unwrap();
662668
let c_str_ptr = c_str.as_ptr();

cranelift/jit/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ mod compiled_blob;
1111
mod memory;
1212

1313
pub use crate::backend::{JITBuilder, JITModule};
14-
pub use crate::memory::{
15-
ArenaMemoryProvider, BranchProtection, JITMemoryProvider, SystemMemoryProvider,
16-
};
14+
pub use crate::memory::{BranchProtection, JITMemoryProvider, SystemMemoryProvider};
15+
16+
#[cfg(not(target_os = "uefi"))]
17+
pub use crate::memory::ArenaMemoryProvider;
1718

1819
/// Version number of this crate.
1920
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

cranelift/jit/src/memory/arena.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ impl Segment {
3939
}
4040

4141
fn set_rw(&mut self) {
42+
#[cfg(not(target_os = "uefi"))]
4243
unsafe {
4344
region::protect(self.ptr, self.len, region::Protection::READ_WRITE)
4445
.expect("unable to change memory protection for jit memory segment");
@@ -56,6 +57,7 @@ impl Segment {
5657
super::set_readable_and_executable(self.ptr, self.len, branch_protection)
5758
.expect("unable to set memory protection for jit memory segment");
5859
} else {
60+
#[cfg(not(target_os = "uefi"))]
5961
unsafe {
6062
region::protect(self.ptr, self.len, self.target_prot)
6163
.expect("unable to change memory protection for jit memory segment");
@@ -93,6 +95,7 @@ impl Segment {
9395
/// Note: Memory will be leaked by default unless
9496
/// [`JITMemoryProvider::free_memory`] is called to ensure function pointers
9597
/// remain valid for the remainder of the program's life.
98+
#[cfg(not(target_os = "uefi"))]
9699
pub struct ArenaMemoryProvider {
97100
alloc: ManuallyDrop<Option<region::Allocation>>,
98101
ptr: *mut u8,
@@ -101,8 +104,10 @@ pub struct ArenaMemoryProvider {
101104
segments: Vec<Segment>,
102105
}
103106

107+
#[cfg(not(target_os = "uefi"))]
104108
unsafe impl Send for ArenaMemoryProvider {}
105109

110+
#[cfg(not(target_os = "uefi"))]
106111
impl ArenaMemoryProvider {
107112
/// Create a new memory region with the given size.
108113
pub fn new_with_size(reserve_size: usize) -> Result<Self, region::Error> {
@@ -206,6 +211,7 @@ impl ArenaMemoryProvider {
206211
}
207212
}
208213

214+
#[cfg(not(target_os = "uefi"))]
209215
impl Drop for ArenaMemoryProvider {
210216
fn drop(&mut self) {
211217
if self.ptr == ptr::null_mut() {
@@ -220,6 +226,7 @@ impl Drop for ArenaMemoryProvider {
220226
}
221227
}
222228

229+
#[cfg(not(target_os = "uefi"))]
223230
impl JITMemoryProvider for ArenaMemoryProvider {
224231
fn allocate(&mut self, size: usize, align: u64, kind: JITMemoryKind) -> io::Result<*mut u8> {
225232
self.allocate_inner(

cranelift/jit/src/memory/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::io;
44
mod arena;
55
mod system;
66

7+
#[cfg(not(target_os = "uefi"))]
78
pub use arena::ArenaMemoryProvider;
89
pub use system::SystemMemoryProvider;
910

@@ -56,6 +57,7 @@ pub(crate) fn set_readable_and_executable(
5657
.expect("Failed cache clear")
5758
};
5859

60+
#[cfg(not(target_os = "uefi"))]
5961
unsafe {
6062
region::protect(ptr, len, region::Protection::READ_EXECUTE).map_err(|e| {
6163
ModuleError::Backend(

cranelift/jit/src/memory/system.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use cranelift_module::{ModuleError, ModuleResult};
22

3-
#[cfg(all(not(target_os = "windows"), feature = "selinux-fix"))]
3+
#[cfg(all(
4+
not(target_os = "windows"),
5+
not(target_os = "uefi"),
6+
feature = "selinux-fix"
7+
))]
48
use memmap2::MmapMut;
59

610
#[cfg(not(any(feature = "selinux-fix", windows)))]
@@ -94,7 +98,11 @@ impl PtrLen {
9498
}
9599

96100
// `MMapMut` from `cfg(feature = "selinux-fix")` already deallocates properly.
97-
#[cfg(all(not(target_os = "windows"), not(feature = "selinux-fix")))]
101+
#[cfg(all(
102+
not(target_os = "windows"),
103+
not(target_os = "uefi"),
104+
not(feature = "selinux-fix")
105+
))]
98106
impl Drop for PtrLen {
99107
fn drop(&mut self) {
100108
if !self.ptr.is_null() {
@@ -182,6 +190,7 @@ impl Memory {
182190
}
183191

184192
/// Set all memory allocated in this `Memory` up to now as readonly.
193+
#[cfg(not(target_os = "uefi"))]
185194
pub(crate) fn set_readonly(&mut self) -> ModuleResult<()> {
186195
self.finish_current();
187196

@@ -258,6 +267,7 @@ impl JITMemoryProvider for SystemMemoryProvider {
258267
}
259268

260269
fn finalize(&mut self, branch_protection: BranchProtection) -> ModuleResult<()> {
270+
#[cfg(not(target_os = "uefi"))]
261271
self.readonly.set_readonly()?;
262272
self.code.set_readable_and_executable(branch_protection)
263273
}

0 commit comments

Comments
 (0)