Commit 0042fea3 by Karl Schimpf

Fix handling unknown branches when parsing switch instructions.

The bitcode reader for the switch insruction did not check if the branch labels were defined. This patch fixes the problem. Includes test for such a case. BUG=None R=stichnot@chromium.org Review URL: https://codereview.chromium.org/1303003002 .
parent 360e3197
...@@ -2415,6 +2415,8 @@ void FunctionParser::ProcessRecord() { ...@@ -2415,6 +2415,8 @@ void FunctionParser::ProcessRecord() {
} }
Ice::CfgNode *DefaultLabel = Ice::CfgNode *DefaultLabel =
isIRGenDisabled ? nullptr : getBranchBasicBlock(Values[2]); isIRGenDisabled ? nullptr : getBranchBasicBlock(Values[2]);
if (DefaultLabel == nullptr)
return;
uint64_t NumCasesRaw = Values[3]; uint64_t NumCasesRaw = Values[3];
if (NumCasesRaw > std::numeric_limits<uint32_t>::max()) { if (NumCasesRaw > std::numeric_limits<uint32_t>::max()) {
std::string Buffer; std::string Buffer;
...@@ -2428,10 +2430,10 @@ void FunctionParser::ProcessRecord() { ...@@ -2428,10 +2430,10 @@ void FunctionParser::ProcessRecord() {
// Now recognize each of the cases. // Now recognize each of the cases.
if (!isValidRecordSize(4 + NumCases * 4, "switch")) if (!isValidRecordSize(4 + NumCases * 4, "switch"))
return; return;
Ice::InstSwitch *Switch = std::unique_ptr<Ice::InstSwitch> Switch(
isIRGenDisabled isIRGenDisabled ? nullptr
? nullptr : Ice::InstSwitch::create(Func.get(), NumCases, Cond,
: Ice::InstSwitch::create(Func.get(), NumCases, Cond, DefaultLabel); DefaultLabel));
unsigned ValCaseIndex = 4; // index to beginning of case entry. unsigned ValCaseIndex = 4; // index to beginning of case entry.
for (uint32_t CaseIndex = 0; CaseIndex < NumCases; for (uint32_t CaseIndex = 0; CaseIndex < NumCases;
++CaseIndex, ValCaseIndex += 4) { ++CaseIndex, ValCaseIndex += 4) {
...@@ -2448,11 +2450,13 @@ void FunctionParser::ProcessRecord() { ...@@ -2448,11 +2450,13 @@ void FunctionParser::ProcessRecord() {
if (isIRGenDisabled) if (isIRGenDisabled)
continue; continue;
Ice::CfgNode *Label = getBranchBasicBlock(Values[ValCaseIndex + 3]); Ice::CfgNode *Label = getBranchBasicBlock(Values[ValCaseIndex + 3]);
if (Label == nullptr)
return;
Switch->addBranch(CaseIndex, Value.getSExtValue(), Label); Switch->addBranch(CaseIndex, Value.getSExtValue(), Label);
} }
if (isIRGenDisabled) if (isIRGenDisabled)
return; return;
CurrentNode->appendInst(Switch); CurrentNode->appendInst(Switch.release());
return; return;
} }
case naclbitc::FUNC_CODE_INST_UNREACHABLE: { case naclbitc::FUNC_CODE_INST_UNREACHABLE: {
......
65535,8,2;
1,1;
65535,17,2;
1,3;
2;
7,32;
21,0,0,1;
65534;
8,2,0,0,0;
65535,19,2;
5,0;
65534;
65535,14,2;
1,0,83,119,105,116,99,104,86,97,114,105,97,98,108,101;
65534;
65535,12,2;
1,6;
12,1,1,2,2,1,1,2,3105555534,1,1,8,4;
11,5;
11,5;
11,5;
11,5;
10;
65534;
65534;
; REQUIRES: no_minimal_build
; RUN: not %pnacl_sz -bitcode-as-text %p/Inputs/bad-switch-case.tbc \
; RUN: -bitcode-format=pnacl -notranslate -build-on-read 2>&1 \
; RUN: | FileCheck -check-prefix=BAD-SWITCH-CASE %s
; BAD-SWITCH-CASE: Reference to basic block 3105555534 not found. Must be less than 6
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment