Skip to content

Commit

Permalink
Merge with upstream (bytecodealliance#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhil authored Jun 13, 2024
2 parents 78b813d + 119daef commit 2ec32ec
Show file tree
Hide file tree
Showing 68 changed files with 1,714 additions and 734 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions cranelift/codegen/src/isa/aarch64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,23 @@ impl TargetIsa for AArch64Backend {
inst::Inst::function_alignment()
}

fn page_size_align_log2(&self) -> u8 {
use target_lexicon::*;
match self.triple().operating_system {
OperatingSystem::MacOSX { .. }
| OperatingSystem::Darwin
| OperatingSystem::Ios
| OperatingSystem::Tvos => {
debug_assert_eq!(1 << 14, 0x4000);
14
}
_ => {
debug_assert_eq!(1 << 16, 0x10000);
16
}
}
}

#[cfg(feature = "disas")]
fn to_capstone(&self) -> Result<capstone::Capstone, capstone::Error> {
use capstone::prelude::*;
Expand Down
13 changes: 13 additions & 0 deletions cranelift/codegen/src/isa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ pub struct TargetFrontendConfig {

/// The pointer width of the target.
pub pointer_width: PointerWidth,

/// The log2 of the target's page size and alignment.
///
/// Note that this may be an upper-bound that is larger than necessary for
/// some platforms since it may depend on runtime configuration.
pub page_size_align_log2: u8,
}

impl TargetFrontendConfig {
Expand Down Expand Up @@ -333,6 +339,12 @@ pub trait TargetIsa: fmt::Display + Send + Sync {
/// alignment, for performance, required by this ISA.
fn function_alignment(&self) -> FunctionAlignment;

/// The log2 of the target's page size and alignment.
///
/// Note that this may be an upper-bound that is larger than necessary for
/// some platforms since it may depend on runtime configuration.
fn page_size_align_log2(&self) -> u8;

/// Create a polymorphic TargetIsa from this specific implementation.
fn wrapped(self) -> OwnedTargetIsa
where
Expand Down Expand Up @@ -433,6 +445,7 @@ impl<'a> dyn TargetIsa + 'a {
TargetFrontendConfig {
default_call_conv: self.default_call_conv(),
pointer_width: self.pointer_width(),
page_size_align_log2: self.page_size_align_log2(),
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions cranelift/codegen/src/isa/riscv64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ impl TargetIsa for Riscv64Backend {
inst::Inst::function_alignment()
}

fn page_size_align_log2(&self) -> u8 {
debug_assert_eq!(1 << 12, 0x1000);
12
}

#[cfg(feature = "disas")]
fn to_capstone(&self) -> Result<capstone::Capstone, capstone::Error> {
use capstone::prelude::*;
Expand Down
5 changes: 5 additions & 0 deletions cranelift/codegen/src/isa/s390x/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ impl TargetIsa for S390xBackend {
inst::Inst::function_alignment()
}

fn page_size_align_log2(&self) -> u8 {
debug_assert_eq!(1 << 12, 0x1000);
12
}

#[cfg(feature = "disas")]
fn to_capstone(&self) -> Result<capstone::Capstone, capstone::Error> {
use capstone::prelude::*;
Expand Down
5 changes: 5 additions & 0 deletions cranelift/codegen/src/isa/x64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ impl TargetIsa for X64Backend {
Inst::function_alignment()
}

fn page_size_align_log2(&self) -> u8 {
debug_assert_eq!(1 << 12, 0x1000);
12
}

#[cfg(feature = "disas")]
fn to_capstone(&self) -> Result<capstone::Capstone, capstone::Error> {
use capstone::prelude::*;
Expand Down
1 change: 1 addition & 0 deletions cranelift/frontend/src/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,7 @@ mod tests {
TargetFrontendConfig {
default_call_conv: CallConv::SystemV,
pointer_width: PointerWidth::U64,
page_size_align_log2: 12,
}
}

Expand Down
11 changes: 7 additions & 4 deletions cranelift/isle/isle/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,17 +639,15 @@ impl<L: Length, C> Length for ContextIterWrapper<L, C> {{
)?;
write!(ctx.out, "{}", &ctx.indent)?;
match ret_kind {
ReturnKind::Plain => write!(ctx.out, "return ")?,
ReturnKind::Option => write!(ctx.out, "return Some(")?,
ReturnKind::Plain | ReturnKind::Option => write!(ctx.out, "return ")?,
ReturnKind::Iterator => write!(ctx.out, "returns.extend(Some(")?,
}
self.emit_expr(ctx, result)?;
if ctx.is_ref.contains(&result) {
write!(ctx.out, ".clone()")?;
}
match ret_kind {
ReturnKind::Plain => writeln!(ctx.out, ";")?,
ReturnKind::Option => writeln!(ctx.out, ");")?,
ReturnKind::Plain | ReturnKind::Option => writeln!(ctx.out, ";")?,
ReturnKind::Iterator => {
writeln!(ctx.out, "));")?;
writeln!(
Expand Down Expand Up @@ -753,6 +751,11 @@ impl<L: Length, C> Length for ContextIterWrapper<L, C> {{
Ok(())
}

&Binding::MakeSome { inner } => {
write!(ctx.out, "Some(")?;
self.emit_expr(ctx, inner)?;
write!(ctx.out, ")")
}
&Binding::MatchSome { source } => {
self.emit_expr(ctx, source)?;
write!(ctx.out, "?")
Expand Down
11 changes: 11 additions & 0 deletions cranelift/isle/isle/src/sema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,17 @@ impl Term {
matches!(self.kind, TermKind::EnumVariant { .. })
}

/// Is this term partial?
pub fn is_partial(&self) -> bool {
matches!(
self.kind,
TermKind::Decl {
flags: TermFlags { partial: true, .. },
..
}
)
}

/// Does this term have a constructor?
pub fn has_constructor(&self) -> bool {
matches!(
Expand Down
11 changes: 11 additions & 0 deletions cranelift/isle/isle/src/trie_again.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ pub enum Binding {
/// get the field names.
field: TupleIndex,
},
/// The result of constructing an Option::Some variant.
MakeSome {
/// Contained expression.
inner: BindingId,
},
/// Pattern-match one of the previous bindings against `Option::Some` and produce a new binding
/// from its contents. There must be a corresponding [Constraint::Some] for each `source` that
/// appears in a `MatchSome` binding. (This currently only happens with external extractors.)
Expand Down Expand Up @@ -250,6 +255,7 @@ impl Binding {
Binding::Iterator { source } => std::slice::from_ref(source),
Binding::MakeVariant { fields, .. } => &fields[..],
Binding::MatchVariant { source, .. } => std::slice::from_ref(source),
Binding::MakeSome { inner } => std::slice::from_ref(inner),
Binding::MatchSome { source } => std::slice::from_ref(source),
Binding::MatchTuple { source, .. } => std::slice::from_ref(source),
}
Expand Down Expand Up @@ -384,6 +390,11 @@ impl RuleSetBuilder {
self.current_rule.pos = rule.pos;
self.current_rule.prio = rule.prio;
self.current_rule.result = rule.visit(self, termenv);
if termenv.terms[rule.root_term.index()].is_partial() {
self.current_rule.result = self.dedup_binding(Binding::MakeSome {
inner: self.current_rule.result,
});
}
self.normalize_equivalence_classes();
let rule = std::mem::take(&mut self.current_rule);

Expand Down
22 changes: 20 additions & 2 deletions cranelift/wasm/src/code_translator/bounds_checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ where
let spectre_mitigations_enabled = env.heap_access_spectre_mitigation();
let pcc = env.proof_carrying_code();

let host_page_size_log2 = env.target_config().page_size_align_log2;
let can_use_virtual_memory = heap.page_size_log2 >= host_page_size_log2;

let make_compare = |builder: &mut FunctionBuilder,
compare_kind: IntCC,
lhs: ir::Value,
Expand Down Expand Up @@ -188,7 +191,9 @@ where
// offset immediates -- which is a common code pattern when accessing
// multiple fields in the same struct that is in linear memory --
// will all emit the same `index > bound` check, which we can GVN.
HeapStyle::Dynamic { bound_gv } if offset_and_size <= heap.offset_guard_size => {
HeapStyle::Dynamic { bound_gv }
if can_use_virtual_memory && offset_and_size <= heap.offset_guard_size =>
{
let bound = get_dynamic_heap_bound(builder, env, heap);
let oob = make_compare(
builder,
Expand Down Expand Up @@ -313,6 +318,10 @@ where
// bound`, since we will end up being out-of-bounds regardless of the
// given `index`.
HeapStyle::Static { bound } if offset_and_size > bound.into() => {
assert!(
can_use_virtual_memory,
"static memories require the ability to use virtual memory"
);
env.before_unconditionally_trapping_memory_access(builder)?;
builder.ins().trap(ir::TrapCode::HeapOutOfBounds);
Unreachable
Expand Down Expand Up @@ -357,10 +366,15 @@ where
// within the guard page region, neither of which require emitting an
// explicit bounds check.
HeapStyle::Static { bound }
if heap.index_type == ir::types::I32
if can_use_virtual_memory
&& heap.index_type == ir::types::I32
&& u64::from(u32::MAX)
<= u64::from(bound) + u64::from(heap.offset_guard_size) - offset_and_size =>
{
assert!(
can_use_virtual_memory,
"static memories require the ability to use virtual memory"
);
Reachable(compute_addr(
&mut builder.cursor(),
heap,
Expand All @@ -386,6 +400,10 @@ where
// precise, not rely on the virtual memory subsystem at all, and not
// factor in the guard pages here.
HeapStyle::Static { bound } => {
assert!(
can_use_virtual_memory,
"static memories require the ability to use virtual memory"
);
// NB: this subtraction cannot wrap because we didn't hit the first
// special case.
let adjusted_bound = u64::from(bound) - offset_and_size;
Expand Down
3 changes: 3 additions & 0 deletions cranelift/wasm/src/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ pub struct HeapData {

/// The memory type for the pointed-to memory, if using proof-carrying code.
pub memory_type: Option<MemoryType>,

/// The log2 of this memory's page size.
pub page_size_log2: u8,
}

/// Style of heap including style-specific information.
Expand Down
20 changes: 5 additions & 15 deletions cranelift/wasm/src/sections_translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use crate::environ::ModuleEnvironment;
use crate::wasm_unsupported;
use crate::{
DataIndex, ElemIndex, FuncIndex, GlobalIndex, Memory, MemoryIndex, TableIndex, Tag, TagIndex,
DataIndex, ElemIndex, FuncIndex, GlobalIndex, MemoryIndex, TableIndex, Tag, TagIndex,
TypeIndex, WasmError, WasmResult,
};
use cranelift_entity::packed_option::ReservedValue;
Expand All @@ -20,20 +20,11 @@ use std::vec::Vec;
use wasmparser::{
Data, DataKind, DataSectionReader, Element, ElementItems, ElementKind, ElementSectionReader,
Export, ExportSectionReader, ExternalKind, FunctionSectionReader, GlobalSectionReader,
ImportSectionReader, MemorySectionReader, MemoryType, Operator, TableSectionReader,
TagSectionReader, TagType, TypeRef, TypeSectionReader,
ImportSectionReader, MemorySectionReader, Operator, TableSectionReader, TagSectionReader,
TagType, TypeRef, TypeSectionReader,
};
use wasmtime_types::ConstExpr;

fn memory(ty: MemoryType) -> Memory {
Memory {
minimum: ty.initial,
maximum: ty.maximum,
shared: ty.shared,
memory64: ty.memory64,
}
}

fn tag(e: TagType) -> Tag {
match e.kind {
wasmparser::TagKind::Exception => Tag {
Expand Down Expand Up @@ -83,7 +74,7 @@ pub fn parse_import_section<'data>(
)?;
}
TypeRef::Memory(ty) => {
environ.declare_memory_import(memory(ty), import.module, import.name)?;
environ.declare_memory_import(ty.into(), import.module, import.name)?;
}
TypeRef::Tag(e) => {
environ.declare_tag_import(tag(e), import.module, import.name)?;
Expand Down Expand Up @@ -147,8 +138,7 @@ pub fn parse_memory_section(
environ.reserve_memories(memories.count())?;

for entry in memories {
let memory = memory(entry?);
environ.declare_memory(memory)?;
environ.declare_memory(entry?.into())?;
}

Ok(())
Expand Down
Loading

0 comments on commit 2ec32ec

Please sign in to comment.