-
Notifications
You must be signed in to change notification settings - Fork 0
/
forbidden_funcs.cc
228 lines (194 loc) · 6.52 KB
/
forbidden_funcs.cc
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// forbidden_funcs - a GCC plugin checking for calls to forbidden functions
// Copyright (C) 2022 Christoph Erhardt
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// 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, see <https://www.gnu.org/licenses/>.
#include <cstdio>
#include <cstring>
#include <string>
#include <unordered_set>
#include <utility>
// clang-format off
#include <gcc-plugin.h>
#include <basic-block.h>
#include <tree.h>
#include <tree-ssa-alias.h>
#include <gimple-expr.h>
#include <gimple.h>
// clang-format on
#include <context.h>
#include <diagnostic-core.h>
#include <gimple-iterator.h>
#include <gimple-walk.h>
#include <plugin-version.h>
#include <tree-pass.h>
#define DLL_PUBLIC __attribute__((visibility("default")))
namespace {
plugin_info pluginInfo = {
.version = "1.0",
.help =
"Treats calls to forbidden functions as `-Wdeprecated`.\n"
"Pass a comma-separated list of (mangled) symbols as argument, e.g.\n"
" `-fplugin-arg-forbidden_funcs-list=func1,func2,func3`.",
};
void extractCommaSeparatedItems(std::unordered_set<std::string>& outSet,
const char inString[]) {
const char* start = inString;
const char* end;
for (end = start; *end != '\0'; ++end) {
if (*end == ',') {
if (end - start > 0) {
outSet.emplace(start, end);
}
start = end + 1;
}
}
if (end - start > 0) {
outSet.emplace(start, end);
}
}
std::unordered_set<std::string> parseArgs(const plugin_name_args* nameArgs) {
std::unordered_set<std::string> forbiddenFuncs;
for (int i = 0; i < nameArgs->argc; ++i) {
const auto& arg = nameArgs->argv[i];
if (std::strcmp(arg.key, "list") == 0 && arg.value != nullptr) {
extractCommaSeparatedItems(forbiddenFuncs, arg.value);
continue;
}
error(
"unrecognized command-line option %<-%s%s%> for plugin %s; "
"did you mean %<-list=%>?",
arg.key,
(arg.value != nullptr) ? "=" : "",
nameArgs->base_name);
}
return forbiddenFuncs;
}
constexpr pass_data passData = {
.type = GIMPLE_PASS,
.name = "forbidden_funcs",
.optinfo_flags = OPTGROUP_NONE,
.tv_id = TV_NONE,
.properties_required = PROP_gimple_any,
.properties_provided = 0,
.properties_destroyed = 0,
.todo_flags_start = 0,
.todo_flags_finish = 0,
};
class ForbiddenFunctionCheck final : public gimple_opt_pass {
private:
ForbiddenFunctionCheck(gcc::context* ctx,
std::unordered_set<std::string> forbiddenFuncs)
: gimple_opt_pass(passData, ctx),
m_forbiddenFuncs(std::move(forbiddenFuncs)) {}
unsigned execute(function* function) override {
basic_block bb;
FOR_ALL_BB_FN(bb, function) {
walk_stmt_info info{};
info.info = this;
walk_gimple_seq(
bb_seq(bb),
nullptr,
[](tree* op, int*, void* arg) {
const auto* info = static_cast<walk_stmt_info*>(arg);
const auto* pass = static_cast<ForbiddenFunctionCheck*>(info->info);
pass->check(gsi_stmt(info->gsi), *op);
return NULL_TREE;
},
&info);
}
return 0;
}
public:
static void createAndRegister(
const char* pluginName,
gcc::context* ctx,
const std::unordered_set<std::string>& forbiddenFuncs) {
// Create the pass
auto* pass = new ForbiddenFunctionCheck(ctx, forbiddenFuncs);
// Register it
register_pass_info passInfo = {
.pass = pass,
.reference_pass_name = "cfg",
.ref_pass_instance_number = 1,
.pos_op = PASS_POS_INSERT_AFTER,
};
register_callback(
pluginName, PLUGIN_PASS_MANAGER_SETUP, nullptr, &passInfo);
// Ensure proper destruction on shutdown
register_callback(
pluginName,
PLUGIN_FINISH,
[](void*, void* userData) {
delete static_cast<ForbiddenFunctionCheck*>(userData);
},
pass);
}
private:
// GCC 6 changed its interfaces to use `gimple*` instead of `gimple`.
#if GCCPLUGIN_VERSION_MAJOR >= 6
using Gimple = const gimple*;
#else
using Gimple = gimple;
#endif
void check(Gimple statement, tree op) const {
if (isFunctionDeclaration(op) && isForbidden(getSymbol(op))) {
showWarning(isCallTo(statement, op) ? "call to forbidden function %qD"
: "use of forbidden function %qD",
statement,
op);
}
}
static bool isFunctionDeclaration(tree op) {
return (DECL_P(op) && TREE_CODE(op) == FUNCTION_DECL);
}
static const char* getSymbol(tree decl) {
return IDENTIFIER_POINTER(DECL_ASSEMBLER_NAME(decl));
}
bool isForbidden(const std::string& symbol) const {
return (m_forbiddenFuncs.find(symbol) != m_forbiddenFuncs.end());
}
static bool isCallTo(Gimple statement, tree funcDecl) {
return (gimple_code(statement) == GIMPLE_CALL &&
gimple_call_fndecl(statement) == funcDecl);
}
static void showWarning(const char message[],
Gimple statement,
tree funcDecl) {
warning_at(gimple_location(statement), OPT_Wdeprecated, message, funcDecl);
}
private:
std::unordered_set<std::string> m_forbiddenFuncs;
};
} // namespace
int plugin_is_GPL_compatible DLL_PUBLIC;
int plugin_init(plugin_name_args* nameArgs,
plugin_gcc_version* version) DLL_PUBLIC;
int plugin_init(plugin_name_args* nameArgs, plugin_gcc_version* version) {
// Check version
if (!plugin_default_version_check(version, &gcc_version)) {
std::fprintf(stderr,
"Plugin %s was built for GCC version %d.%d\n",
nameArgs->base_name,
GCCPLUGIN_VERSION_MAJOR,
GCCPLUGIN_VERSION_MINOR);
return 1;
}
// Set plugin info
register_callback(nameArgs->base_name, PLUGIN_INFO, nullptr, &pluginInfo);
// Create and register analysis pass
const auto forbiddenFuncs = parseArgs(nameArgs);
ForbiddenFunctionCheck::createAndRegister(
nameArgs->base_name, g, forbiddenFuncs);
return 0;
}