OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/quic/crypto/strike_register.h" | |
6 | |
7 #include "base/logging.h" | |
8 | |
9 using std::pair; | |
10 using std::set; | |
11 using std::vector; | |
12 | |
13 namespace net { | |
14 | |
15 // static | |
16 const uint32 StrikeRegister::kExternalNodeSize = 24; | |
17 // static | |
18 const uint32 StrikeRegister::kNil = (1 << 31) | 1; | |
19 // static | |
20 const uint32 StrikeRegister::kExternalFlag = 1 << 23; | |
21 | |
22 // InternalNode represents a non-leaf node in the critbit tree. See the comment | |
23 // in the .h file for details. | |
24 class StrikeRegister::InternalNode { | |
25 public: | |
26 void SetChild(unsigned direction, uint32 child) { | |
27 data_[direction] = (data_[direction] & 0xff) | (child << 8); | |
28 } | |
29 | |
30 void SetCritByte(uint8 critbyte) { | |
31 data_[0] &= 0xffffff00; | |
32 data_[0] |= critbyte; | |
33 } | |
34 | |
35 void SetOtherBits(uint8 otherbits) { | |
36 data_[1] &= 0xffffff00; | |
37 data_[1] |= otherbits; | |
38 } | |
39 | |
40 void SetNextPtr(uint32 next) { | |
41 data_[0] = next; | |
42 } | |
43 | |
44 uint32 next() const { | |
45 return data_[0]; | |
46 } | |
47 | |
48 uint32 child(unsigned n) const { | |
49 return data_[n] >> 8; | |
50 } | |
51 | |
52 uint8 critbyte() const { | |
53 return data_[0]; | |
54 } | |
55 | |
56 uint8 otherbits() const { | |
57 return data_[1]; | |
58 } | |
59 | |
60 // These bytes are organised thus: | |
61 // <24 bits> left child | |
62 // <8 bits> crit-byte | |
63 // <24 bits> right child | |
64 // <8 bits> other-bits | |
65 uint32 data_[2]; | |
66 }; | |
67 | |
68 StrikeRegister::StrikeRegister(unsigned max_entries, | |
69 uint32 current_time, | |
70 uint32 window_secs, | |
71 const uint8 orbit[8]) | |
72 : max_entries_(max_entries), | |
73 window_secs_(window_secs), | |
74 // The horizon is initially set |window_secs| into the future because, if | |
75 // we just crashed, then we may have accepted nonces in the span | |
76 // [current_time...current_time+window_secs) and so we conservatively | |
77 // reject the whole timespan. | |
78 horizon_(current_time + window_secs) { | |
79 memcpy(orbit_, orbit, sizeof(orbit_)); | |
80 | |
81 // We only have 23 bits of index available. | |
82 CHECK_LT(max_entries, 1u << 23); | |
83 CHECK_GT(max_entries, 1u); // There must be at least two entries. | |
84 CHECK_EQ(sizeof(InternalNode), 8u); // in case of compiler changes. | |
85 internal_nodes_ = new InternalNode[max_entries]; | |
86 external_nodes_.reset(new uint8[kExternalNodeSize * max_entries]); | |
87 | |
88 Reset(); | |
89 } | |
90 | |
91 StrikeRegister::~StrikeRegister() { | |
92 delete[] internal_nodes_; | |
93 } | |
94 | |
95 void StrikeRegister::Reset() { | |
96 // Thread a free list through all of the internal nodes. | |
97 internal_node_free_head_ = 0; | |
98 for (unsigned i = 0; i < max_entries_ - 1; i++) | |
99 internal_nodes_[i].SetNextPtr(i + 1); | |
100 internal_nodes_[max_entries_ - 1].SetNextPtr(kNil); | |
101 | |
102 // Also thread a free list through the external nodes. | |
103 external_node_free_head_ = 0; | |
104 for (unsigned i = 0; i < max_entries_ - 1; i++) | |
105 external_node_next_ptr(i) = i + 1; | |
106 external_node_next_ptr(max_entries_ - 1) = kNil; | |
107 | |
108 // This is the root of the tree. | |
109 internal_node_head_ = kNil; | |
110 } | |
111 | |
112 bool StrikeRegister::Insert(const uint8 nonce[32], | |
113 const uint32 current_time) { | |
114 // If current_time is very small or very large then we assume that we have | |
115 // just rolled over / are about to roll over and it's 2038 or 2106. Since | |
116 // we don't deal with this situation we flush everything and start over. | |
117 // This means that we reject everything for 2 * |window_secs_| every 68 | |
118 // years. | |
119 if (current_time < window_secs_ || | |
120 current_time + window_secs_ < current_time) { | |
121 if (internal_node_head_ != kNil) { | |
122 Reset(); | |
123 } | |
124 horizon_ = current_time; | |
125 return false; | |
126 } | |
127 | |
128 // Check to see if the orbit is correct. | |
129 if (memcmp(nonce + sizeof(current_time), orbit_, sizeof(orbit_))) { | |
130 return false; | |
131 } | |
132 const uint32 nonce_time = TimeFromBytes(nonce); | |
133 // We have dropped one or more nonces with a time value of |horizon_|, so | |
134 // we have to reject anything with a timestamp less than or equal to that. | |
135 if (nonce_time <= horizon_) { | |
136 return false; | |
137 } | |
138 | |
139 // Check that the timestamp is in the current window. | |
140 if (nonce_time < (current_time - window_secs_) || | |
141 nonce_time > (current_time + window_secs_)) { | |
142 return false; | |
143 } | |
144 | |
145 // We strip the orbit out of the nonce. | |
146 uint8 value[24]; | |
147 memcpy(value, nonce, sizeof(current_time)); | |
148 memcpy(value + sizeof(current_time), | |
149 nonce + sizeof(current_time) + sizeof(orbit_), | |
150 sizeof(value) - sizeof(current_time)); | |
151 | |
152 // Find the best match to |value| in the crit-bit tree. The best match is | |
153 // simply the value which /could/ match |value|, if any does, so we still | |
154 // need a memcmp to check. | |
155 uint32 best_match_index = BestMatch(value); | |
156 if (best_match_index == kNil) { | |
157 // Empty tree. Just insert the new value at the root. | |
158 uint32 index = GetFreeExternalNode(); | |
159 memcpy(external_node(index), value, sizeof(value)); | |
160 internal_node_head_ = (index | kExternalFlag) << 8; | |
161 return true; | |
162 } | |
163 | |
164 const uint8* best_match = external_node(best_match_index); | |
165 if (memcmp(best_match, value, sizeof(value)) == 0) { | |
166 // We found the value in the tree. | |
167 return false; | |
168 } | |
169 | |
170 // We are going to insert a new entry into the tree, so get the nodes now. | |
171 uint32 internal_node_index = GetFreeInternalNode(); | |
172 uint32 external_node_index = GetFreeExternalNode(); | |
173 | |
174 // If we just evicted the best match, then we have to try and match again. | |
175 // We know that we didn't just empty the tree because we require that | |
176 // max_entries_ >= 2. Also, we know that it doesn't match because, if it | |
177 // did, it would have been returned previously. | |
178 if (external_node_index == best_match_index) { | |
179 best_match_index = BestMatch(value); | |
180 best_match = external_node(best_match_index); | |
181 } | |
182 | |
183 // Now we need to find the first bit where we differ from |best_match|. | |
184 unsigned differing_byte; | |
185 uint8 new_other_bits; | |
186 for (differing_byte = 0; differing_byte < sizeof(value); differing_byte++) { | |
187 new_other_bits = value[differing_byte] ^ best_match[differing_byte]; | |
188 if (new_other_bits) { | |
189 break; | |
190 } | |
191 } | |
192 | |
193 // Once we have the XOR the of first differing byte in new_other_bits we need | |
194 // to find the most significant differing bit. We could do this with a simple | |
195 // for loop, testing bits 7..0. Instead we fold the bits so that we end up | |
196 // with a byte where all the bits below the most significant one, are set. | |
197 new_other_bits |= new_other_bits >> 1; | |
198 new_other_bits |= new_other_bits >> 2; | |
199 new_other_bits |= new_other_bits >> 4; | |
200 // Now this bit trick results in all the bits set, except the original | |
201 // most-significant one. | |
202 new_other_bits = (new_other_bits & ~(new_other_bits >> 1)) ^ 255; | |
203 | |
204 // Consider the effect of ORing against |new_other_bits|. If |value| did not | |
205 // have the critical bit set, the result is the same as |new_other_bits|. If | |
206 // it did, the result is all ones. | |
207 | |
208 unsigned newdirection; | |
209 if ((new_other_bits | value[differing_byte]) == 0xff) { | |
210 newdirection = 1; | |
211 } else { | |
212 newdirection = 0; | |
213 } | |
214 | |
215 memcpy(external_node(external_node_index), value, sizeof(value)); | |
216 InternalNode* inode = &internal_nodes_[internal_node_index]; | |
217 | |
218 inode->SetChild(newdirection, external_node_index | kExternalFlag); | |
219 inode->SetCritByte(differing_byte); | |
220 inode->SetOtherBits(new_other_bits); | |
221 | |
222 // |where_index| is a pointer to the uint32 which needs to be updated in | |
223 // order to insert the new internal node into the tree. The internal nodes | |
224 // store the child indexes in the top 24-bits of a 32-bit word and, to keep | |
225 // the code simple, we define that |internal_node_head_| is organised the | |
226 // same way. | |
227 DCHECK_EQ(internal_node_head_ & 0xff, 0u); | |
228 uint32* where_index = &internal_node_head_; | |
229 while (((*where_index >> 8) & kExternalFlag) == 0) { | |
230 InternalNode* node = &internal_nodes_[*where_index >> 8]; | |
231 if (node->critbyte() > differing_byte) { | |
232 break; | |
233 } | |
234 if (node->critbyte() == differing_byte && | |
235 node->otherbits() > new_other_bits) { | |
236 break; | |
237 } | |
238 if (node->critbyte() == differing_byte && | |
239 node->otherbits() == new_other_bits) { | |
240 CHECK(false); | |
241 } | |
242 | |
243 uint8 c = value[node->critbyte()]; | |
244 const int direction = | |
245 (1 + static_cast<unsigned>(node->otherbits() | c)) >> 8; | |
246 where_index = &node->data_[direction]; | |
247 } | |
248 | |
249 inode->SetChild(newdirection ^ 1, *where_index >> 8); | |
250 *where_index = (*where_index & 0xff) | (internal_node_index << 8); | |
251 | |
252 return true; | |
253 } | |
254 | |
255 void StrikeRegister::Validate() { | |
256 set<uint32> free_internal_nodes; | |
257 for (uint32 i = internal_node_free_head_; i != kNil; | |
258 i = internal_nodes_[i].next()) { | |
259 CHECK_LT(i, max_entries_); | |
260 CHECK_EQ(free_internal_nodes.count(i), 0u); | |
261 free_internal_nodes.insert(i); | |
262 } | |
263 | |
264 set<uint32> free_external_nodes; | |
265 for (uint32 i = external_node_free_head_; i != kNil; | |
266 i = external_node_next_ptr(i)) { | |
267 CHECK_LT(i, max_entries_); | |
268 CHECK_EQ(free_external_nodes.count(i), 0u); | |
269 free_external_nodes.insert(i); | |
270 } | |
271 | |
272 set<uint32> used_external_nodes; | |
273 set<uint32> used_internal_nodes; | |
274 | |
275 if (internal_node_head_ != kNil && | |
276 ((internal_node_head_ >> 8) & kExternalFlag) == 0) { | |
277 vector<pair<unsigned, bool> > bits; | |
278 ValidateTree(internal_node_head_ >> 8, -1, bits, free_internal_nodes, | |
279 free_external_nodes, &used_internal_nodes, | |
280 &used_external_nodes); | |
281 } | |
282 } | |
283 | |
284 // static | |
285 uint32 StrikeRegister::TimeFromBytes(const uint8 d[4]) { | |
286 return static_cast<uint32>(d[0]) << 24 | | |
287 static_cast<uint32>(d[1]) << 16 | | |
288 static_cast<uint32>(d[2]) << 8 | | |
289 static_cast<uint32>(d[3]); | |
290 } | |
291 | |
292 uint32 StrikeRegister::BestMatch(const uint8 v[24]) const { | |
293 if (internal_node_head_ == kNil) { | |
294 return kNil; | |
295 } | |
296 | |
297 uint32 next = internal_node_head_ >> 8; | |
298 while ((next & kExternalFlag) == 0) { | |
299 InternalNode* node = &internal_nodes_[next]; | |
300 uint8 b = v[node->critbyte()]; | |
301 unsigned direction = | |
302 (1 + static_cast<unsigned>(node->otherbits() | b)) >> 8; | |
303 next = node->child(direction); | |
304 } | |
305 | |
306 return next & ~kExternalFlag; | |
307 } | |
308 | |
309 uint32& StrikeRegister::external_node_next_ptr(unsigned i) { | |
310 return *reinterpret_cast<uint32*>(&external_nodes_[i * kExternalNodeSize]); | |
311 } | |
312 | |
313 uint8* StrikeRegister::external_node(unsigned i) { | |
314 return &external_nodes_[i * kExternalNodeSize]; | |
315 } | |
316 | |
317 uint32 StrikeRegister::GetFreeExternalNode() { | |
318 uint32 index = external_node_free_head_; | |
319 if (index == kNil) { | |
320 DropNode(); | |
321 return GetFreeExternalNode(); | |
322 } | |
323 | |
324 external_node_free_head_ = external_node_next_ptr(index); | |
325 return index; | |
326 } | |
327 | |
328 uint32 StrikeRegister::GetFreeInternalNode() { | |
329 uint32 index = internal_node_free_head_; | |
330 if (index == kNil) { | |
331 DropNode(); | |
332 return GetFreeInternalNode(); | |
333 } | |
334 | |
335 internal_node_free_head_ = internal_nodes_[index].next(); | |
336 return index; | |
337 } | |
338 | |
339 void StrikeRegister::DropNode() { | |
340 // DropNode should never be called on an empty tree. | |
341 DCHECK(internal_node_head_ != kNil); | |
342 | |
343 // An internal node in a crit-bit tree always has exactly two children. | |
344 // This means that, if we are removing an external node (which is one of | |
345 // those children), then we also need to remove an internal node. In order | |
346 // to do that we keep pointers to the parent (wherep) and grandparent | |
347 // (whereq) when walking down the tree. | |
348 | |
349 uint32 p = internal_node_head_ >> 8, *wherep = &internal_node_head_, | |
350 *whereq = NULL; | |
351 while ((p & kExternalFlag) == 0) { | |
352 whereq = wherep; | |
353 InternalNode* inode = &internal_nodes_[p]; | |
354 // We always go left, towards the smallest element, exploiting the fact | |
355 // that the timestamp is big-endian and at the start of the value. | |
356 wherep = &inode->data_[0]; | |
357 p = (*wherep) >> 8; | |
358 } | |
359 | |
360 const uint32 ext_index = p & ~kExternalFlag; | |
361 const uint8* ext_node = external_node(ext_index); | |
362 horizon_ = TimeFromBytes(ext_node); | |
363 | |
364 if (!whereq) { | |
365 // We are removing the last element in a tree. | |
366 internal_node_head_ = kNil; | |
367 FreeExternalNode(ext_index); | |
368 return; | |
369 } | |
370 | |
371 // |wherep| points to the left child pointer in the parent so we can add | |
372 // one and dereference to get the right child. | |
373 const uint32 other_child = wherep[1]; | |
374 FreeInternalNode((*whereq) >> 8); | |
375 *whereq = (*whereq & 0xff) | (other_child & 0xffffff00); | |
376 FreeExternalNode(ext_index); | |
377 } | |
378 | |
379 void StrikeRegister::FreeExternalNode(uint32 index) { | |
380 external_node_next_ptr(index) = external_node_free_head_; | |
381 external_node_free_head_ = index; | |
382 } | |
383 | |
384 void StrikeRegister::FreeInternalNode(uint32 index) { | |
385 internal_nodes_[index].SetNextPtr(internal_node_free_head_); | |
386 internal_node_free_head_ = index; | |
387 } | |
388 | |
389 void StrikeRegister::ValidateTree( | |
390 uint32 internal_node, | |
391 int last_bit, | |
392 const vector<pair<unsigned, bool> >& bits, | |
393 const set<uint32>& free_internal_nodes, | |
394 const set<uint32>& free_external_nodes, | |
395 set<uint32>* used_internal_nodes, | |
396 set<uint32>* used_external_nodes) { | |
397 CHECK_LT(internal_node, max_entries_); | |
398 const InternalNode* i = &internal_nodes_[internal_node]; | |
399 unsigned bit = 0; | |
400 switch (i->otherbits()) { | |
401 case 0xff & ~(1 << 7): | |
402 bit = 0; | |
403 break; | |
404 case 0xff & ~(1 << 6): | |
405 bit = 1; | |
406 break; | |
407 case 0xff & ~(1 << 5): | |
408 bit = 2; | |
409 break; | |
410 case 0xff & ~(1 << 4): | |
411 bit = 3; | |
412 break; | |
413 case 0xff & ~(1 << 3): | |
414 bit = 4; | |
415 break; | |
416 case 0xff & ~(1 << 2): | |
417 bit = 5; | |
418 break; | |
419 case 0xff & ~(1 << 1): | |
420 bit = 6; | |
421 break; | |
422 case 0xff & ~1: | |
423 bit = 7; | |
424 break; | |
425 default: | |
426 CHECK(false); | |
427 } | |
428 | |
429 bit += 8 * i->critbyte(); | |
430 if (last_bit > -1) { | |
431 CHECK_GT(bit, static_cast<unsigned>(last_bit)); | |
432 } | |
433 | |
434 CHECK_EQ(free_internal_nodes.count(internal_node), 0u); | |
435 | |
436 for (unsigned child = 0; child < 2; child++) { | |
437 if (i->child(child) & kExternalFlag) { | |
438 uint32 ext = i->child(child) & ~kExternalFlag; | |
439 DCHECK_EQ(free_external_nodes.count(ext), 0u); | |
agl
2013/04/08 14:38:52
ValidateTree is supposed to check the internal con
Ryan Hamilton
2013/04/08 16:19:03
Out of curiosity, is this code in the internal ver
agl
2013/04/08 16:35:15
ValidateTree is in the internal version, yes. It's
ramant (doing other things)
2013/04/08 16:42:09
Done.
| |
440 DCHECK_EQ(used_external_nodes->count(ext), 0u); | |
441 used_external_nodes->insert(ext); | |
442 const uint8* bytes = external_node(ext); | |
443 for (vector<pair<unsigned, bool> >::const_iterator | |
444 i = bits.begin(); i != bits.end(); i++) { | |
445 unsigned byte = i->first / 8; | |
446 DCHECK_LE(byte, 0xffu); | |
447 unsigned bit = i->first % 8; | |
448 static const uint8 kMasks[8] = | |
449 {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01}; | |
450 CHECK_EQ((bytes[byte] & kMasks[bit]) != 0, i->second); | |
451 } | |
452 } else { | |
453 uint32 inter = i->child(child); | |
454 vector<pair<unsigned, bool> > new_bits(bits); | |
455 new_bits.push_back(pair<unsigned, bool>(bit, child != 0)); | |
456 CHECK_EQ(free_internal_nodes.count(inter), 0u); | |
457 CHECK_EQ(used_internal_nodes->count(inter), 0u); | |
458 used_internal_nodes->insert(inter); | |
459 ValidateTree(inter, bit, bits, free_internal_nodes, free_external_nodes, | |
460 used_internal_nodes, used_external_nodes); | |
461 } | |
462 } | |
463 } | |
464 | |
465 } // namespace net | |
OLD | NEW |