diff options
author | Rafael Zaehl <rafael.zaehl@rwth-aachen.de> | 2018-09-29 17:24:06 +0200 |
---|---|---|
committer | MartinNowack <martin.nowack@gmail.com> | 2018-10-24 10:08:19 +0100 |
commit | 7120c775037c911848fa634ae6398baf577d5650 (patch) | |
tree | 5f646355aa5c9801235e62b4452138f9b91985f7 | |
parent | 2b34877c5dbf24eabf331a124b1e68d901a72cba (diff) | |
download | klee-7120c775037c911848fa634ae6398baf577d5650.tar.gz |
Added lowering pass
-rw-r--r-- | lib/Core/Executor.cpp | 9 | ||||
-rw-r--r-- | lib/Module/KModule.cpp | 3 | ||||
-rw-r--r-- | test/Feature/Atomic.c | 72 |
3 files changed, 84 insertions, 0 deletions
diff --git a/lib/Core/Executor.cpp b/lib/Core/Executor.cpp index 0506b685..7c4ee23a 100644 --- a/lib/Core/Executor.cpp +++ b/lib/Core/Executor.cpp @@ -2566,6 +2566,15 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) { // instructions. terminateStateOnExecError(state, "Unexpected ShuffleVector instruction"); break; + case Instruction::AtomicRMW: + terminateStateOnExecError(state, "Unexpected Atomic instruction, should be " + "lowered by LowerAtomicInstructionPass"); + break; + case Instruction::AtomicCmpXchg: + terminateStateOnExecError(state, + "Unexpected AtomicCmpXchg instruction, should be " + "lowered by LowerAtomicInstructionPass"); + break; // Other instructions... // Unhandled default: diff --git a/lib/Module/KModule.cpp b/lib/Module/KModule.cpp index 7b02c856..b025c888 100644 --- a/lib/Module/KModule.cpp +++ b/lib/Module/KModule.cpp @@ -224,6 +224,9 @@ void KModule::instrument(const Interpreter::ModuleOptions &opts) { // NOTE: Must come before division/overshift checks because those passes // don't know how to handle vector instructions. pm.add(createScalarizerPass()); + + // This pass will replace atomic instructions with non-atomic operations + pm.add(createLowerAtomicPass()); if (opts.CheckDivZero) pm.add(new DivCheckPass()); if (opts.CheckOvershift) pm.add(new OvershiftCheckPass()); diff --git a/test/Feature/Atomic.c b/test/Feature/Atomic.c new file mode 100644 index 00000000..8b729de7 --- /dev/null +++ b/test/Feature/Atomic.c @@ -0,0 +1,72 @@ +// REQUIRES: geq-llvm-3.7 +// RUN: %llvmgcc %s -emit-llvm -g -c -o %t.bc +// RUN: rm -rf %t.klee-out +// RUN: %klee --output-dir=%t.klee-out --exit-on-error %t.bc 2>%t.log +// RUN: cat %t.klee-out/assembly.ll | FileCheck %s + +// Checks that KLEE lowers atomic instructions to non-atomic operations + +#include <assert.h> +#include <stdatomic.h> + +void test_add() { + atomic_int a = 7; + atomic_fetch_add(&a, 7); + // CHECK-NOT: atomicrmw add + assert(a == 14); +} + +void test_sub() { + atomic_int a = 7; + atomic_fetch_sub(&a, 7); + // CHECK-NOT: atomicrmw sub + assert(a == 0); +} + +void test_and() { + atomic_int a = 0b10001; + atomic_fetch_and(&a, 0b00101); + // CHECK-NOT: atomicrmw and + assert(a == 0b00001); +} + +void test_or() { + atomic_int a = 0b10001; + atomic_fetch_or(&a, 0b00101); + // CHECK-NOT: atomicrmw or + assert(a == 0b10101); +} + +void test_xor() { + atomic_int a = 0b10001; + atomic_fetch_xor(&a, 0b00101); + // CHECK-NOT: atomicrmw xor + assert(a == 0b10100); +} + +void test_xchg() { + atomic_int a = 7; + atomic_exchange(&a, 10); + // CHECK-NOT: atomicrmw xchg + assert(a == 10); +} + +void test_cmp_xchg() { + atomic_int a = 7; + int b = 7; + atomic_compare_exchange_weak(&a, &b, 10); + // CHECK-NOT: cmpxchg weak + assert(a == 10); + assert(b == 7); +} + +int main() { + test_and(); + test_sub(); + test_and(); + test_or(); + test_xor(); + test_xchg(); + test_cmp_xchg(); + return 0; +} |