[lldb] Don't defend against internal LLVM errors in IRInterpreter
authorRaphael Isemann <teemperor@gmail.com>
Tue, 14 Jan 2020 08:53:50 +0000 (09:53 +0100)
committerRaphael Isemann <teemperor@gmail.com>
Tue, 14 Jan 2020 08:55:34 +0000 (09:55 +0100)
Summary:
Whenever we cast an LLVM instruction to one of its subclasses, we do a double check if the RTTI
enum value actually allows us to cast the class. I don't see a way this can ever happen as even when
LLVM's RTTI system has some corrupt internal state (which we probably should not test in the first
place) we just reuse LLVM RTTI to do the second check.

This also means that if we ever make an actual programming error in this function (e.g., have a enum
value and then cast it to a different subclass), we just silently fall back to the JIT in our tests.

We also can't test this code in any reasonable way.

This removes the checks and uses `llvm::cast` instead which will raise a fatal error when casting fails.

Reviewers: labath, mib

Reviewed By: labath

Subscribers: abidh, JDevlieghere, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D72596

lldb/source/Expression/IRInterpreter.cpp

index aeb0351..b2e4be5 100644 (file)
@@ -800,15 +800,7 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
       }
     } break;
     case Instruction::Alloca: {
-      const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst);
-
-      if (!alloca_inst) {
-        LLDB_LOGF(log, "getOpcode() returns Alloca, but instruction is not an "
-                       "AllocaInst");
-        error.SetErrorToGenericError();
-        error.SetErrorString(interpreter_internal_error);
-        return false;
-      }
+      const AllocaInst *alloca_inst = cast<AllocaInst>(inst);
 
       if (alloca_inst->isArrayAllocation()) {
         LLDB_LOGF(log,
@@ -871,16 +863,7 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
     } break;
     case Instruction::BitCast:
     case Instruction::ZExt: {
-      const CastInst *cast_inst = dyn_cast<CastInst>(inst);
-
-      if (!cast_inst) {
-        LLDB_LOGF(
-            log, "getOpcode() returns %s, but instruction is not a BitCastInst",
-            cast_inst->getOpcodeName());
-        error.SetErrorToGenericError();
-        error.SetErrorString(interpreter_internal_error);
-        return false;
-      }
+      const CastInst *cast_inst = cast<CastInst>(inst);
 
       Value *source = cast_inst->getOperand(0);
 
@@ -896,16 +879,7 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
       frame.AssignValue(inst, S, module);
     } break;
     case Instruction::SExt: {
-      const CastInst *cast_inst = dyn_cast<CastInst>(inst);
-
-      if (!cast_inst) {
-        LLDB_LOGF(
-            log, "getOpcode() returns %s, but instruction is not a BitCastInst",
-            cast_inst->getOpcodeName());
-        error.SetErrorToGenericError();
-        error.SetErrorString(interpreter_internal_error);
-        return false;
-      }
+      const CastInst *cast_inst = cast<CastInst>(inst);
 
       Value *source = cast_inst->getOperand(0);
 
@@ -925,15 +899,7 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
       frame.AssignValue(inst, S_signextend, module);
     } break;
     case Instruction::Br: {
-      const BranchInst *br_inst = dyn_cast<BranchInst>(inst);
-
-      if (!br_inst) {
-        LLDB_LOGF(
-            log, "getOpcode() returns Br, but instruction is not a BranchInst");
-        error.SetErrorToGenericError();
-        error.SetErrorString(interpreter_internal_error);
-        return false;
-      }
+      const BranchInst *br_inst = cast<BranchInst>(inst);
 
       if (br_inst->isConditional()) {
         Value *condition = br_inst->getCondition();
@@ -967,15 +933,7 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
     }
       continue;
     case Instruction::PHI: {
-      const PHINode *phi_inst = dyn_cast<PHINode>(inst);
-
-      if (!phi_inst) {
-        LLDB_LOGF(log,
-                  "getOpcode() returns PHI, but instruction is not a PHINode");
-        error.SetErrorToGenericError();
-        error.SetErrorString(interpreter_internal_error);
-        return false;
-      }
+      const PHINode *phi_inst = cast<PHINode>(inst);
       if (!frame.m_prev_bb) {
         LLDB_LOGF(log,
                   "Encountered PHI node without having jumped from another "
@@ -1002,15 +960,7 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
       }
     } break;
     case Instruction::GetElementPtr: {
-      const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst);
-
-      if (!gep_inst) {
-        LLDB_LOGF(log, "getOpcode() returns GetElementPtr, but instruction is "
-                       "not a GetElementPtrInst");
-        error.SetErrorToGenericError();
-        error.SetErrorString(interpreter_internal_error);
-        return false;
-      }
+      const GetElementPtrInst *gep_inst = cast<GetElementPtrInst>(inst);
 
       const Value *pointer_operand = gep_inst->getPointerOperand();
       Type *src_elem_ty = gep_inst->getSourceElementType();
@@ -1072,16 +1022,7 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
       }
     } break;
     case Instruction::ICmp: {
-      const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst);
-
-      if (!icmp_inst) {
-        LLDB_LOGF(
-            log,
-            "getOpcode() returns ICmp, but instruction is not an ICmpInst");
-        error.SetErrorToGenericError();
-        error.SetErrorString(interpreter_internal_error);
-        return false;
-      }
+      const ICmpInst *icmp_inst = cast<ICmpInst>(inst);
 
       CmpInst::Predicate predicate = icmp_inst->getPredicate();
 
@@ -1168,16 +1109,7 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
       }
     } break;
     case Instruction::IntToPtr: {
-      const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst);
-
-      if (!int_to_ptr_inst) {
-        LLDB_LOGF(log,
-                  "getOpcode() returns IntToPtr, but instruction is not an "
-                  "IntToPtrInst");
-        error.SetErrorToGenericError();
-        error.SetErrorString(interpreter_internal_error);
-        return false;
-      }
+      const IntToPtrInst *int_to_ptr_inst = cast<IntToPtrInst>(inst);
 
       Value *src_operand = int_to_ptr_inst->getOperand(0);
 
@@ -1199,16 +1131,7 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
       }
     } break;
     case Instruction::PtrToInt: {
-      const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst);
-
-      if (!ptr_to_int_inst) {
-        LLDB_LOGF(log,
-                  "getOpcode() returns PtrToInt, but instruction is not an "
-                  "PtrToIntInst");
-        error.SetErrorToGenericError();
-        error.SetErrorString(interpreter_internal_error);
-        return false;
-      }
+      const PtrToIntInst *ptr_to_int_inst = cast<PtrToIntInst>(inst);
 
       Value *src_operand = ptr_to_int_inst->getOperand(0);
 
@@ -1230,16 +1153,7 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
       }
     } break;
     case Instruction::Trunc: {
-      const TruncInst *trunc_inst = dyn_cast<TruncInst>(inst);
-
-      if (!trunc_inst) {
-        LLDB_LOGF(
-            log,
-            "getOpcode() returns Trunc, but instruction is not a TruncInst");
-        error.SetErrorToGenericError();
-        error.SetErrorString(interpreter_internal_error);
-        return false;
-      }
+      const TruncInst *trunc_inst = cast<TruncInst>(inst);
 
       Value *src_operand = trunc_inst->getOperand(0);
 
@@ -1261,15 +1175,7 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
       }
     } break;
     case Instruction::Load: {
-      const LoadInst *load_inst = dyn_cast<LoadInst>(inst);
-
-      if (!load_inst) {
-        LLDB_LOGF(
-            log, "getOpcode() returns Load, but instruction is not a LoadInst");
-        error.SetErrorToGenericError();
-        error.SetErrorString(interpreter_internal_error);
-        return false;
-      }
+      const LoadInst *load_inst = cast<LoadInst>(inst);
 
       // The semantics of Load are:
       //   Create a region D that will contain the loaded data
@@ -1351,16 +1257,7 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
       return true;
     }
     case Instruction::Store: {
-      const StoreInst *store_inst = dyn_cast<StoreInst>(inst);
-
-      if (!store_inst) {
-        LLDB_LOGF(
-            log,
-            "getOpcode() returns Store, but instruction is not a StoreInst");
-        error.SetErrorToGenericError();
-        error.SetErrorString(interpreter_internal_error);
-        return false;
-      }
+      const StoreInst *store_inst = cast<StoreInst>(inst);
 
       // The semantics of Store are:
       //   Resolve the region D containing the data to be stored
@@ -1436,16 +1333,7 @@ bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
       }
     } break;
     case Instruction::Call: {
-      const CallInst *call_inst = dyn_cast<CallInst>(inst);
-
-      if (!call_inst) {
-        LLDB_LOGF(log,
-                  "getOpcode() returns %s, but instruction is not a CallInst",
-                  inst->getOpcodeName());
-        error.SetErrorToGenericError();
-        error.SetErrorString(interpreter_internal_error);
-        return false;
-      }
+      const CallInst *call_inst = cast<CallInst>(inst);
 
       if (CanIgnoreCall(call_inst))
         break;