14
14
15
15
#include " third_party/zynamics/bindiff/differ.h"
16
16
17
- #include < exception>
17
+ #include < algorithm>
18
+ #include < cmath>
19
+ #include < cstdint>
18
20
#include < fstream>
19
- #include < iomanip >
21
+ #include < ios >
20
22
#include < memory>
23
+ #include < string>
21
24
25
+ #include " third_party/absl/base/nullability.h"
26
+ #include " third_party/absl/log/check.h"
22
27
#include " third_party/absl/memory/memory.h"
28
+ #include " third_party/absl/status/status.h"
23
29
#include " third_party/absl/strings/str_cat.h"
30
+ #include " third_party/zynamics/bindiff/call_graph.h"
31
+ #include " third_party/zynamics/bindiff/change_classifier.h"
32
+ #include " third_party/zynamics/bindiff/fixed_points.h"
24
33
#include " third_party/zynamics/bindiff/flow_graph.h"
34
+ #include " third_party/zynamics/bindiff/instruction.h"
25
35
#include " third_party/zynamics/bindiff/match/call_graph.h"
36
+ #include " third_party/zynamics/bindiff/match/context.h"
26
37
#include " third_party/zynamics/bindiff/match/flow_graph.h"
38
+ #include " third_party/zynamics/bindiff/reader.h"
39
+ #include " third_party/zynamics/bindiff/statistics.h"
27
40
#include " third_party/zynamics/binexport/binexport2.pb.h"
28
41
#include " third_party/zynamics/binexport/util/filesystem.h"
29
42
#include " third_party/zynamics/binexport/util/format.h"
30
43
#include " third_party/zynamics/binexport/util/status_macros.h"
44
+ #include " third_party/zynamics/binexport/util/types.h"
31
45
32
46
namespace security ::bindiff {
33
47
@@ -36,7 +50,7 @@ using ::security::binexport::FormatAddress;
36
50
// Return the immediate children of the call graph node denoted by
37
51
// address. Skip nodes that have already been matched.
38
52
void GetUnmatchedChildren (const CallGraph& call_graph, CallGraph::Vertex vertex,
39
- FlowGraphs* children) {
53
+ absl::Nonnull< FlowGraphs*> children) {
40
54
for (auto [edge_it, edge_end] =
41
55
boost::out_edges (vertex, call_graph.GetGraph ());
42
56
edge_it != edge_end; ++edge_it) {
@@ -59,7 +73,7 @@ void GetUnmatchedChildren(const CallGraph& call_graph, CallGraph::Vertex vertex,
59
73
// Returns the immediate parents of the call graph node denoted by address.
60
74
// Skips nodes that have already been matched.
61
75
void GetUnmatchedParents (const CallGraph& call_graph, CallGraph::Vertex vertex,
62
- FlowGraphs* parents) {
76
+ absl::Nonnull< FlowGraphs*> parents) {
63
77
for (auto [edge_it, edge_end] =
64
78
boost::in_edges (vertex, call_graph.GetGraph ());
65
79
edge_it != edge_end; ++edge_it) {
@@ -82,8 +96,8 @@ void GetUnmatchedParents(const CallGraph& call_graph, CallGraph::Vertex vertex,
82
96
// Adds empty flow graphs to all call graph vertices that don't already have one
83
97
// attached (for example for DLL stub functions). Returns an error if a flow
84
98
// graph already exists for a call graph vertex.
85
- absl::Status AddSubsToCallGraph (CallGraph* call_graph,
86
- FlowGraphs* flow_graphs) {
99
+ absl::Status AddSubsToCallGraph (absl::Nonnull< CallGraph*> call_graph,
100
+ absl::Nonnull< FlowGraphs*> flow_graphs) {
87
101
for (auto [it, end] = boost::vertices (call_graph->GetGraph ()); it != end;
88
102
++it) {
89
103
const CallGraph::Vertex vertex = *it;
@@ -104,12 +118,12 @@ absl::Status AddSubsToCallGraph(CallGraph* call_graph,
104
118
return absl::OkStatus ();
105
119
}
106
120
107
- absl::Status SetupGraphsFromProto (const BinExport2& proto,
108
- const std::string& filename,
109
- CallGraph* call_graph,
110
- FlowGraphs* flow_graphs,
111
- FlowGraphInfos* flow_graph_infos,
112
- Instruction::Cache* instruction_cache) {
121
+ absl::Status SetupGraphsFromProto (
122
+ const BinExport2& proto, const std::string& filename,
123
+ absl::Nonnull< CallGraph*> call_graph,
124
+ absl::Nonnull< FlowGraphs*> flow_graphs,
125
+ absl::Nullable< FlowGraphInfos*> flow_graph_infos,
126
+ absl::Nonnull< Instruction::Cache*> instruction_cache) {
113
127
NA_RETURN_IF_ERROR (call_graph->Read (proto, filename));
114
128
for (const auto & proto_flow_graph : proto.flow_graph ()) {
115
129
if (proto_flow_graph.basic_block_index_size () == 0 ) {
@@ -141,9 +155,11 @@ absl::Status SetupGraphsFromProto(const BinExport2& proto,
141
155
return AddSubsToCallGraph (call_graph, flow_graphs);
142
156
}
143
157
144
- absl::Status Read (const std::string& filename, CallGraph* call_graph,
145
- FlowGraphs* flow_graphs, FlowGraphInfos* flow_graph_infos,
146
- Instruction::Cache* instruction_cache) {
158
+ absl::Status Read (const std::string& filename,
159
+ absl::Nonnull<CallGraph*> call_graph,
160
+ absl::Nonnull<FlowGraphs*> flow_graphs,
161
+ absl::Nullable<FlowGraphInfos*> flow_graph_infos,
162
+ absl::Nonnull<Instruction::Cache*> instruction_cache) {
147
163
call_graph->Reset ();
148
164
DeleteFlowGraphs (flow_graphs);
149
165
if (flow_graph_infos) {
@@ -167,7 +183,7 @@ absl::Status Read(const std::string& filename, CallGraph* call_graph,
167
183
flow_graph_infos, instruction_cache);
168
184
}
169
185
170
- void DeleteFlowGraphs (FlowGraphs* flow_graphs) {
186
+ void DeleteFlowGraphs (absl::Nullable< FlowGraphs*> flow_graphs) {
171
187
if (!flow_graphs) {
172
188
return ;
173
189
}
@@ -178,8 +194,10 @@ void DeleteFlowGraphs(FlowGraphs* flow_graphs) {
178
194
flow_graphs->clear ();
179
195
}
180
196
181
- ScopedCleanup::ScopedCleanup (FlowGraphs* flow_graphs1, FlowGraphs* flow_graphs2,
182
- Instruction::Cache* instruction_cache)
197
+ ScopedCleanup::ScopedCleanup (
198
+ absl::Nonnull<FlowGraphs*> flow_graphs1,
199
+ absl::Nonnull<FlowGraphs*> flow_graphs2,
200
+ absl::Nullable<Instruction::Cache*> instruction_cache)
183
201
: flow_graphs1_(flow_graphs1),
184
202
flow_graphs2_ (flow_graphs2),
185
203
instruction_cache_(instruction_cache) {}
@@ -192,13 +210,14 @@ ScopedCleanup::~ScopedCleanup() {
192
210
}
193
211
}
194
212
195
- void ResetMatches (FlowGraphs* flow_graphs) {
213
+ void ResetMatches (absl::Nonnull< FlowGraphs*> flow_graphs) {
196
214
for (auto * flow_graph : *flow_graphs) {
197
215
flow_graph->ResetMatches ();
198
216
}
199
217
}
200
218
201
- void Diff (MatchingContext* context, const MatchingSteps& call_graph_steps,
219
+ void Diff (absl::Nonnull<MatchingContext*> context,
220
+ const MatchingSteps& call_graph_steps,
202
221
const MatchingStepsFlowGraph& basic_block_steps) {
203
222
// The outer loop controls the rigorousness for initial matching while the
204
223
// inner loop tries to resolve ambiguities by drilling down the matchingSteps
@@ -270,13 +289,13 @@ void Diff(MatchingContext* context, const MatchingSteps& call_graph_steps,
270
289
ClassifyChanges (context);
271
290
}
272
291
273
- void Count (const FlowGraph& flow_graph, Counts* counts) {
292
+ void Count (const FlowGraph& flow_graph, absl::Nonnull< Counts*> counts) {
274
293
FlowGraphs flow_graphs;
275
294
CHECK (flow_graphs.insert (&const_cast <FlowGraph&>(flow_graph)).second );
276
295
Count (flow_graphs, counts);
277
296
}
278
297
279
- void Count (const FlowGraphs& flow_graphs, Counts* counts) {
298
+ void Count (const FlowGraphs& flow_graphs, absl::Nonnull< Counts*> counts) {
280
299
uint64_t num_functions = 0 ;
281
300
uint64_t num_basic_blocks = 0 ;
282
301
uint64_t num_instructions = 0 ;
@@ -394,7 +413,8 @@ double GetConfidence(const Histogram& histogram, Confidences* confidences) {
394
413
void GetCountsAndHistogram (const FlowGraphs& flow_graphs1,
395
414
const FlowGraphs& flow_graphs2,
396
415
const FixedPoints& fixed_points,
397
- Histogram* histogram, Counts* counts) {
416
+ absl::Nonnull<Histogram*> histogram,
417
+ absl::Nonnull<Counts*> counts) {
398
418
Counts counts1;
399
419
Counts counts2;
400
420
Count (flow_graphs1, &counts1);
0 commit comments