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

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: Fix test failures 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)
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) {
780 ASSERT(m_gcGeneration == GarbageCollectedMixinTrait<T>::heapObjectHe ader(m_raw)->gcGeneration());
781 }
782 #endif
783 return m_raw;
784 }
775 785
776 void clear() { m_raw = nullptr; } 786 // This method is an accessor without any security checks, and it is
787 // expected to be used under some limited condition.
788 // So do NOT use it, if you do not know what it means.
789 T* unsafeGet() const
790 {
791 return m_raw;
792 }
777 793
794 void clear()
795 {
796 m_raw = nullptr;
797 }
778 798
779 protected: 799 protected:
780 void checkPointer() 800 void checkPointer()
781 { 801 {
782 #if ENABLE(ASSERT) && defined(ADDRESS_SANITIZER) 802 #if ENABLE(ASSERT) && defined(ADDRESS_SANITIZER)
783 if (!m_raw) 803 if (!m_raw)
784 return; 804 return;
785 // HashTable can store a special value (which is not aligned to the 805 // HashTable can store a special value (which is not aligned to the
786 // allocation granularity) to Member<> to represent a deleted entry. 806 // allocation granularity) to Member<> to represent a deleted entry.
787 // Thus we treat a pointer that is not aligned to the granularity 807 // Thus we treat a pointer that is not aligned to the granularity
788 // as a valid pointer. 808 // as a valid pointer.
789 if (reinterpret_cast<intptr_t>(m_raw) % allocationGranularity) 809 if (reinterpret_cast<intptr_t>(m_raw) % allocationGranularity)
790 return; 810 return;
791 811
792 // TODO(haraken): What we really want to check here is that the pointer 812 // TODO(haraken): What we really want to check here is that the pointer
793 // is a traceable object. In other words, the pointer is either of: 813 // is a traceable object. In other words, the pointer is either of:
794 // 814 //
795 // (a) a pointer to the head of an on-heap object. 815 // (a) a pointer to the head of an on-heap object.
796 // (b) a pointer to the head of an on-heap mixin object. 816 // (b) a pointer to the head of an on-heap mixin object.
797 // 817 //
798 // We can check it by calling Heap::isHeapObjectAlive(m_raw), 818 // We can check it by calling Heap::isHeapObjectAlive(m_raw),
799 // but we cannot call it here because it requires to include T.h. 819 // but we cannot call it here because it requires to include T.h.
800 // So we currently only try to implement the check for (a), but do 820 // So we currently only try to implement the check for (a), but do
801 // not insist that T's definition is in scope. 821 // not insist that T's definition is in scope.
802 if (IsFullyDefined<T>::value && !IsGarbageCollectedMixin<T>::value) 822 m_gcGeneration = GarbageCollectedMixinTrait<T>::gcGeneration(m_raw);
803 ASSERT(HeapObjectHeader::fromPayload(m_raw)->checkHeader());
804 #endif 823 #endif
805 } 824 }
806 825
807 T* m_raw; 826 T* m_raw;
808 827
828 #if ENABLE(ASSERT)
829 uint32_t m_gcGeneration;
830 #endif
831
809 template<bool x, WTF::WeakHandlingFlag y, WTF::ShouldWeakPointersBeMarkedStr ongly z, typename U, typename V> friend struct CollectionBackingTraceTrait; 832 template<bool x, WTF::WeakHandlingFlag y, WTF::ShouldWeakPointersBeMarkedStr ongly z, typename U, typename V> friend struct CollectionBackingTraceTrait;
810 friend class Visitor; 833 friend class Visitor;
811
812 }; 834 };
813 835
814 // WeakMember is similar to Member in that it is used to point to other oilpan 836 // WeakMember is similar to Member in that it is used to point to other oilpan
815 // heap allocated objects. 837 // heap allocated objects.
816 // However instead of creating a strong pointer to the object, the WeakMember cr eates 838 // 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 839 // 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 840 // 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. 841 // time of GC the weak pointers will automatically be set to null.
820 template<typename T> 842 template<typename T>
821 class WeakMember : public Member<T> { 843 class WeakMember : public Member<T> {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
865 this->checkPointer(); 887 this->checkPointer();
866 return *this; 888 return *this;
867 } 889 }
868 890
869 WeakMember& operator=(std::nullptr_t) 891 WeakMember& operator=(std::nullptr_t)
870 { 892 {
871 this->m_raw = nullptr; 893 this->m_raw = nullptr;
872 return *this; 894 return *this;
873 } 895 }
874 896
897 T* get() const
898 {
899 // WeakMember may point to a dead object, so we skip the verification.
900 return Member<T>::unsafeGet();
901 }
902
875 private: 903 private:
876 T** cell() const { return const_cast<T**>(&this->m_raw); } 904 T** cell() const { return const_cast<T**>(&this->m_raw); }
877 905
878 template<typename Derived> friend class VisitorHelper; 906 template<typename Derived> friend class VisitorHelper;
879 }; 907 };
880 908
881 // UntracedMember is a pointer to an on-heap object that is not traced for some 909 // 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. 910 // 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 911 // 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 912 // 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 } 967 }
940 968
941 UntracedMember& operator=(std::nullptr_t) 969 UntracedMember& operator=(std::nullptr_t)
942 { 970 {
943 this->m_raw = nullptr; 971 this->m_raw = nullptr;
944 return *this; 972 return *this;
945 } 973 }
946 }; 974 };
947 975
948 // Comparison operators between (Weak)Members, Persistents, and UntracedMembers. 976 // 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(); } 977 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(); } 978 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(); } 979 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(); } 980 template<typename T, typename U> inline bool operator!=(const Persistent<T>& a, const Persistent<U>& b) { return a.get() != b.get(); }
953 981
954 template<typename T, typename U> inline bool operator==(const Member<T>& a, cons t Persistent<U>& b) { return a.get() == b.get(); } 982 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(); } 983 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(); } 984 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(); } 985 template<typename T, typename U> inline bool operator!=(const Persistent<T>& a, const Member<U>& b) { return a.get() != b.unsafeGet(); }
958 986
959 template<typename T> 987 template<typename T>
960 class DummyBase { 988 class DummyBase {
961 public: 989 public:
962 DummyBase() { } 990 DummyBase() { }
963 ~DummyBase() { } 991 ~DummyBase() { }
964 }; 992 };
965 993
966 // We need this explicit instantiation for component build on Windows. 994 // We need this explicit instantiation for component build on Windows.
967 template<> 995 template<>
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after
1407 1435
1408 template<typename T> struct PtrHash<blink::Member<T>> : PtrHash<T*> { 1436 template<typename T> struct PtrHash<blink::Member<T>> : PtrHash<T*> {
1409 template<typename U> 1437 template<typename U>
1410 static unsigned hash(const U& key) { return PtrHash<T*>::hash(key); } 1438 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; } 1439 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; } 1440 static bool equal(const blink::Member<T>& a, T* b) { return a == b; }
1413 template<typename U, typename V> 1441 template<typename U, typename V>
1414 static bool equal(const U& a, const V& b) { return a == b; } 1442 static bool equal(const U& a, const V& b) { return a == b; }
1415 }; 1443 };
1416 1444
1417 template<typename T> struct PtrHash<blink::WeakMember<T>> : PtrHash<blink::Membe r<T>> { 1445 template<typename T> struct PtrHash<blink::WeakMember<T>> : PtrHash<blink::Membe r<T>> { };
1418 };
1419 1446
1420 template<typename T> struct PtrHash<blink::UntracedMember<T>> : PtrHash<blink::M ember<T>> { 1447 template<typename T> struct PtrHash<blink::UntracedMember<T>> : PtrHash<blink::M ember<T>> { };
1421 };
1422 1448
1423 // PtrHash is the default hash for hash tables with members. 1449 // PtrHash is the default hash for hash tables with members.
1424 template<typename T> struct DefaultHash<blink::Member<T>> { 1450 template<typename T> struct DefaultHash<blink::Member<T>> {
1425 using Hash = PtrHash<blink::Member<T>>; 1451 using Hash = PtrHash<blink::Member<T>>;
1426 }; 1452 };
1427 1453
1428 template<typename T> struct DefaultHash<blink::WeakMember<T>> { 1454 template<typename T> struct DefaultHash<blink::WeakMember<T>> {
1429 using Hash = PtrHash<blink::WeakMember<T>>; 1455 using Hash = PtrHash<blink::WeakMember<T>>;
1430 }; 1456 };
1431 1457
(...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. 1530 // TODO(sof): extend WTF::FunctionWrapper call overloading to also handle (C rossThread)WeakPersistent.
1505 static T* unwrap(const StorageType& value) { return value.get(); } 1531 static T* unwrap(const StorageType& value) { return value.get(); }
1506 }; 1532 };
1507 1533
1508 template<typename T> 1534 template<typename T>
1509 PassRefPtr<T> adoptRef(blink::RefCountedGarbageCollected<T>*) = delete; 1535 PassRefPtr<T> adoptRef(blink::RefCountedGarbageCollected<T>*) = delete;
1510 1536
1511 } // namespace WTF 1537 } // namespace WTF
1512 1538
1513 #endif 1539 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698