|
| 1 | +use crate::{ |
| 2 | + ResourceLimiter, |
| 3 | + core::LimiterError, |
| 4 | + errors::{MemoryError, TableError}, |
| 5 | +}; |
| 6 | + |
| 7 | +/// Value returned by [`ResourceLimiter::instances`] default method |
| 8 | +pub const DEFAULT_INSTANCE_LIMIT: usize = 10_000; |
| 9 | + |
| 10 | +/// Value returned by [`ResourceLimiter::tables`] default method |
| 11 | +pub const DEFAULT_TABLE_LIMIT: usize = 10_000; |
| 12 | + |
| 13 | +/// Value returned by [`ResourceLimiter::memories`] default method |
| 14 | +pub const DEFAULT_MEMORY_LIMIT: usize = 10_000; |
| 15 | + |
| 16 | +/// Used to build [`StoreLimits`]. |
| 17 | +pub struct StoreLimitsBuilder(StoreLimits); |
| 18 | + |
| 19 | +impl StoreLimitsBuilder { |
| 20 | + /// Creates a new [`StoreLimitsBuilder`]. |
| 21 | + /// |
| 22 | + /// See the documentation on each builder method for the default for each |
| 23 | + /// value. |
| 24 | + pub fn new() -> Self { |
| 25 | + Self(StoreLimits::default()) |
| 26 | + } |
| 27 | + |
| 28 | + /// The maximum number of bytes a linear memory can grow to. |
| 29 | + /// |
| 30 | + /// Growing a linear memory beyond this limit will fail. This limit is |
| 31 | + /// applied to each linear memory individually, so if a wasm module has |
| 32 | + /// multiple linear memories then they're all allowed to reach up to the |
| 33 | + /// `limit` specified. |
| 34 | + /// |
| 35 | + /// By default, linear memory will not be limited. |
| 36 | + pub fn memory_size(mut self, limit: usize) -> Self { |
| 37 | + self.0.memory_size = Some(limit); |
| 38 | + self |
| 39 | + } |
| 40 | + |
| 41 | + /// The maximum number of elements in a table. |
| 42 | + /// |
| 43 | + /// Growing a table beyond this limit will fail. This limit is applied to |
| 44 | + /// each table individually, so if a wasm module has multiple tables then |
| 45 | + /// they're all allowed to reach up to the `limit` specified. |
| 46 | + /// |
| 47 | + /// By default, table elements will not be limited. |
| 48 | + pub fn table_elements(mut self, limit: usize) -> Self { |
| 49 | + self.0.table_elements = Some(limit); |
| 50 | + self |
| 51 | + } |
| 52 | + |
| 53 | + /// The maximum number of instances that can be created for a [`Store`](crate::Store). |
| 54 | + /// |
| 55 | + /// Module instantiation will fail if this limit is exceeded. |
| 56 | + /// |
| 57 | + /// This value defaults to 10,000. |
| 58 | + pub fn instances(mut self, limit: usize) -> Self { |
| 59 | + self.0.instances = limit; |
| 60 | + self |
| 61 | + } |
| 62 | + |
| 63 | + /// The maximum number of tables that can be created for a [`Store`](crate::Store). |
| 64 | + /// |
| 65 | + /// Module instantiation will fail if this limit is exceeded. |
| 66 | + /// |
| 67 | + /// This value defaults to 10,000. |
| 68 | + pub fn tables(mut self, tables: usize) -> Self { |
| 69 | + self.0.tables = tables; |
| 70 | + self |
| 71 | + } |
| 72 | + |
| 73 | + /// The maximum number of linear memories that can be created for a [`Store`](crate::Store). |
| 74 | + /// |
| 75 | + /// Instantiation will fail with an error if this limit is exceeded. |
| 76 | + /// |
| 77 | + /// This value defaults to 10,000. |
| 78 | + pub fn memories(mut self, memories: usize) -> Self { |
| 79 | + self.0.memories = memories; |
| 80 | + self |
| 81 | + } |
| 82 | + |
| 83 | + /// Indicates that a trap should be raised whenever a growth operation |
| 84 | + /// would fail. |
| 85 | + /// |
| 86 | + /// This operation will force `memory.grow` and `table.grow` instructions |
| 87 | + /// to raise a trap on failure instead of returning -1. This is not |
| 88 | + /// necessarily spec-compliant, but it can be quite handy when debugging a |
| 89 | + /// module that fails to allocate memory and might behave oddly as a result. |
| 90 | + /// |
| 91 | + /// This value defaults to `false`. |
| 92 | + pub fn trap_on_grow_failure(mut self, trap: bool) -> Self { |
| 93 | + self.0.trap_on_grow_failure = trap; |
| 94 | + self |
| 95 | + } |
| 96 | + |
| 97 | + /// Consumes this builder and returns the [`StoreLimits`]. |
| 98 | + pub fn build(self) -> StoreLimits { |
| 99 | + self.0 |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl Default for StoreLimitsBuilder { |
| 104 | + fn default() -> Self { |
| 105 | + Self::new() |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/// Provides limits for a [`Store`](crate::Store). |
| 110 | +/// |
| 111 | +/// This type is created with a [`StoreLimitsBuilder`] and is typically used in |
| 112 | +/// conjunction with [`Store::limiter`](crate::Store::limiter). |
| 113 | +/// |
| 114 | +/// This is a convenience type included to avoid needing to implement the |
| 115 | +/// [`ResourceLimiter`] trait if your use case fits in the static configuration |
| 116 | +/// that this [`StoreLimits`] provides. |
| 117 | +#[derive(Clone, Debug)] |
| 118 | +pub struct StoreLimits { |
| 119 | + memory_size: Option<usize>, |
| 120 | + table_elements: Option<usize>, |
| 121 | + instances: usize, |
| 122 | + tables: usize, |
| 123 | + memories: usize, |
| 124 | + trap_on_grow_failure: bool, |
| 125 | +} |
| 126 | + |
| 127 | +impl Default for StoreLimits { |
| 128 | + fn default() -> Self { |
| 129 | + Self { |
| 130 | + memory_size: None, |
| 131 | + table_elements: None, |
| 132 | + instances: DEFAULT_INSTANCE_LIMIT, |
| 133 | + tables: DEFAULT_TABLE_LIMIT, |
| 134 | + memories: DEFAULT_MEMORY_LIMIT, |
| 135 | + trap_on_grow_failure: false, |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +impl ResourceLimiter for StoreLimits { |
| 141 | + fn memory_growing( |
| 142 | + &mut self, |
| 143 | + _current: usize, |
| 144 | + desired: usize, |
| 145 | + maximum: Option<usize>, |
| 146 | + ) -> Result<bool, LimiterError> { |
| 147 | + let allow = match self.memory_size { |
| 148 | + Some(limit) if desired > limit => false, |
| 149 | + _ => match maximum { |
| 150 | + Some(max) if desired > max => false, |
| 151 | + Some(_) | None => true, |
| 152 | + }, |
| 153 | + }; |
| 154 | + if !allow && self.trap_on_grow_failure { |
| 155 | + return Err(LimiterError::ResourceLimiterDeniedAllocation); |
| 156 | + } |
| 157 | + Ok(allow) |
| 158 | + } |
| 159 | + |
| 160 | + fn memory_grow_failed(&mut self, _error: &MemoryError) -> Result<(), LimiterError> { |
| 161 | + if self.trap_on_grow_failure { |
| 162 | + return Err(LimiterError::ResourceLimiterDeniedAllocation); |
| 163 | + } |
| 164 | + Ok(()) |
| 165 | + } |
| 166 | + |
| 167 | + fn table_growing( |
| 168 | + &mut self, |
| 169 | + _current: usize, |
| 170 | + desired: usize, |
| 171 | + maximum: Option<usize>, |
| 172 | + ) -> Result<bool, LimiterError> { |
| 173 | + let allow = match self.table_elements { |
| 174 | + Some(limit) if desired > limit => false, |
| 175 | + _ => match maximum { |
| 176 | + Some(max) if desired > max => false, |
| 177 | + Some(_) | None => true, |
| 178 | + }, |
| 179 | + }; |
| 180 | + if !allow && self.trap_on_grow_failure { |
| 181 | + return Err(LimiterError::ResourceLimiterDeniedAllocation); |
| 182 | + } |
| 183 | + Ok(allow) |
| 184 | + } |
| 185 | + |
| 186 | + fn table_grow_failed(&mut self, _error: &TableError) -> Result<(), LimiterError> { |
| 187 | + if self.trap_on_grow_failure { |
| 188 | + return Err(LimiterError::ResourceLimiterDeniedAllocation); |
| 189 | + } |
| 190 | + Ok(()) |
| 191 | + } |
| 192 | + |
| 193 | + fn instances(&self) -> usize { |
| 194 | + self.instances |
| 195 | + } |
| 196 | + |
| 197 | + fn tables(&self) -> usize { |
| 198 | + self.tables |
| 199 | + } |
| 200 | + |
| 201 | + fn memories(&self) -> usize { |
| 202 | + self.memories |
| 203 | + } |
| 204 | +} |
0 commit comments