Path: blob/main/crates/cranelift/src/translate/heap.rs
1692 views
//! Heaps to implement WebAssembly linear memories.12use cranelift_codegen::ir::{self, GlobalValue, MemoryType, Type};3use cranelift_entity::entity_impl;4use wasmtime_environ::{IndexType, Memory};56/// An opaque reference to a [`HeapData`][crate::HeapData].7///8/// While the order is stable, it is arbitrary.9#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]10pub struct Heap(u32);11entity_impl!(Heap, "heap");1213/// A heap implementing a WebAssembly linear memory.14///15/// Code compiled from WebAssembly runs in a sandbox where it can't access all16/// process memory. Instead, it is given a small set of memory areas to work in,17/// and all accesses are bounds checked. Wasmtime models this through18/// the concept of *heaps*.19///20/// Heap addresses can be smaller than the native pointer size, for example21/// unsigned `i32` offsets on a 64-bit architecture.22///23/// A heap appears as three consecutive ranges of address space:24///25/// 1. The *mapped pages* are the accessible memory range in the heap. A heap26/// may have a minimum guaranteed size which means that some mapped pages are27/// always present.28///29/// 2. The *unmapped pages* is a possibly empty range of address space that may30/// be mapped in the future when the heap is grown. They are addressable but31/// not accessible.32///33/// 3. The *offset-guard pages* is a range of address space that is guaranteed34/// to always cause a trap when accessed. It is used to optimize bounds35/// checking for heap accesses with a shared base pointer. They are36/// addressable but not accessible.37#[derive(Clone, PartialEq, Hash)]38pub struct HeapData {39/// The address of the start of the heap's storage.40pub base: GlobalValue,4142/// The dynamic byte length of this heap, if needed.43pub bound: GlobalValue,4445/// The type of wasm memory that this heap is operating on.46pub memory: Memory,4748/// The memory type for the pointed-to memory, if using proof-carrying code.49pub pcc_memory_type: Option<MemoryType>,50}5152impl HeapData {53pub fn index_type(&self) -> Type {54match self.memory.idx_type {55IndexType::I32 => ir::types::I32,56IndexType::I64 => ir::types::I64,57}58}59}606162