1 // Copyright 2006 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 // * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 // contained_range_map-inl.h: Hierarchically-organized range map implementation.
30 //
31 // See contained_range_map.h for documentation.
32 //
33 // Author: Mark Mentovai
34
35 #ifndef PROCESSOR_CONTAINED_RANGE_MAP_INL_H__
36 #define PROCESSOR_CONTAINED_RANGE_MAP_INL_H__
37
38 #include "processor/contained_range_map.h"
39
40 #include <assert.h>
41
42 #include "processor/logging.h"
43
44
45 namespace google_breakpad {
46
47
48 template<typename AddressType, typename EntryType>
~ContainedRangeMap()49 ContainedRangeMap<AddressType, EntryType>::~ContainedRangeMap() {
50 // Clear frees the children pointed to by the map, and frees the map itself.
51 Clear();
52 }
53
54
55 template<typename AddressType, typename EntryType>
StoreRange(const AddressType & base,const AddressType & size,const EntryType & entry)56 bool ContainedRangeMap<AddressType, EntryType>::StoreRange(
57 const AddressType& base, const AddressType& size, const EntryType& entry) {
58 AddressType high = base + size - 1;
59
60 // Check for undersize or overflow.
61 if (size <= 0 || high < base) {
62 //TODO(nealsid) We are commenting this out in order to prevent
63 // excessive logging. We plan to move to better logging as this
64 // failure happens quite often and is expected(see comment in
65 // basic_source_line_resolver.cc:671).
66 // BPLOG(INFO) << "StoreRange failed, " << HexString(base) << "+"
67 // << HexString(size) << ", " << HexString(high);
68 return false;
69 }
70
71 if (!map_)
72 map_ = new AddressToRangeMap();
73
74 MapIterator iterator_base = map_->lower_bound(base);
75 MapIterator iterator_high = map_->lower_bound(high);
76 MapIterator iterator_end = map_->end();
77
78 if (iterator_base == iterator_high && iterator_base != iterator_end &&
79 base >= iterator_base->second->base_) {
80 // The new range is entirely within an existing child range.
81
82 // If the new range's geometry is exactly equal to an existing child
83 // range's, it violates the containment rules, and an attempt to store
84 // it must fail. iterator_base->first contains the key, which was the
85 // containing child's high address.
86 if (!allow_equal_range_ && iterator_base->second->base_ == base &&
87 iterator_base->first == high) {
88 // TODO(nealsid): See the TODO above on why this is commented out.
89 // BPLOG(INFO) << "StoreRange failed, identical range is already "
90 // "present: " << HexString(base) << "+" <<
91 // HexString(size);
92 return false;
93 }
94
95 // Pass the new range on to the child to attempt to store.
96 return iterator_base->second->StoreRange(base, size, entry);
97 }
98
99 // iterator_high might refer to an irrelevant range: one whose base address
100 // is higher than the new range's high address. Set contains_high to true
101 // only if iterator_high refers to a range that is at least partially
102 // within the new range.
103 bool contains_high = iterator_high != iterator_end &&
104 high >= iterator_high->second->base_;
105
106 // If the new range encompasses any existing child ranges, it must do so
107 // fully. Partial containment isn't allowed.
108 if ((iterator_base != iterator_end && base > iterator_base->second->base_) ||
109 (contains_high && high < iterator_high->first)) {
110 // TODO(mmentovai): Some symbol files will trip this check frequently
111 // on STACK lines. Too many messages will be produced. These are more
112 // suitable for a DEBUG channel than an INFO channel.
113 // BPLOG(INFO) << "StoreRange failed, new range partially contains "
114 // "existing range: " << HexString(base) << "+" <<
115 // HexString(size);
116 return false;
117 }
118
119 // When copying and erasing contained ranges, the "end" iterator needs to
120 // point one past the last item of the range to copy. If contains_high is
121 // false, the iterator's already in the right place; the increment is safe
122 // because contains_high can't be true if iterator_high == iterator_end.
123 if (contains_high)
124 ++iterator_high;
125
126 // Optimization: if the iterators are equal, no child ranges would be
127 // moved. Create the new child range with a NULL map to conserve space
128 // in leaf nodes, of which there will be many.
129 AddressToRangeMap* child_map = NULL;
130
131 if (iterator_base != iterator_high) {
132 // The children of this range that are contained by the new range must
133 // be transferred over to the new range. Create the new child range map
134 // and copy the pointers to range maps it should contain into it.
135 child_map = new AddressToRangeMap(iterator_base, iterator_high);
136
137 // Remove the copied child pointers from this range's map of children.
138 map_->erase(iterator_base, iterator_high);
139 }
140
141 // Store the new range in the map by its high address. Any children that
142 // the new child range contains were formerly children of this range but
143 // are now this range's grandchildren. Ownership of these is transferred
144 // to the new child range.
145 ContainedRangeMap* new_child =
146 new ContainedRangeMap(base, entry, child_map, allow_equal_range_);
147
148 map_->insert(MapValue(high, new_child));
149 return true;
150 }
151
152
153 template<typename AddressType, typename EntryType>
RetrieveRange(const AddressType & address,EntryType * entry)154 bool ContainedRangeMap<AddressType, EntryType>::RetrieveRange(
155 const AddressType& address, EntryType* entry) const {
156 BPLOG_IF(ERROR, !entry) << "ContainedRangeMap::RetrieveRange requires "
157 "|entry|";
158 assert(entry);
159
160 // If nothing was ever stored, then there's nothing to retrieve.
161 if (!map_)
162 return false;
163
164 // Get an iterator to the child range whose high address is equal to or
165 // greater than the supplied address. If the supplied address is higher
166 // than all of the high addresses in the range, then this range does not
167 // contain a child at address, so return false. If the supplied address
168 // is lower than the base address of the child range, then it is not within
169 // the child range, so return false.
170 MapConstIterator iterator = map_->lower_bound(address);
171 if (iterator == map_->end() || address < iterator->second->base_)
172 return false;
173
174 // The child in iterator->second contains the specified address. Find out
175 // if it has a more-specific descendant that also contains it. If it does,
176 // it will set |entry| appropriately. If not, set |entry| to the child.
177 if (!iterator->second->RetrieveRange(address, entry))
178 *entry = iterator->second->entry_;
179
180 return true;
181 }
182
183 template <typename AddressType, typename EntryType>
RetrieveRanges(const AddressType & address,std::vector<const EntryType * > & entries)184 bool ContainedRangeMap<AddressType, EntryType>::RetrieveRanges(
185 const AddressType& address,
186 std::vector<const EntryType*>& entries) const {
187 // If nothing was ever stored, then there's nothing to retrieve.
188 if (!map_)
189 return false;
190 MapIterator iterator = map_->lower_bound(address);
191 if (iterator == map_->end() || address < iterator->second->base_)
192 return false;
193 iterator->second->RetrieveRanges(address, entries);
194 entries.push_back(&iterator->second->entry_);
195 return true;
196 }
197
198 template<typename AddressType, typename EntryType>
Clear()199 void ContainedRangeMap<AddressType, EntryType>::Clear() {
200 if (map_) {
201 MapConstIterator end = map_->end();
202 for (MapConstIterator child = map_->begin(); child != end; ++child)
203 delete child->second;
204
205 delete map_;
206 map_ = NULL;
207 }
208 }
209
210
211 } // namespace google_breakpad
212
213
214 #endif // PROCESSOR_CONTAINED_RANGE_MAP_INL_H__
215