Skip to content

Commit a7f7571

Browse files
committed
[MERGE #2097 @arunetm] Using WebAssembly.RuntimeErorrs
Merge pull request #2097 from arunetm:b-arpuru/WasmRuntimeErrors-rs2 Throw WebAssembly.RuntimeErorrs for wasm traps. Fixes #1991
2 parents 248bfab + 159e40e commit a7f7571

File tree

9 files changed

+34
-33
lines changed

9 files changed

+34
-33
lines changed

lib/Backend/LowerMDShared.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5892,13 +5892,7 @@ IR::Opnd* LowererMD::GenerateTruncChecks(IR::Instr* instr)
58925892

58935893
m_lowerer->InsertCompareBranch(limitReg, src64, Js::OpCode::BrGt_A, conversion, instr, true /*no NaN check*/);
58945894
instr->InsertBefore(throwLabel);
5895-
IR::Instr *throwInstr = IR::Instr::New(
5896-
Js::OpCode::RuntimeTypeError,
5897-
IR::RegOpnd::New(TyMachReg, m_func),
5898-
IR::IntConstOpnd::New(SCODE_CODE(VBSERR_Overflow), TyInt32, m_func),
5899-
m_func);
5900-
instr->InsertBefore(throwInstr);
5901-
this->m_lowerer->LowerUnaryHelperMem(throwInstr, IR::HelperOp_RuntimeTypeError);
5895+
this->m_lowerer->GenerateThrow(IR::IntConstOpnd::New(SCODE_CODE(VBSERR_Overflow), TyInt32, m_func), instr);
59025896
//no jump here we aren't coming back
59035897

59045898
instr->InsertBefore(conversion);

lib/Backend/amd64/LowererMDArch.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,8 +1133,7 @@ LowererMDArch::LowerWasmMemOp(IR::Instr * instr, IR::Opnd *addrOpnd)
11331133
Lowerer::InsertAdd(true, cmpOpnd, indexOpnd, IR::IntConstOpnd::New(addrOpnd->GetSize(), TyUint64, m_func), helperLabel);
11341134
lowererMD->m_lowerer->InsertCompareBranch(cmpOpnd, arrayLenOpnd, Js::OpCode::BrGt_A, true, helperLabel, helperLabel);
11351135

1136-
// MGTODO : call RuntimeError once implemented
1137-
lowererMD->m_lowerer->GenerateRuntimeError(loadLabel, JSERR_InvalidTypedArrayIndex, IR::HelperOp_RuntimeRangeError);
1136+
lowererMD->m_lowerer->GenerateRuntimeError(loadLabel, WASMERR_ArrayIndexOutOfRange, IR::HelperOp_WebAssemblyRuntimeError);
11381137
Lowerer::InsertBranch(Js::OpCode::Br, loadLabel, helperLabel);
11391138
return doneLabel;
11401139
}

lib/Backend/i386/LowererMDArch.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -988,8 +988,7 @@ LowererMDArch::LowerWasmMemOp(IR::Instr * instr, IR::Opnd *addrOpnd)
988988
lowererMD->m_lowerer->InsertCompareBranch(cmpOpnd, arrayLenOpnd, Js::OpCode::BrGe_A, true, helperLabel, helperLabel);
989989
}
990990

991-
// MGTODO : call RuntimeError once implemented
992-
lowererMD->m_lowerer->GenerateRuntimeError(loadLabel, JSERR_InvalidTypedArrayIndex, IR::HelperOp_RuntimeRangeError);
991+
lowererMD->m_lowerer->GenerateRuntimeError(loadLabel, WASMERR_ArrayIndexOutOfRange, IR::HelperOp_WebAssemblyRuntimeError);
993992
Lowerer::InsertBranch(Js::OpCode::Br, loadLabel, helperLabel);
994993

995994
Assert(indexPair.low->IsRegOpnd());

lib/Parser/rterrors.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,13 @@ RT_ERROR_MSG(WASMERR_InvalidImport, 5672, "", "Import is invalid", kjstTypeError
373373
RT_ERROR_MSG(WASMERR_InvalidGlobalRef, 5673, "", "Global initialization does not support forward reference", kjstTypeError, 0)
374374
RT_ERROR_MSG(WASMERR_NeedMemoryObject, 5674, "%s is not a WebAssembly.Memory", "WebAssembly.Memory object expected", kjstTypeError, 0)
375375
RT_ERROR_MSG(WASMERR_InvalidTypeConversion, 5675, "Invalid WebAssembly type conversion %s to %s", "Invalid WebAssembly type conversion", kjstTypeError, 0)
376-
RT_ERROR_MSG(WASMERR_DivideByZero, 5676, "", "Division by zero", kjstError, 0)
376+
RT_ERROR_MSG(WASMERR_DivideByZero, 5676, "", "Division by zero", kjstWebAssemblyRuntimeError, 0)
377377
RT_ERROR_MSG(WASMERR_ExpectedAnyFunc, 5677, "%s is not AnyFunc", "AnyFunc expected", kjstTypeError, 0)
378378
RT_ERROR_MSG(WASMERR_NeedTableObject, 5678, "%s is not a WebAssembly.Table", "WebAssembly.Table object expected", kjstTypeError, 0)
379379
RT_ERROR_MSG(WASMERR_NeedWebAssemblyFunc, 5679, "%s is not a WebAssembly exported function", "WebAssembly exported function expected", kjstTypeError, 0)
380380
RT_ERROR_MSG(WASMERR_SignatureMismatch, 5680, "%s called with invalid signature", "Function called with invalid signature", kjstWebAssemblyRuntimeError, 0)
381381
RT_ERROR_MSG(WASMERR_ElementSegOutOfRange, 5681, "", "Element segment is out of range", kjstTypeError, 0)
382382
RT_ERROR_MSG(WASMERR_TableIndexOutOfRange, 5682, "", "Table index is out of range", kjstWebAssemblyRuntimeError, 0)
383-
RT_ERROR_MSG(JSERR_CantRedefineProp, 5683, "Cannot redefine property '%s'", "Cannot redefine property", kjstTypeError, 0)
384-
RT_ERROR_MSG(WASMERR_InvalidInstantiateArgument, 5684, "", "Invalid arguments to instantiate", kjstTypeError, 0)
383+
RT_ERROR_MSG(WASMERR_ArrayIndexOutOfRange, 5683, "", "Memory index is out of range", kjstWebAssemblyRuntimeError, 0)
384+
RT_ERROR_MSG(JSERR_CantRedefineProp, 5684, "Cannot redefine property '%s'", "Cannot redefine property", kjstTypeError, 0)
385+
RT_ERROR_MSG(WASMERR_InvalidInstantiateArgument, 5685, "", "Invalid arguments to instantiate", kjstTypeError, 0)

lib/Runtime/Language/InterpreterStackFrame.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7824,7 +7824,7 @@ const byte * InterpreterStackFrame::OP_ProfiledLoopBodyStart(const byte * ip)
78247824
{
78257825
if (aRight == 0)
78267826
{
7827-
JavascriptError::ThrowError(scriptContext, WASMERR_DivideByZero);
7827+
JavascriptError::ThrowWebAssemblyRuntimeError(scriptContext, WASMERR_DivideByZero);
78287828
}
78297829

78307830
return func(aLeft, aRight);
@@ -7834,12 +7834,12 @@ const byte * InterpreterStackFrame::OP_ProfiledLoopBodyStart(const byte * ip)
78347834
{
78357835
if (aRight == 0)
78367836
{
7837-
JavascriptError::ThrowError(scriptContext, WASMERR_DivideByZero);
7837+
JavascriptError::ThrowWebAssemblyRuntimeError(scriptContext, WASMERR_DivideByZero);
78387838
}
78397839

78407840
if (aLeft == MIN && aRight == -1)
78417841
{
7842-
JavascriptError::ThrowError(scriptContext, VBSERR_Overflow);
7842+
JavascriptError::ThrowWebAssemblyRuntimeError(scriptContext, VBSERR_Overflow);
78437843
}
78447844

78457845
return func(aLeft, aRight);
@@ -8466,7 +8466,7 @@ const byte * InterpreterStackFrame::OP_ProfiledLoopBodyStart(const byte * ip)
84668466
JavascriptArrayBuffer* arr = *(JavascriptArrayBuffer**)GetNonVarReg(AsmJsFunctionMemory::ArrayBufferRegister);
84678467
if (index + TypeToSizeMap[playout->ViewType] > arr->GetByteLength())
84688468
{
8469-
JavascriptError::ThrowRangeError(scriptContext, JSERR_InvalidTypedArrayIndex);
8469+
JavascriptError::ThrowWebAssemblyRuntimeError(scriptContext, WASMERR_ArrayIndexOutOfRange);
84708470
}
84718471
BYTE* buffer = arr->GetBuffer();
84728472
switch (playout->ViewType)
@@ -8511,8 +8511,8 @@ const byte * InterpreterStackFrame::OP_ProfiledLoopBodyStart(const byte * ip)
85118511
JavascriptArrayBuffer* arr = *(JavascriptArrayBuffer**)GetNonVarReg(AsmJsFunctionMemory::ArrayBufferRegister);
85128512
if (index + TypeToSizeMap[playout->ViewType] > arr->GetByteLength())
85138513
{
8514-
JavascriptError::ThrowRangeError(scriptContext, JSERR_InvalidTypedArrayIndex);
8515-
}
8514+
JavascriptError::ThrowWebAssemblyRuntimeError(scriptContext, WASMERR_ArrayIndexOutOfRange);
8515+
}
85168516
BYTE* buffer = arr->GetBuffer();
85178517
switch (playout->ViewType)
85188518
{

lib/Runtime/Language/JavascriptConversion.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ namespace Js
15541554
return (int32)src;
15551555
}
15561556

1557-
JavascriptError::ThrowError(ctx, VBSERR_Overflow);
1557+
JavascriptError::ThrowWebAssemblyRuntimeError(ctx, VBSERR_Overflow);
15581558
}
15591559

15601560
uint32 JavascriptConversion::F32TOU32(float src, ScriptContext * ctx)
@@ -1566,7 +1566,7 @@ namespace Js
15661566
return (uint32)src;
15671567
}
15681568

1569-
JavascriptError::ThrowError(ctx, VBSERR_Overflow);
1569+
JavascriptError::ThrowWebAssemblyRuntimeError(ctx, VBSERR_Overflow);
15701570
}
15711571

15721572
int32 JavascriptConversion::F64TOI32(double src, ScriptContext * ctx)
@@ -1578,7 +1578,7 @@ namespace Js
15781578
return (int32)src;
15791579
}
15801580

1581-
JavascriptError::ThrowError(ctx, VBSERR_Overflow);
1581+
JavascriptError::ThrowWebAssemblyRuntimeError(ctx, VBSERR_Overflow);
15821582
}
15831583

15841584
uint32 JavascriptConversion::F64TOU32(double src, ScriptContext * ctx)
@@ -1590,7 +1590,7 @@ namespace Js
15901590
return (uint32)src;
15911591
}
15921592

1593-
JavascriptError::ThrowError(ctx, VBSERR_Overflow);
1593+
JavascriptError::ThrowWebAssemblyRuntimeError(ctx, VBSERR_Overflow);
15941594
}
15951595

15961596
int64 JavascriptConversion::F32TOI64(float src, ScriptContext * ctx)
@@ -1602,7 +1602,7 @@ namespace Js
16021602
return (int64)src;
16031603
}
16041604

1605-
JavascriptError::ThrowError(ctx, VBSERR_Overflow);
1605+
JavascriptError::ThrowWebAssemblyRuntimeError(ctx, VBSERR_Overflow);
16061606
}
16071607

16081608
uint64 JavascriptConversion::F32TOU64(float src, ScriptContext * ctx)
@@ -1614,7 +1614,7 @@ namespace Js
16141614
return (uint64)src;
16151615
}
16161616

1617-
JavascriptError::ThrowError(ctx, VBSERR_Overflow);
1617+
JavascriptError::ThrowWebAssemblyRuntimeError(ctx, VBSERR_Overflow);
16181618
}
16191619

16201620
int64 JavascriptConversion::F64TOI64(double src, ScriptContext * ctx)
@@ -1626,7 +1626,7 @@ namespace Js
16261626
return (int64)src;
16271627
}
16281628

1629-
JavascriptError::ThrowError(ctx, VBSERR_Overflow);
1629+
JavascriptError::ThrowWebAssemblyRuntimeError(ctx, VBSERR_Overflow);
16301630
}
16311631

16321632
uint64 JavascriptConversion::F64TOU64(double src, ScriptContext * ctx)
@@ -1638,7 +1638,7 @@ namespace Js
16381638
return (uint64)src;
16391639
}
16401640

1641-
JavascriptError::ThrowError(ctx, VBSERR_Overflow);
1641+
JavascriptError::ThrowWebAssemblyRuntimeError(ctx, VBSERR_Overflow);
16421642
}
16431643

16441644
int64 JavascriptConversion::ToLength(Var aValue, ScriptContext* scriptContext)

lib/Runtime/Language/JavascriptExceptionOperators.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,14 +1043,14 @@ namespace Js
10431043
JavascriptError::ThrowRangeError(scriptContext, MAKE_HR(messageId));
10441044
}
10451045

1046-
Var JavascriptExceptionOperators::OP_RuntimeReferenceError(MessageId messageId, ScriptContext *scriptContext)
1046+
Var JavascriptExceptionOperators::OP_WebAssemblyRuntimeError(MessageId messageId, ScriptContext *scriptContext)
10471047
{
1048-
JavascriptError::ThrowReferenceError(scriptContext, MAKE_HR(messageId));
1048+
JavascriptError::ThrowWebAssemblyRuntimeError(scriptContext, MAKE_HR(messageId));
10491049
}
10501050

1051-
Var JavascriptExceptionOperators::OP_WebAssemblyRuntimeError(MessageId messageId, ScriptContext* scriptContext)
1051+
Var JavascriptExceptionOperators::OP_RuntimeReferenceError(MessageId messageId, ScriptContext *scriptContext)
10521052
{
1053-
JavascriptError::ThrowWebAssemblyRuntimeError(scriptContext, MAKE_HR(messageId));
1053+
JavascriptError::ThrowReferenceError(scriptContext, MAKE_HR(messageId));
10541054
}
10551055

10561056
// Throw type error on access 'arguments', 'callee' or 'caller' when in a restricted context

lib/Runtime/Library/JavascriptError.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,10 @@ namespace Js
320320
return CreateReferenceError(scriptContext);
321321
case kjstURIError:
322322
return CreateURIError(scriptContext);
323+
case kjstWebAssemblyCompileError:
324+
return CreateWebAssemblyCompileError(scriptContext);
325+
case kjstWebAssemblyRuntimeError:
326+
return CreateWebAssemblyRuntimeError(scriptContext);
323327
default:
324328
AssertMsg(FALSE, "Invalid error type");
325329
__assume(false);
@@ -776,6 +780,10 @@ namespace Js
776780
case kjstURIError:
777781
jsNewError = targetJavascriptLibrary->CreateURIError();
778782
break;
783+
case kjstWebAssemblyCompileError:
784+
jsNewError = targetJavascriptLibrary->CreateWebAssemblyCompileError();
785+
case kjstWebAssemblyRuntimeError:
786+
jsNewError = targetJavascriptLibrary->CreateWebAssemblyRuntimeError();
779787

780788
case kjstCustomError:
781789
default:

test/wasm/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ async function testMemoryApi(baseModule) {
206206
load(index);
207207
console.log("Should have trap with out of bounds error");
208208
} catch (e) {
209-
if (e instanceof RangeError) {
209+
if (e instanceof WebAssembly.RuntimeError) {
210210
console.log(`Correctly trap on heap access at ${index}`);
211211
} else {
212212
console.log(`Unexpected Error: ${e}`);

0 commit comments

Comments
 (0)