Skip to content

Commit 4a18b8a

Browse files
committed
[MERGE #5963 @MikeHolman] don't throw wasm compile exception after finishing bytecode gen
Merge pull request #5963 from MikeHolman:badwasmfunc Fixes #5956
2 parents 2b978f1 + a06ebc1 commit 4a18b8a

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

lib/WasmReader/WasmByteCodeGenerator.cpp

+12-7
Original file line numberDiff line numberDiff line change
@@ -483,10 +483,6 @@ void WasmBytecodeGenerator::GenerateFunctionBytecode(Js::ScriptContext* scriptCo
483483
{
484484
WasmBytecodeGenerator generator(scriptContext, readerinfo, validateOnly);
485485
generator.GenerateFunction();
486-
if (!generator.GetReader()->IsCurrentFunctionCompleted())
487-
{
488-
throw WasmCompilationException(_u("Invalid function format"));
489-
}
490486
}
491487

492488
void WasmBytecodeGenerator::ValidateFunction(Js::ScriptContext* scriptContext, WasmReaderInfo* readerinfo)
@@ -548,7 +544,12 @@ void WasmBytecodeGenerator::GenerateFunction()
548544
gen->m_originalWriter->Reset();
549545
}
550546
}
551-
void Complete() { gen = nullptr; }
547+
void Complete()
548+
{
549+
gen->m_writer->End();
550+
gen->GetReader()->FunctionEnd();
551+
gen = nullptr;
552+
}
552553
};
553554
AutoCleanupGeneratorState autoCleanupGeneratorState(this);
554555
Js::ByteCodeLabel exitLabel = m_writer->DefineLabel();
@@ -574,8 +575,12 @@ void WasmBytecodeGenerator::GenerateFunction()
574575
m_writer->MarkAsmJsLabel(exitLabel);
575576
m_writer->EmptyAsm(Js::OpCodeAsmJs::Ret);
576577
m_writer->SetCallSiteCount(this->currentProfileId);
577-
m_writer->End();
578-
GetReader()->FunctionEnd();
578+
579+
if (!GetReader()->IsCurrentFunctionCompleted())
580+
{
581+
throw WasmCompilationException(_u("Invalid function format"));
582+
}
583+
579584
autoCleanupGeneratorState.Complete();
580585
// Make sure we don't have any unforeseen exceptions as we finalize the body
581586
AutoDisableInterrupt autoDisableInterrupt(m_scriptContext->GetThreadContext(), true);

test/wasm/badfuncformat.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//-------------------------------------------------------------------------------------------------------
2+
// Copyright (C) Microsoft Corporation and contributors. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4+
//-------------------------------------------------------------------------------------------------------
5+
6+
var wasmCode = new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,127,3,2,1,0,5,4,1,1,0,0,7,5,1,1,102,0,0,10,6,1,4,0,0,11,11,0,11,4,110,97,109,101,1,4,1,0,1,102]);
7+
var wasmModule = new WebAssembly.Module(wasmCode);
8+
var wasmInstance = new WebAssembly.Instance(wasmModule, {});
9+
let threw = false;
10+
try
11+
{
12+
wasmInstance.exports.f();
13+
}
14+
catch(e)
15+
{
16+
threw = true;
17+
}
18+
print(threw ? "Pass" : "Fail");

test/wasm/rlexe.xml

+5
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@
164164
<compile-flags>-wasm</compile-flags>
165165
</default>
166166
</test>
167+
<test>
168+
<default>
169+
<files>badfuncformat.js</files>
170+
</default>
171+
</test>
167172
<test>
168173
<default>
169174
<files>table_imports.js</files>

0 commit comments

Comments
 (0)