Skip to content

Commit 6786842

Browse files
cblichmanncopybara-github
authored andcommitted
Add nullability annotations, clean up includes
PiperOrigin-RevId: 658766306 Change-Id: Ib99ac19c7433869259b56da668d3e4d46f3361b0
1 parent ad4bc32 commit 6786842

10 files changed

+136
-46
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ target_link_libraries(bindiff_shared
255255
absl::btree
256256
absl::flat_hash_map
257257
absl::hash
258+
absl::memory
259+
absl::nullability
258260
absl::str_format
259261
absl::strings
260262
absl::status

differ.cc

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,34 @@
1414

1515
#include "third_party/zynamics/bindiff/differ.h"
1616

17-
#include <exception>
17+
#include <algorithm>
18+
#include <cmath>
19+
#include <cstdint>
1820
#include <fstream>
19-
#include <iomanip>
21+
#include <ios>
2022
#include <memory>
23+
#include <string>
2124

25+
#include "third_party/absl/base/nullability.h"
26+
#include "third_party/absl/log/check.h"
2227
#include "third_party/absl/memory/memory.h"
28+
#include "third_party/absl/status/status.h"
2329
#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"
2433
#include "third_party/zynamics/bindiff/flow_graph.h"
34+
#include "third_party/zynamics/bindiff/instruction.h"
2535
#include "third_party/zynamics/bindiff/match/call_graph.h"
36+
#include "third_party/zynamics/bindiff/match/context.h"
2637
#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"
2740
#include "third_party/zynamics/binexport/binexport2.pb.h"
2841
#include "third_party/zynamics/binexport/util/filesystem.h"
2942
#include "third_party/zynamics/binexport/util/format.h"
3043
#include "third_party/zynamics/binexport/util/status_macros.h"
44+
#include "third_party/zynamics/binexport/util/types.h"
3145

3246
namespace security::bindiff {
3347

@@ -36,7 +50,7 @@ using ::security::binexport::FormatAddress;
3650
// Return the immediate children of the call graph node denoted by
3751
// address. Skip nodes that have already been matched.
3852
void GetUnmatchedChildren(const CallGraph& call_graph, CallGraph::Vertex vertex,
39-
FlowGraphs* children) {
53+
absl::Nonnull<FlowGraphs*> children) {
4054
for (auto [edge_it, edge_end] =
4155
boost::out_edges(vertex, call_graph.GetGraph());
4256
edge_it != edge_end; ++edge_it) {
@@ -59,7 +73,7 @@ void GetUnmatchedChildren(const CallGraph& call_graph, CallGraph::Vertex vertex,
5973
// Returns the immediate parents of the call graph node denoted by address.
6074
// Skips nodes that have already been matched.
6175
void GetUnmatchedParents(const CallGraph& call_graph, CallGraph::Vertex vertex,
62-
FlowGraphs* parents) {
76+
absl::Nonnull<FlowGraphs*> parents) {
6377
for (auto [edge_it, edge_end] =
6478
boost::in_edges(vertex, call_graph.GetGraph());
6579
edge_it != edge_end; ++edge_it) {
@@ -82,8 +96,8 @@ void GetUnmatchedParents(const CallGraph& call_graph, CallGraph::Vertex vertex,
8296
// Adds empty flow graphs to all call graph vertices that don't already have one
8397
// attached (for example for DLL stub functions). Returns an error if a flow
8498
// 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) {
87101
for (auto [it, end] = boost::vertices(call_graph->GetGraph()); it != end;
88102
++it) {
89103
const CallGraph::Vertex vertex = *it;
@@ -104,12 +118,12 @@ absl::Status AddSubsToCallGraph(CallGraph* call_graph,
104118
return absl::OkStatus();
105119
}
106120

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) {
113127
NA_RETURN_IF_ERROR(call_graph->Read(proto, filename));
114128
for (const auto& proto_flow_graph : proto.flow_graph()) {
115129
if (proto_flow_graph.basic_block_index_size() == 0) {
@@ -141,9 +155,11 @@ absl::Status SetupGraphsFromProto(const BinExport2& proto,
141155
return AddSubsToCallGraph(call_graph, flow_graphs);
142156
}
143157

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) {
147163
call_graph->Reset();
148164
DeleteFlowGraphs(flow_graphs);
149165
if (flow_graph_infos) {
@@ -167,7 +183,7 @@ absl::Status Read(const std::string& filename, CallGraph* call_graph,
167183
flow_graph_infos, instruction_cache);
168184
}
169185

170-
void DeleteFlowGraphs(FlowGraphs* flow_graphs) {
186+
void DeleteFlowGraphs(absl::Nullable<FlowGraphs*> flow_graphs) {
171187
if (!flow_graphs) {
172188
return;
173189
}
@@ -178,8 +194,10 @@ void DeleteFlowGraphs(FlowGraphs* flow_graphs) {
178194
flow_graphs->clear();
179195
}
180196

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)
183201
: flow_graphs1_(flow_graphs1),
184202
flow_graphs2_(flow_graphs2),
185203
instruction_cache_(instruction_cache) {}
@@ -192,13 +210,14 @@ ScopedCleanup::~ScopedCleanup() {
192210
}
193211
}
194212

195-
void ResetMatches(FlowGraphs* flow_graphs) {
213+
void ResetMatches(absl::Nonnull<FlowGraphs*> flow_graphs) {
196214
for (auto* flow_graph : *flow_graphs) {
197215
flow_graph->ResetMatches();
198216
}
199217
}
200218

201-
void Diff(MatchingContext* context, const MatchingSteps& call_graph_steps,
219+
void Diff(absl::Nonnull<MatchingContext*> context,
220+
const MatchingSteps& call_graph_steps,
202221
const MatchingStepsFlowGraph& basic_block_steps) {
203222
// The outer loop controls the rigorousness for initial matching while the
204223
// inner loop tries to resolve ambiguities by drilling down the matchingSteps
@@ -270,13 +289,13 @@ void Diff(MatchingContext* context, const MatchingSteps& call_graph_steps,
270289
ClassifyChanges(context);
271290
}
272291

273-
void Count(const FlowGraph& flow_graph, Counts* counts) {
292+
void Count(const FlowGraph& flow_graph, absl::Nonnull<Counts*> counts) {
274293
FlowGraphs flow_graphs;
275294
CHECK(flow_graphs.insert(&const_cast<FlowGraph&>(flow_graph)).second);
276295
Count(flow_graphs, counts);
277296
}
278297

279-
void Count(const FlowGraphs& flow_graphs, Counts* counts) {
298+
void Count(const FlowGraphs& flow_graphs, absl::Nonnull<Counts*> counts) {
280299
uint64_t num_functions = 0;
281300
uint64_t num_basic_blocks = 0;
282301
uint64_t num_instructions = 0;
@@ -394,7 +413,8 @@ double GetConfidence(const Histogram& histogram, Confidences* confidences) {
394413
void GetCountsAndHistogram(const FlowGraphs& flow_graphs1,
395414
const FlowGraphs& flow_graphs2,
396415
const FixedPoints& fixed_points,
397-
Histogram* histogram, Counts* counts) {
416+
absl::Nonnull<Histogram*> histogram,
417+
absl::Nonnull<Counts*> counts) {
398418
Counts counts1;
399419
Counts counts2;
400420
Count(flow_graphs1, &counts1);

differ.h

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
#ifndef DIFFER_H_
1616
#define DIFFER_H_
1717

18-
#include <array>
18+
#include <cstddef>
1919
#include <string>
2020

21+
#include "third_party/absl/base/nullability.h"
2122
#include "third_party/absl/container/btree_map.h"
2223
#include "third_party/absl/status/status.h"
2324
#include "third_party/zynamics/bindiff/call_graph.h"
2425
#include "third_party/zynamics/bindiff/fixed_points.h"
2526
#include "third_party/zynamics/bindiff/flow_graph.h"
27+
#include "third_party/zynamics/bindiff/instruction.h"
2628
#include "third_party/zynamics/bindiff/match/context.h"
2729
#include "third_party/zynamics/bindiff/reader.h"
2830
#include "third_party/zynamics/bindiff/statistics.h"
@@ -37,13 +39,15 @@ using Confidences = absl::btree_map<std::string, double>;
3739

3840
// Main entry point to the differ, runs the core algorithm and produces a
3941
// (partial) matching between the two inputs.
40-
void Diff(MatchingContext* context, const MatchingSteps& call_graph_steps,
42+
void Diff(absl::Nonnull<MatchingContext*> context,
43+
const MatchingSteps& call_graph_steps,
4144
const MatchingStepsFlowGraph& basic_block_steps);
4245

4346
class ScopedCleanup {
4447
public:
45-
ScopedCleanup(FlowGraphs* flow_graphs1, FlowGraphs* flow_graphs2,
46-
Instruction::Cache* instruction_cache);
48+
ScopedCleanup(absl::Nonnull<FlowGraphs*> flow_graphs1,
49+
absl::Nonnull<FlowGraphs*> flow_graphs2,
50+
absl::Nullable<Instruction::Cache*> instruction_cache);
4751
~ScopedCleanup();
4852

4953
private:
@@ -52,16 +56,18 @@ class ScopedCleanup {
5256
Instruction::Cache* instruction_cache_;
5357
};
5458

55-
void DeleteFlowGraphs(FlowGraphs* flow_graphs);
59+
void DeleteFlowGraphs(absl::Nullable<FlowGraphs*> flow_graphs);
5660

5761
// Removes all fixed point assignments from flow graphs so they can be used
5862
// again for a different comparison.
59-
void ResetMatches(FlowGraphs* flow_graphs);
63+
void ResetMatches(absl::Nonnull<FlowGraphs*> flow_graphs);
6064

6165
// Loads a .BinExport file into the internal data structures.
62-
absl::Status Read(const std::string& filename, CallGraph* call_graph,
63-
FlowGraphs* flow_graphs, FlowGraphInfos* flow_graph_infos,
64-
Instruction::Cache* instruction_cache);
66+
absl::Status Read(const std::string& filename,
67+
absl::Nonnull<CallGraph*> call_graph,
68+
absl::Nonnull<FlowGraphs*> flow_graphs,
69+
absl::Nullable<FlowGraphInfos*> flow_graph_infos,
70+
absl::Nonnull<Instruction::Cache*> instruction_cache);
6571

6672
// Gets the similarity score for two full binaries.
6773
double GetSimilarityScore(const CallGraph& call_graph1,
@@ -80,12 +86,14 @@ double GetConfidence(const Histogram& histogram, Confidences* confidences);
8086
void GetCountsAndHistogram(const FlowGraphs& flow_graphs1,
8187
const FlowGraphs& flow_graphs2,
8288
const FixedPoints& fixed_points,
83-
Histogram* histogram, Counts* counts);
89+
absl::Nonnull<Histogram*> histogram,
90+
absl::Nonnull<Counts*> counts);
8491

8592
// Collects various statistics (no of instructions/edges/basic blocks).
86-
void Count(const FlowGraphs& flow_graphs, Counts* counts);
87-
void Count(const FlowGraph& flow_graph, Counts* counts);
88-
void Count(const FixedPoint& fixed_point, Counts* counts, Histogram* histogram);
93+
void Count(const FlowGraphs& flow_graphs, absl::Nonnull<Counts*> counts);
94+
void Count(const FlowGraph& flow_graph, absl::Nonnull<Counts*> counts);
95+
void Count(const FixedPoint& fixed_point, absl::Nonnull<Counts*> counts,
96+
absl::Nonnull<Histogram*> histogram);
8997

9098
} // namespace security::bindiff
9199

flow_graph.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,30 @@
2020
#include <boost/graph/filtered_graph.hpp> // NOLINT
2121
#include <boost/graph/iteration_macros.hpp> // NOLINT
2222
#include <cassert>
23+
#include <cstddef>
2324
#include <cstdint>
24-
#include <sstream>
25+
#include <iterator>
26+
#include <limits>
27+
#include <numeric>
28+
#include <string>
29+
#include <utility>
30+
#include <vector>
2531

2632
#include "third_party/absl/log/check.h"
2733
#include "third_party/absl/log/log.h"
34+
#include "third_party/absl/status/status.h"
2835
#include "third_party/absl/strings/str_cat.h"
2936
#include "third_party/zynamics/bindiff/call_graph.h"
3037
#include "third_party/zynamics/bindiff/comment.h"
3138
#include "third_party/zynamics/bindiff/fixed_points.h"
3239
#include "third_party/zynamics/bindiff/graph_util.h"
40+
#include "third_party/zynamics/bindiff/instruction.h"
3341
#include "third_party/zynamics/bindiff/prime_signature.h"
3442
#include "third_party/zynamics/binexport/binexport.h"
3543
#include "third_party/zynamics/binexport/binexport2.pb.h"
3644
#include "third_party/zynamics/binexport/util/format.h"
3745
#include "third_party/zynamics/binexport/util/hash.h"
46+
#include "third_party/zynamics/binexport/util/types.h"
3847

3948
namespace security::bindiff {
4049

ida/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ ida_target_link_libraries(${bindiff_ida_plugin_name}
4545
bindiff_shared
4646
absl::cleanup
4747
absl::function_ref
48+
absl::memory
49+
absl::nullability
4850
absl::str_format
4951
absl::time
5052
)

ida/main_plugin.cc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@
1414

1515
#include "third_party/zynamics/bindiff/ida/main_plugin.h"
1616

17+
#include <cstdarg>
18+
#include <cstdint>
1719
#include <cstdio>
20+
#include <cstring>
1821
#include <exception>
19-
#include <fstream>
2022
#include <limits>
2123
#include <memory>
2224
#include <stdexcept>
2325
#include <string>
2426
#include <thread> // NOLINT(build/c++11)
27+
#include <utility>
28+
#include <vector>
2529

2630
// clang-format off
2731
#include "third_party/zynamics/binexport/ida/begin_idasdk.inc" // NOLINT
@@ -43,22 +47,21 @@
4347
#include "third_party/zynamics/binexport/ida/end_idasdk.inc" // NOLINT
4448
// clang-format on
4549

46-
#include "third_party/absl/base/macros.h"
4750
#include "third_party/absl/log/log.h"
51+
#include "third_party/absl/memory/memory.h"
4852
#include "third_party/absl/status/status.h"
4953
#include "third_party/absl/status/statusor.h"
5054
#include "third_party/absl/strings/ascii.h"
51-
#include "third_party/absl/strings/escaping.h"
5255
#include "third_party/absl/strings/match.h"
5356
#include "third_party/absl/strings/numbers.h"
5457
#include "third_party/absl/strings/str_cat.h"
5558
#include "third_party/absl/strings/string_view.h"
56-
#include "third_party/absl/time/time.h"
5759
#include "third_party/absl/types/span.h"
5860
#include "third_party/zynamics/bindiff/change_classifier.h"
5961
#include "third_party/zynamics/bindiff/config.h"
6062
#include "third_party/zynamics/bindiff/database_writer.h"
6163
#include "third_party/zynamics/bindiff/differ.h"
64+
#include "third_party/zynamics/bindiff/flow_graph.h"
6265
#include "third_party/zynamics/bindiff/groundtruth_writer.h"
6366
#include "third_party/zynamics/bindiff/ida/bindiff_icon.h"
6467
#include "third_party/zynamics/bindiff/ida/matched_functions_chooser.h"
@@ -70,6 +73,8 @@
7073
#include "third_party/zynamics/bindiff/match/call_graph.h"
7174
#include "third_party/zynamics/bindiff/match/context.h"
7275
#include "third_party/zynamics/bindiff/match/flow_graph.h"
76+
#include "third_party/zynamics/bindiff/reader.h"
77+
#include "third_party/zynamics/bindiff/sqlite.h"
7378
#include "third_party/zynamics/bindiff/version.h"
7479
#include "third_party/zynamics/binexport/ida/digest.h"
7580
#include "third_party/zynamics/binexport/ida/log_sink.h"

ida/matched_functions_chooser.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@
1515
#include "third_party/zynamics/bindiff/ida/matched_functions_chooser.h"
1616

1717
#include <cstring>
18-
#include <vector>
18+
#include <string>
1919

20+
// clang-format off
21+
#include "third_party/zynamics/binexport/ida/begin_idasdk.inc" // NOLINT
22+
#include <kernwin.hpp> // NOLINT
23+
#include "third_party/zynamics/binexport/ida/end_idasdk.inc" // NOLINT
24+
// clang-format on
25+
26+
#include "third_party/absl/base/macros.h"
2027
#include "third_party/absl/log/log.h"
2128
#include "third_party/absl/status/status.h"
2229
#include "third_party/absl/strings/str_cat.h"
2330
#include "third_party/absl/strings/str_format.h"
31+
#include "third_party/absl/types/span.h"
32+
#include "third_party/zynamics/bindiff/change_classifier.h"
2433
#include "third_party/zynamics/bindiff/ida/main_plugin.h"
2534
#include "third_party/zynamics/bindiff/ida/results.h"
2635
#include "third_party/zynamics/bindiff/ida/statistics_chooser.h"

0 commit comments

Comments
 (0)