Skip to content

Truly independent web browser

LibWasm: Reject function types with more than 1000 parameters or results

Tim Ledbetterf6a5f0883f4f
diff --git a/Libraries/LibWasm/Module/src/validator.rs b/Libraries/LibWasm/Module/src/validator.rs
index a094673d97e..f667a6cbd06 100644
--- a/Libraries/LibWasm/Module/src/validator.rs
+++ b/Libraries/LibWasm/Module/src/validator.rs
@@ -144,6 +144,12 @@ const MEMORY64_MAX_PAGES: u64 = 1 << 48;
// The maximum depth of a defined subtype hierarchy is 63 (where a type defined with no supertype has depth 0).
const MAX_SUBTYPING_DEPTH: u32 = 63;
+// The maximum number of parameters to any function or block is 1,000.
+const MAX_FUNCTION_PARAMS: usize = 1_000;
+
+// The maximum number of return values for any function or block is 1,000.
+const MAX_FUNCTION_RESULTS: usize = 1_000;
+
// The maximum number of locals declared in a function, including implicitly declared as parameters, is 50,000.
const MAX_FUNCTION_LOCALS: usize = 50_000;
@@ -2206,6 +2212,12 @@ fn validate_types(module: &Module, identities: &mut Vec<u32>) -> Result<()> {
};
match &sub_type.composite {
CompositeType::Func(function) => {
+ if function.params.len() > MAX_FUNCTION_PARAMS {
+ return invalid("too many parameters");
+ }
+ if function.results.len() > MAX_FUNCTION_RESULTS {
+ return invalid("too many results");
+ }
function
.params
.iter()
diff --git a/Libraries/LibWasm/Tests/Parser/test-function-type-limits.js b/Libraries/LibWasm/Tests/Parser/test-function-type-limits.js
new file mode 100644
index 00000000000..ecc0a493a5e
--- /dev/null
+++ b/Libraries/LibWasm/Tests/Parser/test-function-type-limits.js
@@ -0,0 +1,32 @@
+function uleb(value) {
+ const bytes = [];
+ do {
+ const byte = value & 0x7f;
+ value >>= 7;
+ bytes.push(value ? byte | 0x80 : byte);
+ } while (value);
+ return bytes;
+}
+
+function moduleWithFunctionType(paramCount, resultCount) {
+ const i32 = 0x7f;
+ const types = [
+ 1,
+ 0x60,
+ ...uleb(paramCount),
+ ...Array(paramCount).fill(i32),
+ ...uleb(resultCount),
+ ...Array(resultCount).fill(i32),
+ ];
+ return new Uint8Array([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 1, ...uleb(types.length), ...types]);
+}
+
+test("a function type has at most 1000 parameters", () => {
+ parseWebAssemblyModule(moduleWithFunctionType(1000, 0));
+ expect(() => parseWebAssemblyModule(moduleWithFunctionType(1001, 0))).toThrow(SyntaxError);
+});
+
+test("a function type has at most 1000 results", () => {
+ parseWebAssemblyModule(moduleWithFunctionType(0, 1000));
+ expect(() => parseWebAssemblyModule(moduleWithFunctionType(0, 1001))).toThrow(SyntaxError);
+});