use super::{
check_max, combine_type_sizes,
operators::{ty_to_str, OperatorValidator, OperatorValidatorAllocations},
types::{EntityType, Type, TypeAlloc, TypeId, TypeList},
};
use crate::limits::*;
use crate::validator::core::arc::MaybeOwned;
use crate::{
BinaryReaderError, ConstExpr, Data, DataKind, Element, ElementKind, ExternalKind, FuncType,
Global, GlobalType, HeapType, MemoryType, RefType, Result, Table, TableInit, TableType,
TagType, TypeRef, ValType, VisitOperator, WasmFeatures, WasmFuncType, WasmModuleResources,
};
use indexmap::IndexMap;
use std::mem;
use std::{collections::HashSet, sync::Arc};
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Debug)]
pub enum Order {
Initial,
Type,
Import,
Function,
Table,
Memory,
Tag,
Global,
Export,
Start,
Element,
DataCount,
Code,
Data,
}
impl Default for Order {
fn default() -> Order {
Order::Initial
}
}
#[derive(Default)]
pub(crate) struct ModuleState {
pub module: arc::MaybeOwned<Module>,
order: Order,
pub data_segment_count: u32,
pub expected_code_bodies: Option<u32>,
const_expr_allocs: OperatorValidatorAllocations,
code_section_index: Option<usize>,
}
impl ModuleState {
pub fn update_order(&mut self, order: Order, offset: usize) -> Result<()> {
if self.order >= order {
return Err(BinaryReaderError::new("section out of order", offset));
}
self.order = order;
Ok(())
}
pub fn validate_end(&self, offset: usize) -> Result<()> {
if let Some(data_count) = self.module.data_count {
if data_count != self.data_segment_count {
return Err(BinaryReaderError::new(
"data count and data section have inconsistent lengths",
offset,
));
}
}
if let Some(n) = self.expected_code_bodies {
if n > 0 {
return Err(BinaryReaderError::new(
"function and code section have inconsistent lengths",
offset,
));
}
}
Ok(())
}
pub fn next_code_index_and_type(&mut self, offset: usize) -> Result<(u32, u32)> {
let index = self
.code_section_index
.get_or_insert(self.module.num_imported_functions as usize);
if *index >= self.module.functions.len() {
return Err(BinaryReaderError::new(
"code section entry exceeds number of functions",
offset,
));
}
let ty = self.module.functions[*index];
*index += 1;
Ok(((*index - 1) as u32, ty))
}
pub fn add_global(
&mut self,
global: Global,
features: &WasmFeatures,
types: &TypeList,
offset: usize,
) -> Result<()> {
self.module
.check_global_type(&global.ty, features, types, offset)?;
self.check_const_expr(&global.init_expr, global.ty.content_type, features, types)?;
self.module.assert_mut().globals.push(global.ty);
Ok(())
}
pub fn add_table(
&mut self,
table: Table<'_>,
features: &WasmFeatures,
types: &TypeList,
offset: usize,
) -> Result<()> {
self.module
.check_table_type(&table.ty, features, types, offset)?;
match &table.init {
TableInit::RefNull => {
if !table.ty.element_type.nullable {
bail!(offset, "type mismatch: non-defaultable element type");
}
}
TableInit::Expr(expr) => {
if !features.function_references {
bail!(
offset,
"tables with expression initializers require \
the function-references proposal"
);
}
self.check_const_expr(expr, table.ty.element_type.into(), features, types)?;
}
}
self.module.assert_mut().tables.push(table.ty);
Ok(())
}
pub fn add_data_segment(
&mut self,
data: Data,
features: &WasmFeatures,
types: &TypeList,
offset: usize,
) -> Result<()> {
match data.kind {
DataKind::Passive => Ok(()),
DataKind::Active {
memory_index,
offset_expr,
} => {
let ty = self.module.memory_at(memory_index, offset)?.index_type();
self.check_const_expr(&offset_expr, ty, features, types)
}
}
}
pub fn add_element_segment(
&mut self,
e: Element,
features: &WasmFeatures,
types: &TypeList,
offset: usize,
) -> Result<()> {
if e.ty != RefType::FUNCREF {
self.module
.check_value_type(ValType::Ref(e.ty), features, types, offset)?;
}
match e.kind {
ElementKind::Active {
table_index,
offset_expr,
} => {
let table = self.module.table_at(table_index, offset)?;
if !self
.module
.matches(ValType::Ref(e.ty), ValType::Ref(table.element_type), types)
{
return Err(BinaryReaderError::new(
format!(
"type mismatch: invalid element type `{}` for table type `{}`",
ty_to_str(e.ty.into()),
ty_to_str(table.element_type.into()),
),
offset,
));
}
self.check_const_expr(&offset_expr, ValType::I32, features, types)?;
}
ElementKind::Passive | ElementKind::Declared => {
if !features.bulk_memory {
return Err(BinaryReaderError::new(
"bulk memory must be enabled",
offset,
));
}
}
}
let validate_count = |count: u32| -> Result<(), BinaryReaderError> {
if count > MAX_WASM_TABLE_ENTRIES as u32 {
Err(BinaryReaderError::new(
"number of elements is out of bounds",
offset,
))
} else {
Ok(())
}
};
match e.items {
crate::ElementItems::Functions(reader) => {
let count = reader.count();
if !e.ty.nullable && count <= 0 {
return Err(BinaryReaderError::new(
"a non-nullable element must come with an initialization expression",
offset,
));
}
validate_count(count)?;
for f in reader.into_iter_with_offsets() {
let (offset, f) = f?;
self.module.get_func_type(f, types, offset)?;
self.module.assert_mut().function_references.insert(f);
}
}
crate::ElementItems::Expressions(reader) => {
validate_count(reader.count())?;
for expr in reader {
self.check_const_expr(&expr?, ValType::Ref(e.ty), features, types)?;
}
}
}
self.module.assert_mut().element_types.push(e.ty);
Ok(())
}
fn check_const_expr(
&mut self,
expr: &ConstExpr<'_>,
expected_ty: ValType,
features: &WasmFeatures,
types: &TypeList,
) -> Result<()> {
let mut validator = VisitConstOperator {
offset: 0,
order: self.order,
uninserted_funcref: false,
ops: OperatorValidator::new_const_expr(
features,
expected_ty,
mem::take(&mut self.const_expr_allocs),
),
resources: OperatorValidatorResources {
types,
module: &mut self.module,
},
};
let mut ops = expr.get_operators_reader();
while !ops.eof() {
validator.offset = ops.original_position();
ops.visit_operator(&mut validator)??;
}
validator.ops.finish(ops.original_position())?;
assert!(!validator.uninserted_funcref);
self.const_expr_allocs = validator.ops.into_allocations();
return Ok(());
struct VisitConstOperator<'a> {
offset: usize,
uninserted_funcref: bool,
ops: OperatorValidator,
resources: OperatorValidatorResources<'a>,
order: Order,
}
impl VisitConstOperator<'_> {
fn validator(&mut self) -> impl VisitOperator<'_, Output = Result<()>> {
self.ops.with_resources(&self.resources, self.offset)
}
fn validate_extended_const(&mut self) -> Result<()> {
if self.ops.features.extended_const {
Ok(())
} else {
Err(BinaryReaderError::new(
"constant expression required: non-constant operator",
self.offset,
))
}
}
fn validate_global(&mut self, index: u32) -> Result<()> {
let module = &self.resources.module;
let global = module.global_at(index, self.offset)?;
if index >= module.num_imported_globals {
return Err(BinaryReaderError::new(
"constant expression required: global.get of locally defined global",
self.offset,
));
}
if global.mutable {
return Err(BinaryReaderError::new(
"constant expression required: global.get of mutable global",
self.offset,
));
}
Ok(())
}
fn insert_ref_func(&mut self, index: u32) {
if self.order == Order::Data {
self.uninserted_funcref = true;
} else {
self.resources
.module
.assert_mut()
.function_references
.insert(index);
}
}
}
macro_rules! define_visit_operator {
($(@$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident)*) => {
$(
#[allow(unused_variables)]
fn $visit(&mut self $($(,$arg: $argty)*)?) -> Self::Output {
define_visit_operator!(@visit self $visit $($($arg)*)?)
}
)*
};
(@visit $self:ident visit_i32_const $val:ident) => {{
$self.validator().visit_i32_const($val)
}};
(@visit $self:ident visit_i64_const $val:ident) => {{
$self.validator().visit_i64_const($val)
}};
(@visit $self:ident visit_f32_const $val:ident) => {{
$self.validator().visit_f32_const($val)
}};
(@visit $self:ident visit_f64_const $val:ident) => {{
$self.validator().visit_f64_const($val)
}};
(@visit $self:ident visit_v128_const $val:ident) => {{
$self.validator().visit_v128_const($val)
}};
(@visit $self:ident visit_ref_null $val:ident) => {{
$self.validator().visit_ref_null($val)
}};
(@visit $self:ident visit_end) => {{
$self.validator().visit_end()
}};
(@visit $self:ident visit_i32_add) => {{
$self.validate_extended_const()?;
$self.validator().visit_i32_add()
}};
(@visit $self:ident visit_i32_sub) => {{
$self.validate_extended_const()?;
$self.validator().visit_i32_sub()
}};
(@visit $self:ident visit_i32_mul) => {{
$self.validate_extended_const()?;
$self.validator().visit_i32_mul()
}};
(@visit $self:ident visit_i64_add) => {{
$self.validate_extended_const()?;
$self.validator().visit_i64_add()
}};
(@visit $self:ident visit_i64_sub) => {{
$self.validate_extended_const()?;
$self.validator().visit_i64_sub()
}};
(@visit $self:ident visit_i64_mul) => {{
$self.validate_extended_const()?;
$self.validator().visit_i64_mul()
}};
(@visit $self:ident visit_global_get $idx:ident) => {{
$self.validate_global($idx)?;
$self.validator().visit_global_get($idx)
}};
(@visit $self:ident visit_ref_func $idx:ident) => {{
$self.insert_ref_func($idx);
$self.validator().visit_ref_func($idx)
}};
(@visit $self:ident $op:ident $($args:tt)*) => {{
Err(BinaryReaderError::new(
"constant expression required: non-constant operator",
$self.offset,
))
}}
}
impl<'a> VisitOperator<'a> for VisitConstOperator<'a> {
type Output = Result<()>;
for_each_operator!(define_visit_operator);
}
}
}
pub(crate) struct Module {
pub snapshot: Option<Arc<TypeList>>,
pub types: Vec<TypeId>,
pub tables: Vec<TableType>,
pub memories: Vec<MemoryType>,
pub globals: Vec<GlobalType>,
pub element_types: Vec<RefType>,
pub data_count: Option<u32>,
pub functions: Vec<u32>,
pub tags: Vec<TypeId>,
pub function_references: HashSet<u32>,
pub imports: IndexMap<(String, String), Vec<EntityType>>,
pub exports: IndexMap<String, EntityType>,
pub type_size: u32,
num_imported_globals: u32,
num_imported_functions: u32,
}
impl Module {
pub fn add_type(
&mut self,
ty: crate::Type,
features: &WasmFeatures,
types: &mut TypeAlloc,
offset: usize,
check_limit: bool,
) -> Result<()> {
let ty = match ty {
crate::Type::Func(t) => {
for ty in t.params().iter().chain(t.results()) {
self.check_value_type(*ty, features, types, offset)?;
}
if t.results().len() > 1 && !features.multi_value {
return Err(BinaryReaderError::new(
"func type returns multiple values but the multi-value feature is not enabled",
offset,
));
}
Type::Func(t)
}
};
if check_limit {
check_max(self.types.len(), 1, MAX_WASM_TYPES, "types", offset)?;
}
let id = types.push_defined(ty);
self.types.push(id);
Ok(())
}
pub fn add_import(
&mut self,
import: crate::Import,
features: &WasmFeatures,
types: &TypeList,
offset: usize,
) -> Result<()> {
let entity = self.check_type_ref(&import.ty, features, types, offset)?;
let (len, max, desc) = match import.ty {
TypeRef::Func(type_index) => {
self.functions.push(type_index);
self.num_imported_functions += 1;
(self.functions.len(), MAX_WASM_FUNCTIONS, "functions")
}
TypeRef::Table(ty) => {
self.tables.push(ty);
(self.tables.len(), self.max_tables(features), "tables")
}
TypeRef::Memory(ty) => {
self.memories.push(ty);
(self.memories.len(), self.max_memories(features), "memories")
}
TypeRef::Tag(ty) => {
self.tags.push(self.types[ty.func_type_idx as usize]);
(self.tags.len(), MAX_WASM_TAGS, "tags")
}
TypeRef::Global(ty) => {
if !features.mutable_global && ty.mutable {
return Err(BinaryReaderError::new(
"mutable global support is not enabled",
offset,
));
}
self.globals.push(ty);
self.num_imported_globals += 1;
(self.globals.len(), MAX_WASM_GLOBALS, "globals")
}
};
check_max(len, 0, max, desc, offset)?;
self.type_size = combine_type_sizes(self.type_size, entity.type_size(), offset)?;
self.imports
.entry((import.module.to_string(), import.name.to_string()))
.or_default()
.push(entity);
Ok(())
}
pub fn add_export(
&mut self,
name: &str,
ty: EntityType,
features: &WasmFeatures,
offset: usize,
check_limit: bool,
) -> Result<()> {
if !features.mutable_global {
if let EntityType::Global(global_type) = ty {
if global_type.mutable {
return Err(BinaryReaderError::new(
"mutable global support is not enabled",
offset,
));
}
}
}
if check_limit {
check_max(self.exports.len(), 1, MAX_WASM_EXPORTS, "exports", offset)?;
}
self.type_size = combine_type_sizes(self.type_size, ty.type_size(), offset)?;
match self.exports.insert(name.to_string(), ty) {
Some(_) => Err(format_err!(
offset,
"duplicate export name `{name}` already defined"
)),
None => Ok(()),
}
}
pub fn add_function(&mut self, type_index: u32, types: &TypeList, offset: usize) -> Result<()> {
self.func_type_at(type_index, types, offset)?;
self.functions.push(type_index);
Ok(())
}
pub fn add_memory(
&mut self,
ty: MemoryType,
features: &WasmFeatures,
offset: usize,
) -> Result<()> {
self.check_memory_type(&ty, features, offset)?;
self.memories.push(ty);
Ok(())
}
pub fn add_tag(
&mut self,
ty: TagType,
features: &WasmFeatures,
types: &TypeList,
offset: usize,
) -> Result<()> {
self.check_tag_type(&ty, features, types, offset)?;
self.tags.push(self.types[ty.func_type_idx as usize]);
Ok(())
}
pub fn type_at(&self, idx: u32, offset: usize) -> Result<TypeId> {
self.types
.get(idx as usize)
.copied()
.ok_or_else(|| format_err!(offset, "unknown type {idx}: type index out of bounds"))
}
fn func_type_at<'a>(
&self,
type_index: u32,
types: &'a TypeList,
offset: usize,
) -> Result<&'a FuncType> {
types[self.type_at(type_index, offset)?]
.as_func_type()
.ok_or_else(|| format_err!(offset, "type index {type_index} is not a function type"))
}
pub fn check_type_ref(
&self,
type_ref: &TypeRef,
features: &WasmFeatures,
types: &TypeList,
offset: usize,
) -> Result<EntityType> {
Ok(match type_ref {
TypeRef::Func(type_index) => {
self.func_type_at(*type_index, types, offset)?;
EntityType::Func(self.types[*type_index as usize])
}
TypeRef::Table(t) => {
self.check_table_type(t, features, types, offset)?;
EntityType::Table(*t)
}
TypeRef::Memory(t) => {
self.check_memory_type(t, features, offset)?;
EntityType::Memory(*t)
}
TypeRef::Tag(t) => {
self.check_tag_type(t, features, types, offset)?;
EntityType::Tag(self.types[t.func_type_idx as usize])
}
TypeRef::Global(t) => {
self.check_global_type(t, features, types, offset)?;
EntityType::Global(*t)
}
})
}
fn check_table_type(
&self,
ty: &TableType,
features: &WasmFeatures,
types: &TypeList,
offset: usize,
) -> Result<()> {
if ty.element_type != RefType::FUNCREF {
self.check_value_type(ValType::Ref(ty.element_type), features, types, offset)?
}
self.check_limits(ty.initial, ty.maximum, offset)?;
if ty.initial > MAX_WASM_TABLE_ENTRIES as u32 {
return Err(BinaryReaderError::new(
"minimum table size is out of bounds",
offset,
));
}
Ok(())
}
fn check_memory_type(
&self,
ty: &MemoryType,
features: &WasmFeatures,
offset: usize,
) -> Result<()> {
self.check_limits(ty.initial, ty.maximum, offset)?;
let (true_maximum, err) = if ty.memory64 {
if !features.memory64 {
return Err(BinaryReaderError::new(
"memory64 must be enabled for 64-bit memories",
offset,
));
}
(
MAX_WASM_MEMORY64_PAGES,
"memory size must be at most 2**48 pages",
)
} else {
(
MAX_WASM_MEMORY32_PAGES,
"memory size must be at most 65536 pages (4GiB)",
)
};
if ty.initial > true_maximum {
return Err(BinaryReaderError::new(err, offset));
}
if let Some(maximum) = ty.maximum {
if maximum > true_maximum {
return Err(BinaryReaderError::new(err, offset));
}
}
if ty.shared {
if !features.threads {
return Err(BinaryReaderError::new(
"threads must be enabled for shared memories",
offset,
));
}
if ty.maximum.is_none() {
return Err(BinaryReaderError::new(
"shared memory must have maximum size",
offset,
));
}
}
Ok(())
}
pub(crate) fn imports_for_module_type(
&self,
offset: usize,
) -> Result<IndexMap<(String, String), EntityType>> {
self.imports
.iter()
.map(|((module, name), types)| {
if types.len() != 1 {
bail!(
offset,
"module has a duplicate import name `{module}:{name}` \
that is not allowed in components",
);
}
Ok(((module.clone(), name.clone()), types[0]))
})
.collect::<Result<_>>()
}
fn check_value_type(
&self,
ty: ValType,
features: &WasmFeatures,
types: &TypeList,
offset: usize,
) -> Result<()> {
match features.check_value_type(ty) {
Ok(()) => Ok(()),
Err(e) => Err(BinaryReaderError::new(e, offset)),
}?;
match ty {
ValType::Ref(rt) => {
self.check_ref_type(rt, types, offset)?;
}
_ => (),
}
Ok(())
}
fn check_ref_type(&self, ty: RefType, types: &TypeList, offset: usize) -> Result<()> {
match ty.heap_type {
HeapType::Func | HeapType::Extern => (),
HeapType::TypedFunc(type_index) => {
self.func_type_at(type_index.into(), types, offset)?;
}
}
Ok(())
}
fn eq_valtypes(&self, ty1: ValType, ty2: ValType, types: &TypeList) -> bool {
match (ty1, ty2) {
(ValType::Ref(rt1), ValType::Ref(rt2)) => {
rt1.nullable == rt2.nullable
&& match (rt1.heap_type, rt2.heap_type) {
(HeapType::Func, HeapType::Func) => true,
(HeapType::Extern, HeapType::Extern) => true,
(HeapType::TypedFunc(n1), HeapType::TypedFunc(n2)) => {
let n1 = self.func_type_at(n1.into(), types, 0).unwrap();
let n2 = self.func_type_at(n2.into(), types, 0).unwrap();
self.eq_fns(n1, n2, types)
}
(_, _) => false,
}
}
_ => ty1 == ty2,
}
}
fn eq_fns(&self, f1: &impl WasmFuncType, f2: &impl WasmFuncType, types: &TypeList) -> bool {
f1.len_inputs() == f2.len_inputs()
&& f2.len_outputs() == f2.len_outputs()
&& f1
.inputs()
.zip(f2.inputs())
.all(|(t1, t2)| self.eq_valtypes(t1, t2, types))
&& f1
.outputs()
.zip(f2.outputs())
.all(|(t1, t2)| self.eq_valtypes(t1, t2, types))
}
pub(crate) fn matches(&self, ty1: ValType, ty2: ValType, types: &TypeList) -> bool {
fn matches_null(null1: bool, null2: bool) -> bool {
(null1 == null2) || null2
}
let matches_heap = |ty1: HeapType, ty2: HeapType, types: &TypeList| -> bool {
match (ty1, ty2) {
(HeapType::TypedFunc(n1), HeapType::TypedFunc(n2)) => {
let n1 = self.func_type_at(n1.into(), types, 0).unwrap();
let n2 = self.func_type_at(n2.into(), types, 0).unwrap();
self.eq_fns(n1, n2, types)
}
(HeapType::TypedFunc(_), HeapType::Func) => true,
(_, _) => ty1 == ty2,
}
};
let matches_ref = |ty1: RefType, ty2: RefType, types: &TypeList| -> bool {
matches_heap(ty1.heap_type, ty2.heap_type, types)
&& matches_null(ty1.nullable, ty2.nullable)
};
match (ty1, ty2) {
(ValType::Ref(rt1), ValType::Ref(rt2)) => matches_ref(rt1, rt2, types),
(_, _) => ty1 == ty2,
}
}
fn check_tag_type(
&self,
ty: &TagType,
features: &WasmFeatures,
types: &TypeList,
offset: usize,
) -> Result<()> {
if !features.exceptions {
return Err(BinaryReaderError::new(
"exceptions proposal not enabled",
offset,
));
}
let ty = self.func_type_at(ty.func_type_idx, types, offset)?;
if !ty.results().is_empty() {
return Err(BinaryReaderError::new(
"invalid exception type: non-empty tag result type",
offset,
));
}
Ok(())
}
fn check_global_type(
&self,
ty: &GlobalType,
features: &WasmFeatures,
types: &TypeList,
offset: usize,
) -> Result<()> {
self.check_value_type(ty.content_type, features, types, offset)
}
fn check_limits<T>(&self, initial: T, maximum: Option<T>, offset: usize) -> Result<()>
where
T: Into<u64>,
{
if let Some(max) = maximum {
if initial.into() > max.into() {
return Err(BinaryReaderError::new(
"size minimum must not be greater than maximum",
offset,
));
}
}
Ok(())
}
pub fn max_tables(&self, features: &WasmFeatures) -> usize {
if features.reference_types {
MAX_WASM_TABLES
} else {
1
}
}
pub fn max_memories(&self, features: &WasmFeatures) -> usize {
if features.multi_memory {
MAX_WASM_MEMORIES
} else {
1
}
}
pub fn export_to_entity_type(
&mut self,
export: &crate::Export,
offset: usize,
) -> Result<EntityType> {
let check = |ty: &str, index: u32, total: usize| {
if index as usize >= total {
Err(format_err!(
offset,
"unknown {ty} {index}: exported {ty} index out of bounds",
))
} else {
Ok(())
}
};
Ok(match export.kind {
ExternalKind::Func => {
check("function", export.index, self.functions.len())?;
self.function_references.insert(export.index);
EntityType::Func(self.types[self.functions[export.index as usize] as usize])
}
ExternalKind::Table => {
check("table", export.index, self.tables.len())?;
EntityType::Table(self.tables[export.index as usize])
}
ExternalKind::Memory => {
check("memory", export.index, self.memories.len())?;
EntityType::Memory(self.memories[export.index as usize])
}
ExternalKind::Global => {
check("global", export.index, self.globals.len())?;
EntityType::Global(self.globals[export.index as usize])
}
ExternalKind::Tag => {
check("tag", export.index, self.tags.len())?;
EntityType::Tag(self.tags[export.index as usize])
}
})
}
pub fn get_func_type<'a>(
&self,
func_idx: u32,
types: &'a TypeList,
offset: usize,
) -> Result<&'a FuncType> {
match self.functions.get(func_idx as usize) {
Some(idx) => self.func_type_at(*idx, types, offset),
None => Err(format_err!(
offset,
"unknown function {func_idx}: func index out of bounds",
)),
}
}
fn global_at(&self, idx: u32, offset: usize) -> Result<&GlobalType> {
match self.globals.get(idx as usize) {
Some(t) => Ok(t),
None => Err(format_err!(
offset,
"unknown global {idx}: global index out of bounds"
)),
}
}
fn table_at(&self, idx: u32, offset: usize) -> Result<&TableType> {
match self.tables.get(idx as usize) {
Some(t) => Ok(t),
None => Err(format_err!(
offset,
"unknown table {idx}: table index out of bounds"
)),
}
}
fn memory_at(&self, idx: u32, offset: usize) -> Result<&MemoryType> {
match self.memories.get(idx as usize) {
Some(t) => Ok(t),
None => Err(format_err!(
offset,
"unknown memory {idx}: memory index out of bounds"
)),
}
}
}
impl Default for Module {
fn default() -> Self {
Self {
snapshot: Default::default(),
types: Default::default(),
tables: Default::default(),
memories: Default::default(),
globals: Default::default(),
element_types: Default::default(),
data_count: Default::default(),
functions: Default::default(),
tags: Default::default(),
function_references: Default::default(),
imports: Default::default(),
exports: Default::default(),
type_size: 1,
num_imported_globals: Default::default(),
num_imported_functions: Default::default(),
}
}
}
struct OperatorValidatorResources<'a> {
module: &'a mut MaybeOwned<Module>,
types: &'a TypeList,
}
impl WasmModuleResources for OperatorValidatorResources<'_> {
type FuncType = crate::FuncType;
fn table_at(&self, at: u32) -> Option<TableType> {
self.module.tables.get(at as usize).cloned()
}
fn memory_at(&self, at: u32) -> Option<MemoryType> {
self.module.memories.get(at as usize).cloned()
}
fn tag_at(&self, at: u32) -> Option<&Self::FuncType> {
Some(
self.types[*self.module.tags.get(at as usize)?]
.as_func_type()
.unwrap(),
)
}
fn global_at(&self, at: u32) -> Option<GlobalType> {
self.module.globals.get(at as usize).cloned()
}
fn func_type_at(&self, at: u32) -> Option<&Self::FuncType> {
Some(
self.types[*self.module.types.get(at as usize)?]
.as_func_type()
.unwrap(),
)
}
fn type_index_of_function(&self, at: u32) -> Option<u32> {
self.module.functions.get(at as usize).cloned()
}
fn type_of_function(&self, at: u32) -> Option<&Self::FuncType> {
self.func_type_at(self.type_index_of_function(at)?)
}
fn check_value_type(&self, t: ValType, features: &WasmFeatures, offset: usize) -> Result<()> {
self.module
.check_value_type(t, features, self.types, offset)
}
fn element_type_at(&self, at: u32) -> Option<RefType> {
self.module.element_types.get(at as usize).cloned()
}
fn matches(&self, t1: ValType, t2: ValType) -> bool {
self.module.matches(t1, t2, self.types)
}
fn element_count(&self) -> u32 {
self.module.element_types.len() as u32
}
fn data_count(&self) -> Option<u32> {
self.module.data_count
}
fn is_function_referenced(&self, idx: u32) -> bool {
self.module.function_references.contains(&idx)
}
}
pub struct ValidatorResources(pub(crate) Arc<Module>);
impl WasmModuleResources for ValidatorResources {
type FuncType = crate::FuncType;
fn table_at(&self, at: u32) -> Option<TableType> {
self.0.tables.get(at as usize).cloned()
}
fn memory_at(&self, at: u32) -> Option<MemoryType> {
self.0.memories.get(at as usize).cloned()
}
fn tag_at(&self, at: u32) -> Option<&Self::FuncType> {
Some(
self.0.snapshot.as_ref().unwrap()[*self.0.tags.get(at as usize)?]
.as_func_type()
.unwrap(),
)
}
fn global_at(&self, at: u32) -> Option<GlobalType> {
self.0.globals.get(at as usize).cloned()
}
fn func_type_at(&self, at: u32) -> Option<&Self::FuncType> {
Some(
self.0.snapshot.as_ref().unwrap()[*self.0.types.get(at as usize)?]
.as_func_type()
.unwrap(),
)
}
fn type_index_of_function(&self, at: u32) -> Option<u32> {
self.0.functions.get(at as usize).cloned()
}
fn type_of_function(&self, at: u32) -> Option<&Self::FuncType> {
self.func_type_at(self.type_index_of_function(at)?)
}
fn check_value_type(&self, t: ValType, features: &WasmFeatures, offset: usize) -> Result<()> {
self.0
.check_value_type(t, features, self.0.snapshot.as_ref().unwrap(), offset)
}
fn element_type_at(&self, at: u32) -> Option<RefType> {
self.0.element_types.get(at as usize).cloned()
}
fn matches(&self, t1: ValType, t2: ValType) -> bool {
self.0.matches(t1, t2, self.0.snapshot.as_ref().unwrap())
}
fn element_count(&self) -> u32 {
self.0.element_types.len() as u32
}
fn data_count(&self) -> Option<u32> {
self.0.data_count
}
fn is_function_referenced(&self, idx: u32) -> bool {
self.0.function_references.contains(&idx)
}
}
const _: () = {
fn assert_send<T: Send>() {}
fn assert() {
assert_send::<ValidatorResources>();
}
};
mod arc {
use std::ops::Deref;
use std::sync::Arc;
enum Inner<T> {
Owned(T),
Shared(Arc<T>),
Empty, }
pub struct MaybeOwned<T> {
inner: Inner<T>,
}
impl<T> MaybeOwned<T> {
#[inline]
fn as_mut(&mut self) -> Option<&mut T> {
match &mut self.inner {
Inner::Owned(x) => Some(x),
Inner::Shared(_) => None,
Inner::Empty => Self::unreachable(),
}
}
#[inline]
pub fn assert_mut(&mut self) -> &mut T {
self.as_mut().unwrap()
}
pub fn arc(&mut self) -> &Arc<T> {
self.make_shared();
match &self.inner {
Inner::Shared(x) => x,
_ => Self::unreachable(),
}
}
#[inline]
fn make_shared(&mut self) {
if let Inner::Shared(_) = self.inner {
return;
}
let inner = std::mem::replace(&mut self.inner, Inner::Empty);
let x = match inner {
Inner::Owned(x) => x,
_ => Self::unreachable(),
};
let x = Arc::new(x);
self.inner = Inner::Shared(x);
}
#[cold]
#[inline(never)]
fn unreachable() -> ! {
unreachable!()
}
}
impl<T: Default> Default for MaybeOwned<T> {
fn default() -> MaybeOwned<T> {
MaybeOwned {
inner: Inner::Owned(T::default()),
}
}
}
impl<T> Deref for MaybeOwned<T> {
type Target = T;
fn deref(&self) -> &T {
match &self.inner {
Inner::Owned(x) => x,
Inner::Shared(x) => x,
Inner::Empty => Self::unreachable(),
}
}
}
}