OLD | NEW |
---|---|
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 Loading... | |
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 && !ThreadState::current()->isConstructingGC Mixin()) { | |
haraken
2015/11/16 09:38:20
I begin to think that we should introduce gcGenera
peria
2015/11/16 12:28:45
Done.
| |
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. | |
haraken
2015/11/16 09:38:20
I believe that unsafeGet() should not be needed. Y
peria
2015/11/16 12:28:45
Yes, let's.
| |
790 T* unsafeGet() const | |
791 { | |
792 return m_raw; | |
793 } | |
777 | 794 |
795 void clear() | |
796 { | |
797 m_raw = nullptr; | |
798 } | |
778 | 799 |
779 protected: | 800 protected: |
780 void checkPointer() | 801 void checkPointer() |
781 { | 802 { |
782 #if ENABLE(ASSERT) && defined(ADDRESS_SANITIZER) | 803 #if ENABLE(ASSERT) && defined(ADDRESS_SANITIZER) |
783 if (!m_raw) | 804 if (!m_raw) |
784 return; | 805 return; |
785 // HashTable can store a special value (which is not aligned to the | 806 // HashTable can store a special value (which is not aligned to the |
786 // allocation granularity) to Member<> to represent a deleted entry. | 807 // allocation granularity) to Member<> to represent a deleted entry. |
787 // Thus we treat a pointer that is not aligned to the granularity | 808 // Thus we treat a pointer that is not aligned to the granularity |
788 // as a valid pointer. | 809 // as a valid pointer. |
789 if (reinterpret_cast<intptr_t>(m_raw) % allocationGranularity) | 810 if (reinterpret_cast<intptr_t>(m_raw) % allocationGranularity) |
790 return; | 811 return; |
791 | 812 |
792 // TODO(haraken): What we really want to check here is that the pointer | 813 // m_gcGeneration verifies use-after-free of this Member handle. |
793 // is a traceable object. In other words, the pointer is either of: | 814 // If m_gcGeneration is gcGenerationForFreeListEntry or m_raw is |
794 // | 815 // nullptr, the verification is skipped. |
795 // (a) a pointer to the head of an on-heap object. | 816 // For technical reason, we set gcGenerationForFreeListEntry in |
796 // (b) a pointer to the head of an on-heap mixin object. | 817 // m_gcGeneration in case that this method is called in a constructor |
797 // | 818 // of a GCMixin object. |
798 // We can check it by calling Heap::isHeapObjectAlive(m_raw), | 819 // 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. | 820 // GCMixin constructor. |
800 // So we currently only try to implement the check for (a), but do | 821 if (!ThreadState::current()->isConstructingGCMixin()) { |
801 // not insist that T's definition is in scope. | 822 HeapObjectHeader* header = HeapObjectHeaderTrait<T>::heapObjectHeade r(m_raw); |
802 if (IsFullyDefined<T>::value && !IsGarbageCollectedMixin<T>::value) | 823 if (header) |
803 ASSERT(HeapObjectHeader::fromPayload(m_raw)->checkHeader()); | 824 m_gcGeneration = header->gcGeneration(); |
haraken
2015/11/16 09:38:20
Who assigns 0 to m_gcGeneration when we fail at ge
peria
2015/11/16 12:28:45
Oops.
Set it by default.
| |
825 } | |
804 #endif | 826 #endif |
805 } | 827 } |
806 | 828 |
807 T* m_raw; | 829 T* m_raw; |
808 | 830 |
831 #if ENABLE(ASSERT) && defined(ADDRESS_SANITIZER) | |
832 uint32_t m_gcGeneration; | |
833 #endif | |
834 | |
809 template<bool x, WTF::WeakHandlingFlag y, WTF::ShouldWeakPointersBeMarkedStr ongly z, typename U, typename V> friend struct CollectionBackingTraceTrait; | 835 template<bool x, WTF::WeakHandlingFlag y, WTF::ShouldWeakPointersBeMarkedStr ongly z, typename U, typename V> friend struct CollectionBackingTraceTrait; |
810 friend class Visitor; | 836 friend class Visitor; |
811 | |
812 }; | 837 }; |
813 | 838 |
814 // WeakMember is similar to Member in that it is used to point to other oilpan | 839 // WeakMember is similar to Member in that it is used to point to other oilpan |
815 // heap allocated objects. | 840 // heap allocated objects. |
816 // However instead of creating a strong pointer to the object, the WeakMember cr eates | 841 // 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 | 842 // 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 | 843 // 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. | 844 // time of GC the weak pointers will automatically be set to null. |
820 template<typename T> | 845 template<typename T> |
821 class WeakMember : public Member<T> { | 846 class WeakMember : public Member<T> { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
865 this->checkPointer(); | 890 this->checkPointer(); |
866 return *this; | 891 return *this; |
867 } | 892 } |
868 | 893 |
869 WeakMember& operator=(std::nullptr_t) | 894 WeakMember& operator=(std::nullptr_t) |
870 { | 895 { |
871 this->m_raw = nullptr; | 896 this->m_raw = nullptr; |
872 return *this; | 897 return *this; |
873 } | 898 } |
874 | 899 |
900 // TODO(peria): Remove this get() and use unsafeGet() at only | |
901 // where it is required. | |
902 T* get() const | |
903 { | |
904 // WeakMember may point to a dead object, so we skip the verification. | |
905 return Member<T>::unsafeGet(); | |
906 } | |
907 | |
875 private: | 908 private: |
876 T** cell() const { return const_cast<T**>(&this->m_raw); } | 909 T** cell() const { return const_cast<T**>(&this->m_raw); } |
877 | 910 |
878 template<typename Derived> friend class VisitorHelper; | 911 template<typename Derived> friend class VisitorHelper; |
879 }; | 912 }; |
880 | 913 |
881 // UntracedMember is a pointer to an on-heap object that is not traced for some | 914 // 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. | 915 // 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 | 916 // 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 | 917 // 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 Loading... | |
939 } | 972 } |
940 | 973 |
941 UntracedMember& operator=(std::nullptr_t) | 974 UntracedMember& operator=(std::nullptr_t) |
942 { | 975 { |
943 this->m_raw = nullptr; | 976 this->m_raw = nullptr; |
944 return *this; | 977 return *this; |
945 } | 978 } |
946 }; | 979 }; |
947 | 980 |
948 // Comparison operators between (Weak)Members, Persistents, and UntracedMembers. | 981 // 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(); } | 982 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(); } | 983 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(); } | 984 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(); } | 985 template<typename T, typename U> inline bool operator!=(const Persistent<T>& a, const Persistent<U>& b) { return a.get() != b.get(); } |
953 | 986 |
954 template<typename T, typename U> inline bool operator==(const Member<T>& a, cons t Persistent<U>& b) { return a.get() == b.get(); } | 987 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(); } | 988 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(); } | 989 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(); } | 990 template<typename T, typename U> inline bool operator!=(const Persistent<T>& a, const Member<U>& b) { return a.get() != b.unsafeGet(); } |
958 | 991 |
959 template<typename T> | 992 template<typename T> |
960 class DummyBase { | 993 class DummyBase { |
961 public: | 994 public: |
962 DummyBase() { } | 995 DummyBase() { } |
963 ~DummyBase() { } | 996 ~DummyBase() { } |
964 }; | 997 }; |
965 | 998 |
966 // We need this explicit instantiation for component build on Windows. | 999 // We need this explicit instantiation for component build on Windows. |
967 template<> | 1000 template<> |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1407 | 1440 |
1408 template<typename T> struct PtrHash<blink::Member<T>> : PtrHash<T*> { | 1441 template<typename T> struct PtrHash<blink::Member<T>> : PtrHash<T*> { |
1409 template<typename U> | 1442 template<typename U> |
1410 static unsigned hash(const U& key) { return PtrHash<T*>::hash(key); } | 1443 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; } | 1444 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; } | 1445 static bool equal(const blink::Member<T>& a, T* b) { return a == b; } |
1413 template<typename U, typename V> | 1446 template<typename U, typename V> |
1414 static bool equal(const U& a, const V& b) { return a == b; } | 1447 static bool equal(const U& a, const V& b) { return a == b; } |
1415 }; | 1448 }; |
1416 | 1449 |
1417 template<typename T> struct PtrHash<blink::WeakMember<T>> : PtrHash<blink::Membe r<T>> { | 1450 template<typename T> struct PtrHash<blink::WeakMember<T>> : PtrHash<blink::Membe r<T>> { }; |
1418 }; | |
1419 | 1451 |
1420 template<typename T> struct PtrHash<blink::UntracedMember<T>> : PtrHash<blink::M ember<T>> { | 1452 template<typename T> struct PtrHash<blink::UntracedMember<T>> : PtrHash<blink::M ember<T>> { }; |
1421 }; | |
1422 | 1453 |
1423 // PtrHash is the default hash for hash tables with members. | 1454 // PtrHash is the default hash for hash tables with members. |
1424 template<typename T> struct DefaultHash<blink::Member<T>> { | 1455 template<typename T> struct DefaultHash<blink::Member<T>> { |
1425 using Hash = PtrHash<blink::Member<T>>; | 1456 using Hash = PtrHash<blink::Member<T>>; |
1426 }; | 1457 }; |
1427 | 1458 |
1428 template<typename T> struct DefaultHash<blink::WeakMember<T>> { | 1459 template<typename T> struct DefaultHash<blink::WeakMember<T>> { |
1429 using Hash = PtrHash<blink::WeakMember<T>>; | 1460 using Hash = PtrHash<blink::WeakMember<T>>; |
1430 }; | 1461 }; |
1431 | 1462 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1504 // TODO(sof): extend WTF::FunctionWrapper call overloading to also handle (C rossThread)WeakPersistent. | 1535 // TODO(sof): extend WTF::FunctionWrapper call overloading to also handle (C rossThread)WeakPersistent. |
1505 static T* unwrap(const StorageType& value) { return value.get(); } | 1536 static T* unwrap(const StorageType& value) { return value.get(); } |
1506 }; | 1537 }; |
1507 | 1538 |
1508 template<typename T> | 1539 template<typename T> |
1509 PassRefPtr<T> adoptRef(blink::RefCountedGarbageCollected<T>*) = delete; | 1540 PassRefPtr<T> adoptRef(blink::RefCountedGarbageCollected<T>*) = delete; |
1510 | 1541 |
1511 } // namespace WTF | 1542 } // namespace WTF |
1512 | 1543 |
1513 #endif | 1544 #endif |
OLD | NEW |