Skip to content

Commit

Permalink
don't throw wasm compile exception after finishing bytecode gen
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeHolman committed Feb 20, 2019
1 parent 96c69fc commit a06ebc1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/WasmReader/WasmByteCodeGenerator.cpp
Expand Up @@ -483,10 +483,6 @@ void WasmBytecodeGenerator::GenerateFunctionBytecode(Js::ScriptContext* scriptCo
{
WasmBytecodeGenerator generator(scriptContext, readerinfo, validateOnly);
generator.GenerateFunction();
if (!generator.GetReader()->IsCurrentFunctionCompleted())
{
throw WasmCompilationException(_u("Invalid function format"));
}
}

void WasmBytecodeGenerator::ValidateFunction(Js::ScriptContext* scriptContext, WasmReaderInfo* readerinfo)
Expand Down Expand Up @@ -548,7 +544,12 @@ void WasmBytecodeGenerator::GenerateFunction()
gen->m_originalWriter->Reset();
}
}
void Complete() { gen = nullptr; }
void Complete()
{
gen->m_writer->End();
gen->GetReader()->FunctionEnd();
gen = nullptr;
}
};
AutoCleanupGeneratorState autoCleanupGeneratorState(this);
Js::ByteCodeLabel exitLabel = m_writer->DefineLabel();
Expand All @@ -574,8 +575,12 @@ void WasmBytecodeGenerator::GenerateFunction()
m_writer->MarkAsmJsLabel(exitLabel);
m_writer->EmptyAsm(Js::OpCodeAsmJs::Ret);
m_writer->SetCallSiteCount(this->currentProfileId);
m_writer->End();
GetReader()->FunctionEnd();

if (!GetReader()->IsCurrentFunctionCompleted())
{
throw WasmCompilationException(_u("Invalid function format"));
}

autoCleanupGeneratorState.Complete();
// Make sure we don't have any unforeseen exceptions as we finalize the body
AutoDisableInterrupt autoDisableInterrupt(m_scriptContext->GetThreadContext(), true);
Expand Down
18 changes: 18 additions & 0 deletions test/wasm/badfuncformat.js
@@ -0,0 +1,18 @@
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------

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]);
var wasmModule = new WebAssembly.Module(wasmCode);
var wasmInstance = new WebAssembly.Instance(wasmModule, {});
let threw = false;
try
{
wasmInstance.exports.f();
}
catch(e)
{
threw = true;
}
print(threw ? "Pass" : "Fail");
5 changes: 5 additions & 0 deletions test/wasm/rlexe.xml
Expand Up @@ -164,6 +164,11 @@
<compile-flags>-wasm</compile-flags>
</default>
</test>
<test>
<default>
<files>badfuncformat.js</files>
</default>
</test>
<test>
<default>
<files>table_imports.js</files>
Expand Down

0 comments on commit a06ebc1

Please sign in to comment.