Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
unowned_ptr.h
Go to the documentation of this file.
1// Copyright 2017 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CORE_FXCRT_UNOWNED_PTR_H_
6#define CORE_FXCRT_UNOWNED_PTR_H_
7
8// UnownedPtr is a smart pointer class that behaves very much like a
9// standard C-style pointer. The advantages of using it over native T*
10// pointers are:
11//
12// 1. It documents the nature of the pointer with no need to add a comment
13// explaining that is it // Not owned.
14//
15// 2. An attempt to delete an unowned ptr will fail to compile rather
16// than silently succeeding, since it is a class and not a raw pointer.
17//
18// 3. It is initialized to nullptr by default.
19//
20// When implemented via PartitionAlloc, additional properties apply.
21//
22// 4. When built using one of the dangling pointer detectors, the class
23// detects that the object being pointed to remains alive.
24//
25// 5. When built against PartitionAlloc's BRP feature, it provides the same
26// UaF protections as base::raw_ptr<T>
27//
28// Hence, when using UnownedPtr, no dangling pointers are ever permitted,
29// even if they are not de-referenced after becoming dangling. The style of
30// programming required is that the lifetime an object containing an
31// UnownedPtr must be strictly less than the object to which it points.
32//
33// The same checks are also performed at assignment time to prove that the
34// old value was not a dangling pointer.
35//
36// The array indexing operator[] is not supported on an unowned ptr,
37// because an unowned ptr expresses a one to one relationship with some
38// other heap object. Use pdfium::span<> for the cases where indexing
39// into an unowned array is desired, which performs the same checks.
40
41#include "build/build_config.h"
42
43#if defined(PDF_USE_PARTITION_ALLOC)
44#include "partition_alloc/partition_alloc_buildflags.h"
45#include "partition_alloc/pointers/raw_ptr.h"
46
47#if !BUILDFLAG(USE_PARTITION_ALLOC)
48#error "pdf_use_partition_alloc=true requires use_partition_alloc=true"
49#endif
50
51#if BUILDFLAG(ENABLE_DANGLING_RAW_PTR_CHECKS) || BUILDFLAG(USE_ASAN_UNOWNED_PTR)
52#define UNOWNED_PTR_DANGLING_CHECKS
53#endif
54
55static_assert(raw_ptr<int>::kZeroOnConstruct, "Unsafe build arguments");
56static_assert(raw_ptr<int>::kZeroOnMove, "Unsafe build arguments");
57
58template <typename T>
59using UnownedPtr = raw_ptr<T>;
60
61#else // defined(PDF_USE_PARTITION_ALLOC)
62
63#include <cstddef>
64#include <cstdint>
65#include <functional>
66#include <type_traits>
67#include <utility>
68
69#include "core/fxcrt/unowned_ptr_exclusion.h"
70#include "third_party/base/compiler_specific.h"
71
72namespace fxcrt {
73
74template <class T>
75class TRIVIAL_ABI GSL_POINTER UnownedPtr {
76 public:
77 constexpr UnownedPtr() noexcept = default;
78
79 // Deliberately implicit to allow returning nullptrs.
80 // NOLINTNEXTLINE(runtime/explicit)
81 constexpr UnownedPtr(std::nullptr_t ptr) {}
82
83 explicit constexpr UnownedPtr(T* pObj) noexcept : m_pObj(pObj) {}
84
85 // Copy-construct an UnownedPtr.
86 // Required in addition to copy conversion constructor below.
87 constexpr UnownedPtr(const UnownedPtr& that) noexcept = default;
88
89 // Move-construct an UnownedPtr. After construction, |that| will be NULL.
90 // Required in addition to move conversion constructor below.
91 constexpr UnownedPtr(UnownedPtr&& that) noexcept
92 : m_pObj(that.ExtractAsDangling()) {}
93
94 // Copy-conversion constructor.
95 template <class U,
96 typename = typename std::enable_if<
98 UnownedPtr(const UnownedPtr<U>& that) : m_pObj(static_cast<U*>(that)) {}
99
100 // Move-conversion constructor.
101 template <class U,
102 typename = typename std::enable_if<
103 std::is_convertible<U*, T*>::value>::type>
104 UnownedPtr(UnownedPtr<U>&& that) noexcept
105 : m_pObj(that.ExtractAsDangling()) {}
106
107 // Assign an UnownedPtr from nullptr.
108 UnownedPtr& operator=(std::nullptr_t) noexcept {
109 m_pObj = nullptr;
110 return *this;
111 }
112
113 // Assign an UnownedPtr from a raw ptr.
114 UnownedPtr& operator=(T* that) noexcept {
115 m_pObj = that;
116 return *this;
117 }
118
119 // Copy-assign an UnownedPtr.
120 // Required in addition to copy conversion assignment below.
121 UnownedPtr& operator=(const UnownedPtr& that) noexcept = default;
122
123 // Move-assign an UnownedPtr. After assignment, |that| will be NULL.
124 // Required in addition to move conversion assignment below.
125 UnownedPtr& operator=(UnownedPtr&& that) noexcept {
126 if (*this != that) {
127 m_pObj = that.ExtractAsDangling();
128 }
129 return *this;
130 }
131
132 // Copy-convert assignment.
133 template <class U,
134 typename = typename std::enable_if<
135 std::is_convertible<U*, T*>::value>::type>
136 UnownedPtr& operator=(const UnownedPtr<U>& that) noexcept {
137 if (*this != that) {
138 m_pObj = static_cast<U*>(that);
139 }
140 return *this;
141 }
142
143 // Move-convert assignment. After assignment, |that| will be NULL.
144 template <class U,
145 typename = typename std::enable_if<
146 std::is_convertible<U*, T*>::value>::type>
147 UnownedPtr& operator=(UnownedPtr<U>&& that) noexcept {
148 if (*this != that) {
149 m_pObj = that.ExtractAsDangling();
150 }
151 return *this;
152 }
153
155 m_pObj = nullptr;
156 }
157
158 bool operator==(std::nullptr_t ptr) const { return m_pObj == nullptr; }
159 bool operator==(const UnownedPtr& that) const {
160 return m_pObj == static_cast<T*>(that);
161 }
162 bool operator<(const UnownedPtr& that) const {
163 return std::less<T*>()(m_pObj, static_cast<T*>(that));
164 }
165
166 operator T*() const noexcept { return m_pObj; }
167 T* get() const noexcept { return m_pObj; }
168
169 T* ExtractAsDangling() { return std::exchange(m_pObj, nullptr); }
170 void ClearAndDelete() { delete std::exchange(m_pObj, nullptr); }
171
172 explicit operator bool() const { return !!m_pObj; }
173 T& operator*() const { return *m_pObj; }
174 T* operator->() const { return m_pObj; }
175
176 private:
177 UNOWNED_PTR_EXCLUSION T* m_pObj = nullptr;
178};
179
180} // namespace fxcrt
181
182using fxcrt::UnownedPtr;
183
184#endif // defined(PDF_USE_PARTITION_ALLOC)
185
186namespace pdfium {
187
188// Type-deducing wrapper to make an UnownedPtr from an ordinary pointer,
189// since equivalent constructor is explicit.
190template <typename T>
192 return UnownedPtr<T>(that);
193}
194
195} // namespace pdfium
196
197#endif // CORE_FXCRT_UNOWNED_PTR_H_
uint32_t FX_HashCode_GetLoweredAsIfW(ByteStringView str)
uint32_t FX_HashCode_GetLoweredA(ByteStringView str)
uint32_t FX_HashCode_GetAsIfW(ByteStringView str)
uint32_t FX_HashCode_GetA(ByteStringView str)
CPDF_Creator(CPDF_Document *pDoc, RetainPtr< IFX_RetainableWriteStream > archive)
bool SetFileVersion(int32_t fileVersion)
void RemoveSecurity()
bool Create(uint32_t flags)
virtual FX_FILESIZE CurrentOffset() const =0
static RetainPtr< IFX_SeekableReadStream > CreateFromFilename(const char *filename)
Definition fx_stream.cpp:68
virtual size_t ReadBlock(pdfium::span< uint8_t > buffer)
Definition fx_stream.cpp:88
virtual FX_FILESIZE GetPosition()
Definition fx_stream.cpp:84
virtual bool ReadBlockAtOffset(pdfium::span< uint8_t > buffer, FX_FILESIZE offset)=0
virtual bool IsEOF()
Definition fx_stream.cpp:80
bool WriteBlock(pdfium::span< const uint8_t > buffer) override
Definition fx_stream.cpp:92
virtual bool WriteBlockAtOffset(pdfium::span< const uint8_t > data, FX_FILESIZE offset)=0
bool WriteBlock(pdfium::span< const uint8_t > buffer) override
Definition fx_stream.cpp:76
virtual bool Flush()=0
virtual FX_FILESIZE GetSize()=0
bool WriteFilesize(FX_FILESIZE size)
Definition fx_stream.cpp:61
bool WriteByte(uint8_t byte)
Definition fx_stream.cpp:51
bool WriteString(ByteStringView str)
Definition fx_stream.cpp:47
virtual ~IFX_WriteStream()=default
bool WriteDWord(uint32_t i)
Definition fx_stream.cpp:55
virtual bool WriteBlock(pdfium::span< const uint8_t > data)=0
void TrimLeft(ByteStringView targets)
bool IsValidLength(size_t length) const
Definition bytestring.h:121
bool Contains(ByteStringView lpszSub, size_t start=0) const
Definition bytestring.h:187
const_iterator begin() const
Definition bytestring.h:102
bool IsValidIndex(size_t index) const
Definition bytestring.h:120
static ByteString FormatFloat(float f)
ByteStringView AsStringView() const
Definition bytestring.h:87
uint32_t GetID() const
Definition bytestring.h:213
void Reserve(size_t len)
ByteString(const char *ptr)
bool EqualNoCase(ByteStringView str) const
static ByteString Format(const char *pFormat,...)
ByteString & operator+=(const ByteString &str)
bool operator!=(ByteStringView str) const
Definition bytestring.h:131
size_t GetLength() const
Definition bytestring.h:115
ByteString Substr(size_t first, size_t count) const
size_t GetStringLength() const
Definition bytestring.h:116
size_t InsertAtBack(char ch)
Definition bytestring.h:162
bool operator==(ByteStringView str) const
pdfium::span< const uint8_t > raw_span() const
Definition bytestring.h:97
void AllocCopy(ByteString &dest, size_t nCopyLen, size_t nCopyIndex) const
void AssignCopy(const char *pSrcData, size_t nSrcLen)
bool Contains(char ch, size_t start=0) const
Definition bytestring.h:191
absl::optional< size_t > ReverseFind(char ch) const
intptr_t ReferenceCountForTesting() const
size_t Delete(size_t index, size_t count=1)
const_reverse_iterator rend() const
Definition bytestring.h:111
ByteString & operator+=(char ch)
const uint8_t * raw_str() const
Definition bytestring.h:80
void ReallocBeforeWrite(size_t nNewLen)
ByteString(ByteStringView bstrc)
RetainPtr< StringData > m_pData
Definition bytestring.h:225
static ByteString FormatInteger(int i)
pdfium::span< char > GetBuffer(size_t nMinBufLength)
void Trim(char target)
bool operator==(const ByteString &other) const
bool operator==(const char *ptr) const
const_iterator end() const
Definition bytestring.h:103
ByteString(const ByteString &other)
size_t Remove(char ch)
ByteString(const char *pStr, size_t len)
bool operator<(ByteStringView str) const
ByteString(wchar_t)=delete
ByteString & operator+=(const char *str)
ByteString & operator+=(ByteStringView str)
ByteString(const uint8_t *pStr, size_t len)
size_t Replace(ByteStringView pOld, ByteStringView pNew)
absl::optional< size_t > Find(ByteStringView subStr, size_t start=0) const
bool operator!=(const ByteString &other) const
Definition bytestring.h:132
void SetAt(size_t index, char c)
ByteString & operator=(ByteStringView str)
ByteString(ByteStringView str1, ByteStringView str2)
void Trim(ByteStringView targets)
ByteString & operator=(const char *str)
static ByteString FormatV(const char *pFormat, va_list argList)
void TrimRight(char target)
const char * c_str() const
Definition bytestring.h:76
ByteString Substr(size_t offset) const
ByteString & operator=(const ByteString &that)
ByteString & operator=(ByteString &&that) noexcept
bool IsEmpty() const
Definition bytestring.h:119
void TrimRight(ByteStringView targets)
bool operator<(const ByteString &other) const
ByteString(const std::initializer_list< ByteStringView > &list)
CharType operator[](const size_t index) const
Definition bytestring.h:150
const_reverse_iterator rbegin() const
Definition bytestring.h:108
CharType Back() const
Definition bytestring.h:156
CharType Front() const
Definition bytestring.h:155
bool operator!=(const char *ptr) const
Definition bytestring.h:130
int Compare(ByteStringView str) const
size_t Insert(size_t index, char ch)
void ReleaseBuffer(size_t nNewLength)
ByteString(const fxcrt::ostringstream &outStream)
void TrimLeft(char target)
size_t InsertAtFront(char ch)
Definition bytestring.h:161
ByteString First(size_t count) const
void AllocBeforeWrite(size_t nNewLen)
bool operator<(const char *ptr) const
void Concat(const char *pSrcData, size_t nSrcLen)
absl::optional< size_t > Find(char ch, size_t start=0) const
ByteString(ByteString &&other) noexcept
ByteString Last(size_t count) const
U * AsRaw() const
Definition retain_ptr.h:107
RetainPtr & operator=(const RetainPtr< U > &that)
Definition retain_ptr.h:89
operator bool() const
Definition retain_ptr.h:149
void Unleak(T *ptr)
Definition retain_ptr.h:130
RetainPtr(std::nullptr_t ptr)
Definition retain_ptr.h:35
void Reset(T *obj=nullptr)
Definition retain_ptr.h:116
bool operator!=(const RetainPtr &that) const
Definition retain_ptr.h:133
void Swap(RetainPtr &that)
Definition retain_ptr.h:126
RetainPtr() noexcept=default
RetainPtr(RetainPtr &&that) noexcept
Definition retain_ptr.h:48
bool operator!=(const U &that) const
Definition retain_ptr.h:141
RetainPtr & operator=(const RetainPtr &that)
Definition retain_ptr.h:72
bool operator==(const RetainPtr &that) const
Definition retain_ptr.h:132
T & operator*() const
Definition retain_ptr.h:150
bool operator==(const U &that) const
Definition retain_ptr.h:136
RetainPtr & operator=(RetainPtr &&that) noexcept
Definition retain_ptr.h:80
T * operator->() const
Definition retain_ptr.h:151
RetainPtr & operator=(RetainPtr< U > &&that) noexcept
Definition retain_ptr.h:99
RetainPtr(const RetainPtr< U > &that)
Definition retain_ptr.h:54
T * Get() const noexcept
Definition retain_ptr.h:123
~RetainPtr()=default
bool operator<(const RetainPtr &that) const
Definition retain_ptr.h:145
RetainPtr(const RetainPtr &that) noexcept
Definition retain_ptr.h:44
operator T*() const noexcept
Definition retain_ptr.h:122
RetainPtr & operator=(std::nullptr_t) noexcept
Definition retain_ptr.h:65
UnownedPtr< T > BackPointer() const
Definition retain_ptr.h:125
RetainPtr(RetainPtr< U > &&that) noexcept
Definition retain_ptr.h:60
RetainPtr< U > As() const
Definition retain_ptr.h:112
RetainPtr(T *pObj) noexcept
Definition retain_ptr.h:37
bool HasOneRef() const
Definition retain_ptr.h:162
Retainable()=default
virtual ~Retainable()=default
T * get() const noexcept
UnownedPtr(const UnownedPtr< U > &that)
Definition unowned_ptr.h:98
bool operator==(const UnownedPtr &that) const
operator T*() const noexcept
UnownedPtr(UnownedPtr< U > &&that) noexcept
UnownedPtr & operator=(UnownedPtr< U > &&that) noexcept
constexpr UnownedPtr(const UnownedPtr &that) noexcept=default
constexpr UnownedPtr(T *pObj) noexcept
Definition unowned_ptr.h:83
UnownedPtr & operator=(T *that) noexcept
bool operator==(std::nullptr_t ptr) const
UnownedPtr & operator=(UnownedPtr &&that) noexcept
UnownedPtr & operator=(std::nullptr_t) noexcept
UnownedPtr & operator=(const UnownedPtr< U > &that) noexcept
T * operator->() const
constexpr UnownedPtr() noexcept=default
bool operator<(const UnownedPtr &that) const
UnownedPtr & operator=(const UnownedPtr &that) noexcept=default
T & operator*() const
constexpr UnownedPtr(UnownedPtr &&that) noexcept
Definition unowned_ptr.h:91
constexpr UnownedPtr(std::nullptr_t ptr)
Definition unowned_ptr.h:81
#define FPDFCREATE_INCREMENTAL
#define FPDFCREATE_NO_ORIGINAL
NOINLINE void FX_OutOfMemoryTerminate(size_t size)
Definition fx_memory.cpp:37
void FXMEM_DefaultFree(void *pointer)
Definition fx_memory.cpp:33
void * FXMEM_DefaultAlloc(size_t byte_size)
Definition fx_memory.cpp:21
void * FXMEM_DefaultCalloc(size_t num_elems, size_t byte_size)
Definition fx_memory.cpp:25
void * FXMEM_DefaultRealloc(void *pointer, size_t new_size)
Definition fx_memory.cpp:29
void * FX_ArrayBufferAllocate(size_t length)
void FX_InitializeMemoryAllocators()
void FX_ArrayBufferFree(void *data)
void * FX_ArrayBufferAllocateUninitialized(size_t length)
void * FX_Random_MT_Start(uint32_t dwSeed)
Definition fx_random.cpp:85
uint32_t FX_Random_MT_Generate(void *pContext)
Definition fx_random.cpp:96
void FX_Random_MT_Close(void *pContext)
#define FX_FILESIZE
Definition fx_types.h:19
bool operator==(const char *lhs, const ByteString &rhs)
Definition bytestring.h:233
bool operator<(const ByteStringView &lhs, const char *rhs)
Definition bytestring.h:251
ByteString operator+(const ByteString &str1, const ByteString &str2)
Definition bytestring.h:270
ByteString operator+(ByteStringView str1, const char *str2)
Definition bytestring.h:258
bool operator<(const ByteStringView &lhs, const ByteString &rhs)
Definition bytestring.h:248
ByteString operator+(const ByteString &str1, char ch)
Definition bytestring.h:273
ByteString operator+(const ByteString &str1, const char *str2)
Definition bytestring.h:279
void PrintTo(const ByteString &str, std::ostream *os)
ByteString operator+(char ch, const ByteString &str2)
Definition bytestring.h:276
ByteString operator+(const ByteString &str1, ByteStringView str2)
Definition bytestring.h:285
ByteString operator+(const char *str1, const ByteString &str2)
Definition bytestring.h:282
bool operator!=(ByteStringView lhs, const ByteString &rhs)
Definition bytestring.h:242
bool operator!=(const char *lhs, const ByteString &rhs)
Definition bytestring.h:239
ByteString operator+(ByteStringView str1, ByteStringView str2)
Definition bytestring.h:255
ByteString operator+(ByteStringView str1, char ch)
Definition bytestring.h:264
bool operator<(const char *lhs, const ByteString &rhs)
Definition bytestring.h:245
ByteString operator+(char ch, ByteStringView str2)
Definition bytestring.h:267
ByteString operator+(ByteStringView str1, const ByteString &str2)
Definition bytestring.h:288
ByteString operator+(const char *str1, ByteStringView str2)
Definition bytestring.h:261
bool operator==(ByteStringView lhs, const ByteString &rhs)
Definition bytestring.h:236
void * CallocOrDie2D(size_t w, size_t h, size_t member_size)
Definition fx_memory.cpp:79
void * AllocOrDie2D(size_t w, size_t h, size_t member_size)
Definition fx_memory.cpp:65
void * StringAllocOrDie(size_t num_members, size_t member_size)
Definition fx_memory.cpp:94
void * Alloc(size_t num_members, size_t member_size)
void * StringAlloc(size_t num_members, size_t member_size)
void * ReallocOrDie(void *ptr, size_t num_members, size_t member_size)
Definition fx_memory.cpp:86
void * Realloc(void *ptr, size_t num_members, size_t member_size)
void StringDealloc(void *ptr)
void * AllocOrDie(size_t num_members, size_t member_size)
Definition fx_memory.cpp:57
void * Calloc(size_t num_members, size_t member_size)
void * CallocOrDie(size_t num_members, size_t member_size)
Definition fx_memory.cpp:71
void Dealloc(void *ptr)
UnownedPtr< T > WrapUnowned(T *that)
RetainPtr< T > MakeRetain(Args &&... args)
Definition retain_ptr.h:209
RetainPtr< T > WrapRetain(T *that)
Definition retain_ptr.h:216
#define CHECK(cvref)
void operator()(void *ptr) const
pointer allocate(size_type n, const void *hint=0)
void deallocate(pointer p, size_type n)
const_pointer address(const_reference x) const noexcept
FxPartitionAllocAllocator() noexcept=default
size_type max_size() const noexcept
~FxPartitionAllocAllocator()=default
pointer address(reference x) const noexcept
FxPartitionAllocAllocator(const FxPartitionAllocAllocator< U, Alloc, Free > &other) noexcept
bool operator!=(const FxPartitionAllocAllocator &that)
void construct(U *p, Args &&... args)
bool operator==(const FxPartitionAllocAllocator &that)
FxPartitionAllocAllocator(const FxPartitionAllocAllocator &other) noexcept=default
void operator()(T *ptr) const
Definition retain_ptr.h:24
size_t operator()(const ByteString &str) const
Definition bytestring.h:315
#define UNOWNED_PTR_EXCLUSION