Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.

Commit 4658390

Browse files
kouteDaviRain-Su
authored andcommitted
Adjust maximum memory pages hard limit for the pooling instantiation strategy (paritytech#11482)
* Run `sc-executor-wasmtime` unit tests for all instantiation strategies * Adjust maximum memory pages hard limit for the pooling instantiation strategy
1 parent c3a29d5 commit 4658390

File tree

4 files changed

+106
-25
lines changed

4 files changed

+106
-25
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/executor/wasmtime/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ wat = "1.0"
3737
sc-runtime-test = { version = "2.0.0", path = "../runtime-test" }
3838
sp-io = { version = "6.0.0", path = "../../../primitives/io" }
3939
tempfile = "3.3.0"
40+
paste = "1.0"

client/executor/wasmtime/src/runtime.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,21 @@ fn common_config(semantics: &Semantics) -> std::result::Result<wasmtime::Config,
341341
);
342342

343343
if use_pooling {
344+
const WASM_PAGE_SIZE: u64 = 65536;
345+
const MAX_WASM_PAGES: u64 = 0x10000;
346+
347+
let memory_pages = if let Some(max_memory_size) = semantics.max_memory_size {
348+
let max_memory_size = max_memory_size as u64;
349+
let mut pages = max_memory_size / WASM_PAGE_SIZE;
350+
if max_memory_size % WASM_PAGE_SIZE != 0 {
351+
pages += 1;
352+
}
353+
354+
std::cmp::min(MAX_WASM_PAGES, pages)
355+
} else {
356+
MAX_WASM_PAGES
357+
};
358+
344359
config.allocation_strategy(wasmtime::InstanceAllocationStrategy::Pooling {
345360
strategy: wasmtime::PoolingAllocationStrategy::ReuseAffinity,
346361

@@ -353,7 +368,7 @@ fn common_config(semantics: &Semantics) -> std::result::Result<wasmtime::Config,
353368
// memory_pages: 2070
354369
size: 64 * 1024,
355370
table_elements: 2048,
356-
memory_pages: 4096,
371+
memory_pages,
357372

358373
// We can only have a single of those.
359374
tables: 1,

client/executor/wasmtime/src/tests.rs

+88-24
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,54 @@ use crate::InstantiationStrategy;
2424

2525
type HostFunctions = sp_io::SubstrateHostFunctions;
2626

27+
#[macro_export]
28+
macro_rules! test_wasm_execution {
29+
(@no_legacy_instance_reuse $method_name:ident) => {
30+
paste::item! {
31+
#[test]
32+
fn [<$method_name _recreate_instance_cow>]() {
33+
$method_name(
34+
InstantiationStrategy::RecreateInstanceCopyOnWrite
35+
);
36+
}
37+
38+
#[test]
39+
fn [<$method_name _recreate_instance_vanilla>]() {
40+
$method_name(
41+
InstantiationStrategy::RecreateInstance
42+
);
43+
}
44+
45+
#[test]
46+
fn [<$method_name _pooling_cow>]() {
47+
$method_name(
48+
InstantiationStrategy::PoolingCopyOnWrite
49+
);
50+
}
51+
52+
#[test]
53+
fn [<$method_name _pooling_vanilla>]() {
54+
$method_name(
55+
InstantiationStrategy::Pooling
56+
);
57+
}
58+
}
59+
};
60+
61+
($method_name:ident) => {
62+
test_wasm_execution!(@no_legacy_instance_reuse $method_name);
63+
64+
paste::item! {
65+
#[test]
66+
fn [<$method_name _legacy_instance_reuse>]() {
67+
$method_name(
68+
InstantiationStrategy::LegacyInstanceReuse
69+
);
70+
}
71+
}
72+
};
73+
}
74+
2775
struct RuntimeBuilder {
2876
code: Option<String>,
2977
instantiation_strategy: InstantiationStrategy,
@@ -36,12 +84,10 @@ struct RuntimeBuilder {
3684
}
3785

3886
impl RuntimeBuilder {
39-
/// Returns a new builder that won't use the fast instance reuse mechanism, but instead will
40-
/// create a new runtime instance each time.
41-
fn new_on_demand() -> Self {
87+
fn new(instantiation_strategy: InstantiationStrategy) -> Self {
4288
Self {
4389
code: None,
44-
instantiation_strategy: InstantiationStrategy::RecreateInstance,
90+
instantiation_strategy,
4591
canonicalize_nans: false,
4692
deterministic_stack: false,
4793
extra_heap_pages: 1024,
@@ -128,9 +174,9 @@ impl RuntimeBuilder {
128174
}
129175
}
130176

131-
#[test]
132-
fn test_nan_canonicalization() {
133-
let mut builder = RuntimeBuilder::new_on_demand().canonicalize_nans(true);
177+
test_wasm_execution!(test_nan_canonicalization);
178+
fn test_nan_canonicalization(instantiation_strategy: InstantiationStrategy) {
179+
let mut builder = RuntimeBuilder::new(instantiation_strategy).canonicalize_nans(true);
134180
let runtime = builder.build();
135181

136182
let mut instance = runtime.new_instance().expect("failed to instantiate a runtime");
@@ -166,11 +212,11 @@ fn test_nan_canonicalization() {
166212
assert_eq!(res, CANONICAL_NAN_BITS);
167213
}
168214

169-
#[test]
170-
fn test_stack_depth_reaching() {
215+
test_wasm_execution!(test_stack_depth_reaching);
216+
fn test_stack_depth_reaching(instantiation_strategy: InstantiationStrategy) {
171217
const TEST_GUARD_PAGE_SKIP: &str = include_str!("test-guard-page-skip.wat");
172218

173-
let mut builder = RuntimeBuilder::new_on_demand()
219+
let mut builder = RuntimeBuilder::new(instantiation_strategy)
174220
.use_wat(TEST_GUARD_PAGE_SKIP.to_string())
175221
.deterministic_stack(true);
176222

@@ -186,33 +232,46 @@ fn test_stack_depth_reaching() {
186232
}
187233
}
188234

189-
#[test]
190-
fn test_max_memory_pages_imported_memory_without_precompilation() {
191-
test_max_memory_pages(true, false);
235+
test_wasm_execution!(test_max_memory_pages_imported_memory_without_precompilation);
236+
fn test_max_memory_pages_imported_memory_without_precompilation(
237+
instantiation_strategy: InstantiationStrategy,
238+
) {
239+
test_max_memory_pages(instantiation_strategy, true, false);
192240
}
193241

194-
#[test]
195-
fn test_max_memory_pages_exported_memory_without_precompilation() {
196-
test_max_memory_pages(false, false);
242+
test_wasm_execution!(test_max_memory_pages_exported_memory_without_precompilation);
243+
fn test_max_memory_pages_exported_memory_without_precompilation(
244+
instantiation_strategy: InstantiationStrategy,
245+
) {
246+
test_max_memory_pages(instantiation_strategy, false, false);
197247
}
198248

199-
#[test]
200-
fn test_max_memory_pages_imported_memory_with_precompilation() {
201-
test_max_memory_pages(true, true);
249+
test_wasm_execution!(@no_legacy_instance_reuse test_max_memory_pages_imported_memory_with_precompilation);
250+
fn test_max_memory_pages_imported_memory_with_precompilation(
251+
instantiation_strategy: InstantiationStrategy,
252+
) {
253+
test_max_memory_pages(instantiation_strategy, true, true);
202254
}
203255

204-
#[test]
205-
fn test_max_memory_pages_exported_memory_with_precompilation() {
206-
test_max_memory_pages(false, true);
256+
test_wasm_execution!(@no_legacy_instance_reuse test_max_memory_pages_exported_memory_with_precompilation);
257+
fn test_max_memory_pages_exported_memory_with_precompilation(
258+
instantiation_strategy: InstantiationStrategy,
259+
) {
260+
test_max_memory_pages(instantiation_strategy, false, true);
207261
}
208262

209-
fn test_max_memory_pages(import_memory: bool, precompile_runtime: bool) {
263+
fn test_max_memory_pages(
264+
instantiation_strategy: InstantiationStrategy,
265+
import_memory: bool,
266+
precompile_runtime: bool,
267+
) {
210268
fn try_instantiate(
211269
max_memory_size: Option<usize>,
212270
wat: String,
271+
instantiation_strategy: InstantiationStrategy,
213272
precompile_runtime: bool,
214273
) -> Result<(), Box<dyn std::error::Error>> {
215-
let mut builder = RuntimeBuilder::new_on_demand()
274+
let mut builder = RuntimeBuilder::new(instantiation_strategy)
216275
.use_wat(wat)
217276
.max_memory_size(max_memory_size)
218277
.precompile_runtime(precompile_runtime);
@@ -265,6 +324,7 @@ fn test_max_memory_pages(import_memory: bool, precompile_runtime: bool) {
265324
*/
266325
memory(64511, None, import_memory)
267326
),
327+
instantiation_strategy,
268328
precompile_runtime,
269329
)
270330
.unwrap();
@@ -288,6 +348,7 @@ fn test_max_memory_pages(import_memory: bool, precompile_runtime: bool) {
288348
// 1 initial, max is not specified.
289349
memory(1, None, import_memory)
290350
),
351+
instantiation_strategy,
291352
precompile_runtime,
292353
)
293354
.unwrap();
@@ -309,6 +370,7 @@ fn test_max_memory_pages(import_memory: bool, precompile_runtime: bool) {
309370
// Max is 2048.
310371
memory(1, Some(2048), import_memory)
311372
),
373+
instantiation_strategy,
312374
precompile_runtime,
313375
)
314376
.unwrap();
@@ -342,6 +404,7 @@ fn test_max_memory_pages(import_memory: bool, precompile_runtime: bool) {
342404
// Zero starting pages.
343405
memory(0, None, import_memory)
344406
),
407+
instantiation_strategy,
345408
precompile_runtime,
346409
)
347410
.unwrap();
@@ -375,6 +438,7 @@ fn test_max_memory_pages(import_memory: bool, precompile_runtime: bool) {
375438
// Initial=1, meaning after heap pages mount the total will be already 1025.
376439
memory(1, None, import_memory)
377440
),
441+
instantiation_strategy,
378442
precompile_runtime,
379443
)
380444
.unwrap();

0 commit comments

Comments
 (0)