forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwarf_cu_to_module_unittest.cc
1865 lines (1619 loc) · 69.9 KB
/
dwarf_cu_to_module_unittest.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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2010 Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
// dwarf_cu_to_module.cc: Unit tests for google_breakpad::DwarfCUToModule.
#include <vector>
#include "breakpad_googletest_includes.h"
#include "common/dwarf_cu_to_module.h"
using std::vector;
using dwarf2reader::AttributeList;
using dwarf2reader::DIEHandler;
using dwarf2reader::DwarfTag;
using dwarf2reader::DwarfAttribute;
using dwarf2reader::DwarfForm;
using dwarf2reader::DwarfInline;
using dwarf2reader::RootDIEHandler;
using google_breakpad::DwarfCUToModule;
using google_breakpad::Module;
using ::testing::_;
using ::testing::AtMost;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::Test;
using ::testing::TestWithParam;
using ::testing::Values;
using ::testing::ValuesIn;
// Mock classes.
class MockLineToModuleFunctor: public DwarfCUToModule::LineToModuleFunctor {
public:
MOCK_METHOD4(mock_apply, void(const char *program, uint64 length,
Module *module, vector<Module::Line> *lines));
void operator()(const char *program, uint64 length,
Module *module, vector<Module::Line> *lines) {
mock_apply(program, length, module, lines);
}
};
class MockWarningReporter: public DwarfCUToModule::WarningReporter {
public:
MockWarningReporter(const string &filename, uint64 cu_offset)
: DwarfCUToModule::WarningReporter(filename, cu_offset) { }
MOCK_METHOD1(SetCUName, void(const string &name));
MOCK_METHOD2(UnknownSpecification, void(uint64 offset, uint64 target));
MOCK_METHOD2(UnknownAbstractOrigin, void(uint64 offset, uint64 target));
MOCK_METHOD1(MissingSection, void(const string §ion_name));
MOCK_METHOD1(BadLineInfoOffset, void(uint64 offset));
MOCK_METHOD1(UncoveredFunction, void(const Module::Function &function));
MOCK_METHOD1(UncoveredLine, void(const Module::Line &line));
MOCK_METHOD1(UnnamedFunction, void(uint64 offset));
};
// A fixture class including all the objects needed to handle a
// compilation unit, and their entourage. It includes member functions
// for doing common kinds of setup and tests.
class CUFixtureBase {
public:
// If we have:
//
// vector<Module::Line> lines;
// AppendLinesFunctor appender(lines);
//
// then doing:
//
// appender(line_program, length, module, line_vector);
//
// will append lines to the end of line_vector. We can use this with
// MockLineToModuleFunctor like this:
//
// MockLineToModuleFunctor l2m;
// EXPECT_CALL(l2m, mock_apply(_,_,_,_))
// .WillOnce(DoAll(Invoke(appender), Return()));
//
// in which case calling l2m with some line vector will append lines.
class AppendLinesFunctor {
public:
AppendLinesFunctor(const vector<Module::Line> *lines) : lines_(lines) { }
void operator()(const char *program, uint64 length,
Module *module, vector<Module::Line> *lines) {
lines->insert(lines->end(), lines_->begin(), lines_->end());
}
private:
const vector<Module::Line> *lines_;
};
CUFixtureBase()
: module_("module-name", "module-os", "module-arch", "module-id"),
file_context_("dwarf-filename", &module_),
language_(dwarf2reader::DW_LANG_none),
language_signed_(false),
appender_(&lines_),
reporter_("dwarf-filename", 0xcf8f9bb6443d29b5LL),
root_handler_(&file_context_, &line_reader_, &reporter_),
functions_filled_(false),
incrementing_offset_(0x0000000000100000ULL) {
// By default, expect no warnings to be reported, and expect the
// compilation unit's name to be provided. The test can override
// these expectations.
EXPECT_CALL(reporter_, SetCUName("compilation-unit-name")).Times(1);
EXPECT_CALL(reporter_, UnknownSpecification(_, _)).Times(0);
EXPECT_CALL(reporter_, UnknownAbstractOrigin(_, _)).Times(0);
EXPECT_CALL(reporter_, MissingSection(_)).Times(0);
EXPECT_CALL(reporter_, BadLineInfoOffset(_)).Times(0);
EXPECT_CALL(reporter_, UncoveredFunction(_)).Times(0);
EXPECT_CALL(reporter_, UncoveredLine(_)).Times(0);
EXPECT_CALL(reporter_, UnnamedFunction(_)).Times(0);
// By default, expect the line program reader not to be invoked. We
// may override this in StartCU.
EXPECT_CALL(line_reader_, mock_apply(_,_,_,_)).Times(0);
// The handler will consult this section map to decide what to
// pass to our line reader.
file_context_.section_map[".debug_line"] = std::make_pair(dummy_line_program_,
dummy_line_size_);
}
// Add a line with the given address, size, filename, and line
// number to the end of the statement list the handler will receive
// when it invokes its LineToModuleFunctor. Call this before calling
// StartCU.
void PushLine(Module::Address address, Module::Address size,
const string &filename, int line_number);
// Use LANGUAGE for the compilation unit. More precisely, arrange
// for StartCU to pass the compilation unit's root DIE a
// DW_AT_language attribute whose value is LANGUAGE.
void SetLanguage(dwarf2reader::DwarfLanguage language) {
language_ = language;
}
// If SIGNED true, have StartCU report DW_AT_language as a signed
// attribute; if false, have it report it as unsigned.
void SetLanguageSigned(bool is_signed) { language_signed_ = is_signed; }
// Call the handler this.root_handler_'s StartCompilationUnit and
// StartRootDIE member functions, passing it appropriate attributes as
// determined by prior calls to PushLine and SetLanguage. Leave
// this.root_handler_ ready to hear about children: call
// this.root_handler_.EndAttributes, but not this.root_handler_.Finish.
void StartCU();
// Add some strange attributes/form pairs to the end of ATTRS.
void PushBackStrangeAttributes(dwarf2reader::AttributeList *attrs);
// Have HANDLER process some strange attribute/form/value triples.
// These will match those promised by PushBackStrangeAttributes.
void ProcessStrangeAttributes(dwarf2reader::DIEHandler *handler);
// Start a child DIE of PARENT with the given tag and name. Leave
// the handler ready to hear about children: call EndAttributes, but
// not Finish.
DIEHandler *StartNamedDIE(DIEHandler *parent, DwarfTag tag,
const string &name);
// Start a child DIE of PARENT with the given tag and a
// DW_AT_specification attribute whose value is SPECIFICATION. Leave
// the handler ready to hear about children: call EndAttributes, but
// not Finish. If NAME is non-zero, use it as the DW_AT_name
// attribute.
DIEHandler *StartSpecifiedDIE(DIEHandler *parent, DwarfTag tag,
uint64 offset, const char *name = NULL);
// Define a function as a child of PARENT with the given name,
// address, and size. Call EndAttributes and Finish; one cannot
// define children of the defined function's DIE.
void DefineFunction(DIEHandler *parent, const string &name,
Module::Address address, Module::Address size);
// Create a declaration DIE as a child of PARENT with the given
// offset, tag and name. If NAME is the empty string, don't provide
// a DW_AT_name attribute. Call EndAttributes and Finish.
void DeclarationDIE(DIEHandler *parent, uint64 offset,
DwarfTag tag, const string &name);
// Create a definition DIE as a child of PARENT with the given tag
// that refers to the declaration DIE at offset SPECIFICATION as its
// specification. If NAME is non-empty, pass it as the DW_AT_name
// attribute. If SIZE is non-zero, record ADDRESS and SIZE as
// low_pc/high_pc attributes.
void DefinitionDIE(DIEHandler *parent, DwarfTag tag,
uint64 specification, const string &name,
Module::Address address = 0, Module::Address size = 0);
// Create an inline DW_TAG_subprogram DIE as a child of PARENT. If
// SPECIFICATION is non-zero, then the DIE refers to the declaration DIE at
// offset SPECIFICATION as its specification. If Name is non-empty, pass it
// as the DW_AT_name attribute.
void AbstractInstanceDIE(DIEHandler *parent, uint64 offset,
DwarfInline type, uint64 specification,
const string &name,
DwarfForm form = dwarf2reader::DW_FORM_data1);
// Create a DW_TAG_subprogram DIE as a child of PARENT that refers to
// ORIGIN in its DW_AT_abstract_origin attribute. If NAME is the empty
// string, don't provide a DW_AT_name attribute.
void DefineInlineInstanceDIE(DIEHandler *parent, const string &name,
uint64 origin, Module::Address address,
Module::Address size);
// The following Test* functions should be called after calling
// this.root_handler_.Finish. After that point, no further calls
// should be made on the handler.
// Test that the number of functions defined in the module this.module_ is
// equal to EXPECTED.
void TestFunctionCount(size_t expected);
// Test that the I'th function (ordered by address) in the module
// this.module_ has the given name, address, and size, and that its
// parameter size is zero.
void TestFunction(int i, const string &name,
Module::Address address, Module::Address size);
// Test that the number of source lines owned by the I'th function
// in the module this.module_ is equal to EXPECTED.
void TestLineCount(int i, size_t expected);
// Test that the J'th line (ordered by address) of the I'th function
// (again, by address) has the given address, size, filename, and
// line number.
void TestLine(int i, int j, Module::Address address, Module::Address size,
const string &filename, int number);
// Actual objects under test.
Module module_;
DwarfCUToModule::FileContext file_context_;
// If this is not DW_LANG_none, we'll pass it as a DW_AT_language
// attribute to the compilation unit. This defaults to DW_LANG_none.
dwarf2reader::DwarfLanguage language_;
// If this is true, report DW_AT_language as a signed value; if false,
// report it as an unsigned value.
bool language_signed_;
// If this is not empty, we'll give the CU a DW_AT_stmt_list
// attribute that, when passed to line_reader_, adds these lines to the
// provided lines array.
vector<Module::Line> lines_;
// Mock line program reader.
MockLineToModuleFunctor line_reader_;
AppendLinesFunctor appender_;
static const char dummy_line_program_[];
static const size_t dummy_line_size_;
MockWarningReporter reporter_;
DwarfCUToModule root_handler_;
private:
// Fill functions_, if we haven't already.
void FillFunctions();
// If functions_filled_ is true, this is a table of functions we've
// extracted from module_, sorted by address.
vector<Module::Function *> functions_;
// True if we have filled the above vector with this.module_'s function list.
bool functions_filled_;
// When a unique offset value is needed, grab one from here and increment
// it.
uint64 incrementing_offset_;
};
const char CUFixtureBase::dummy_line_program_[] = "lots of fun data";
const size_t CUFixtureBase::dummy_line_size_ =
sizeof (CUFixtureBase::dummy_line_program_);
void CUFixtureBase::PushLine(Module::Address address, Module::Address size,
const string &filename, int line_number) {
Module::Line l;
l.address = address;
l.size = size;
l.file = module_.FindFile(filename);
l.number = line_number;
lines_.push_back(l);
}
void CUFixtureBase::StartCU() {
// If we have lines, make the line reader expect to be invoked at
// most once. (Hey, if the handler can pass its tests without
// bothering to read the line number data, that's great.)
// Have it add the lines passed to PushLine. Otherwise, leave the
// initial expectation (no calls) in force.
if (!lines_.empty())
EXPECT_CALL(line_reader_,
mock_apply(&dummy_line_program_[0], dummy_line_size_,
&module_, _))
.Times(AtMost(1))
.WillOnce(DoAll(Invoke(appender_), Return()));
ASSERT_TRUE(root_handler_
.StartCompilationUnit(0x51182ec307610b51ULL, 0x81, 0x44,
0x4241b4f33720dd5cULL, 3));
{
dwarf2reader::AttributeList attrs;
attrs.push_back(make_pair(dwarf2reader::DW_AT_name,
dwarf2reader::DW_FORM_strp));
if (!lines_.empty())
attrs.push_back(make_pair(dwarf2reader::DW_AT_stmt_list,
dwarf2reader::DW_FORM_ref4));
if (language_ != dwarf2reader::DW_LANG_none)
attrs.push_back(make_pair(dwarf2reader::DW_AT_language,
language_signed_
? dwarf2reader::DW_FORM_sdata
: dwarf2reader::DW_FORM_udata));
ASSERT_TRUE(root_handler_.StartRootDIE(0x02e56bfbda9e7337ULL,
dwarf2reader::DW_TAG_compile_unit,
attrs));
}
root_handler_.ProcessAttributeString(dwarf2reader::DW_AT_name,
dwarf2reader::DW_FORM_strp,
"compilation-unit-name");
if (!lines_.empty())
root_handler_.ProcessAttributeUnsigned(dwarf2reader::DW_AT_stmt_list,
dwarf2reader::DW_FORM_ref4,
0);
if (language_ != dwarf2reader::DW_LANG_none) {
if (language_signed_)
root_handler_.ProcessAttributeSigned(dwarf2reader::DW_AT_language,
dwarf2reader::DW_FORM_sdata,
language_);
else
root_handler_.ProcessAttributeUnsigned(dwarf2reader::DW_AT_language,
dwarf2reader::DW_FORM_udata,
language_);
}
ASSERT_TRUE(root_handler_.EndAttributes());
}
void CUFixtureBase::PushBackStrangeAttributes(
dwarf2reader::AttributeList *attrs) {
attrs->push_back(make_pair((DwarfAttribute) 0xf560dead,
(DwarfForm) 0x4106e4db));
attrs->push_back(make_pair((DwarfAttribute) 0x85380095,
(DwarfForm) 0x0f16fe87));
attrs->push_back(make_pair((DwarfAttribute) 0xf7f7480f,
(DwarfForm) 0x829e038a));
attrs->push_back(make_pair((DwarfAttribute) 0xa55ffb51,
(DwarfForm) 0x2f43b041));
attrs->push_back(make_pair((DwarfAttribute) 0x2fde304a,
(DwarfForm) 0x895ffa23));
}
void CUFixtureBase::ProcessStrangeAttributes(
dwarf2reader::DIEHandler *handler) {
handler->ProcessAttributeUnsigned((DwarfAttribute) 0xf560dead,
(DwarfForm) 0x4106e4db,
0xa592571997facda1ULL);
handler->ProcessAttributeSigned((DwarfAttribute) 0x85380095,
(DwarfForm) 0x0f16fe87,
0x12602a4e3bf1f446LL);
handler->ProcessAttributeReference((DwarfAttribute) 0xf7f7480f,
(DwarfForm) 0x829e038a,
0x50fddef44734fdecULL);
static const char buffer[10] = "frobynode";
handler->ProcessAttributeBuffer((DwarfAttribute) 0xa55ffb51,
(DwarfForm) 0x2f43b041,
buffer, sizeof(buffer));
handler->ProcessAttributeString((DwarfAttribute) 0x2f43b041,
(DwarfForm) 0x895ffa23,
"strange string");
}
DIEHandler *CUFixtureBase::StartNamedDIE(DIEHandler *parent,
DwarfTag tag,
const string &name) {
dwarf2reader::AttributeList attrs;
attrs.push_back(make_pair(dwarf2reader::DW_AT_name,
dwarf2reader::DW_FORM_strp));
PushBackStrangeAttributes(&attrs);
dwarf2reader::DIEHandler *handler
= parent->FindChildHandler(incrementing_offset_++, tag, attrs);
if (!handler)
return NULL;
handler->ProcessAttributeString(dwarf2reader::DW_AT_name,
dwarf2reader::DW_FORM_strp,
name);
ProcessStrangeAttributes(handler);
if (!handler->EndAttributes()) {
handler->Finish();
delete handler;
return NULL;
}
return handler;
}
DIEHandler *CUFixtureBase::StartSpecifiedDIE(DIEHandler *parent,
DwarfTag tag,
uint64 specification,
const char *name) {
dwarf2reader::AttributeList attrs;
if (name)
attrs.push_back(make_pair(dwarf2reader::DW_AT_name,
dwarf2reader::DW_FORM_strp));
attrs.push_back(make_pair(dwarf2reader::DW_AT_specification,
dwarf2reader::DW_FORM_ref4));
dwarf2reader::DIEHandler *handler
= parent->FindChildHandler(incrementing_offset_++, tag, attrs);
if (!handler)
return NULL;
if (name)
handler->ProcessAttributeString(dwarf2reader::DW_AT_name,
dwarf2reader::DW_FORM_strp,
name);
handler->ProcessAttributeReference(dwarf2reader::DW_AT_specification,
dwarf2reader::DW_FORM_ref4,
specification);
if (!handler->EndAttributes()) {
handler->Finish();
delete handler;
return NULL;
}
return handler;
}
void CUFixtureBase::DefineFunction(dwarf2reader::DIEHandler *parent,
const string &name, Module::Address address,
Module::Address size) {
dwarf2reader::AttributeList func_attrs;
func_attrs.push_back(make_pair(dwarf2reader::DW_AT_name,
dwarf2reader::DW_FORM_strp));
func_attrs.push_back(make_pair(dwarf2reader::DW_AT_low_pc,
dwarf2reader::DW_FORM_addr));
func_attrs.push_back(make_pair(dwarf2reader::DW_AT_high_pc,
dwarf2reader::DW_FORM_addr));
PushBackStrangeAttributes(&func_attrs);
dwarf2reader::DIEHandler *func
= parent->FindChildHandler(incrementing_offset_++,
dwarf2reader::DW_TAG_subprogram,
func_attrs);
ASSERT_TRUE(func != NULL);
func->ProcessAttributeString(dwarf2reader::DW_AT_name,
dwarf2reader::DW_FORM_strp,
name);
func->ProcessAttributeUnsigned(dwarf2reader::DW_AT_low_pc,
dwarf2reader::DW_FORM_addr,
address);
func->ProcessAttributeUnsigned(dwarf2reader::DW_AT_high_pc,
dwarf2reader::DW_FORM_addr,
address + size);
ProcessStrangeAttributes(func);
EXPECT_TRUE(func->EndAttributes());
func->Finish();
delete func;
}
void CUFixtureBase::DeclarationDIE(DIEHandler *parent, uint64 offset,
DwarfTag tag,
const string &name) {
dwarf2reader::AttributeList attrs;
if (!name.empty())
attrs.push_back(make_pair(dwarf2reader::DW_AT_name,
dwarf2reader::DW_FORM_strp));
attrs.push_back(make_pair(dwarf2reader::DW_AT_declaration,
dwarf2reader::DW_FORM_flag));
dwarf2reader::DIEHandler *die = parent->FindChildHandler(offset, tag, attrs);
ASSERT_TRUE(die != NULL);
if (!name.empty())
die->ProcessAttributeString(dwarf2reader::DW_AT_name,
dwarf2reader::DW_FORM_strp,
name);
die->ProcessAttributeUnsigned(dwarf2reader::DW_AT_declaration,
dwarf2reader::DW_FORM_flag,
1);
EXPECT_TRUE(die->EndAttributes());
die->Finish();
delete die;
}
void CUFixtureBase::DefinitionDIE(DIEHandler *parent,
DwarfTag tag,
uint64 specification,
const string &name,
Module::Address address,
Module::Address size) {
dwarf2reader::AttributeList attrs;
attrs.push_back(make_pair(dwarf2reader::DW_AT_specification,
dwarf2reader::DW_FORM_ref4));
if (!name.empty())
attrs.push_back(make_pair(dwarf2reader::DW_AT_name,
dwarf2reader::DW_FORM_strp));
if (size) {
attrs.push_back(make_pair(dwarf2reader::DW_AT_low_pc,
dwarf2reader::DW_FORM_addr));
attrs.push_back(make_pair(dwarf2reader::DW_AT_high_pc,
dwarf2reader::DW_FORM_addr));
}
dwarf2reader::DIEHandler *die
= parent->FindChildHandler(0x6ccfea031a9e6cc9ULL, tag, attrs);
ASSERT_TRUE(die != NULL);
die->ProcessAttributeReference(dwarf2reader::DW_AT_specification,
dwarf2reader::DW_FORM_ref4,
specification);
if (!name.empty())
die->ProcessAttributeString(dwarf2reader::DW_AT_name,
dwarf2reader::DW_FORM_strp,
name);
if (size) {
die->ProcessAttributeUnsigned(dwarf2reader::DW_AT_low_pc,
dwarf2reader::DW_FORM_addr,
address);
die->ProcessAttributeUnsigned(dwarf2reader::DW_AT_high_pc,
dwarf2reader::DW_FORM_addr,
address + size);
}
EXPECT_TRUE(die->EndAttributes());
die->Finish();
delete die;
}
void CUFixtureBase::AbstractInstanceDIE(DIEHandler *parent,
uint64 offset,
DwarfInline type,
uint64 specification,
const string &name,
DwarfForm form) {
dwarf2reader::AttributeList attrs;
if (specification != 0ULL)
attrs.push_back(make_pair(dwarf2reader::DW_AT_specification,
dwarf2reader::DW_FORM_ref4));
attrs.push_back(make_pair(dwarf2reader::DW_AT_inline, form));
if (!name.empty())
attrs.push_back(make_pair(dwarf2reader::DW_AT_name,
dwarf2reader::DW_FORM_strp));
dwarf2reader::DIEHandler *die
= parent->FindChildHandler(offset, dwarf2reader::DW_TAG_subprogram, attrs);
ASSERT_TRUE(die != NULL);
if (specification != 0ULL)
die->ProcessAttributeReference(dwarf2reader::DW_AT_specification,
dwarf2reader::DW_FORM_ref4,
specification);
if (form == dwarf2reader::DW_FORM_sdata) {
die->ProcessAttributeSigned(dwarf2reader::DW_AT_inline, form, type);
} else {
die->ProcessAttributeUnsigned(dwarf2reader::DW_AT_inline, form, type);
}
if (!name.empty())
die->ProcessAttributeString(dwarf2reader::DW_AT_name,
dwarf2reader::DW_FORM_strp,
name);
EXPECT_TRUE(die->EndAttributes());
die->Finish();
delete die;
}
void CUFixtureBase::DefineInlineInstanceDIE(DIEHandler *parent,
const string &name,
uint64 origin,
Module::Address address,
Module::Address size) {
dwarf2reader::AttributeList func_attrs;
if (!name.empty())
func_attrs.push_back(make_pair(dwarf2reader::DW_AT_name,
dwarf2reader::DW_FORM_strp));
func_attrs.push_back(make_pair(dwarf2reader::DW_AT_low_pc,
dwarf2reader::DW_FORM_addr));
func_attrs.push_back(make_pair(dwarf2reader::DW_AT_high_pc,
dwarf2reader::DW_FORM_addr));
func_attrs.push_back(make_pair(dwarf2reader::DW_AT_abstract_origin,
dwarf2reader::DW_FORM_ref4));
PushBackStrangeAttributes(&func_attrs);
dwarf2reader::DIEHandler *func
= parent->FindChildHandler(0x11c70f94c6e87ccdLL,
dwarf2reader::DW_TAG_subprogram,
func_attrs);
ASSERT_TRUE(func != NULL);
if (!name.empty()) {
func->ProcessAttributeString(dwarf2reader::DW_AT_name,
dwarf2reader::DW_FORM_strp,
name);
}
func->ProcessAttributeUnsigned(dwarf2reader::DW_AT_low_pc,
dwarf2reader::DW_FORM_addr,
address);
func->ProcessAttributeUnsigned(dwarf2reader::DW_AT_high_pc,
dwarf2reader::DW_FORM_addr,
address + size);
func->ProcessAttributeReference(dwarf2reader::DW_AT_abstract_origin,
dwarf2reader::DW_FORM_ref4,
origin);
ProcessStrangeAttributes(func);
EXPECT_TRUE(func->EndAttributes());
func->Finish();
delete func;
}
void CUFixtureBase::FillFunctions() {
if (functions_filled_)
return;
module_.GetFunctions(&functions_, functions_.end());
sort(functions_.begin(), functions_.end(),
Module::Function::CompareByAddress);
functions_filled_ = true;
}
void CUFixtureBase::TestFunctionCount(size_t expected) {
FillFunctions();
ASSERT_EQ(expected, functions_.size());
}
void CUFixtureBase::TestFunction(int i, const string &name,
Module::Address address,
Module::Address size) {
FillFunctions();
ASSERT_LT((size_t) i, functions_.size());
Module::Function *function = functions_[i];
EXPECT_EQ(name, function->name);
EXPECT_EQ(address, function->address);
EXPECT_EQ(size, function->size);
EXPECT_EQ(0U, function->parameter_size);
}
void CUFixtureBase::TestLineCount(int i, size_t expected) {
FillFunctions();
ASSERT_LT((size_t) i, functions_.size());
ASSERT_EQ(expected, functions_[i]->lines.size());
}
void CUFixtureBase::TestLine(int i, int j,
Module::Address address, Module::Address size,
const string &filename, int number) {
FillFunctions();
ASSERT_LT((size_t) i, functions_.size());
ASSERT_LT((size_t) j, functions_[i]->lines.size());
Module::Line *line = &functions_[i]->lines[j];
EXPECT_EQ(address, line->address);
EXPECT_EQ(size, line->size);
EXPECT_EQ(filename, line->file->name.c_str());
EXPECT_EQ(number, line->number);
}
// Include caller locations for our test subroutines.
#define TRACE(call) do { SCOPED_TRACE("called from here"); call; } while (0)
#define PushLine(a,b,c,d) TRACE(PushLine((a),(b),(c),(d)))
#define SetLanguage(a) TRACE(SetLanguage(a))
#define StartCU() TRACE(StartCU())
#define DefineFunction(a,b,c,d) TRACE(DefineFunction((a),(b),(c),(d)))
#define DeclarationDIE(a,b,c,d) TRACE(DeclarationDIE((a),(b),(c),(d)))
#define DefinitionDIE(a,b,c,d,e,f) TRACE(DefinitionDIE((a),(b),(c),(d),(e),(f)))
#define TestFunctionCount(a) TRACE(TestFunctionCount(a))
#define TestFunction(a,b,c,d) TRACE(TestFunction((a),(b),(c),(d)))
#define TestLineCount(a,b) TRACE(TestLineCount((a),(b)))
#define TestLine(a,b,c,d,e,f) TRACE(TestLine((a),(b),(c),(d),(e),(f)))
class SimpleCU: public CUFixtureBase, public Test {
};
TEST_F(SimpleCU, OneFunc) {
PushLine(0x938cf8c07def4d34ULL, 0x55592d727f6cd01fLL, "line-file", 246571772);
StartCU();
DefineFunction(&root_handler_, "function1",
0x938cf8c07def4d34ULL, 0x55592d727f6cd01fLL);
root_handler_.Finish();
TestFunctionCount(1);
TestFunction(0, "function1", 0x938cf8c07def4d34ULL, 0x55592d727f6cd01fLL);
TestLineCount(0, 1);
TestLine(0, 0, 0x938cf8c07def4d34ULL, 0x55592d727f6cd01fLL, "line-file",
246571772);
}
TEST_F(SimpleCU, IrrelevantRootChildren) {
StartCU();
dwarf2reader::AttributeList no_attrs;
EXPECT_FALSE(root_handler_
.FindChildHandler(0x7db32bff4e2dcfb1ULL,
dwarf2reader::DW_TAG_lexical_block, no_attrs));
}
TEST_F(SimpleCU, IrrelevantNamedScopeChildren) {
StartCU();
dwarf2reader::AttributeList no_attrs;
DIEHandler *class_A_handler
= StartNamedDIE(&root_handler_, dwarf2reader::DW_TAG_class_type, "class_A");
EXPECT_TRUE(class_A_handler != NULL);
EXPECT_FALSE(class_A_handler
->FindChildHandler(0x02e55999b865e4e9ULL,
dwarf2reader::DW_TAG_lexical_block,
no_attrs));
delete class_A_handler;
}
// Verify that FileContexts can safely be deleted unused.
TEST_F(SimpleCU, UnusedFileContext) {
Module m("module-name", "module-os", "module-arch", "module-id");
DwarfCUToModule::FileContext fc("dwarf-filename", &m);
// Kludge: satisfy reporter_'s expectation.
reporter_.SetCUName("compilation-unit-name");
}
TEST_F(SimpleCU, InlineFunction) {
PushLine(0x1758a0f941b71efbULL, 0x1cf154f1f545e146ULL, "line-file", 75173118);
StartCU();
AbstractInstanceDIE(&root_handler_, 0x1e8dac5d507ed7abULL,
dwarf2reader::DW_INL_inlined, 0, "inline-name");
DefineInlineInstanceDIE(&root_handler_, "", 0x1e8dac5d507ed7abULL,
0x1758a0f941b71efbULL, 0x1cf154f1f545e146ULL);
root_handler_.Finish();
TestFunctionCount(1);
TestFunction(0, "inline-name",
0x1758a0f941b71efbULL, 0x1cf154f1f545e146ULL);
}
TEST_F(SimpleCU, InlineFunctionSignedAttribute) {
PushLine(0x1758a0f941b71efbULL, 0x1cf154f1f545e146ULL, "line-file", 75173118);
StartCU();
AbstractInstanceDIE(&root_handler_, 0x1e8dac5d507ed7abULL,
dwarf2reader::DW_INL_inlined, 0, "inline-name",
dwarf2reader::DW_FORM_sdata);
DefineInlineInstanceDIE(&root_handler_, "", 0x1e8dac5d507ed7abULL,
0x1758a0f941b71efbULL, 0x1cf154f1f545e146ULL);
root_handler_.Finish();
TestFunctionCount(1);
TestFunction(0, "inline-name",
0x1758a0f941b71efbULL, 0x1cf154f1f545e146ULL);
}
// Any DIE with an DW_AT_inline attribute can be cited by
// DW_AT_abstract_origin attributes --- even if the value of the
// DW_AT_inline attribute is DW_INL_not_inlined.
TEST_F(SimpleCU, AbstractOriginNotInlined) {
PushLine(0x2805c4531be6ca0eULL, 0x686b52155a8d4d2cULL, "line-file", 6111581);
StartCU();
AbstractInstanceDIE(&root_handler_, 0x93e9cdad52826b39ULL,
dwarf2reader::DW_INL_not_inlined, 0, "abstract-instance");
DefineInlineInstanceDIE(&root_handler_, "", 0x93e9cdad52826b39ULL,
0x2805c4531be6ca0eULL, 0x686b52155a8d4d2cULL);
root_handler_.Finish();
TestFunctionCount(1);
TestFunction(0, "abstract-instance",
0x2805c4531be6ca0eULL, 0x686b52155a8d4d2cULL);
}
TEST_F(SimpleCU, UnknownAbstractOrigin) {
EXPECT_CALL(reporter_, UnknownAbstractOrigin(_, 1ULL)).WillOnce(Return());
EXPECT_CALL(reporter_, UnnamedFunction(0x11c70f94c6e87ccdLL))
.WillOnce(Return());
PushLine(0x1758a0f941b71efbULL, 0x1cf154f1f545e146ULL, "line-file", 75173118);
StartCU();
AbstractInstanceDIE(&root_handler_, 0x1e8dac5d507ed7abULL,
dwarf2reader::DW_INL_inlined, 0, "inline-name");
DefineInlineInstanceDIE(&root_handler_, "", 1ULL,
0x1758a0f941b71efbULL, 0x1cf154f1f545e146ULL);
root_handler_.Finish();
TestFunctionCount(1);
TestFunction(0, "<name omitted>",
0x1758a0f941b71efbULL, 0x1cf154f1f545e146ULL);
}
TEST_F(SimpleCU, UnnamedFunction) {
EXPECT_CALL(reporter_, UnnamedFunction(_)).WillOnce(Return());
PushLine(0x72b80e41a0ac1d40ULL, 0x537174f231ee181cULL, "line-file", 14044850);
StartCU();
DefineFunction(&root_handler_, "",
0x72b80e41a0ac1d40ULL, 0x537174f231ee181cULL);
root_handler_.Finish();
TestFunctionCount(1);
TestFunction(0, "<name omitted>",
0x72b80e41a0ac1d40ULL, 0x537174f231ee181cULL);
}
// An address range.
struct Range {
Module::Address start, end;
};
// Test data for pairing functions and lines.
struct Situation {
// Two function intervals, and two line intervals.
Range functions[2], lines[2];
// The number of lines we expect to be assigned to each of the
// functions, and the address ranges.
int paired_count[2];
Range paired[2][2];
// The number of functions that are not entirely covered by lines,
// and vice versa.
int uncovered_functions, uncovered_lines;
};
#define PAIRING(func1_start, func1_end, func2_start, func2_end, \
line1_start, line1_end, line2_start, line2_end, \
func1_num_lines, func2_num_lines, \
func1_line1_start, func1_line1_end, \
func1_line2_start, func1_line2_end, \
func2_line1_start, func2_line1_end, \
func2_line2_start, func2_line2_end, \
uncovered_functions, uncovered_lines) \
{ { { func1_start, func1_end }, { func2_start, func2_end } }, \
{ { line1_start, line1_end }, { line2_start, line2_end } }, \
{ func1_num_lines, func2_num_lines }, \
{ { { func1_line1_start, func1_line1_end }, \
{ func1_line2_start, func1_line2_end } }, \
{ { func2_line1_start, func2_line1_end }, \
{ func2_line2_start, func2_line2_end } } }, \
uncovered_functions, uncovered_lines },
Situation situations[] = {
#include "common/testdata/func-line-pairing.h"
};
#undef PAIRING
class FuncLinePairing: public CUFixtureBase,
public TestWithParam<Situation> { };
INSTANTIATE_TEST_CASE_P(AllSituations, FuncLinePairing,
ValuesIn(situations));
TEST_P(FuncLinePairing, Pairing) {
const Situation &s = GetParam();
PushLine(s.lines[0].start,
s.lines[0].end - s.lines[0].start,
"line-file", 67636963);
PushLine(s.lines[1].start,
s.lines[1].end - s.lines[1].start,
"line-file", 67636963);
if (s.uncovered_functions)
EXPECT_CALL(reporter_, UncoveredFunction(_))
.Times(s.uncovered_functions)
.WillRepeatedly(Return());
if (s.uncovered_lines)
EXPECT_CALL(reporter_, UncoveredLine(_))
.Times(s.uncovered_lines)
.WillRepeatedly(Return());
StartCU();
DefineFunction(&root_handler_, "function1",
s.functions[0].start,
s.functions[0].end - s.functions[0].start);
DefineFunction(&root_handler_, "function2",
s.functions[1].start,
s.functions[1].end - s.functions[1].start);
root_handler_.Finish();
TestFunctionCount(2);
TestFunction(0, "function1",
s.functions[0].start,
s.functions[0].end - s.functions[0].start);
TestLineCount(0, s.paired_count[0]);
for (int i = 0; i < s.paired_count[0]; i++)
TestLine(0, i, s.paired[0][i].start,
s.paired[0][i].end - s.paired[0][i].start,
"line-file", 67636963);
TestFunction(1, "function2",
s.functions[1].start,
s.functions[1].end - s.functions[1].start);
TestLineCount(1, s.paired_count[1]);
for (int i = 0; i < s.paired_count[1]; i++)
TestLine(1, i, s.paired[1][i].start,
s.paired[1][i].end - s.paired[1][i].start,
"line-file", 67636963);
}
TEST_F(FuncLinePairing, EmptyCU) {
StartCU();
root_handler_.Finish();
TestFunctionCount(0);
}
TEST_F(FuncLinePairing, LinesNoFuncs) {
PushLine(40, 2, "line-file", 82485646);
EXPECT_CALL(reporter_, UncoveredLine(_)).WillOnce(Return());
StartCU();
root_handler_.Finish();
TestFunctionCount(0);
}
TEST_F(FuncLinePairing, FuncsNoLines) {
EXPECT_CALL(reporter_, UncoveredFunction(_)).WillOnce(Return());
StartCU();
DefineFunction(&root_handler_, "function1", 0x127da12ffcf5c51fULL, 0x1000U);
root_handler_.Finish();
TestFunctionCount(1);
TestFunction(0, "function1", 0x127da12ffcf5c51fULL, 0x1000U);
}
TEST_F(FuncLinePairing, GapThenFunction) {
PushLine(20, 2, "line-file-2", 174314698);
PushLine(10, 2, "line-file-1", 263008005);
StartCU();
DefineFunction(&root_handler_, "function1", 10, 2);
DefineFunction(&root_handler_, "function2", 20, 2);
root_handler_.Finish();
TestFunctionCount(2);
TestFunction(0, "function1", 10, 2);
TestLineCount(0, 1);
TestLine(0, 0, 10, 2, "line-file-1", 263008005);
TestFunction(1, "function2", 20, 2);
TestLineCount(1, 1);
TestLine(1, 0, 20, 2, "line-file-2", 174314698);
}
// If GCC emits padding after one function to align the start of
// the next, then it will attribute the padding instructions to
// the last source line of function (to reduce the size of the
// line number info), but omit it from the DW_AT_{low,high}_pc
// range given in .debug_info (since it costs nothing to be
// precise there). If we did use at least some of the line
// we're about to skip, then assume this is what happened, and
// don't warn.
TEST_F(FuncLinePairing, GCCAlignmentStretch) {
PushLine(10, 10, "line-file", 63351048);
PushLine(20, 10, "line-file", 61661044);
StartCU();
DefineFunction(&root_handler_, "function1", 10, 5);
// five-byte gap between functions, covered by line 63351048.
// This should not elicit a warning.
DefineFunction(&root_handler_, "function2", 20, 10);
root_handler_.Finish();
TestFunctionCount(2);
TestFunction(0, "function1", 10, 5);
TestLineCount(0, 1);
TestLine(0, 0, 10, 5, "line-file", 63351048);
TestFunction(1, "function2", 20, 10);
TestLineCount(1, 1);
TestLine(1, 0, 20, 10, "line-file", 61661044);
}
// Unfortunately, neither the DWARF parser's handler interface nor the
// DIEHandler interface is capable of expressing a function that abuts
// the end of the address space: the high_pc value looks like zero.
TEST_F(FuncLinePairing, LineAtEndOfAddressSpace) {
PushLine(0xfffffffffffffff0ULL, 16, "line-file", 63351048);
EXPECT_CALL(reporter_, UncoveredLine(_)).WillOnce(Return());
StartCU();
DefineFunction(&root_handler_, "function1", 0xfffffffffffffff0ULL, 6);
DefineFunction(&root_handler_, "function2", 0xfffffffffffffffaULL, 5);
root_handler_.Finish();
TestFunctionCount(2);
TestFunction(0, "function1", 0xfffffffffffffff0ULL, 6);
TestLineCount(0, 1);