From 0a30165f0095308656be28309d5b49fdc74b9ad6 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Mon, 4 Apr 2022 19:10:31 -0700 Subject: [PATCH 1/5] Fix convertJsFunctionToWasm when the signature has more than 122 arguments --- src/runtime_functions.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/runtime_functions.js b/src/runtime_functions.js index d638a41f55835..bec745f1b4626 100644 --- a/src/runtime_functions.js +++ b/src/runtime_functions.js @@ -4,6 +4,16 @@ * SPDX-License-Identifier: MIT */ +// This gives correct answers for everything less than 2^{14} = 16384 +// I hope nobody is contemplating functions with 16384 arguments... +function uleb128Encode(n){ + if(n < 128) { + return [n]; + } + return [(n % 128) + 128, n >> 7]; +} + + // Wraps a JS function as a wasm function with a given signature. function convertJsFunctionToWasm(func, sig) { #if WASM2JS @@ -34,8 +44,6 @@ function convertJsFunctionToWasm(func, sig) { // The module is static, with the exception of the type section, which is // generated based on the signature passed in. var typeSection = [ - 0x01, // id: section, - 0x00, // length: 0 (placeholder) 0x01, // count: 1 0x60, // form: func ]; @@ -49,7 +57,7 @@ function convertJsFunctionToWasm(func, sig) { }; // Parameters, length + signatures - typeSection.push(sigParam.length); + typeSection = typeSection.concat(uleb128Encode(sigParam.length)); for (var i = 0; i < sigParam.length; ++i) { typeSection.push(typeCodes[sigParam[i]]); } @@ -62,9 +70,12 @@ function convertJsFunctionToWasm(func, sig) { typeSection = typeSection.concat([0x01, typeCodes[sigRet]]); } - // Write the overall length of the type section back into the section header - // (excepting the 2 bytes for the section id and length) - typeSection[1] = typeSection.length - 2; + // Write the section code and overall length of the type section into the + // section header + typeSection = [0x01 /* Type section code */].concat( + uleb128Encode(typeSection.length), + typeSection + ); // Rest of the module is static var bytes = new Uint8Array([ From df1eadd5b5539f8adf899c09e71d70ae7127e54b Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Tue, 5 Apr 2022 13:21:42 -0700 Subject: [PATCH 2/5] Apply suggestions from code review Co-authored-by: Alon Zakai --- src/runtime_functions.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/runtime_functions.js b/src/runtime_functions.js index bec745f1b4626..165df240a6c5c 100644 --- a/src/runtime_functions.js +++ b/src/runtime_functions.js @@ -7,10 +7,13 @@ // This gives correct answers for everything less than 2^{14} = 16384 // I hope nobody is contemplating functions with 16384 arguments... function uleb128Encode(n){ - if(n < 128) { +#ASSERTIONS + assert(n < 16384); +#endif + if (n < 128) { return [n]; } - return [(n % 128) + 128, n >> 7]; + return [(n % 128) | 128, n >> 7]; } From b6b2652bc735c7c186996b18759f38f524991219 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Tue, 5 Apr 2022 13:23:19 -0700 Subject: [PATCH 3/5] #ASSERTIONS ==> #if ASSERTIONS --- src/runtime_functions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime_functions.js b/src/runtime_functions.js index 165df240a6c5c..e4920b7c5c8e0 100644 --- a/src/runtime_functions.js +++ b/src/runtime_functions.js @@ -7,7 +7,7 @@ // This gives correct answers for everything less than 2^{14} = 16384 // I hope nobody is contemplating functions with 16384 arguments... function uleb128Encode(n){ -#ASSERTIONS +#if ASSERTIONS assert(n < 16384); #endif if (n < 128) { From d782fcd14d70a251997e2abb566f30e56e75e0fa Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 5 Apr 2022 14:23:36 -0700 Subject: [PATCH 4/5] Update src/runtime_functions.js --- src/runtime_functions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime_functions.js b/src/runtime_functions.js index e4920b7c5c8e0..9798cf10c3506 100644 --- a/src/runtime_functions.js +++ b/src/runtime_functions.js @@ -6,7 +6,7 @@ // This gives correct answers for everything less than 2^{14} = 16384 // I hope nobody is contemplating functions with 16384 arguments... -function uleb128Encode(n){ +function uleb128Encode(n) { #if ASSERTIONS assert(n < 16384); #endif From 0c7fa14f0db41ede1064ad387c22acdfa3145632 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 5 Apr 2022 14:23:58 -0700 Subject: [PATCH 5/5] Update src/runtime_functions.js --- src/runtime_functions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime_functions.js b/src/runtime_functions.js index 9798cf10c3506..5c3dd68ac004d 100644 --- a/src/runtime_functions.js +++ b/src/runtime_functions.js @@ -11,7 +11,7 @@ function uleb128Encode(n) { assert(n < 16384); #endif if (n < 128) { - return [n]; + return [n]; } return [(n % 128) | 128, n >> 7]; }