forked from google/binexport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binexport2_writer.cc
822 lines (764 loc) · 33 KB
/
binexport2_writer.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
// Copyright 2011-2017 Google Inc. 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.
// New BinExport protocol buffer based file format. Should be more complete and
// more compact than the original one.
//
// Example rates for item
// 000026157fb0ada54135ef1f182585fc3edbca4769b9ea3629d6cda9161dc566:
// 1.2M Nov 15 16:37 000026.BinExport
// 199K Nov 15 16:37 000026.BinExport.gz
// 500K Nov 15 16:37 000026.BinExport2
// 107K Nov 15 16:37 000026.BinExport2.gz
//
// So the new format is smaller than the original by about a factor ~2, despite
// the fact that the original completely omitted operands! Both formats compress
// equally well by another factor of ~5.
#include "third_party/zynamics/binexport/binexport2_writer.h"
#include <cinttypes>
#include <string>
#include <fstream>
#include <unordered_map>
#include "base/integral_types.h"
#include "base/logging.h"
#include "third_party/absl/strings/str_cat.h"
#include "third_party/absl/strings/string_view.h"
#include "third_party/absl/time/clock.h"
#include "third_party/absl/time/time.h"
#define final // Hack: Allow to override generated protobuf code.
#include <google/protobuf/io/coded_stream.h> // NOLINT
#include "third_party/zynamics/binexport/binexport2.pb.h"
#undef final // Undo hack
#include "third_party/zynamics/binexport/call_graph.h"
#include "third_party/zynamics/binexport/flow_graph.h"
#include "third_party/zynamics/binexport/function.h"
namespace {
// Sorts by descending occurrence count then by mnemonic string. Don't be
// confused by operator > - this function is called as an operator less-than.
bool SortMnemonicsByOccurrenceCount(const std::pair<string, int32>& one,
const std::pair<string, int32>& two) {
if (one.second != two.second) {
return one.second > two.second;
}
return one.first < two.first;
}
// Sorts by mnemonic string.
bool SortMnemonicsAlphabetically(const std::pair<string, int32>& one,
const std::pair<string, int32>& two) {
return one.first < two.first;
}
// Stores unique mnemonics in result proto. Returns a vector of mnemonics sorted
// lexicographically for quick lookups. Every mnemonic maps to its index in the
// result proto array.
void WriteMnemonics(const Instructions& instructions,
std::vector<std::pair<string, int32>>* mnemonics,
BinExport2* proto) {
// Get a histogram of mnemonics. Sort that histogram by descending occurrence
// count. Store mnemonics in result proto buffer. Remember the indices
// assigned in the proto array. Sort the index vector by mnemonic string to
// allow for quick binary search by string and mapping from string to index.
std::unordered_map<string, int32> mnemonic_histogram;
for (const auto& instruction : instructions) {
if (instruction.HasFlag(FLAG_INVALID)) {
continue;
}
++mnemonic_histogram[instruction.GetMnemonic()];
}
mnemonics->reserve(mnemonic_histogram.size());
for (const auto& mnemonic : mnemonic_histogram) {
mnemonics->push_back(mnemonic);
}
std::sort(mnemonics->begin(), mnemonics->end(),
&SortMnemonicsByOccurrenceCount);
proto->mutable_mnemonic()->Reserve(mnemonics->size());
for (auto& mnemonic : *mnemonics) {
mnemonic.second = proto->mnemonic_size(); // Remember current index.
proto->add_mnemonic()->set_name(mnemonic.first);
}
std::sort(mnemonics->begin(), mnemonics->end(), &SortMnemonicsAlphabetically);
}
// Translates from BinDetego internal expression type to the expression type
// used by the protocol buffer.
BinExport2::Expression::Type ExpressionTypeToProtoType(Expression::Type type) {
switch (type) {
case Expression::TYPE_SYMBOL:
return BinExport2::Expression::SYMBOL;
case Expression::TYPE_IMMEDIATE_INT:
return BinExport2::Expression::IMMEDIATE_INT;
case Expression::TYPE_IMMEDIATE_FLOAT:
return BinExport2::Expression::IMMEDIATE_FLOAT;
case Expression::TYPE_OPERATOR:
return BinExport2::Expression::OPERATOR;
case Expression::TYPE_REGISTER:
return BinExport2::Expression::REGISTER;
case Expression::TYPE_SIZEPREFIX:
return BinExport2::Expression::SIZE_PREFIX;
case Expression::TYPE_DEREFERENCE:
return BinExport2::Expression::DEREFERENCE;
// Only used by IDA, we map these to simple symbols.
case Expression::TYPE_GLOBALVARIABLE:
case Expression::TYPE_JUMPLABEL:
case Expression::TYPE_STACKVARIABLE:
case Expression::TYPE_FUNCTION:
return BinExport2::Expression::SYMBOL;
default:
LOG(QFATAL) << "Invalid expression type: " << type;
return BinExport2::Expression::IMMEDIATE_INT; // Not reached
}
}
bool SortExpressionsById(const Expression* one, const Expression* two) {
return one->GetId() < two->GetId();
}
// Stores expression trees.
void WriteExpressions(BinExport2* proto) {
std::vector<const Expression*> expressions;
expressions.reserve(Expression::GetExpressions().size());
for (const auto& expression_cache_entry : Expression::GetExpressions()) {
expressions.push_back(&expression_cache_entry.second);
}
std::sort(expressions.begin(), expressions.end(), &SortExpressionsById);
proto->mutable_expression()->Reserve(expressions.size());
for (const Expression* expression : expressions) {
// Proto expressions use a zero based index, C++ expressions are one based.
QCHECK_EQ(expression->GetId() - 1, proto->expression_size());
QCHECK(!expression->GetSymbol().empty() || expression->IsImmediate());
BinExport2::Expression* proto_expression(proto->add_expression());
if (!expression->GetSymbol().empty()) {
proto_expression->set_symbol(expression->GetSymbol());
}
if (expression->GetParent() != nullptr) {
proto_expression->set_parent_index(expression->GetParent()->GetId() - 1);
}
if (expression->IsImmediate()) {
proto_expression->set_immediate(expression->GetImmediate());
}
if (expression->IsRelocation()) {
proto_expression->set_is_relocation(true);
}
const auto type = ExpressionTypeToProtoType(expression->GetType());
if (type != BinExport2::Expression::IMMEDIATE_INT) {
// Only store if different from default value.
proto_expression->set_type(type);
}
}
}
bool SortOperandsById(const Operand* one, const Operand* two) {
return one->GetId() < two->GetId();
}
// Stores per operand expression trees.
void WriteOperands(BinExport2* proto) {
std::vector<const Operand*> operands;
operands.reserve(Operand::GetOperands().size());
for (const auto& operand_cache_entry : Operand::GetOperands()) {
operands.push_back(&operand_cache_entry.second);
}
std::sort(operands.begin(), operands.end(), &SortOperandsById);
proto->mutable_operand()->Reserve(operands.size());
for (const Operand* operand : operands) {
// Proto expressions use a zero based index, C++ expressions are one based.
QCHECK_EQ(operand->GetId() - 1, proto->operand_size());
BinExport2::Operand* proto_operand(proto->add_operand());
proto_operand->mutable_expression_index()->Reserve(
operand->GetExpressionCount());
const auto* previous_expression = *(operand->begin());
for (const auto* expression : *operand) {
QCHECK(expression->GetParent() != previous_expression->GetParent() ||
expression->GetPosition() >= previous_expression->GetPosition());
proto_operand->add_expression_index(expression->GetId() - 1);
previous_expression = expression;
}
}
}
// Binary search for the given mnemonic. It is a fatal error to look for a
// mnemonic that is not actually contained in the set.
int32 GetMnemonicIndex(const std::vector<std::pair<string, int32>>& mnemonics,
const string& mnemonic) {
const auto it = lower_bound(mnemonics.begin(), mnemonics.end(),
std::make_pair(mnemonic, 0));
QCHECK(it != mnemonics.end()) << "Unknown mnemonic: " << mnemonic;
QCHECK_EQ(mnemonic, it->first);
return it->second;
}
// Find call targets for the given instruction and store them in the protocol
// buffer.
void WriteCallTargets(Address instruction_address,
const AddressReferences& address_references,
BinExport2::Instruction* proto_instruction) {
const AddressReferences::const_iterator reference =
lower_bound(address_references.begin(), address_references.end(),
AddressReference(instruction_address, std::make_pair(-1, -1),
0, TYPE_CALL_DIRECT));
for (AddressReferences::const_iterator i = reference;
i != address_references.end() && i->source_ == instruction_address;
++i) {
if (!i->IsCall()) {
continue;
}
proto_instruction->add_call_target(i->target_);
}
}
void WriteInstructions(
const FlowGraph& flow_graph, const Instructions& instructions,
const std::vector<std::pair<string, int32>>& mnemonics,
const AddressReferences& address_references,
std::vector<std::pair<Address, int32>>* instruction_indices,
BinExport2* proto) {
QCHECK(std::is_sorted(address_references.begin(), address_references.end()));
proto->mutable_instruction()->Reserve(instructions.size());
const Instruction* previous_instruction(nullptr);
for (const Instruction& instruction : instructions) {
if (instruction.HasFlag(FLAG_INVALID)) {
previous_instruction = nullptr;
continue;
}
instruction_indices->push_back(
std::make_pair(instruction.GetAddress(), proto->instruction_size()));
BinExport2::Instruction* proto_instruction(proto->add_instruction());
QCHECK_EQ(instruction.GetSize(), instruction.GetBytes().size());
// Write the full instruction address iff:
// - there is no previous instruction
// - the previous instruction doesn't have code flow into the current one
// - the previous instruction overlaps the current one
// - the current instruction is a function entry point
if (previous_instruction == nullptr || !previous_instruction->IsFlow() ||
previous_instruction->GetAddress() + previous_instruction->GetSize() !=
instruction.GetAddress() ||
flow_graph.GetFunction(instruction.GetAddress())) {
proto_instruction->set_address(instruction.GetAddress());
}
proto_instruction->set_raw_bytes(instruction.GetBytes());
if (const auto index =
GetMnemonicIndex(mnemonics, instruction.GetMnemonic())) {
// Only store if different from default value.
proto_instruction->set_mnemonic_index(index);
}
proto_instruction->mutable_operand_index()->Reserve(
instruction.GetOperandCount());
for (const auto* operand : instruction) {
QCHECK_GT(operand->GetId(), 0);
proto_instruction->add_operand_index(operand->GetId() - 1);
}
WriteCallTargets(instruction.GetAddress(), address_references,
proto_instruction);
previous_instruction = &instruction;
}
std::sort(instruction_indices->begin(), instruction_indices->end());
}
void WriteBasicBlocks(
const Instructions& instructions,
const std::vector<std::pair<Address, int32>>& instruction_indices,
BinExport2* proto) {
CHECK((instruction_indices.empty() && BasicBlock::blocks().empty()) ||
(!instruction_indices.empty() && !BasicBlock::blocks().empty()));
proto->mutable_basic_block()->Reserve(BasicBlock::blocks().size());
auto instruction_index_it = instruction_indices.begin();
int id = 0;
for (auto& basic_block : BasicBlock::blocks()) {
// Normally, cache elements should not be modified as changing the objects
// might change their ordering. However, we are only modifying id here
// which doesn't affect the order.
BinExport2::BasicBlock proto_basic_block;
bool basic_block_is_invalid = false;
int begin_index = -1, end_index = -1;
for (const auto& instruction : *basic_block.second) {
// The whole basic block is invalid if it contains a single invalid
// instruction.
if (instruction.HasFlag(FLAG_INVALID)) {
basic_block_is_invalid = true;
break;
}
if (instruction_index_it == instruction_indices.end() ||
instruction.GetAddress() != instruction_index_it->first) {
instruction_index_it =
lower_bound(instruction_indices.begin(), instruction_indices.end(),
std::make_pair(instruction.GetAddress(), 0));
}
QCHECK(instruction_index_it != instruction_indices.end());
QCHECK_EQ(instruction_index_it->first, instruction.GetAddress());
const int instruction_index = instruction_index_it->second;
++instruction_index_it;
if (begin_index < 0) {
begin_index = instruction_index;
end_index = begin_index + 1;
} else if (instruction_index != end_index) {
// Sequence is broken, store an interval.
BinExport2::BasicBlock::IndexRange* index_range(
proto_basic_block.add_instruction_index());
index_range->set_begin_index(begin_index);
if (end_index != begin_index + 1) {
// We omit the end index in the single instruction interval case.
index_range->set_end_index(end_index);
}
begin_index = instruction_index;
end_index = begin_index + 1;
} else {
// Sequence is unbroken, remember end_index.
end_index = instruction_index + 1;
}
}
BinExport2::BasicBlock::IndexRange* index_range(
proto_basic_block.add_instruction_index());
index_range->set_begin_index(begin_index);
if (end_index != begin_index + 1) {
// We omit the end index in the single instruction interval case.
index_range->set_end_index(end_index);
}
if (!basic_block_is_invalid) {
basic_block.second->set_id(id++);
*proto->add_basic_block() = proto_basic_block;
}
}
}
// Translates from BinDetego internal flow graph edge type to the edge type
// used by the protocol buffer.
BinExport2::FlowGraph::Edge::Type FlowGraphEdgeTypeToProtoType(
FlowGraphEdge::Type type) {
switch (type) {
case FlowGraphEdge::TYPE_TRUE:
return BinExport2::FlowGraph::Edge::CONDITION_TRUE;
case FlowGraphEdge::TYPE_FALSE:
return BinExport2::FlowGraph::Edge::CONDITION_FALSE;
case FlowGraphEdge::TYPE_UNCONDITIONAL:
return BinExport2::FlowGraph::Edge::UNCONDITIONAL;
case FlowGraphEdge::TYPE_SWITCH:
return BinExport2::FlowGraph::Edge::SWITCH;
default:
LOG(QFATAL) << "Invalid flow graph edge type: " << type;
return BinExport2::FlowGraph::Edge::UNCONDITIONAL; // Not reached
}
}
void WriteFlowGraphs(const FlowGraph& flow_graph, BinExport2* proto) {
proto->mutable_flow_graph()->Reserve(flow_graph.GetFunctions().size());
for (const auto& address_to_function : flow_graph.GetFunctions()) {
const Function& function = *address_to_function.second;
if (function.GetBasicBlocks().empty() ||
function.GetType(true /* raw type */) == Function::TYPE_INVALID) {
continue; // Skip empty flow graphs, they only exist as call graph nodes.
}
BinExport2::FlowGraph* proto_flow_graph = proto->add_flow_graph();
proto_flow_graph->mutable_basic_block_index()->Reserve(
function.GetBasicBlocks().size());
for (const BasicBlock* basic_block : function.GetBasicBlocks()) {
if (basic_block->GetEntryPoint() == function.GetEntryPoint()) {
proto_flow_graph->set_entry_basic_block_index(basic_block->id());
}
proto_flow_graph->add_basic_block_index(basic_block->id());
}
QCHECK_GE(proto_flow_graph->entry_basic_block_index(), 0);
QCHECK_EQ(proto_flow_graph->basic_block_index_size(),
function.GetBasicBlocks().size());
std::vector<Function::Edges::const_iterator> back_edges;
function.GetBackEdges(&back_edges);
auto back_edge = back_edges.begin();
proto_flow_graph->mutable_edge()->Reserve(function.GetEdges().size());
for (const FlowGraphEdge& edge : function.GetEdges()) {
BinExport2::FlowGraph::Edge* proto_edge = proto_flow_graph->add_edge();
const BasicBlock* source =
CHECK_NOTNULL(function.GetBasicBlockForAddress(edge.source));
const BasicBlock* target =
CHECK_NOTNULL(function.GetBasicBlockForAddress(edge.target));
proto_edge->set_source_basic_block_index(source->id());
proto_edge->set_target_basic_block_index(target->id());
const auto type = FlowGraphEdgeTypeToProtoType(edge.type);
if (type != BinExport2::FlowGraph::Edge::UNCONDITIONAL) {
// Only store if different from default value.
proto_edge->set_type(type);
}
// Advance the back edge iterator. Note that back edges and regular edges
// are sorted the same way, so we can iterate through the vectors in lock
// step.
for (; back_edge != back_edges.end() &&
(*back_edge)->source < edge.source &&
(*back_edge)->target < edge.target;
++back_edge) {
}
if (back_edge != back_edges.end() &&
(*back_edge)->source == edge.source &&
(*back_edge)->target == edge.target) {
proto_edge->set_is_back_edge(true);
++back_edge;
}
}
}
}
// Translates from BinDetego internal call graph function type to the function
// type used by the protocol buffer.
BinExport2::CallGraph::Vertex::Type CallGraphVertexTypeToProtoType(
Function::FunctionType type) {
switch (type) {
case Function::TYPE_STANDARD:
return BinExport2::CallGraph::Vertex::NORMAL;
case Function::TYPE_LIBRARY:
return BinExport2::CallGraph::Vertex::LIBRARY;
case Function::TYPE_IMPORTED:
return BinExport2::CallGraph::Vertex::IMPORTED;
case Function::TYPE_THUNK:
return BinExport2::CallGraph::Vertex::THUNK;
case Function::TYPE_INVALID:
return BinExport2::CallGraph::Vertex::INVALID;
default:
LOG(QFATAL) << "Invalid call graph vertex type: " << type;
return BinExport2::CallGraph::Vertex::NORMAL; // Not reached
}
}
// Used for binary searching the call graph vertex array for a particular
// function.
bool SortByAddress(const BinExport2::CallGraph::Vertex& one,
const BinExport2::CallGraph::Vertex& two) {
return one.address() < two.address();
}
// Functions in the original call_graph are sorted by address and added
// sequentially to the protocol buffer. Hence we can binary search for a
// particular address.
// It is a fatal error to look for an address that is not actually contained in
// the graph.
int32 GetVertexIndex(const BinExport2::CallGraph& call_graph, uint64 address) {
BinExport2::CallGraph::Vertex vertex;
vertex.set_address(address);
const auto& it =
lower_bound(call_graph.vertex().begin(), call_graph.vertex().end(),
vertex, &SortByAddress);
QCHECK(it != call_graph.vertex().end())
<< "Can't find a call graph node for: "
<< absl::StrCat(absl::Hex(address, absl::kZeroPad8));
QCHECK_EQ(address, it->address())
<< "Can't find a call graph node for: "
<< absl::StrCat(absl::Hex(address, absl::kZeroPad8));
return it - call_graph.vertex().begin();
}
void WriteCallGraph(const CallGraph& call_graph, const FlowGraph& flow_graph,
BinExport2* proto) {
BinExport2::CallGraph* proto_call_graph(proto->mutable_call_graph());
proto_call_graph->mutable_vertex()->Reserve(flow_graph.GetFunctions().size());
// Create used libraries list.
std::vector<const LibraryManager::LibraryRecord*> used_libraries;
call_graph.GetLibraryManager().GetUsedLibraries(&used_libraries);
std::unordered_map<int, int> use_index;
for (int i = 0; i < used_libraries.size(); ++i) {
use_index[used_libraries[i]->library_index] = i;
}
// Used for verifying that functions are sorted by address.
uint64 previous_entry_point_address = 0;
std::unordered_map<string, int32> module_index;
for (const auto& function_it : flow_graph.GetFunctions()) {
const Function& function(*function_it.second);
QCHECK_GE(function.GetEntryPoint(), previous_entry_point_address);
previous_entry_point_address = function.GetEntryPoint();
QCHECK(call_graph.GetFunctions().find(function.GetEntryPoint()) !=
call_graph.GetFunctions().end());
BinExport2::CallGraph::Vertex* proto_function(
proto_call_graph->add_vertex());
proto_function->set_address(function.GetEntryPoint());
const auto vertex_type =
CallGraphVertexTypeToProtoType(function.GetType(false));
if (vertex_type != BinExport2::CallGraph::Vertex::NORMAL) {
// Only store if different from default value.
proto_function->set_type(vertex_type);
}
if (function.HasRealName()) {
proto_function->set_mangled_name(function.GetName(Function::MANGLED));
if (function.GetName(Function::DEMANGLED) !=
function.GetName(Function::MANGLED)) {
proto_function->set_demangled_name(
function.GetName(Function::DEMANGLED));
}
}
int library_index = function.GetLibraryIndex();
if (library_index != -1) {
// We serialize use index, not library index (as the latter refers to the
// array of all known libraries).
proto_function->set_library_index(use_index[library_index]);
}
const string& module = function.GetModuleName();
if (!module.empty()) {
auto it = module_index.emplace(module, module_index.size());
proto_function->set_module_index(it.first->second);
}
}
if (!module_index.empty()) {
proto->mutable_module()->Reserve(module_index.size());
// We are O(N^2) here by number of classes, shouldn't be a big deal.
for (int i = 0; i < module_index.size(); ++i) {
auto* module = proto->add_module();
module->set_name(std::find_if(
module_index.begin(), module_index.end(),
[i] (const std::pair<string, int32>& kv) -> bool {
return kv.second == i;
})->first);
}
}
proto_call_graph->mutable_edge()->Reserve(call_graph.GetEdges().size());
for (const EdgeInfo& edge : call_graph.GetEdges()) {
BinExport2::CallGraph::Edge* proto_edge(proto_call_graph->add_edge());
const uint64 source_address(CHECK_NOTNULL(edge.function_)->GetEntryPoint());
const uint64 target_address(edge.target_);
proto_edge->set_source_vertex_index(
GetVertexIndex(*proto_call_graph, source_address));
proto_edge->set_target_vertex_index(
GetVertexIndex(*proto_call_graph, target_address));
}
proto->mutable_library()->Reserve(used_libraries.size());
for (const auto* used : used_libraries) {
auto* library = proto->add_library();
library->set_name(used->name);
library->set_is_static(used->IsStatic());
}
}
void WriteStrings(
const AddressReferences& address_references,
const AddressSpace& address_space,
const std::vector<std::pair<Address, int32>>& instruction_indices,
BinExport2* proto) {
std::unordered_map<string, int> string_to_string_index;
for (const auto& reference : address_references) {
if (reference.kind_ != TYPE_DATA_STRING &&
reference.kind_ != TYPE_DATA_WIDE_STRING) {
continue;
}
// String length must be > 0.
if (reference.size_ == 0) {
continue;
}
const auto instruction =
lower_bound(instruction_indices.begin(), instruction_indices.end(),
std::make_pair(reference.source_, 0));
// Only add strings and string references if there is an instruction
// actually referencing the string.
if (instruction == instruction_indices.end() ||
instruction->first != reference.source_) {
continue;
}
string content;
// TODO(timkornau): Add support for UTF16 strings.
if (reference.kind_ != TYPE_DATA_STRING) {
continue;
}
content =
string(reinterpret_cast<const char*>(&address_space[reference.target_]),
reference.size_);
auto it =
string_to_string_index.emplace(content, proto->string_table_size());
// Deduplicate strings.
if (it.second != false) {
proto->add_string_table(it.first->first);
}
auto* proto_string_reference = proto->add_string_reference();
proto_string_reference->set_instruction_index(instruction->second);
proto_string_reference->set_instruction_operand_index(
reference.source_operand_);
proto_string_reference->set_operand_expression_index(
reference.source_expression_);
proto_string_reference->set_string_table_index(it.first->second);
}
}
void WriteDataReferences(
const AddressReferences& address_references,
const AddressSpace& address_space,
const std::vector<std::pair<Address, int32>>& instruction_indices,
BinExport2* proto) {
// Cache address -> instruction mapping.
std::unordered_map<Address, int32> address_to_index;
for (const auto& index : instruction_indices) {
address_to_index[index.first] = index.second;
}
for (const auto& reference : address_references) {
if (reference.kind_ != TYPE_DATA) {
continue;
}
// Invalid reference.
if (reference.target_ == 0) {
continue;
}
const auto instruction = address_to_index.find(reference.source_);
// Only add data references if there is a referring instruction.
if (instruction == address_to_index.end()) {
continue;
}
if (address_space.IsValidAddress(reference.target_)) {
auto* proto_data_reference = proto->add_data_reference();
proto_data_reference->set_instruction_index(instruction->second);
proto_data_reference->set_address(reference.target_);
}
}
}
void WriteComments(
const CallGraph& call_graph,
const std::vector<std::pair<Address, int32>>& instruction_indices,
BinExport2* proto) {
std::unordered_map<const string*, int> comment_to_index;
for (const Comment& comment : call_graph.GetComments()) {
const auto new_comment = comment_to_index.insert(
std::make_pair(comment.comment_, proto->string_table_size()));
if (new_comment.second) {
proto->add_string_table(*comment.comment_);
}
auto* proto_comment = proto->add_address_comment();
const auto instruction =
lower_bound(instruction_indices.begin(), instruction_indices.end(),
std::make_pair(comment.address_, 0));
QCHECK(instruction != instruction_indices.end());
proto_comment->set_instruction_index(instruction->second);
// TODO(user) Fill these properly once we actually have
// operand/expression comments.
// proto_comment->set_instruction_operand_index(0);
// proto_comment->set_operand_expression_index(0);
proto_comment->set_string_table_index(new_comment.first->second);
}
}
void WriteSections(const AddressSpace& address_space, BinExport2* proto) {
for (const auto& data : address_space.data()) {
auto* section_proto = proto->add_section();
section_proto->set_address(data.first);
section_proto->set_size(data.second.size());
section_proto->set_flag_r(address_space.IsReadable(data.first));
section_proto->set_flag_w(address_space.IsWritable(data.first));
section_proto->set_flag_x(address_space.IsExecutable(data.first));
}
}
#ifndef GOOGLE
class BinExport2MetaWithDifferentOrder : public BinExport2_Meta {
public:
BinExport2MetaWithDifferentOrder(const BinExport2_Meta& from)
: BinExport2_Meta(from) {}
void SerializeWithCachedSizes(
google::protobuf::io::CodedOutputStream* output) const override;
google::protobuf::uint8* InternalSerializeWithCachedSizesToArray(
bool deterministic, google::protobuf::uint8* target) const override;
};
void BinExport2MetaWithDifferentOrder::SerializeWithCachedSizes(
google::protobuf::io::CodedOutputStream* output) const {
// optional bytes padding_v1 = 5;
if (has_padding_v1()) {
google::protobuf::internal::WireFormatLite::WriteBytesMaybeAliased(
0, this->padding_v1(), output);
}
// optional string executable_name = 1;
if (has_executable_name()) {
google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
1, this->executable_name(), output);
}
// optional string executable_id = 2;
if (has_executable_id()) {
google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
2, this->executable_id(), output);
}
// optional string architecture_name = 3;
if (has_architecture_name()) {
google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
3, this->architecture_name(), output);
}
// optional int64 timestamp = 4;
if (has_timestamp()) {
google::protobuf::internal::WireFormatLite::WriteInt64(4, this->timestamp(),
output);
}
}
google::protobuf::uint8*
BinExport2MetaWithDifferentOrder::InternalSerializeWithCachedSizesToArray(
bool /* deterministic */, google::protobuf::uint8* target) const {
// optional bytes padding_v1 = 5;
if (has_padding_v1()) {
target = google::protobuf::internal::WireFormatLite::WriteBytesToArray(
5, this->padding_v1(), target);
}
// optional string executable_name = 1;
if (has_executable_name()) {
target = google::protobuf::internal::WireFormatLite::WriteStringToArray(
1, this->executable_name(), target);
}
// optional string executable_id = 2;
if (has_executable_id()) {
target = google::protobuf::internal::WireFormatLite::WriteStringToArray(
2, this->executable_id(), target);
}
// optional string architecture_name = 3;
if (has_architecture_name()) {
target = google::protobuf::internal::WireFormatLite::WriteStringToArray(
3, this->architecture_name(), target);
}
// optional int64 timestamp = 4;
if (has_timestamp()) {
target = google::protobuf::internal::WireFormatLite::WriteInt64ToArray(
4, this->timestamp(), target);
}
return target;
}
#endif
// Writes a binary protocol buffer to the specified filename. Returns true if
// successful. Logs an error and returns false if not.
util::Status WriteProtoToFile(const string& filename, BinExport2* proto) {
std::ofstream stream(filename, std::ios::binary | std::ios::out);
// Apply padding for compatibility with BinDiff < 4.3.
auto* meta = new BinExport2MetaWithDifferentOrder(proto->meta_information());
meta->set_padding_v1(string(7, '\0'));
proto->set_allocated_meta_information(meta); // Transfer ownership
if (!proto->SerializeToOstream(&stream)) {
return util::Status(
util::error::UNKNOWN,
absl::StrCat("Error serializing data to: '", filename, "'."));
}
return util::OkStatus();
}
} // namespace
BinExport2Writer::BinExport2Writer(const string& result_filename,
const string& executable_filename,
const string& executable_hash,
const string& architecture)
: filename_(result_filename),
executable_filename_(executable_filename),
executable_hash_(executable_hash),
architecture_(architecture) {}
util::Status BinExport2Writer::WriteToProto(
const CallGraph& call_graph, const FlowGraph& flow_graph,
const Instructions& instructions,
const AddressReferences& address_references, const TypeSystem* type_system,
const AddressSpace& address_space, BinExport2* proto) const {
auto* meta_information = proto->mutable_meta_information();
meta_information->set_executable_name(executable_filename_);
meta_information->set_executable_id(executable_hash_);
meta_information->set_architecture_name(architecture_);
meta_information->set_timestamp(absl::ToUnixSeconds(absl::Now()));
WriteExpressions(proto);
WriteOperands(proto);
{
std::vector<std::pair<string, int32>> mnemonics;
WriteMnemonics(instructions, &mnemonics, proto);
std::vector<std::pair<Address, int32>> instruction_indices;
WriteInstructions(flow_graph, instructions, mnemonics, address_references,
&instruction_indices, proto);
WriteBasicBlocks(instructions, instruction_indices, proto);
WriteComments(call_graph, instruction_indices, proto);
WriteStrings(address_references, address_space, instruction_indices, proto);
WriteDataReferences(address_references, address_space, instruction_indices,
proto);
}
WriteFlowGraphs(flow_graph, proto);
WriteCallGraph(call_graph, flow_graph, proto);
WriteSections(address_space, proto);
return util::OkStatus();
}
util::Status BinExport2Writer::Write(
const CallGraph& call_graph, const FlowGraph& flow_graph,
const Instructions& instructions,
const AddressReferences& address_references, const TypeSystem* type_system,
const AddressSpace& address_space) {
LOG(INFO) << "Writing to: \"" << filename_ << "\".";
BinExport2 proto;
const auto status =
WriteToProto(call_graph, flow_graph, instructions, address_references,
type_system, address_space, &proto);
if (!status.ok()) {
return status;
}
return WriteProtoToFile(filename_, &proto);
}