[PATCH] D14694: [ARM] Prevent use of a value pointed by end() iterator when placing a jump table

Petr Pavlu via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 16 00:37:22 PST 2015


petpav01 created this revision.
petpav01 added a subscriber: llvm-commits.
Herald added subscribers: rengolin, aemerson.

Function `ARMConstantIslands::doInitialJumpTablePlacement()` iterates over all basic blocks in a machine function. It calls `MI = MBB.getLastNonDebugInstr()` to get the last instruction in each block and then uses `MI->getOpcode()` to decide what to do. If `getLastNonDebugInstr()` returns `MBB.end()` (for example, when the block does not contain any instructions) then calling `getOpcode()` on
this value is incorrect. Avoid this problem by checking the result of `getLastNonDebugInstr()`.

The problem can be shown by compiling the following example with `clang --target=armv8a-none-none-eabi -O3`:
```
void v0();
void v1();
void v2();
void v3();
void v4();
int a;
 
void test(void) {
  switch (a) {
    default:
      v0();
      break;
    case 1:
      v1();
      break;
    case 2:
      v2();
      break;
    case 3:
      v3();
      break;
    case 4:
      v4();
      break;
  }
  try {
    throw 0;
  } catch (int) {
  }
}
```

http://reviews.llvm.org/D14694

Files:
  lib/Target/ARM/ARMConstantIslandPass.cpp

Index: lib/Target/ARM/ARMConstantIslandPass.cpp
===================================================================
--- lib/Target/ARM/ARMConstantIslandPass.cpp
+++ lib/Target/ARM/ARMConstantIslandPass.cpp
@@ -589,6 +589,8 @@
   MachineBasicBlock *LastCorrectlyNumberedBB = nullptr;
   for (MachineBasicBlock &MBB : *MF) {
     auto MI = MBB.getLastNonDebugInstr();
+    if (MI == MBB.end())
+      continue;
 
     unsigned JTOpcode;
     switch (MI->getOpcode()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D14694.40252.patch
Type: text/x-patch
Size: 467 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151116/2ff9ab33/attachment.bin>


More information about the llvm-commits mailing list