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

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

Issue 1411603007: [Oilpan] Add use-after-free detector in Member<> Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Introduce gcGenerationUnchecked Created 5 years, 1 month 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 /* 1 /*
2 * Copyright (C) 2014 Google Inc. All rights reserved. 2 * Copyright (C) 2014 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 753 matching lines...) Expand 10 before | Expand all | Expand 10 after
764 m_raw = nullptr; 764 m_raw = nullptr;
765 return *this; 765 return *this;
766 } 766 }
767 767
768 void swap(Member<T>& other) 768 void swap(Member<T>& other)
769 { 769 {
770 std::swap(m_raw, other.m_raw); 770 std::swap(m_raw, other.m_raw);
771 checkPointer(); 771 checkPointer();
772 } 772 }
773 773
774 T* get() const { return m_raw; } 774 T* get() const
775 {
776 #if ENABLE(ASSERT) && defined(ADDRESS_SANITIZER)
777 // Verify that this handle points to a live on-heap object, if a pointer
778 // has been assigned to this handle out of GarbageCollectedMixin constru ctions.
779 if (m_raw && m_gcGeneration != gcGenerationUnchecked && !ThreadState::cu rrent()->isConstructingGCMixin()) {
780 HeapObjectHeader* header = HeapObjectHeaderTrait<T>::heapObjectHeade r(m_raw);
781 ASSERT(!header || (m_gcGeneration == header->gcGeneration()));
782 }
783 #endif
784 return m_raw;
785 }
775 786
776 void clear() { m_raw = nullptr; } 787 // This method is an accessor without any security checks, and it is
788 // expected to be used under some limited condition.
789 // So do NOT use it, if you do not know what it means.
790 // TODO(peria): We should remove this method.
791 T* unsafeGet() const
792 {
793 return m_raw;
794 }
777 795
796 void clear()
797 {
798 m_raw = nullptr;
799 }
778 800
779 protected: 801 protected:
780 void checkPointer() 802 void checkPointer()
781 { 803 {
782 #if ENABLE(ASSERT) && defined(ADDRESS_SANITIZER) 804 #if ENABLE(ASSERT) && defined(ADDRESS_SANITIZER)
783 if (!m_raw) 805 if (!m_raw)
784 return; 806 return;
785 // HashTable can store a special value (which is not aligned to the 807 // HashTable can store a special value (which is not aligned to the
786 // allocation granularity) to Member<> to represent a deleted entry. 808 // allocation granularity) to Member<> to represent a deleted entry.
787 // Thus we treat a pointer that is not aligned to the granularity 809 // Thus we treat a pointer that is not aligned to the granularity
788 // as a valid pointer. 810 // as a valid pointer.
789 if (reinterpret_cast<intptr_t>(m_raw) % allocationGranularity) 811 if (reinterpret_cast<intptr_t>(m_raw) % allocationGranularity)
790 return; 812 return;
791 813
792 // TODO(haraken): What we really want to check here is that the pointer 814 // m_gcGeneration verifies use-after-free of this Member handle.
793 // is a traceable object. In other words, the pointer is either of: 815 // If m_gcGeneration is gcGenerationForFreeListEntry or m_raw is
794 // 816 // nullptr, the verification is skipped.
795 // (a) a pointer to the head of an on-heap object. 817 // For technical reason, we set gcGenerationForFreeListEntry in
796 // (b) a pointer to the head of an on-heap mixin object. 818 // m_gcGeneration in case that this method is called in a constructor
797 // 819 // of a GCMixin object.
798 // We can check it by calling Heap::isHeapObjectAlive(m_raw), 820 // TODO(peria): Set m_gcGeneration even if this is called in a
799 // but we cannot call it here because it requires to include T.h. 821 // GCMixin constructor.
800 // So we currently only try to implement the check for (a), but do 822 m_gcGeneration = gcGenerationUnchecked;
801 // not insist that T's definition is in scope. 823 if (IsFullyDefined<T>::value && !ThreadState::current()->isConstructingG CMixin()) {
802 if (IsFullyDefined<T>::value && !IsGarbageCollectedMixin<T>::value) 824 HeapObjectHeader* header = HeapObjectHeaderTrait<T>::heapObjectHeade r(m_raw);
803 ASSERT(HeapObjectHeader::fromPayload(m_raw)->checkHeader()); 825 if (header) {
826 m_gcGeneration = header->gcGeneration();
827 ASSERT(m_gcGeneration != gcGenerationForFreeListEntry);
828 }
829 }
804 #endif 830 #endif
805 } 831 }
806 832
807 T* m_raw; 833 T* m_raw;
808 834
835 #if ENABLE(ASSERT) && defined(ADDRESS_SANITIZER)
836 uint32_t m_gcGeneration;
837 #endif
838
809 template<bool x, WTF::WeakHandlingFlag y, WTF::ShouldWeakPointersBeMarkedStr ongly z, typename U, typename V> friend struct CollectionBackingTraceTrait; 839 template<bool x, WTF::WeakHandlingFlag y, WTF::ShouldWeakPointersBeMarkedStr ongly z, typename U, typename V> friend struct CollectionBackingTraceTrait;
810 friend class Visitor; 840 friend class Visitor;
811
812 }; 841 };
813 842
814 // WeakMember is similar to Member in that it is used to point to other oilpan 843 // WeakMember is similar to Member in that it is used to point to other oilpan
815 // heap allocated objects. 844 // heap allocated objects.
816 // However instead of creating a strong pointer to the object, the WeakMember cr eates 845 // However instead of creating a strong pointer to the object, the WeakMember cr eates
817 // a weak pointer, which does not keep the pointee alive. Hence if all pointers to 846 // a weak pointer, which does not keep the pointee alive. Hence if all pointers to
818 // to a heap allocated object are weak the object will be garbage collected. At the 847 // to a heap allocated object are weak the object will be garbage collected. At the
819 // time of GC the weak pointers will automatically be set to null. 848 // time of GC the weak pointers will automatically be set to null.
820 template<typename T> 849 template<typename T>
821 class WeakMember : public Member<T> { 850 class WeakMember : public Member<T> {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
865 this->checkPointer(); 894 this->checkPointer();
866 return *this; 895 return *this;
867 } 896 }
868 897
869 WeakMember& operator=(std::nullptr_t) 898 WeakMember& operator=(std::nullptr_t)
870 { 899 {
871 this->m_raw = nullptr; 900 this->m_raw = nullptr;
872 return *this; 901 return *this;
873 } 902 }
874 903
904 // TODO(peria): Remove this get() and use unsafeGet() at only
905 // where it is required.
906 T* get() const
907 {
908 // WeakMember may point to a dead object, so we skip the verification.
909 return Member<T>::unsafeGet();
910 }
911
875 private: 912 private:
876 T** cell() const { return const_cast<T**>(&this->m_raw); } 913 T** cell() const { return const_cast<T**>(&this->m_raw); }
877 914
878 template<typename Derived> friend class VisitorHelper; 915 template<typename Derived> friend class VisitorHelper;
879 }; 916 };
880 917
881 // UntracedMember is a pointer to an on-heap object that is not traced for some 918 // UntracedMember is a pointer to an on-heap object that is not traced for some
882 // reason. Please don't use this unless you understand what you're doing. 919 // reason. Please don't use this unless you understand what you're doing.
883 // Basically, all pointers to on-heap objects must be stored in either of 920 // Basically, all pointers to on-heap objects must be stored in either of
884 // Persistent, Member or WeakMember. It is not allowed to leave raw pointers to 921 // Persistent, Member or WeakMember. It is not allowed to leave raw pointers to
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
939 } 976 }
940 977
941 UntracedMember& operator=(std::nullptr_t) 978 UntracedMember& operator=(std::nullptr_t)
942 { 979 {
943 this->m_raw = nullptr; 980 this->m_raw = nullptr;
944 return *this; 981 return *this;
945 } 982 }
946 }; 983 };
947 984
948 // Comparison operators between (Weak)Members, Persistents, and UntracedMembers. 985 // Comparison operators between (Weak)Members, Persistents, and UntracedMembers.
949 template<typename T, typename U> inline bool operator==(const Member<T>& a, cons t Member<U>& b) { return a.get() == b.get(); } 986 template<typename T, typename U> inline bool operator==(const Member<T>& a, cons t Member<U>& b) { return a.unsafeGet() == b.unsafeGet(); }
950 template<typename T, typename U> inline bool operator!=(const Member<T>& a, cons t Member<U>& b) { return a.get() != b.get(); } 987 template<typename T, typename U> inline bool operator!=(const Member<T>& a, cons t Member<U>& b) { return a.unsafeGet() != b.unsafeGet(); }
951 template<typename T, typename U> inline bool operator==(const Persistent<T>& a, const Persistent<U>& b) { return a.get() == b.get(); } 988 template<typename T, typename U> inline bool operator==(const Persistent<T>& a, const Persistent<U>& b) { return a.get() == b.get(); }
952 template<typename T, typename U> inline bool operator!=(const Persistent<T>& a, const Persistent<U>& b) { return a.get() != b.get(); } 989 template<typename T, typename U> inline bool operator!=(const Persistent<T>& a, const Persistent<U>& b) { return a.get() != b.get(); }
953 990
954 template<typename T, typename U> inline bool operator==(const Member<T>& a, cons t Persistent<U>& b) { return a.get() == b.get(); } 991 template<typename T, typename U> inline bool operator==(const Member<T>& a, cons t Persistent<U>& b) { return a.unsafeGet() == b.get(); }
955 template<typename T, typename U> inline bool operator!=(const Member<T>& a, cons t Persistent<U>& b) { return a.get() != b.get(); } 992 template<typename T, typename U> inline bool operator!=(const Member<T>& a, cons t Persistent<U>& b) { return a.unsafeGet() != b.get(); }
956 template<typename T, typename U> inline bool operator==(const Persistent<T>& a, const Member<U>& b) { return a.get() == b.get(); } 993 template<typename T, typename U> inline bool operator==(const Persistent<T>& a, const Member<U>& b) { return a.get() == b.unsafeGet(); }
957 template<typename T, typename U> inline bool operator!=(const Persistent<T>& a, const Member<U>& b) { return a.get() != b.get(); } 994 template<typename T, typename U> inline bool operator!=(const Persistent<T>& a, const Member<U>& b) { return a.get() != b.unsafeGet(); }
958 995
959 template<typename T> 996 template<typename T>
960 class DummyBase { 997 class DummyBase {
961 public: 998 public:
962 DummyBase() { } 999 DummyBase() { }
963 ~DummyBase() { } 1000 ~DummyBase() { }
964 }; 1001 };
965 1002
966 // We need this explicit instantiation for component build on Windows. 1003 // We need this explicit instantiation for component build on Windows.
967 template<> 1004 template<>
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after
1407 1444
1408 template<typename T> struct PtrHash<blink::Member<T>> : PtrHash<T*> { 1445 template<typename T> struct PtrHash<blink::Member<T>> : PtrHash<T*> {
1409 template<typename U> 1446 template<typename U>
1410 static unsigned hash(const U& key) { return PtrHash<T*>::hash(key); } 1447 static unsigned hash(const U& key) { return PtrHash<T*>::hash(key); }
1411 static bool equal(T* a, const blink::Member<T>& b) { return a == b; } 1448 static bool equal(T* a, const blink::Member<T>& b) { return a == b; }
1412 static bool equal(const blink::Member<T>& a, T* b) { return a == b; } 1449 static bool equal(const blink::Member<T>& a, T* b) { return a == b; }
1413 template<typename U, typename V> 1450 template<typename U, typename V>
1414 static bool equal(const U& a, const V& b) { return a == b; } 1451 static bool equal(const U& a, const V& b) { return a == b; }
1415 }; 1452 };
1416 1453
1417 template<typename T> struct PtrHash<blink::WeakMember<T>> : PtrHash<blink::Membe r<T>> { 1454 template<typename T> struct PtrHash<blink::WeakMember<T>> : PtrHash<blink::Membe r<T>> { };
1418 };
1419 1455
1420 template<typename T> struct PtrHash<blink::UntracedMember<T>> : PtrHash<blink::M ember<T>> { 1456 template<typename T> struct PtrHash<blink::UntracedMember<T>> : PtrHash<blink::M ember<T>> { };
1421 };
1422 1457
1423 // PtrHash is the default hash for hash tables with members. 1458 // PtrHash is the default hash for hash tables with members.
1424 template<typename T> struct DefaultHash<blink::Member<T>> { 1459 template<typename T> struct DefaultHash<blink::Member<T>> {
1425 using Hash = PtrHash<blink::Member<T>>; 1460 using Hash = PtrHash<blink::Member<T>>;
1426 }; 1461 };
1427 1462
1428 template<typename T> struct DefaultHash<blink::WeakMember<T>> { 1463 template<typename T> struct DefaultHash<blink::WeakMember<T>> {
1429 using Hash = PtrHash<blink::WeakMember<T>>; 1464 using Hash = PtrHash<blink::WeakMember<T>>;
1430 }; 1465 };
1431 1466
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1504 // TODO(sof): extend WTF::FunctionWrapper call overloading to also handle (C rossThread)WeakPersistent. 1539 // TODO(sof): extend WTF::FunctionWrapper call overloading to also handle (C rossThread)WeakPersistent.
1505 static T* unwrap(const StorageType& value) { return value.get(); } 1540 static T* unwrap(const StorageType& value) { return value.get(); }
1506 }; 1541 };
1507 1542
1508 template<typename T> 1543 template<typename T>
1509 PassRefPtr<T> adoptRef(blink::RefCountedGarbageCollected<T>*) = delete; 1544 PassRefPtr<T> adoptRef(blink::RefCountedGarbageCollected<T>*) = delete;
1510 1545
1511 } // namespace WTF 1546 } // namespace WTF
1512 1547
1513 #endif 1548 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698