1/*
2 * Copyright (C) 2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <wtf/DumbPtrTraits.h>
29#include <wtf/Gigacage.h>
30#include <wtf/PtrTag.h>
31
32#include <climits>
33
34namespace WTF {
35
36constexpr bool tagCagedPtr = true;
37
38template<Gigacage::Kind passedKind, typename T, bool shouldTag = false, typename PtrTraits = DumbPtrTraits<T>>
39class CagedPtr {
40public:
41 static constexpr Gigacage::Kind kind = passedKind;
42
43 CagedPtr() : CagedPtr(nullptr) { }
44 CagedPtr(std::nullptr_t)
45 : m_ptr(shouldTag ? tagArrayPtr<T>(nullptr, 0) : nullptr)
46 { }
47
48 CagedPtr(T* ptr, unsigned size)
49 : m_ptr(shouldTag ? tagArrayPtr(ptr, size) : ptr)
50 { }
51
52 T* get(unsigned size) const
53 {
54 ASSERT(m_ptr);
55 T* ptr = PtrTraits::unwrap(m_ptr);
56 T* cagedPtr = Gigacage::caged(kind, ptr);
57 T* untaggedPtr = shouldTag ? untagArrayPtr(mergePointers(ptr, cagedPtr), size) : cagedPtr;
58 return untaggedPtr;
59 }
60
61 T* getMayBeNull(unsigned size) const
62 {
63 T* ptr = PtrTraits::unwrap(m_ptr);
64 T* cagedPtr = Gigacage::cagedMayBeNull(kind, ptr);
65 T* untaggedPtr = shouldTag ? untagArrayPtr(mergePointers(ptr, cagedPtr), size) : cagedPtr;
66 return untaggedPtr;
67 }
68
69 T* getUnsafe() const
70 {
71 T* ptr = PtrTraits::unwrap(m_ptr);
72 ptr = shouldTag ? removeArrayPtrTag(ptr) : ptr;
73 return Gigacage::cagedMayBeNull(kind, ptr);
74 }
75
76 // We need the template here so that the type of U is deduced at usage time rather than class time. U should always be T.
77 template<typename U = T>
78 typename std::enable_if<!std::is_same<void, U>::value, T>::type&
79 /* T& */ at(unsigned index, unsigned size) const { return get(size)[index]; }
80
81 void recage(unsigned oldSize, unsigned newSize)
82 {
83 auto ptr = get(oldSize);
84 ASSERT(ptr == getUnsafe());
85 *this = CagedPtr(ptr, newSize);
86 }
87
88 CagedPtr(CagedPtr& other)
89 : m_ptr(other.m_ptr)
90 {
91 }
92
93 CagedPtr& operator=(const CagedPtr& ptr)
94 {
95 m_ptr = ptr.m_ptr;
96 return *this;
97 }
98
99 CagedPtr(CagedPtr&& other)
100 : m_ptr(PtrTraits::exchange(other.m_ptr, nullptr))
101 {
102 }
103
104 CagedPtr& operator=(CagedPtr&& ptr)
105 {
106 m_ptr = PtrTraits::exchange(ptr.m_ptr, nullptr);
107 return *this;
108 }
109
110 bool operator==(const CagedPtr& other) const
111 {
112 bool result = m_ptr == other.m_ptr;
113 ASSERT(result == (getUnsafe() == other.getUnsafe()));
114 return result;
115 }
116
117 bool operator!=(const CagedPtr& other) const
118 {
119 return !(*this == other);
120 }
121
122 explicit operator bool() const
123 {
124 return getUnsafe() != nullptr;
125 }
126
127protected:
128 static inline T* mergePointers(T* sourcePtr, T* cagedPtr)
129 {
130#if CPU(ARM64E)
131 constexpr unsigned numberOfPACBits = 25;
132 constexpr uintptr_t mask = (1ull << ((sizeof(T*) * CHAR_BIT) - numberOfPACBits)) - 1;
133 return reinterpret_cast<T*>((reinterpret_cast<uintptr_t>(sourcePtr) & ~mask) | (reinterpret_cast<uintptr_t>(cagedPtr) & mask));
134#else
135 UNUSED_PARAM(sourcePtr);
136 return cagedPtr;
137#endif
138 }
139
140 typename PtrTraits::StorageType m_ptr;
141};
142
143} // namespace WTF
144
145using WTF::CagedPtr;
146using WTF::tagCagedPtr;
147
148