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

Side by Side Diff: third_party/WebKit/Source/platform/heap/Member.h

Issue 2592063002: SameThreadCheckedMember<>: verify same-thread usage of heap references. (Closed)
Patch Set: clang compile fix, pt2 Created 3 years, 12 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef Member_h 5 #ifndef Member_h
6 #define Member_h 6 #define Member_h
7 7
8 #include "wtf/Allocator.h" 8 #include "wtf/Allocator.h"
9 #include "wtf/HashFunctions.h" 9 #include "wtf/HashFunctions.h"
10 #include "wtf/HashTraits.h" 10 #include "wtf/HashTraits.h"
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 protected: 245 protected:
246 template <bool x, 246 template <bool x,
247 WTF::WeakHandlingFlag y, 247 WTF::WeakHandlingFlag y,
248 WTF::ShouldWeakPointersBeMarkedStrongly z, 248 WTF::ShouldWeakPointersBeMarkedStrongly z,
249 typename U, 249 typename U,
250 typename V> 250 typename V>
251 friend struct CollectionBackingTraceTrait; 251 friend struct CollectionBackingTraceTrait;
252 friend class Visitor; 252 friend class Visitor;
253 }; 253 };
254 254
255 // A checked version of Member<>, verifying that only same-thread references
256 // are kept in the smart pointer. Intended to be used to diagnose unclean
257 // thread reference usage in release builds. It simply exposes the debug-only
258 // MemberBase<> checking we already have in place for select usage to diagnose
259 // per-thread issues.
260 template <typename T>
261 class SameThreadCheckedMember : public Member<T> {
haraken 2016/12/21 12:52:16 Instead of introducing SafeThreadCheckedMember and
sof 2016/12/21 12:56:36 Where would you store the "created ThreadState" ?
262 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
263 typedef Member<T> Parent;
264
265 public:
266 SameThreadCheckedMember() : Parent() { saveCreationThreadState(); }
267 SameThreadCheckedMember(std::nullptr_t) : Parent(nullptr) {
268 saveCreationThreadState();
269 }
270
271 SameThreadCheckedMember(T* raw) : Parent(raw) {
272 saveCreationThreadState();
273 checkPointer();
274 }
275
276 SameThreadCheckedMember(T& raw) : Parent(raw) {
277 saveCreationThreadState();
278 checkPointer();
279 }
280
281 SameThreadCheckedMember(WTF::HashTableDeletedValueType x) : Parent(x) {
282 saveCreationThreadState();
283 checkPointer();
284 }
285
286 SameThreadCheckedMember(const SameThreadCheckedMember& other)
287 : Parent(other) {
288 saveCreationThreadState();
289 }
290 template <typename U>
291 SameThreadCheckedMember(const SameThreadCheckedMember<U>& other)
292 : Parent(other) {
293 saveCreationThreadState();
294 checkPointer();
295 }
296
297 template <typename U>
298 SameThreadCheckedMember(const Persistent<U>& other) : Parent(other) {
299 saveCreationThreadState();
300 checkPointer();
301 }
302
303 template <typename U>
304 SameThreadCheckedMember& operator=(const Persistent<U>& other) {
305 Parent::operator=(other);
306 checkPointer();
307 return *this;
308 }
309
310 template <typename U>
311 SameThreadCheckedMember& operator=(const SameThreadCheckedMember<U>& other) {
312 Parent::operator=(other);
313 checkPointer();
314 return *this;
315 }
316
317 template <typename U>
318 SameThreadCheckedMember& operator=(const WeakMember<U>& other) {
319 Parent::operator=(other);
320 checkPointer();
321 return *this;
322 }
323
324 template <typename U>
325 SameThreadCheckedMember& operator=(U* other) {
326 Parent::operator=(other);
327 checkPointer();
328 return *this;
329 }
330
331 SameThreadCheckedMember& operator=(std::nullptr_t) {
332 Parent::operator=(nullptr);
333 return *this;
334 }
335
336 protected:
337 template <bool x,
338 WTF::WeakHandlingFlag y,
339 WTF::ShouldWeakPointersBeMarkedStrongly z,
340 typename U,
341 typename V>
342 friend struct CollectionBackingTraceTrait;
343 friend class Visitor;
344
345 private:
346 void checkPointer() {
347 if (!this->m_raw)
348 return;
349 // HashTable can store a special value (which is not aligned to the
350 // allocation granularity) to Member<> to represent a deleted entry.
351 // Thus we treat a pointer that is not aligned to the granularity
352 // as a valid pointer.
353 if (reinterpret_cast<intptr_t>(this->m_raw) % allocationGranularity)
354 return;
355
356 ThreadState* current = ThreadState::current();
357 DCHECK(current);
358 // m_creationThreadState may be null when this is used in a heap
359 // collection which initialized the Member with memset and the
360 // constructor wasn't called.
361 if (m_creationThreadState) {
362 // Member should point to objects that belong in the same ThreadHeap.
363 CHECK_EQ(&ThreadState::fromObject(this->m_raw)->heap(),
364 &m_creationThreadState->heap());
365 // Member should point to objects that belong in the same ThreadHeap.
366 CHECK_EQ(&current->heap(), &m_creationThreadState->heap());
367 } else {
368 CHECK_EQ(&ThreadState::fromObject(this->m_raw)->heap(), &current->heap());
369 }
370 }
371
372 void saveCreationThreadState() {
373 m_creationThreadState = ThreadState::current();
374 // All Members should be created in an attached thread, but an empty
375 // value Member may be created on an unattached thread by a heap
376 // collection iterator.
377 CHECK(this->m_creationThreadState || !this->m_raw);
378 }
379
380 const ThreadState* m_creationThreadState;
381 };
382
255 // WeakMember is similar to Member in that it is used to point to other oilpan 383 // WeakMember is similar to Member in that it is used to point to other oilpan
256 // heap allocated objects. 384 // heap allocated objects.
257 // However instead of creating a strong pointer to the object, the WeakMember 385 // However instead of creating a strong pointer to the object, the WeakMember
258 // creates a weak pointer, which does not keep the pointee alive. Hence if all 386 // creates a weak pointer, which does not keep the pointee alive. Hence if all
259 // pointers to to a heap allocated object are weak the object will be garbage 387 // pointers to to a heap allocated object are weak the object will be garbage
260 // collected. At the time of GC the weak pointers will automatically be set to 388 // collected. At the time of GC the weak pointers will automatically be set to
261 // null. 389 // null.
262 template <typename T> 390 template <typename T>
263 class WeakMember : public MemberBase<T, TracenessMemberConfiguration::Traced> { 391 class WeakMember : public MemberBase<T, TracenessMemberConfiguration::Traced> {
264 typedef MemberBase<T, TracenessMemberConfiguration::Traced> Parent; 392 typedef MemberBase<T, TracenessMemberConfiguration::Traced> Parent;
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 using Hash = MemberHash<T>; 524 using Hash = MemberHash<T>;
397 }; 525 };
398 526
399 template <typename T> 527 template <typename T>
400 struct DefaultHash<blink::UntracedMember<T>> { 528 struct DefaultHash<blink::UntracedMember<T>> {
401 STATIC_ONLY(DefaultHash); 529 STATIC_ONLY(DefaultHash);
402 using Hash = MemberHash<T>; 530 using Hash = MemberHash<T>;
403 }; 531 };
404 532
405 template <typename T> 533 template <typename T>
534 struct DefaultHash<blink::SameThreadCheckedMember<T>> {
535 STATIC_ONLY(DefaultHash);
536 using Hash = MemberHash<T>;
537 };
538
539 template <typename T>
406 struct DefaultHash<blink::TraceWrapperMember<T>> { 540 struct DefaultHash<blink::TraceWrapperMember<T>> {
407 STATIC_ONLY(DefaultHash); 541 STATIC_ONLY(DefaultHash);
408 using Hash = MemberHash<T>; 542 using Hash = MemberHash<T>;
409 }; 543 };
410 544
411 template <typename T> 545 template <typename T>
412 struct IsTraceable<blink::Member<T>> { 546 struct IsTraceable<blink::Member<T>> {
413 STATIC_ONLY(IsTraceable); 547 STATIC_ONLY(IsTraceable);
414 static const bool value = true; 548 static const bool value = true;
415 }; 549 };
416 550
417 template <typename T> 551 template <typename T>
418 struct IsWeak<blink::WeakMember<T>> { 552 struct IsWeak<blink::WeakMember<T>> {
419 STATIC_ONLY(IsWeak); 553 STATIC_ONLY(IsWeak);
420 static const bool value = true; 554 static const bool value = true;
421 }; 555 };
422 556
423 template <typename T> 557 template <typename T>
424 struct IsTraceable<blink::WeakMember<T>> { 558 struct IsTraceable<blink::WeakMember<T>> {
425 STATIC_ONLY(IsTraceable); 559 STATIC_ONLY(IsTraceable);
426 static const bool value = true; 560 static const bool value = true;
427 }; 561 };
428 562
429 template <typename T> 563 template <typename T>
564 struct IsTraceable<blink::SameThreadCheckedMember<T>> {
565 STATIC_ONLY(IsTraceable);
566 static const bool value = true;
567 };
568
569 template <typename T>
430 struct IsTraceable<blink::TraceWrapperMember<T>> { 570 struct IsTraceable<blink::TraceWrapperMember<T>> {
431 STATIC_ONLY(IsTraceable); 571 STATIC_ONLY(IsTraceable);
432 static const bool value = true; 572 static const bool value = true;
433 }; 573 };
434 574
435 } // namespace WTF 575 } // namespace WTF
436 576
437 #endif // Member_h 577 #endif // Member_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/heap/HeapAllocator.h ('k') | third_party/WebKit/Source/platform/heap/ThreadingTraits.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698