Skip to content

Commit be76ea8

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

7 files changed

Lines changed: 26 additions & 7 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ mod memory;
1212

1313
pub use crate::backend::{JITBuilder, JITModule};
1414
pub use crate::memory::{
15-
ArenaMemoryProvider, BranchProtection, JITMemoryProvider, SystemMemoryProvider,
15+
BranchProtection, JITMemoryProvider, SystemMemoryProvider,
1616
};
1717

18+
#[cfg(not(target_os = "uefi"))]
19+
pub use crate::memory::ArenaMemoryProvider;
20+
1821
/// Version number of this crate.
1922
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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cranelift_module::{ModuleError, ModuleResult};
22

3-
#[cfg(all(not(target_os = "windows"), feature = "selinux-fix"))]
3+
#[cfg(all(not(target_os = "windows"), not(target_os = "uefi"), feature = "selinux-fix"))]
44
use memmap2::MmapMut;
55

66
#[cfg(not(any(feature = "selinux-fix", windows)))]
@@ -94,7 +94,7 @@ impl PtrLen {
9494
}
9595

9696
// `MMapMut` from `cfg(feature = "selinux-fix")` already deallocates properly.
97-
#[cfg(all(not(target_os = "windows"), not(feature = "selinux-fix")))]
97+
#[cfg(all(not(target_os = "windows"), not(target_os = "uefi"), not(feature = "selinux-fix")))]
9898
impl Drop for PtrLen {
9999
fn drop(&mut self) {
100100
if !self.ptr.is_null() {
@@ -182,6 +182,7 @@ impl Memory {
182182
}
183183

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

@@ -258,6 +259,7 @@ impl JITMemoryProvider for SystemMemoryProvider {
258259
}
259260

260261
fn finalize(&mut self, branch_protection: BranchProtection) -> ModuleResult<()> {
262+
#[cfg(not(target_os = "uefi"))]
261263
self.readonly.set_readonly()?;
262264
self.code.set_readable_and_executable(branch_protection)
263265
}

0 commit comments

Comments
 (0)