A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Add a fallible-allocation-only version of `String` (#12534)
Nick Fitzgeraldbf42bd4f14a5
diff --git a/crates/core/src/alloc.rs b/crates/core/src/alloc.rsindex 9cb048c824..eb690047bb 100644--- a/crates/core/src/alloc.rs+++ b/crates/core/src/alloc.rs@@ -2,6 +2,7 @@mod arc;mod boxed;+mod string;mod try_clone;mod try_collect;mod try_new;@@ -12,6 +13,7 @@ pub use boxed::{new_boxed_slice_from_fallible_iter, new_boxed_slice_from_iter,new_boxed_slice_from_iter_with_len, new_uninit_boxed_slice,};+pub use string::String;pub use try_clone::TryClone;pub use try_collect::{TryCollect, TryExtend, TryFromIterator};pub use try_new::{TryNew, try_new};diff --git a/crates/core/src/alloc/string.rs b/crates/core/src/alloc/string.rsnew file mode 100644index 0000000000..79f2b32299--- /dev/null+++ b/crates/core/src/alloc/string.rs@@ -0,0 +1,108 @@+use super::TryClone;+use crate::error::OutOfMemory;+use core::{fmt, ops};+use std_alloc::string as inner;++/// A newtype wrapper around [`std::string::String`] that only exposes+/// fallible-allocation methods.+pub struct String {+ inner: inner::String,+}++impl TryClone for String {+ fn try_clone(&self) -> Result<Self, OutOfMemory> {+ let mut s = Self::new();+ s.push_str(self)?;+ Ok(s)+ }+}++impl fmt::Debug for String {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ fmt::Debug::fmt(&self.inner, f)+ }+}++impl fmt::Display for String {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ fmt::Display::fmt(&self.inner, f)+ }+}++impl ops::Deref for String {+ type Target = str;++ #[inline]+ fn deref(&self) -> &Self::Target {+ &self.inner+ }+}++impl ops::DerefMut for String {+ #[inline]+ fn deref_mut(&mut self) -> &mut Self::Target {+ &mut self.inner+ }+}++impl From<inner::String> for String {+ #[inline]+ fn from(inner: inner::String) -> Self {+ Self { inner }+ }+}++impl String {+ /// Same as [`std::string::String::new`].+ #[inline]+ pub fn new() -> Self {+ Self {+ inner: inner::String::new(),+ }+ }++ /// Same as [`std::string::String::with_capacity`] but returns an error on+ /// allocation failure.+ #[inline]+ pub fn with_capacity(capacity: usize) -> Result<Self, OutOfMemory> {+ let mut s = Self::new();+ s.reserve(capacity)?;+ Ok(s)+ }++ /// Same as [`std::string::String::reserve`] but returns an error on+ /// allocation failure.+ #[inline]+ pub fn reserve(&mut self, additional: usize) -> Result<(), OutOfMemory> {+ self.inner+ .try_reserve(additional)+ .map_err(|_| OutOfMemory::new(self.len().saturating_add(additional)))+ }++ /// Same as [`std::string::String::reserve_exact`] but returns an error on+ /// allocation failure.+ #[inline]+ pub fn reserve_exact(&mut self, additional: usize) -> Result<(), OutOfMemory> {+ self.inner+ .try_reserve_exact(additional)+ .map_err(|_| OutOfMemory::new(self.len().saturating_add(additional)))+ }++ /// Same as [`std::string::String::push`] but returns an error on allocation+ /// failure.+ #[inline]+ pub fn push(&mut self, c: char) -> Result<(), OutOfMemory> {+ self.reserve(c.len_utf8())?;+ self.inner.push(c);+ Ok(())+ }++ /// Same as [`std::string::String::push_str`] but returns an error on+ /// allocation failure.+ #[inline]+ pub fn push_str(&mut self, s: &str) -> Result<(), OutOfMemory> {+ self.reserve(s.len())?;+ self.inner.push_str(s);+ Ok(())+ }+}diff --git a/crates/environ/src/collections.rs b/crates/environ/src/collections.rsindex 18c00288bc..5ddbaf8385 100644--- a/crates/environ/src/collections.rs+++ b/crates/environ/src/collections.rs@@ -9,7 +9,7 @@ pub use entity_set::EntitySet;pub use hash_set::HashSet;pub use primary_map::PrimaryMap;pub use secondary_map::SecondaryMap;-pub use wasmtime_core::alloc::{TryClone, TryNew, Vec, try_new};+pub use wasmtime_core::alloc::{String, TryClone, TryNew, Vec, try_new};/// Collections which abort on OOM.//diff --git a/crates/fuzzing/tests/oom.rs b/crates/fuzzing/tests/oom.rsindex 43fb320a54..dec2295da1 100644--- a/crates/fuzzing/tests/oom.rs+++ b/crates/fuzzing/tests/oom.rs@@ -210,6 +210,50 @@ fn vec_push() -> Result<()> {})}+#[test]+fn string_with_capacity() -> Result<()> {+ OomTest::new().test(|| {+ let _s = String::with_capacity(100)?;+ Ok(())+ })+}++#[test]+fn string_reserve() -> Result<()> {+ OomTest::new().test(|| {+ let mut s = String::new();+ s.reserve(10)?;+ Ok(())+ })+}++#[test]+fn string_reserve_exact() -> Result<()> {+ OomTest::new().test(|| {+ let mut s = String::new();+ s.reserve_exact(3)?;+ Ok(())+ })+}++#[test]+fn string_push() -> Result<()> {+ OomTest::new().test(|| {+ let mut s = String::new();+ s.push('c')?;+ Ok(())+ })+}++#[test]+fn string_push_str() -> Result<()> {+ OomTest::new().test(|| {+ let mut s = String::new();+ s.push_str("hello")?;+ Ok(())+ })+}+#[test]fn config_new() -> Result<()> {OomTest::new().test(|| {