Skip to content

Commit

Permalink
add ifdefs for C++ exception handling
Browse files Browse the repository at this point in the history
restoring old behavior without EH support
  • Loading branch information
jbuening authored and ccadar committed Feb 16, 2021
1 parent dbe3e45 commit 5f08b02
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 4 deletions.
10 changes: 10 additions & 0 deletions lib/Core/Executor.cpp
Expand Up @@ -288,10 +288,12 @@ cl::list<Executor::TerminateReason> ExitOnErrorType(
clEnumValN(Executor::ReportError, "ReportError",
"klee_report_error called"),
clEnumValN(Executor::User, "User", "Wrong klee_* functions invocation"),
#ifdef SUPPORT_KLEE_EH_CXX
clEnumValN(Executor::UncaughtException, "UncaughtException",
"Exception was not caught"),
clEnumValN(Executor::UnexpectedException, "UnexpectedException",
"An unexpected exception was thrown"),
#endif
clEnumValN(Executor::Unhandled, "Unhandled",
"Unhandled instruction hit") KLEE_LLVM_CL_VAL_END),
cl::ZeroOrMore,
Expand Down Expand Up @@ -441,8 +443,10 @@ const char *Executor::TerminateReasonNames[] = {
[ ReadOnly ] = "readonly",
[ ReportError ] = "reporterror",
[ User ] = "user",
#ifdef SUPPORT_KLEE_EH_CXX
[ UncaughtException ] = "uncaught_exception",
[ UnexpectedException ] = "unexpected_exception",
#endif
[ Unhandled ] = "xxx",
};

Expand Down Expand Up @@ -1743,10 +1747,12 @@ void Executor::executeCall(ExecutionState &state, KInstruction *ki, Function *f,
break;
}

#ifdef SUPPORT_KLEE_EH_CXX
case Intrinsic::eh_typeid_for: {
bindLocal(ki, state, getEhTypeidFor(arguments.at(0)));
break;
}
#endif

case Intrinsic::vaend:
// va_end is a noop for the interpreter.
Expand Down Expand Up @@ -2026,6 +2032,7 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) {
++state.pc;
}

#ifdef SUPPORT_KLEE_EH_CXX
if (ri->getFunction()->getName() == "_klee_eh_cxx_personality") {
assert(dyn_cast<ConstantExpr>(result) &&
"result from personality fn must be a concrete value");
Expand Down Expand Up @@ -2059,6 +2066,7 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) {
// never return normally from the personality fn
break;
}
#endif // SUPPORT_KLEE_EH_CXX

if (!isVoidReturn) {
Type *t = caller->getType();
Expand Down Expand Up @@ -3152,6 +3160,7 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) {
terminateStateOnExecError(state, "Unexpected ShuffleVector instruction");
break;

#ifdef SUPPORT_KLEE_EH_CXX
case Instruction::Resume: {
auto *cui = dyn_cast_or_null<CleanupPhaseUnwindingInformation>(
state.unwindingInformation.get());
Expand Down Expand Up @@ -3230,6 +3239,7 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) {

break;
}
#endif // SUPPORT_KLEE_EH_CXX

case Instruction::AtomicRMW:
terminateStateOnExecError(state, "Unexpected Atomic instruction, should be "
Expand Down
2 changes: 2 additions & 0 deletions lib/Core/Executor.h
Expand Up @@ -110,8 +110,10 @@ class Executor : public Interpreter {
ReadOnly,
ReportError,
User,
#ifdef SUPPORT_KLEE_EH_CXX
UncaughtException,
UnexpectedException,
#endif
Unhandled,
};

Expand Down
8 changes: 7 additions & 1 deletion lib/Core/SpecialFunctionHandler.cpp
Expand Up @@ -18,6 +18,7 @@
#include "StatsTracker.h"
#include "TimingSolver.h"

#include "klee/Config/config.h"
#include "klee/Module/KInstruction.h"
#include "klee/Module/KModule.h"
#include "klee/Solver/SolverCmdLine.h"
Expand Down Expand Up @@ -115,7 +116,11 @@ static SpecialFunctionHandler::HandlerInfo handlerInfo[] = {
add("malloc", handleMalloc, true),
add("memalign", handleMemalign, true),
add("realloc", handleRealloc, true),

#ifdef SUPPORT_KLEE_EH_CXX
add("_klee_eh_Unwind_RaiseException_impl", handleEhUnwindRaiseExceptionImpl, false),
add("klee_eh_typeid_for", handleEhTypeid, true),
#endif

// operator delete[](void*)
add("_ZdaPv", handleDeleteArray, false),
Expand All @@ -140,7 +145,6 @@ static SpecialFunctionHandler::HandlerInfo handlerInfo[] = {
add("__ubsan_handle_sub_overflow", handleSubOverflow, false),
add("__ubsan_handle_mul_overflow", handleMulOverflow, false),
add("__ubsan_handle_divrem_overflow", handleDivRemOverflow, false),
add("klee_eh_typeid_for", handleEhTypeid, true),

#undef addDNR
#undef add
Expand Down Expand Up @@ -458,6 +462,7 @@ void SpecialFunctionHandler::handleMemalign(ExecutionState &state,
alignment);
}

#ifdef SUPPORT_KLEE_EH_CXX
void SpecialFunctionHandler::handleEhUnwindRaiseExceptionImpl(
ExecutionState &state, KInstruction *target,
std::vector<ref<Expr>> &arguments) {
Expand Down Expand Up @@ -495,6 +500,7 @@ void SpecialFunctionHandler::handleEhTypeid(ExecutionState &state,

executor.bindLocal(target, state, executor.getEhTypeidFor(arguments[0]));
}
#endif // SUPPORT_KLEE_EH_CXX

void SpecialFunctionHandler::handleAssume(ExecutionState &state,
KInstruction *target,
Expand Down
10 changes: 7 additions & 3 deletions lib/Core/SpecialFunctionHandler.h
Expand Up @@ -10,6 +10,8 @@
#ifndef KLEE_SPECIALFUNCTIONHANDLER_H
#define KLEE_SPECIALFUNCTIONHANDLER_H

#include "klee/Config/config.h"

#include <iterator>
#include <map>
#include <vector>
Expand Down Expand Up @@ -109,8 +111,12 @@ namespace klee {
HANDLER(handleDefineFixedObject);
HANDLER(handleDelete);
HANDLER(handleDeleteArray);
HANDLER(handleExit);
#ifdef SUPPORT_KLEE_EH_CXX
HANDLER(handleEhUnwindRaiseExceptionImpl);
HANDLER(handleEhTypeid);
#endif
HANDLER(handleErrnoLocation);
HANDLER(handleExit);
HANDLER(handleFree);
HANDLER(handleGetErrno);
HANDLER(handleGetObjSize);
Expand All @@ -119,8 +125,6 @@ namespace klee {
HANDLER(handleMakeSymbolic);
HANDLER(handleMalloc);
HANDLER(handleMemalign);
HANDLER(handleEhUnwindRaiseExceptionImpl);
HANDLER(handleEhTypeid);
HANDLER(handleMarkGlobal);
HANDLER(handleOpenMerge);
HANDLER(handleCloseMerge);
Expand Down
5 changes: 5 additions & 0 deletions lib/Module/IntrinsicCleaner.cpp
Expand Up @@ -363,6 +363,9 @@ bool IntrinsicCleanerPass::runOnBasicBlock(BasicBlock &b, Module &M) {
case Intrinsic::dbg_declare:
#if LLVM_VERSION_CODE >= LLVM_VERSION(7, 0)
case Intrinsic::dbg_label:
#endif
#ifndef SUPPORT_KLEE_EH_CXX
case Intrinsic::eh_typeid_for:
#endif
case Intrinsic::exp2:
case Intrinsic::exp:
Expand Down Expand Up @@ -399,10 +402,12 @@ bool IntrinsicCleanerPass::runOnBasicBlock(BasicBlock &b, Module &M) {
dirty = true;
break;

#ifdef SUPPORT_KLEE_EH_CXX
case Intrinsic::eh_typeid_for: {
// Don't lower this, we need it for exception handling
break;
}
#endif

// Warn about any unrecognized intrinsics.
default: {
Expand Down
2 changes: 2 additions & 0 deletions tools/klee/main.cpp
Expand Up @@ -782,8 +782,10 @@ static const char *modelledExternals[] = {
"klee_warning",
"klee_warning_once",
"klee_stack_trace",
#ifdef SUPPORT_KLEE_EH_CXX
"_klee_eh_Unwind_RaiseException_impl",
"klee_eh_typeid_for",
#endif
"llvm.dbg.declare",
"llvm.dbg.value",
"llvm.va_start",
Expand Down

0 comments on commit 5f08b02

Please sign in to comment.