Skip to content

Fix convertJsFunctionToWasm when the signature has more than 122 arguments #16658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/runtime_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
* 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 ASSERTIONS
assert(n < 16384);
#endif
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
Expand Down Expand Up @@ -34,8 +47,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
];
Expand All @@ -49,7 +60,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]]);
}
Expand All @@ -62,9 +73,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([
Expand Down