Skip to content
Draft
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: 2 additions & 1 deletion lib/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ wasmer-derive = { path = "../derive", version = "=7.1.0" }
wasmer-types = { path = "../types", version = "=7.1.0" }
target-lexicon = { workspace = true, default-features = false }
# - Optional dependencies for `sys`.
wasmer-compiler-singlepass = { path = "../compiler-singlepass", version = "=7.1.0", optional = true }
wasmer-compiler-singlepass = { path = "../compiler-singlepass", version = "=7.1.0", optional = true, default-features = false, features = ["std", "unwind"] }
wasmer-compiler-cranelift = { path = "../compiler-cranelift", version = "=7.1.0", optional = true }
wasmer-compiler-llvm = { path = "../compiler-llvm", version = "=7.1.0", optional = true }
corosensei = { workspace = true, optional = true }
Expand Down Expand Up @@ -145,6 +145,7 @@ compiler = [
"wasmer-compiler/compiler",
]
singlepass = ["compiler", "wasmer-compiler-singlepass"]
singlepass_rayon = ["wasmer-compiler-singlepass?/rayon"]
cranelift = ["compiler", "wasmer-compiler-cranelift"]
llvm = ["compiler", "wasmer-compiler-llvm"]

Expand Down
11 changes: 11 additions & 0 deletions lib/api/src/backend/sys/entities/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,17 @@ impl Module {
}
}

#[cfg(not(target_arch = "wasm32"))]
/// Extracts all local function locations for external usage
/// 3 parameters are: local function index, starting address, function length
pub fn local_function_infos(&self) -> Vec<(u32, usize, usize)> {
self.artifact
.finished_function_extents()
.into_iter()
.map(|(index, extent)| (index.as_u32(), extent.ptr.0 as usize, extent.length))
.collect()
}

pub(crate) fn name(&self) -> Option<&str> {
self.info().name.as_deref()
}
Expand Down
37 changes: 37 additions & 0 deletions lib/api/src/entities/store/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,42 @@ impl StoreContext {
})
}

/// # Safety
///
/// Only use this in cases where you know some other code is in process
/// of StorePtrWrapper (and can trigger a drop). One example is using
/// wasmer function in coroutines which can yield.
pub(crate) unsafe fn force_clean(id: StoreId) {
if !Self::is_active(id) {
return;
}
STORE_CONTEXT_STACK.with(|cell| {
let mut stack = cell.borrow_mut();
let top = stack.pop().expect("An entry must exist!");
assert_eq!(top.id, id);
});
}

/// # Safety
///
/// Only use this in cases where you know some other code is in process
/// of StorePtrWrapper (and can trigger a drop). One example is using
/// wasmer function in coroutines which can yield.
pub(crate) unsafe fn force_create(store_ptr: *mut StoreInner) {
let store_id = unsafe { store_ptr.as_ref().unwrap().objects.id() };
if Self::is_active(store_id) {
return;
}
STORE_CONTEXT_STACK.with(|cell| {
let mut stack = cell.borrow_mut();
stack.push(Self {
id: store_id,
borrow_count: 1,
entry: UnsafeCell::new(StoreContextEntry::Sync(store_ptr)),
});
});
}

/// Returns true if there are no active store context entries.
pub(crate) fn is_empty() -> bool {
STORE_CONTEXT_STACK.with(|cell| {
Expand Down Expand Up @@ -346,6 +382,7 @@ impl Drop for StorePtrWrapper {
let id = self.as_mut().objects_mut().id();
STORE_CONTEXT_STACK.with(|cell| {
let mut stack = cell.borrow_mut();
let ids: Vec<_> = stack.iter().map(|s| (s.id, s.borrow_count)).collect();
let top = stack
.last_mut()
.expect("No store context installed on this thread");
Expand Down
20 changes: 20 additions & 0 deletions lib/api/src/entities/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,26 @@ impl Store {
self.inner.store.engine_mut()
}

#[cfg(feature = "sys")]
#[inline]
/// Cleaning current store context, see StoreContext::force_clean
/// for details
pub fn force_clean(&mut self) {
unsafe {
StoreContext::force_clean(self.id());
}
}

#[cfg(feature = "sys")]
#[inline]
/// Re-create current store context, see StoreContext::force_create
/// for details
pub fn force_create(&mut self) {
unsafe {
StoreContext::force_create(self.as_store_mut().inner as *mut _);
}
}

/// Checks whether two stores are identical. A store is considered
/// equal to another store if both have the same engine.
pub fn same(a: &Self, b: &Self) -> bool {
Expand Down
14 changes: 8 additions & 6 deletions lib/compiler-llvm/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,16 +321,18 @@ impl LLVM {

let target_triple = self.target_triple(target);
let llvm_target = InkwellTarget::from_triple(&target_triple).unwrap();
let mut llvm_target_machine_options = TargetMachineOptions::new()
let llvm_target_machine_options = TargetMachineOptions::new()
.set_cpu(match triple.architecture {
Architecture::Riscv64(_) => "generic-rv64",
Architecture::Riscv32(_) => "generic-rv32",
Architecture::LoongArch64 => "generic-la64",
_ => "generic",
})
.set_features(match triple.architecture {
Architecture::Riscv64(_) => "+m,+a,+c,+d,+f",
Architecture::Riscv32(_) => "+m,+a,+c,+d,+f",
// Our RISC-V experiment only uses rv[32|64]im. Floating point, compressed
// instruction, atomic operations are not supported.
Architecture::Riscv64(_) => "+m",
Architecture::Riscv32(_) => "+m",
Architecture::LoongArch64 => "+f,+d",
_ => &llvm_cpu_features,
})
Expand All @@ -346,9 +348,9 @@ impl LLVM {
}
_ => self.code_model(self.target_binary_format(target)),
});
if let Architecture::Riscv64(_) = triple.architecture {
llvm_target_machine_options = llvm_target_machine_options.set_abi("lp64d");
}
// if let Architecture::Riscv64(_) = triple.architecture {
// llvm_target_machine_options = llvm_target_machine_options.set_abi("lp64d");
// }
let target_machine = llvm_target
.create_target_machine_from_options(&target_triple, llvm_target_machine_options)
.unwrap();
Expand Down
65 changes: 62 additions & 3 deletions lib/compiler-llvm/src/object_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,64 @@ static LIBCALLS_ELF: phf::Map<&'static str, LibCall> = phf::phf_map! {
"wasmer_eh_personality2" => LibCall::EHPersonality2,
"wasmer_vm_dbg_usize" => LibCall::DebugUsize,
"wasmer_vm_dbg_str" => LibCall::DebugStr,
// Floating-point & 64-bit mul/div routie functions. Those might
// be generated by LLVM when generating objects for 32-bit platforms,
// or platforms without floating point supports. Modern Rust & C toolchains
// provide implementations for thos functions by default.
"__adddf3" => LibCall::Adddf3,
"__addsf3" => LibCall::Addsf3,
"__divdf3" => LibCall::Divdf3,
"__divdi3" => LibCall::Divdi3,
"__divsf3" => LibCall::Divsf3,
"__divsi3" => LibCall::Divsi3,
"__eqdf2" => LibCall::Eqdf2,
"__eqsf2" => LibCall::Eqsf2,
"__extendsfdf2" => LibCall::Extendsfdf2,
"__fixdfdi" => LibCall::Fixdfdi,
"__fixdfsi" => LibCall::Fixdfsi,
"__fixsfdi" => LibCall::Fixsfdi,
"__fixsfsi" => LibCall::Fixsfsi,
"__fixunsdfdi" => LibCall::Fixunsdfdi,
"__fixunsdfsi" => LibCall::Fixunsdfsi,
"__fixunssfdi" => LibCall::Fixunssfdi,
"__fixunssfsi" => LibCall::Fixunssfsi,
"__floatdidf" => LibCall::Floatdidf,
"__floatdisf" => LibCall::Floatdisf,
"__floatsidf" => LibCall::Floatsidf,
"__floatsisf" => LibCall::Floatsisf,
"__floatundidf" => LibCall::Floatundidf,
"__floatundisf" => LibCall::Floatundisf,
"__floatunsidf" => LibCall::Floatunsidf,
"__floatunsisf" => LibCall::Floatunsisf,
"__gedf2" => LibCall::Gedf2,
"__gesf2" => LibCall::Gesf2,
"__gtdf2" => LibCall::Gtdf2,
"__gtsf2" => LibCall::Gtsf2,
"__ledf2" => LibCall::Ledf2,
"__lesf2" => LibCall::Lesf2,
"__ltdf2" => LibCall::Ltdf2,
"__ltsf2" => LibCall::Ltsf2,
"__moddi3" => LibCall::Moddi3,
"__modsi3" => LibCall::Modsi3,
"__muldf3" => LibCall::Muldf3,
"__muldi3" => LibCall::Muldi3,
"__mulsf3" => LibCall::Mulsf3,
"__mulsi3" => LibCall::Mulsi3,
"__nedf2" => LibCall::Nedf2,
"__negdf2" => LibCall::Negdf2,
"__negsf2" => LibCall::Negsf2,
"__nesf2" => LibCall::Nesf2,
"__subdf3" => LibCall::Subdf3,
"__subsf3" => LibCall::Subsf3,
"__truncdfsf2" => LibCall::Truncdfsf2,
"__udivdi3" => LibCall::Udivdi3,
"__udivsi3" => LibCall::Udivsi3,
"__umoddi3" => LibCall::Umoddi3,
"__umodsi3" => LibCall::Umodsi3,
"__unorddf2" => LibCall::Unorddf2,
"__unordsf2" => LibCall::Unordsf2,
"memset" => LibCall::Memset,
"sqrt" => LibCall::Sqrt,
};

static LIBCALLS_MACHO: phf::Map<&'static str, LibCall> = phf::phf_map! {
Expand Down Expand Up @@ -755,7 +813,7 @@ where
object::macho::ARM64_RELOC_ADDEND => RelocationKind::MachoArm64RelocAddend,
_ => {
return Err(CompileError::Codegen(format!(
"unknown relocation {reloc:?}",
"unknown relocation 1 {reloc:?}",
)));
}
},
Expand All @@ -780,13 +838,14 @@ where
object::macho::X86_64_RELOC_TLV => RelocationKind::MachoX86_64RelocTlv,
_ => {
return Err(CompileError::Codegen(format!(
"unknown relocation {reloc:?}"
"unknown relocation 2 {reloc:?}"
)));
}
},
_ => {
return Err(CompileError::Codegen(format!(
"unknown relocation {reloc:?}",
"unknown relocation 3 {reloc:?}, arch: {:?}",
obj.architecture(),
)));
}
};
Expand Down
40 changes: 22 additions & 18 deletions lib/compiler/src/engine/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,24 +721,7 @@ impl Artifact {
return Ok(()); // already done
}

let finished_function_extents = self
.allocated
.as_ref()
.expect("It must be allocated")
.finished_functions
.values()
.copied()
.zip(
self.allocated
.as_ref()
.expect("It must be allocated")
.finished_function_lengths
.values()
.copied(),
)
.map(|(ptr, length)| FunctionExtent { ptr, length })
.collect::<PrimaryMap<LocalFunctionIndex, _>>()
.into_boxed_slice();
let finished_function_extents = self.finished_function_extents();

let frame_info_registration = &mut self
.allocated
Expand Down Expand Up @@ -785,6 +768,27 @@ impl Artifact {
.finished_functions
}

/// Returns function extents for external processing, e.g., debugging & profiling
pub fn finished_function_extents(&self) -> BoxedSlice<LocalFunctionIndex, FunctionExtent> {
self.allocated
.as_ref()
.expect("It must be allocated")
.finished_functions
.values()
.copied()
.zip(
self.allocated
.as_ref()
.expect("It must be allocated")
.finished_function_lengths
.values()
.copied(),
)
.map(|(ptr, length)| FunctionExtent { ptr, length })
.collect::<PrimaryMap<LocalFunctionIndex, _>>()
.into_boxed_slice()
}

/// Returns the function call trampolines allocated in memory of this
/// `Artifact`, ready to be run.
pub fn finished_function_call_trampolines(&self) -> &BoxedSlice<SignatureIndex, VMTrampoline> {
Expand Down
4 changes: 2 additions & 2 deletions lib/compiler/src/engine/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ fn apply_relocation(
RelocationKind::Aarch64AdrPrelPgHi21 => unsafe {
let (reloc_address, delta) = r.for_address(body, target_func_address as u64);

let delta = delta as isize;
let delta = delta as i64;
assert!(
((-1 << 32)..(1 << 32)).contains(&delta),
((-1i64 << 32)..(1i64 << 32)).contains(&delta),
"can't generate page-relative relocation with ±4GB `adrp` instruction"
);

Expand Down
20 changes: 17 additions & 3 deletions lib/compiler/src/engine/unwind/dummy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Module for Dummy unwind registry.

use wasmer_types::CompiledFunctionUnwindInfo;
use crate::types::unwind::CompiledFunctionUnwindInfoReference;

/// Represents a registry of function unwind information when the host system
/// support any one in specific.
Expand All @@ -18,14 +18,28 @@ impl DummyUnwindRegistry {
_base_address: usize,
_func_start: u32,
_func_len: u32,
_info: &CompiledFunctionUnwindInfo,
_info: &CompiledFunctionUnwindInfoReference,
) -> Result<(), String> {
// Do nothing
Ok(())
}

pub(crate) fn register_compact_unwind(
&mut self,
_compact_unwind: Option<&[u8]>,
_eh_personality_addr_in_got: Option<usize>,
) -> Result<(), String> {
// Do nothing
Ok(())
}

/// Publishes all registered functions.
pub fn publish(&mut self, eh_frame: Option<&[u8]>) -> Result<(), String> {
pub fn publish(&mut self, _eh_frame: Option<&[u8]>) -> Result<(), String> {
// Do nothing
Ok(())
}

pub fn publish_eh_frame(&mut self, _eh_frame: Option<&[u8]>) -> Result<(), String> {
// Do nothing
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/src/engine/unwind/systemv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,4 @@ impl Drop for UnwindRegistry {
}
}
}
}
}
18 changes: 9 additions & 9 deletions lib/compiler/src/object/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
},
};
use object::{
FileFlags, RelocationEncoding, RelocationFlags, RelocationKind, SectionKind, SymbolFlags,
RelocationEncoding, RelocationFlags, RelocationKind, SectionKind, SymbolFlags,
SymbolKind, SymbolScope, elf, macho,
write::{
Object, Relocation, StandardSection, StandardSegment, Symbol as ObjSymbol, SymbolId,
Expand Down Expand Up @@ -67,15 +67,15 @@ pub fn get_object_for_target(triple: &Triple) -> Result<Object<'static>, ObjectE
Endianness::Big => object::Endianness::Big,
};

let mut object = Object::new(obj_binary_format, obj_architecture, obj_endianness);
let object = Object::new(obj_binary_format, obj_architecture, obj_endianness);

if let Architecture::Riscv64(_) = triple.architecture {
object.flags = FileFlags::Elf {
e_flags: elf::EF_RISCV_FLOAT_ABI_DOUBLE,
os_abi: 2,
abi_version: 0,
};
}
// if let Architecture::Riscv64(_) = triple.architecture {
// object.flags = FileFlags::Elf {
// e_flags: elf::EF_RISCV_FLOAT_ABI_DOUBLE,
// os_abi: 2,
// abi_version: 0,
// };
// }

Ok(object)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ wasmparser = { workspace = true, default-features = false, optional = true }

# `rand` uses `getrandom` transitively, and to be able to
# compile the project for `js`, we need to enable this feature
[dependencies.getrandom]
[target.'cfg(not(target_os = "zkvm"))'.dependencies.getrandom]
workspace = true
features = ["wasm_js"]

Expand Down
Loading
Loading