llvm.org GIT mirror llvm / b001472
[DWARF] Fix DWARFVerifier::DieRangeInfo::contains It didn't handle empty LHS correctly. If two ranges of LHS were contiguous and jointly contained one range of RHS, it could also be incorrect. DWARFAddressRange::contains can be removed and its tests can be merged into DWARFVerifier::DieRangeInfo::contains git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@358387 91177308-0d34-0410-b5e6-96231b3b80d8 Fangrui Song 6 months ago
3 changed file(s) with 49 addition(s) and 91 deletion(s). Raw diff Collapse all Expand all
4141 return LowPC < RHS.HighPC && RHS.LowPC < HighPC;
4242 }
4343
44 /// Returns true if [LowPC, HighPC) fully contains [RHS.LowPC, RHS.HighPC).
45 bool contains(const DWARFAddressRange &RHS) const {
46 assert(valid() && RHS.valid());
47 return LowPC <= RHS.LowPC && RHS.HighPC <= HighPC;
48 }
49
5044 void dump(raw_ostream &OS, uint32_t AddressSize,
5145 DIDumpOptions DumpOpts = {}) const;
5246 };
5959 }
6060
6161 bool DWARFVerifier::DieRangeInfo::contains(const DieRangeInfo &RHS) const {
62 // Both list of ranges are sorted so we can make this fast.
63
64 if (Ranges.empty() || RHS.Ranges.empty())
65 return false;
66
67 // Since the ranges are sorted we can advance where we start searching with
68 // this object's ranges as we traverse RHS.Ranges.
69 auto End = Ranges.end();
70 auto Iter = findRange(RHS.Ranges.front());
71
72 // Now linearly walk the ranges in this object and see if they contain each
73 // ranges from RHS.Ranges.
74 for (const auto &R : RHS.Ranges) {
75 while (Iter != End) {
76 if (Iter->contains(R))
77 break;
78 ++Iter;
79 }
80 if (Iter == End)
62 auto I1 = Ranges.begin(), E1 = Ranges.end();
63 auto I2 = RHS.Ranges.begin(), E2 = RHS.Ranges.end();
64 if (I2 == E2)
65 return true;
66
67 DWARFAddressRange R = *I2;
68 while (I1 != E1) {
69 bool Covered = I1->LowPC <= R.LowPC;
70 if (R.LowPC == R.HighPC || (Covered && R.HighPC <= I1->HighPC)) {
71 if (++I2 == E2)
72 return true;
73 R = *I2;
74 continue;
75 }
76 if (!Covered)
8177 return false;
82 }
83 return true;
78 if (R.LowPC < I1->HighPC)
79 R.LowPC = I1->HighPC;
80 ++I1;
81 }
82 return false;
8483 }
8584
8685 bool DWARFVerifier::DieRangeInfo::intersects(const DieRangeInfo &RHS) const {
29822982 VerifySuccess(*DwarfContext);
29832983 }
29842984
2985 TEST(DWARFDebugInfo, TestDwarfRangesContains) {
2986 DWARFAddressRange R(0x10, 0x20);
2987
2988 //----------------------------------------------------------------------
2989 // Test ranges that start before R...
2990 //----------------------------------------------------------------------
2991 // Other range ends before start of R
2992 ASSERT_FALSE(R.contains({0x0f, 0x10}));
2993 // Other range end address is start of a R
2994 ASSERT_FALSE(R.contains({0x0f, 0x11}));
2995 // Other range end address is at and of R
2996 ASSERT_FALSE(R.contains({0x0f, 0x20}));
2997 // Other range end address is past end of R
2998 ASSERT_FALSE(R.contains({0x0f, 0x40}));
2999
3000 //----------------------------------------------------------------------
2985 TEST(DWARFDebugInfo, TestDWARFDieRangeInfoContains) {
2986 DWARFVerifier::DieRangeInfo Empty;
2987 ASSERT_TRUE(Empty.contains(Empty));
2988
2989 DWARFVerifier::DieRangeInfo Ranges(
2990 {{0x10, 0x20}, {0x30, 0x40}, {0x40, 0x50}});
2991
2992 ASSERT_TRUE(Ranges.contains(Empty));
2993 ASSERT_FALSE(Ranges.contains({{{0x0f, 0x10}}}));
2994 ASSERT_FALSE(Ranges.contains({{{0x0f, 0x20}}}));
2995 ASSERT_FALSE(Ranges.contains({{{0x0f, 0x21}}}));
2996
30012997 // Test ranges that start at R's start address
3002 //----------------------------------------------------------------------
3003 // Ensure empty ranges matches
3004 ASSERT_TRUE(R.contains({0x10, 0x10}));
3005 // 1 byte of Range
3006 ASSERT_TRUE(R.contains({0x10, 0x11}));
3007 // same as Range
3008 ASSERT_TRUE(R.contains({0x10, 0x20}));
3009 // 1 byte past Range
3010 ASSERT_FALSE(R.contains({0x10, 0x21}));
3011
3012 //----------------------------------------------------------------------
3013 // Test ranges that start inside Range
3014 //----------------------------------------------------------------------
3015 // empty in range
3016 ASSERT_TRUE(R.contains({0x11, 0x11}));
3017 // all in Range
3018 ASSERT_TRUE(R.contains({0x11, 0x1f}));
3019 // ends at end of Range
3020 ASSERT_TRUE(R.contains({0x11, 0x20}));
3021 // ends past Range
3022 ASSERT_FALSE(R.contains({0x11, 0x21}));
3023
3024 //----------------------------------------------------------------------
2998 ASSERT_TRUE(Ranges.contains({{{0x10, 0x10}}}));
2999 ASSERT_TRUE(Ranges.contains({{{0x10, 0x11}}}));
3000 ASSERT_TRUE(Ranges.contains({{{0x10, 0x20}}}));
3001 ASSERT_FALSE(Ranges.contains({{{0x10, 0x21}}}));
3002
3003 ASSERT_TRUE(Ranges.contains({{{0x11, 0x12}}}));
3004
30253005 // Test ranges that start at last bytes of Range
3026 //----------------------------------------------------------------------
3027 // ends at end of Range
3028 ASSERT_TRUE(R.contains({0x1f, 0x20}));
3029 // ends past Range
3030 ASSERT_FALSE(R.contains({0x1f, 0x21}));
3031
3032 //----------------------------------------------------------------------
3006 ASSERT_TRUE(Ranges.contains({{{0x1f, 0x20}}}));
3007 ASSERT_FALSE(Ranges.contains({{{0x1f, 0x21}}}));
3008
30333009 // Test ranges that start after Range
3034 //----------------------------------------------------------------------
3035 // empty considered in Range
3036 ASSERT_TRUE(R.contains({0x20, 0x20}));
3037 // valid past Range
3038 ASSERT_FALSE(R.contains({0x20, 0x21}));
3039 }
3040
3041 TEST(DWARFDebugInfo, TestDWARFDieRangeInfoContains) {
3042 DWARFVerifier::DieRangeInfo Ranges({{0x10, 0x20}, {0x30, 0x40}});
3043
3044 ASSERT_FALSE(Ranges.contains({{{0x0f, 0x10}}}));
3045 ASSERT_FALSE(Ranges.contains({{{0x20, 0x30}}}));
3046 ASSERT_FALSE(Ranges.contains({{{0x40, 0x41}}}));
3047 ASSERT_TRUE(Ranges.contains({{{0x10, 0x20}}}));
3048 ASSERT_TRUE(Ranges.contains({{{0x11, 0x12}}}));
3049 ASSERT_TRUE(Ranges.contains({{{0x1f, 0x20}}}));
3050 ASSERT_TRUE(Ranges.contains({{{0x30, 0x40}}}));
3010 ASSERT_TRUE(Ranges.contains({{{0x20, 0x20}}}));
3011 ASSERT_FALSE(Ranges.contains({{{0x20, 0x21}}}));
3012
30513013 ASSERT_TRUE(Ranges.contains({{{0x31, 0x32}}}));
30523014 ASSERT_TRUE(Ranges.contains({{{0x3f, 0x40}}}));
30533015 ASSERT_TRUE(Ranges.contains({{{0x10, 0x20}, {0x30, 0x40}}}));
30603022 {0x31, 0x32},
30613023 {0x32, 0x33}}}));
30623024 ASSERT_FALSE(Ranges.contains(
3063 {{{0x11, 0x12}, {0x12, 0x13}, {0x31, 0x32}, {0x32, 0x41}}}));
3025 {{{0x11, 0x12}, {0x12, 0x13}, {0x31, 0x32}, {0x32, 0x51}}}));
3026 ASSERT_TRUE(Ranges.contains({{{0x11, 0x12}, {0x30, 0x50}}}));
3027 ASSERT_FALSE(Ranges.contains({{{0x30, 0x51}}}));
3028 ASSERT_FALSE(Ranges.contains({{{0x50, 0x51}}}));
30643029 }
30653030
30663031 namespace {