Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Add fallible allocation methods to `cranelift_entity::PrimaryMap` (#12395)

Nick Fitzgerald595215b327c9
diff --git a/cranelift/entity/src/primary.rs b/cranelift/entity/src/primary.rs
index 6c1dbc18ad..babf4d5790 100644
--- a/cranelift/entity/src/primary.rs
+++ b/cranelift/entity/src/primary.rs
@@ -11,6 +11,7 @@ use core::ops::{Index, IndexMut};
use core::slice;
#[cfg(feature = "enable-serde")]
use serde_derive::{Deserialize, Serialize};
+use wasmtime_error::OutOfMemory;
/// A primary mapping `K -> V` allocating dense entity references.
///
@@ -57,6 +58,13 @@ where
}
}
+ /// Like `with_capacity` but returns an error on allocation failure.
+ pub fn try_with_capacity(capacity: usize) -> Result<Self, OutOfMemory> {
+ let mut map = Self::new();
+ map.try_reserve(capacity)?;
+ Ok(map)
+ }
+
/// Check if `k` is a valid key in the map.
pub fn is_valid(&self, k: K) -> bool {
k.index() < self.elems.len()
@@ -153,11 +161,25 @@ where
self.elems.reserve(additional)
}
+ /// Like `reserve` but returns an error on allocation failure.
+ pub fn try_reserve(&mut self, additional: usize) -> Result<(), OutOfMemory> {
+ self.elems
+ .try_reserve(additional)
+ .map_err(|_| OutOfMemory::new(self.len().saturating_add(additional)))
+ }
+
/// Reserves the minimum capacity for exactly `additional` more elements to be inserted.
pub fn reserve_exact(&mut self, additional: usize) {
self.elems.reserve_exact(additional)
}
+ /// Like `reserve_exact` but returns an error on allocation failure.
+ pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), OutOfMemory> {
+ self.elems
+ .try_reserve_exact(additional)
+ .map_err(|_| OutOfMemory::new(self.len().saturating_add(additional)))
+ }
+
/// Shrinks the capacity of the `PrimaryMap` as much as possible.
pub fn shrink_to_fit(&mut self) {
self.elems.shrink_to_fit()
diff --git a/crates/fuzzing/tests/oom.rs b/crates/fuzzing/tests/oom.rs
index 1ba6b91b0b..e7d370bc6d 100644
--- a/crates/fuzzing/tests/oom.rs
+++ b/crates/fuzzing/tests/oom.rs
@@ -5,7 +5,7 @@ use std::{
sync::atomic::{AtomicU32, Ordering::SeqCst},
};
use wasmtime::{error::OutOfMemory, *};
-use wasmtime_environ::collections::*;
+use wasmtime_environ::{PrimaryMap, collections::*};
use wasmtime_fuzzing::oom::{OomTest, OomTestAllocator};
#[global_allocator]
@@ -68,6 +68,44 @@ fn compound_bit_set_try_ensure_capacity() -> Result<()> {
})
}
+#[test]
+fn primary_map_try_with_capacity() -> Result<()> {
+ #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
+ struct Key(u32);
+ wasmtime_environ::entity_impl!(Key);
+
+ OomTest::new().test(|| {
+ let _map = PrimaryMap::<Key, u32>::try_with_capacity(32)?;
+ Ok(())
+ })
+}
+
+#[test]
+fn primary_map_try_reserve() -> Result<()> {
+ #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
+ struct Key(u32);
+ wasmtime_environ::entity_impl!(Key);
+
+ OomTest::new().test(|| {
+ let mut map = PrimaryMap::<Key, u32>::new();
+ map.try_reserve(100)?;
+ Ok(())
+ })
+}
+
+#[test]
+fn primary_map_try_reserve_exact() -> Result<()> {
+ #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
+ struct Key(u32);
+ wasmtime_environ::entity_impl!(Key);
+
+ OomTest::new().test(|| {
+ let mut map = PrimaryMap::<Key, u32>::new();
+ map.try_reserve_exact(13)?;
+ Ok(())
+ })
+}
+
#[test]
fn config_new() -> Result<()> {
OomTest::new().test(|| {