-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathClamBCRemoveFreezeInsts.cpp
119 lines (102 loc) · 3.61 KB
/
ClamBCRemoveFreezeInsts.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Compile LLVM bytecode to ClamAV bytecode.
*
* Copyright (C) 2020-2023 Sourcefire, Inc.
*
* Authors: Andy Ragusa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "clambc.h"
#include "ClamBCUtilities.h"
#include <llvm/IR/Module.h>
#include <llvm/IR/Instructions.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Passes/PassBuilder.h>
#include <llvm/Passes/PassPlugin.h>
#include <vector>
using namespace llvm;
using namespace std;
namespace
{
/*
* Freeze Instructions are to guarantee sane behaviour in the case of undefs or poison values. The interpreter
* has no notion of freeze instructions, so we are removing them. The verifier will fail if there are undef or
* poison values in the IR, so this is safe to do.
*/
struct ClamBCRemoveFreezeInsts : public PassInfoMixin<ClamBCRemoveFreezeInsts> {
protected:
Module *pMod = nullptr;
bool bChanged = false;
virtual void gatherFreezeInsts(Function *pFunc, std::vector<FreezeInst *> &freezeInsts)
{
for (auto fi = pFunc->begin(), fe = pFunc->end(); fi != fe; fi++) {
BasicBlock *pBB = llvm::cast<BasicBlock>(fi);
for (auto bi = pBB->begin(), be = pBB->end(); bi != be; bi++) {
if (FreezeInst *pfi = llvm::dyn_cast<FreezeInst>(bi)) {
freezeInsts.push_back(pfi);
}
}
}
}
virtual void processFunction(Function *pFunc)
{
vector<FreezeInst *> freezeInsts;
gatherFreezeInsts(pFunc, freezeInsts);
for (size_t i = 0; i < freezeInsts.size(); i++) {
bChanged = true;
FreezeInst *pfi = freezeInsts[i];
pfi->replaceAllUsesWith(pfi->getOperand(0));
pfi->eraseFromParent();
}
}
public:
virtual ~ClamBCRemoveFreezeInsts() {}
PreservedAnalyses run(Module &m, ModuleAnalysisManager &MAM)
{
pMod = &m;
for (auto i = pMod->begin(), e = pMod->end(); i != e; i++) {
Function *pFunc = llvm::cast<Function>(i);
if (pFunc->isDeclaration()) {
continue;
}
processFunction(pFunc);
}
if (bChanged) {
return PreservedAnalyses::none();
} else {
return PreservedAnalyses::all();
}
}
}; // end of struct ClamBCRemoveFreezeInsts
} // end of anonymous namespace
// This part is the new way of registering your pass
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
llvmGetPassPluginInfo()
{
return {
LLVM_PLUGIN_API_VERSION, "ClamBCRemoveFreezeInsts", "v0.1",
[](PassBuilder &PB) {
PB.registerPipelineParsingCallback(
[](StringRef Name, ModulePassManager &FPM,
ArrayRef<PassBuilder::PipelineElement>) {
if (Name == "clambc-remove-freeze-insts") {
FPM.addPass(ClamBCRemoveFreezeInsts());
return true;
}
return false;
});
}};
}