Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Side by Side Diff: runtime/vm/port.cc

Issue 217693002: - Make ports be less predictable. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/port.h ('k') | runtime/vm/random.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/port.h" 5 #include "vm/port.h"
6 6
7 #include "platform/utils.h" 7 #include "platform/utils.h"
8 #include "vm/dart_api_impl.h" 8 #include "vm/dart_api_impl.h"
9 #include "vm/isolate.h" 9 #include "vm/isolate.h"
10 #include "vm/message_handler.h" 10 #include "vm/message_handler.h"
11 #include "vm/thread.h" 11 #include "vm/thread.h"
12 12
13 namespace dart { 13 namespace dart {
14 14
15 DECLARE_FLAG(bool, trace_isolates); 15 DECLARE_FLAG(bool, trace_isolates);
16 16
17 Mutex* PortMap::mutex_ = NULL; 17 Mutex* PortMap::mutex_ = NULL;
18 PortMap::Entry* PortMap::map_ = NULL; 18 PortMap::Entry* PortMap::map_ = NULL;
19 MessageHandler* PortMap::deleted_entry_ = reinterpret_cast<MessageHandler*>(1); 19 MessageHandler* PortMap::deleted_entry_ = reinterpret_cast<MessageHandler*>(1);
20 intptr_t PortMap::capacity_ = 0; 20 intptr_t PortMap::capacity_ = 0;
21 intptr_t PortMap::used_ = 0; 21 intptr_t PortMap::used_ = 0;
22 intptr_t PortMap::deleted_ = 0; 22 intptr_t PortMap::deleted_ = 0;
23 Dart_Port PortMap::next_port_ = 7111; 23 Random* PortMap::prng_ = NULL;
24 24
25 25
26 intptr_t PortMap::FindPort(Dart_Port port) { 26 intptr_t PortMap::FindPort(Dart_Port port) {
27 intptr_t index = port % capacity_; 27 intptr_t index = port % capacity_;
28 intptr_t start_index = index; 28 intptr_t start_index = index;
29 Entry entry = map_[index]; 29 Entry entry = map_[index];
30 while (entry.handler != NULL) { 30 while (entry.handler != NULL) {
31 if (entry.port == port) { 31 if (entry.port == port) {
32 return index; 32 return index;
33 } 33 }
(...skipping 22 matching lines...) Expand all
56 } 56 }
57 } 57 }
58 delete[] map_; 58 delete[] map_;
59 map_ = new_ports; 59 map_ = new_ports;
60 capacity_ = new_capacity; 60 capacity_ = new_capacity;
61 deleted_ = 0; 61 deleted_ = 0;
62 } 62 }
63 63
64 64
65 Dart_Port PortMap::AllocatePort() { 65 Dart_Port PortMap::AllocatePort() {
66 Dart_Port result = next_port_; 66 const Dart_Port kMASK = 0x3fffffff;
67 Dart_Port result = prng_->NextUInt32() & kMASK;
67 68
68 do { 69 // Keep getting new values while we have an illegal port number or the port
69 // TODO(iposva): Use an approved hashing function to have less predictable 70 // number is already in use.
70 // port ids, or make them not accessible from Dart code or both. 71 while ((result == 0) || (FindPort(result) >= 0)) {
hausner 2014/03/28 23:27:30 You could turn this into a do { } while() loop.
Ivan Posva 2014/03/28 23:39:26 I tried to write it as an repeat-until, but that s
71 next_port_++; 72 result = prng_->NextUInt32() & kMASK;
72 } while (FindPort(next_port_) >= 0); 73 }
73 74
74 ASSERT(result != 0); 75 ASSERT(result != 0);
76 ASSERT(FindPort(result) < 0);
75 return result; 77 return result;
76 } 78 }
77 79
78 80
79 void PortMap::SetLive(Dart_Port port) { 81 void PortMap::SetLive(Dart_Port port) {
80 MutexLocker ml(mutex_); 82 MutexLocker ml(mutex_);
81 intptr_t index = FindPort(port); 83 intptr_t index = FindPort(port);
82 ASSERT(index >= 0); 84 ASSERT(index >= 0);
83 map_[index].live = true; 85 map_[index].live = true;
84 map_[index].handler->increment_live_ports(); 86 map_[index].handler->increment_live_ports();
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 return NULL; 253 return NULL;
252 } 254 }
253 255
254 MessageHandler* handler = map_[index].handler; 256 MessageHandler* handler = map_[index].handler;
255 return handler->GetIsolate(); 257 return handler->GetIsolate();
256 } 258 }
257 259
258 260
259 void PortMap::InitOnce() { 261 void PortMap::InitOnce() {
260 mutex_ = new Mutex(); 262 mutex_ = new Mutex();
263 prng_ = new Random();
261 264
262 static const intptr_t kInitialCapacity = 8; 265 static const intptr_t kInitialCapacity = 8;
263 // TODO(iposva): Verify whether we want to keep exponentially growing. 266 // TODO(iposva): Verify whether we want to keep exponentially growing.
264 ASSERT(Utils::IsPowerOfTwo(kInitialCapacity)); 267 ASSERT(Utils::IsPowerOfTwo(kInitialCapacity));
265 map_ = new Entry[kInitialCapacity]; 268 map_ = new Entry[kInitialCapacity];
266 memset(map_, 0, kInitialCapacity * sizeof(Entry)); 269 memset(map_, 0, kInitialCapacity * sizeof(Entry));
267 capacity_ = kInitialCapacity; 270 capacity_ = kInitialCapacity;
268 used_ = 0; 271 used_ = 0;
269 deleted_ = 0; 272 deleted_ = 0;
270 } 273 }
271 274
272 } // namespace dart 275 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/port.h ('k') | runtime/vm/random.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698