klee
WorkaroundLLVMPR39177.cpp
Go to the documentation of this file.
1//===-- WorkaroundLLVMPR39177.cpp -------------------------------*- C++ -*-===//
2//
3// The KLEE Symbolic Virtual Machine
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// This pass provides a workaround for LLVM bug PR39177 within the KLEE repo.
11// For more information on this, please refer to the comments in
12// cmake/workaround_llvm_pr39177.cmake
13
14#include "Passes.h"
16
17#include "llvm/Transforms/Utils/Cloning.h"
18
19using namespace llvm;
20
21namespace klee {
22
23bool WorkaroundLLVMPR39177Pass::runOnModule(Module &M) {
24 bool modified = false;
25
26 const char *libfunctions[] = {
27 "strlen",
28 "strchr",
29 "strncmp",
30 "strcpy",
31 "strncpy",
32 "__memcpy_chk",
33 "memchr",
34 "memcmp",
35 "putchar",
36 "puts",
37 "fputc",
38 "fputc_unlocked",
39 "fputs",
40 "fputs_unlocked",
41 "fwrite",
42 "malloc",
43 "calloc",
44 "fwrite_unlocked",
45 "fgetc_unlocked",
46 "fgets_unlocked",
47 "fread_unlocked",
48 "memset_pattern16",
49 "fopen"
50 };
51
52 for (auto *funcname : libfunctions) {
53 if (M.getFunction(funcname) != nullptr)
54 continue;
55
56 GlobalValue *gv = M.getNamedValue(funcname);
57 auto *alias = dyn_cast_or_null<GlobalAlias>(gv);
58 if (alias == nullptr)
59 continue;
60
61 // get aliasee function if exists
62 while (auto *ga = dyn_cast<GlobalAlias>(alias->getAliasee())) {
63 assert(ga != alias && "alias pointing to itself");
64 alias = ga;
65 }
66 Function *f = dyn_cast<Function>(alias->getAliasee());
67 if (f == nullptr)
68 continue;
69
70 std::string aliasName = alias->getName().str();
71
72 // clone function
73 ValueToValueMapTy VMap;
74 Function *g = CloneFunction(f, VMap);
75
76 // replace alias with cloned function
77 alias->replaceAllUsesWith(g);
78 g->takeName(alias);
79 alias->eraseFromParent();
80
82 "WorkaroundLLVMPR39177: replaced alias @%s with clone of function @%s",
83 aliasName.c_str(), f->getName().str().c_str());
84 modified = true;
85 }
86
87 return modified;
88}
89
90char WorkaroundLLVMPR39177Pass::ID = 0;
91
92} // namespace klee
Definition: main.cpp:291
void klee_message(const char *msg,...) __attribute__((format(printf