-
Notifications
You must be signed in to change notification settings - Fork 243
/
verifier_main.cc
259 lines (228 loc) · 8.22 KB
/
verifier_main.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Copyright 2014 The Kythe Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <sys/resource.h>
#include <unistd.h>
#include <string>
#include "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include "absl/flags/usage.h"
#include "absl/log/log.h"
#include "absl/strings/str_format.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "assertion_ast.h"
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/io/zero_copy_stream.h"
#include "google/protobuf/io/zero_copy_stream_impl.h"
#include "kythe/cxx/common/init.h"
#include "kythe/proto/storage.pb.h"
#include "verifier.h"
ABSL_FLAG(bool, show_protos, false,
"Show protocol buffers read from standard in");
ABSL_FLAG(bool, show_goals, false, "Show goals after parsing");
ABSL_FLAG(bool, ignore_dups, false,
"Ignore duplicate facts during verification");
ABSL_FLAG(bool, ignore_code_conflicts, false,
"Ignore conflicting /kythe/code facts during verification");
ABSL_FLAG(bool, graphviz, false,
"Only dump facts as a GraphViz-compatible graph");
ABSL_FLAG(bool, annotated_graphviz, false,
"Solve and annotate a GraphViz graph.");
ABSL_FLAG(bool, minimal_graphviz, false,
"Solve and dump a GraphViz graph eliding unused nodes.");
ABSL_FLAG(std::string, goal_prefix, "//-", "Denote goals with this string.");
ABSL_FLAG(bool, use_file_nodes, false,
"Look for assertions in UTF8 file nodes.");
ABSL_FLAG(bool, check_for_singletons, true, "Fail on singleton variables.");
ABSL_FLAG(std::string, goal_regex, "",
"If nonempty, denote goals with this regex. "
"The regex must match the entire line. Expects one capture group.");
ABSL_FLAG(bool, convert_marked_source, false,
"Convert MarkedSource-valued facts to subgraphs.");
ABSL_FLAG(bool, show_anchors, false, "Show anchor locations instead of @s");
ABSL_FLAG(bool, show_vnames, true,
"Show VNames for nodes which also have labels.");
ABSL_FLAG(bool, show_fact_prefix, true,
"Include the /kythe or /kythe/edge prefix on facts and edges.");
ABSL_FLAG(bool, file_vnames, true,
"Find file vnames by matching file content.");
ABSL_FLAG(bool, use_fast_solver, false,
"Use the fast solver. EXPERIMENTAL; NOT ALL FEATURES ARE CURRENTLY "
"SUPPORTED.");
ABSL_FLAG(bool, print_timing_information, false,
"Print timing information for profiling.");
namespace {
// The fast solver needs extra stack space for modest programs.
constexpr rlim_t kSolverStack = 64L * 1024L * 1024L;
} // namespace
int main(int argc, char** argv) {
GOOGLE_PROTOBUF_VERIFY_VERSION;
kythe::InitializeProgram(argv[0]);
absl::SetProgramUsageMessage(R"(Verification tool for Kythe databases.
Reads Kythe facts from standard input and checks them against one or more rule
files. See https://kythe.io/docs/kythe-verifier.html for more details on
invocation and rule syntax.
Example:
${INDEXER_BIN} -i $1 | ${VERIFIER_BIN} --show_protos --show_goals $1
cat foo.entries | ${VERIFIER_BIN} goals1.cc goals2.cc
cat foo.entries | ${VERIFIER_BIN} --use_file_nodes
)");
auto start_time = absl::Now();
std::vector<char*> remain = absl::ParseCommandLine(argc, argv);
kythe::verifier::Verifier v;
if (absl::GetFlag(FLAGS_goal_regex).empty()) {
v.SetGoalCommentPrefix(absl::GetFlag(FLAGS_goal_prefix));
} else {
std::string error;
if (!v.SetGoalCommentRegex(absl::GetFlag(FLAGS_goal_regex), &error)) {
absl::FPrintF(stderr, "While parsing goal regex: %s\n", error);
return 1;
}
}
if (absl::GetFlag(FLAGS_ignore_dups)) {
v.IgnoreDuplicateFacts();
}
if (absl::GetFlag(FLAGS_ignore_code_conflicts)) {
v.IgnoreCodeConflicts();
}
if (absl::GetFlag(FLAGS_annotated_graphviz)) {
v.SaveEVarAssignments();
}
if (absl::GetFlag(FLAGS_minimal_graphviz)) {
v.SaveEVarAssignments();
v.ElideUnlabeled();
}
if (absl::GetFlag(FLAGS_use_file_nodes)) {
v.UseFileNodes();
}
if (absl::GetFlag(FLAGS_convert_marked_source)) {
v.ConvertMarkedSource();
}
if (absl::GetFlag(FLAGS_show_anchors)) {
v.ShowAnchors();
}
if (absl::GetFlag(FLAGS_show_vnames)) {
v.ShowLabeledVnames();
}
if (!absl::GetFlag(FLAGS_file_vnames)) {
v.IgnoreFileVnames();
}
if (absl::GetFlag(FLAGS_show_fact_prefix)) {
v.ShowFactPrefix();
}
v.UseFastSolver(absl::GetFlag(FLAGS_use_fast_solver));
if (absl::GetFlag(FLAGS_use_fast_solver)) {
struct rlimit rl;
int r = getrlimit(RLIMIT_STACK, &rl);
if (r != 0) {
perror("failed getting solver stack size");
return 1;
}
if (rl.rlim_cur < kSolverStack) {
rl.rlim_cur = kSolverStack;
r = setrlimit(RLIMIT_STACK, &rl);
if (r != 0) {
perror("failed increasing solver stack size");
return 1;
}
}
}
std::string dbname = "database";
size_t facts = 0;
auto start_read_time = absl::Now();
kythe::proto::Entry entry;
uint32_t byte_size;
google::protobuf::io::FileInputStream raw_input(STDIN_FILENO);
for (;;) {
google::protobuf::io::CodedInputStream coded_input(&raw_input);
coded_input.SetTotalBytesLimit(INT_MAX);
if (!coded_input.ReadVarint32(&byte_size)) {
break;
}
auto limit = coded_input.PushLimit(byte_size);
if (!entry.ParseFromCodedStream(&coded_input)) {
absl::FPrintF(stderr, "Error reading around fact %zu\n", facts);
return 1;
}
if (absl::GetFlag(FLAGS_show_protos)) {
entry.PrintDebugString();
putchar('\n');
}
if (!v.AssertSingleFact(&dbname, facts, entry)) {
absl::FPrintF(stderr, "Error asserting fact %zu\n", facts);
return 1;
}
++facts;
}
auto done_read_time = absl::Now();
if (!absl::GetFlag(FLAGS_use_fast_solver) && !v.PrepareDatabase()) {
return 1;
}
auto done_db_time = absl::Now();
if (!absl::GetFlag(FLAGS_graphviz)) {
std::vector<std::string> rule_files(remain.begin() + 1, remain.end());
if (rule_files.empty() && !absl::GetFlag(FLAGS_use_file_nodes)) {
absl::FPrintF(stderr, "No rule files specified\n");
return 1;
}
for (const auto& rule_file : rule_files) {
if (rule_file.empty()) {
continue;
}
if (!v.LoadInlineRuleFile(rule_file)) {
absl::FPrintF(stderr, "Failed loading %s.\n", rule_file);
return 2;
}
}
}
if (absl::GetFlag(FLAGS_check_for_singletons) && v.CheckForSingletonEVars()) {
return 1;
}
if (absl::GetFlag(FLAGS_show_goals)) {
v.ShowGoals();
}
int result = 0;
auto pre_solve_time = absl::Now();
if (!v.VerifyAllGoals()) {
// Flush stdout in case any data is stuck in buffers, to avoid mangling a
// joined output stream.
fflush(stdout);
absl::FPrintF(
stderr, "Could not verify all goals. The furthest we reached was:\n ");
v.DumpErrorGoal(v.highest_group_reached(), v.highest_goal_reached());
result = 1;
}
auto post_solve_time = absl::Now();
if (absl::GetFlag(FLAGS_graphviz) ||
absl::GetFlag(FLAGS_annotated_graphviz) ||
absl::GetFlag(FLAGS_minimal_graphviz)) {
v.DumpAsDot();
}
if (absl::GetFlag(FLAGS_print_timing_information)) {
auto print_timer = [](absl::Duration duration, absl::string_view name) {
absl::FPrintF(stderr, "%s took %ld msec\n", name,
absl::ToInt64Milliseconds(duration));
};
print_timer(start_read_time - start_time, "startup");
print_timer(done_read_time - start_read_time, "read input stream");
print_timer(done_db_time - done_read_time, "database preparation");
print_timer(pre_solve_time - done_db_time, "other prep");
print_timer(post_solve_time - pre_solve_time, "solve");
print_timer(post_solve_time - start_time, "total elapsed");
}
return result;
}