Coverage Report

Created: 2026-10-31 07:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wasm-tools/crates/wasmparser/src/limits.rs
Line
Count
Source
1
/* Copyright 2017 Mozilla Foundation
2
 *
3
 * Licensed under the Apache License, Version 2.0 (the "License");
4
 * you may not use this file except in compliance with the License.
5
 * You may obtain a copy of the License at
6
 *
7
 *     http://www.apache.org/licenses/LICENSE-2.0
8
 *
9
 * Unless required by applicable law or agreed to in writing, software
10
 * distributed under the License is distributed on an "AS IS" BASIS,
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 * See the License for the specific language governing permissions and
13
 * limitations under the License.
14
 */
15
16
#![cfg_attr(not(feature = "validate"), allow(dead_code))]
17
18
// The following limits are imposed by wasmparser on WebAssembly modules.
19
// The limits are agreed upon with other engines for consistency.
20
//
21
// See https://webassembly.github.io/spec/js-api/#limits for details.
22
pub const MAX_WASM_TYPES: usize = 1_000_000;
23
pub const MAX_WASM_SUPERTYPES: usize = 5;
24
pub const MAX_WASM_FUNCTIONS: usize = 1_000_000;
25
pub const MAX_WASM_IMPORTS: usize = 1_000_000;
26
pub const MAX_WASM_EXPORTS: usize = 1_000_000;
27
pub const MAX_WASM_GLOBALS: usize = 1_000_000;
28
pub const MAX_WASM_ELEMENT_SEGMENTS: usize = 100_000;
29
pub const MAX_WASM_DATA_SEGMENTS: usize = 100_000;
30
pub const MAX_WASM_STRING_SIZE: usize = 100_000;
31
pub const MAX_WASM_FUNCTION_SIZE: usize = 7_654_321;
32
pub const MAX_WASM_FUNCTION_LOCALS: u32 = 50000;
33
pub const MAX_WASM_FUNCTION_PARAMS: usize = 1000;
34
pub const MAX_WASM_FUNCTION_RETURNS: usize = 1000;
35
pub const _MAX_WASM_TABLE_SIZE: usize = 10_000_000;
36
pub const MAX_WASM_TABLE_ENTRIES: usize = 10_000_000;
37
pub const MAX_WASM_TABLES: usize = 100_000;
38
pub const MAX_WASM_MEMORIES: usize = 100;
39
pub const MAX_WASM_TAGS: usize = 1_000_000;
40
pub const MAX_WASM_BR_TABLE_SIZE: usize = MAX_WASM_FUNCTION_SIZE;
41
pub const MAX_WASM_STRUCT_FIELDS: usize = 10_000;
42
pub const MAX_WASM_CATCHES: usize = 10_000;
43
pub const MAX_WASM_SUBTYPING_DEPTH: usize = 63;
44
pub const MAX_WASM_HANDLERS: usize = 10_000;
45
pub const MAX_WASM_TYPE_SIZE: u32 = 1_000_000;
46
pub const MAX_WASM_SELECT_RESULT_SIZE: usize = 10; // values other than 1 are currently invalid
47
pub const MAX_WASM_MODULES_AND_COMPONENTS: usize = 1000;
48
49
pub const DEFAULT_WASM_PAGE_SIZE: u64 = 1 << 16;
50
51
39.6k
pub fn max_wasm_memory32_pages(page_size: u64) -> u64 {
52
39.6k
    assert!(page_size.is_power_of_two());
53
39.6k
    assert!(page_size <= DEFAULT_WASM_PAGE_SIZE);
54
39.6k
    u32::try_from((1_u128 << 32) / u128::from(page_size)).unwrap_or(u32::MAX) as u64
55
39.6k
}
56
57
36.3k
pub fn max_wasm_memory64_pages(page_size: u64) -> u64 {
58
36.3k
    assert!(page_size.is_power_of_two());
59
36.3k
    assert!(page_size <= DEFAULT_WASM_PAGE_SIZE);
60
36.3k
    u64::try_from((1_u128 << 64) / u128::from(page_size)).unwrap_or(u64::MAX)
61
36.3k
}
62
63
// Component-related limits
64
65
#[cfg(feature = "component-model")]
66
pub use self::component_limits::*;
67
#[cfg(feature = "component-model")]
68
mod component_limits {
69
    pub const MAX_WASM_MODULE_SIZE: u32 = 1024 * 1024 * 1024; //= 1 GiB
70
    pub const MAX_WASM_MODULE_TYPE_DECLS: usize = 100_000;
71
    pub const MAX_WASM_COMPONENT_TYPE_DECLS: usize = 1_000_000;
72
    pub const MAX_WASM_INSTANCE_TYPE_DECLS: usize = 1_000_000;
73
    pub const MAX_WASM_RECORD_FIELDS: usize = 10_000;
74
    pub const MAX_WASM_VARIANT_CASES: usize = 10_000;
75
    pub const MAX_WASM_TUPLE_TYPES: usize = 10_000;
76
    /// Fixed-length lists don't contribute their length to the component
77
    /// type-size budget, so the element count gets a separate limit of 1 Gi.
78
    pub const MAX_WASM_FIXED_LENGTH_LIST_ELEMENTS: usize = 1_073_741_824;
79
    pub const MAX_WASM_FLAG_NAMES: usize = 1_000;
80
    pub const MAX_WASM_ENUM_CASES: usize = 10_000;
81
    pub const MAX_WASM_COMPONENT_TYPE_DEPTH: u32 = 100;
82
    pub const MAX_WASM_VALUE_TYPE_BYTE_SIZE: u32 = 1 << 28;
83
    pub const MAX_WASM_INSTANTIATION_EXPORTS: usize = 100_000;
84
    pub const MAX_WASM_CANONICAL_OPTIONS: usize = 10;
85
    pub const MAX_WASM_INSTANTIATION_ARGS: usize = 100_000;
86
    pub const MAX_WASM_START_ARGS: usize = 1000;
87
    pub const MAX_WASM_MODULES: usize = 1_000;
88
    pub const MAX_WASM_COMPONENTS: usize = 1_000;
89
    pub const MAX_WASM_INSTANCES: usize = 4_096;
90
    pub const MAX_WASM_VALUES: usize = 1_000;
91
92
    /// Core items in components such as globals/memories/tables don't actually
93
    /// create new definitions but are instead just aliases to preexisting items.
94
    /// This means they have a different limit than the core wasm based limits.
95
    pub const MAX_CORE_INDEX_SPACE_ITEMS: usize = 1_000_000;
96
}