Skip to content

Commit 18df9d2

Browse files
committed
[libc++] Add an ABI setting to harden unique_ptr<T[]>::operator[] (llvm#91798)
This allows catching OOB accesses inside `unique_ptr<T[]>` when the size of the allocation is known. The size of the allocation can be known when the unique_ptr has been created with make_unique & friends or when the type necessitates an array cookie before the allocation. This is a re-aplpication of 45a09d1 which had been reverted in f11abac due to unrelated CI failures.
1 parent 8e6bba2 commit 18df9d2

File tree

13 files changed

+645
-58
lines changed

13 files changed

+645
-58
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
set(LIBCXX_HARDENING_MODE "fast" CACHE STRING "")
2-
set(LIBCXX_ABI_DEFINES "_LIBCPP_ABI_BOUNDED_ITERATORS;_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING;_LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR" CACHE STRING "")
2+
set(LIBCXX_ABI_DEFINES "_LIBCPP_ABI_BOUNDED_ITERATORS;_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING;_LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR;_LIBCPP_ABI_BOUNDED_UNIQUE_PTR" CACHE STRING "")

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ set(files
536536
__memory/allocator_arg_t.h
537537
__memory/allocator_destructor.h
538538
__memory/allocator_traits.h
539+
__memory/array_cookie.h
539540
__memory/assume_aligned.h
540541
__memory/auto_ptr.h
541542
__memory/builtin_new_allocator.h

libcxx/include/__configuration/abi.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@
181181
# define _LIBCPP_ABI_NO_COMPRESSED_PAIR_PADDING
182182
#endif
183183

184+
// Tracks the bounds of the array owned by std::unique_ptr<T[]>, allowing it to trap when accessed out-of-bounds.
185+
// Note that limited bounds checking is also available outside of this ABI configuration, but only some categories
186+
// of types can be checked.
187+
//
188+
// ABI impact: This causes the layout of std::unique_ptr<T[]> to change and its size to increase.
189+
// #define _LIBCPP_ABI_BOUNDED_UNIQUE_PTR
190+
184191
#if defined(_LIBCPP_COMPILER_CLANG_BASED)
185192
# if defined(__APPLE__)
186193
# if defined(__i386__) || defined(__x86_64__)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// -*- C++ -*-
2+
//===----------------------------------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef _LIBCPP___MEMORY_ARRAY_COOKIE_H
11+
#define _LIBCPP___MEMORY_ARRAY_COOKIE_H
12+
13+
#include <__config>
14+
#include <__configuration/abi.h>
15+
#include <__type_traits/integral_constant.h>
16+
#include <__type_traits/is_trivially_destructible.h>
17+
#include <__type_traits/negation.h>
18+
#include <cstddef>
19+
20+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21+
# pragma GCC system_header
22+
#endif
23+
24+
_LIBCPP_BEGIN_NAMESPACE_STD
25+
26+
// Trait representing whether a type requires an array cookie at the start of its allocation when
27+
// allocated as `new T[n]` and deallocated as `delete array`.
28+
//
29+
// Under the Itanium C++ ABI [1], we know that an array cookie is available unless `T` is trivially
30+
// destructible and the call to `operator delete[]` is not a sized operator delete. Under ABIs other
31+
// than the Itanium ABI, we assume there are no array cookies.
32+
//
33+
// [1]: https://itanium-cxx-abi.github.io/cxx-abi/abi.html#array-cookies
34+
#ifdef _LIBCPP_ABI_ITANIUM
35+
// TODO: Use a builtin instead
36+
// TODO: We should factor in the choice of the usual deallocation function in this determination.
37+
template <class _Tp>
38+
struct __has_array_cookie : _Not<is_trivially_destructible<_Tp> > {};
39+
#else
40+
template <class _Tp>
41+
struct __has_array_cookie : false_type {};
42+
#endif
43+
44+
template <class _Tp>
45+
// Avoid failures when -fsanitize-address-poison-custom-array-cookie is enabled
46+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_SANITIZE("address") size_t __get_array_cookie(_Tp const* __ptr) {
47+
static_assert(
48+
__has_array_cookie<_Tp>::value, "Trying to access the array cookie of a type that is not guaranteed to have one");
49+
size_t const* __cookie = reinterpret_cast<size_t const*>(__ptr) - 1; // TODO: Use a builtin instead
50+
return *__cookie;
51+
}
52+
53+
_LIBCPP_END_NAMESPACE_STD
54+
55+
#endif // _LIBCPP___MEMORY_ARRAY_COOKIE_H

libcxx/include/__memory/unique_ptr.h

Lines changed: 123 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@
1010
#ifndef _LIBCPP___MEMORY_UNIQUE_PTR_H
1111
#define _LIBCPP___MEMORY_UNIQUE_PTR_H
1212

13+
#include <__assert>
1314
#include <__compare/compare_three_way.h>
1415
#include <__compare/compare_three_way_result.h>
1516
#include <__compare/three_way_comparable.h>
1617
#include <__config>
1718
#include <__functional/hash.h>
1819
#include <__functional/operations.h>
1920
#include <__memory/allocator_traits.h> // __pointer
21+
#include <__memory/array_cookie.h>
2022
#include <__memory/auto_ptr.h>
2123
#include <__memory/compressed_pair.h>
24+
#include <__memory/pointer_traits.h>
2225
#include <__type_traits/add_lvalue_reference.h>
2326
#include <__type_traits/common_type.h>
2427
#include <__type_traits/conditional.h>
@@ -27,6 +30,7 @@
2730
#include <__type_traits/integral_constant.h>
2831
#include <__type_traits/is_array.h>
2932
#include <__type_traits/is_assignable.h>
33+
#include <__type_traits/is_constant_evaluated.h>
3034
#include <__type_traits/is_constructible.h>
3135
#include <__type_traits/is_convertible.h>
3236
#include <__type_traits/is_function.h>
@@ -41,7 +45,9 @@
4145
#include <__utility/declval.h>
4246
#include <__utility/forward.h>
4347
#include <__utility/move.h>
48+
#include <__utility/private_constructor_tag.h>
4449
#include <cstddef>
50+
#include <cstdint>
4551

4652
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
4753
# pragma GCC system_header
@@ -292,6 +298,91 @@ class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
292298
}
293299
};
294300

301+
// Bounds checking in unique_ptr<T[]>
302+
// ==================================
303+
//
304+
// We provide some helper classes that allow bounds checking when accessing a unique_ptr<T[]>.
305+
// There are a few cases where bounds checking can be implemented:
306+
//
307+
// 1. When an array cookie (see [1]) exists at the beginning of the array allocation, we are
308+
// able to reuse that cookie to extract the size of the array and perform bounds checking.
309+
// An array cookie is a size inserted at the beginning of the allocation by the compiler.
310+
// That size is inserted implicitly when doing `new T[n]` in some cases, and its purpose
311+
// is to allow the runtime to destroy the `n` array elements when doing `delete array`.
312+
// When we are able to use array cookies, we reuse information already available in the
313+
// current runtime, so bounds checking does not require changing libc++'s ABI.
314+
//
315+
// 2. When the "bounded unique_ptr" ABI configuration (controlled by `_LIBCPP_ABI_BOUNDED_UNIQUE_PTR`)
316+
// is enabled, we store the size of the allocation (when it is known) so we can check it when
317+
// indexing into the `unique_ptr`. That changes the layout of `std::unique_ptr<T[]>`, which is
318+
// an ABI break from the default configuration.
319+
//
320+
// Note that even under this ABI configuration, we can't always know the size of the unique_ptr.
321+
// Indeed, the size of the allocation can only be known when the unique_ptr is created via
322+
// make_unique or a similar API. For example, it can't be known when constructed from an arbitrary
323+
// pointer, in which case we are not able to check the bounds on access:
324+
//
325+
// unique_ptr<T[], MyDeleter> ptr(new T[3]);
326+
//
327+
// When we don't know the size of the allocation via the API used to create the unique_ptr, we
328+
// try to fall back to using an array cookie when available.
329+
//
330+
// Finally, note that when this ABI configuration is enabled, we have no choice but to always
331+
// make space for a size to be stored in the unique_ptr. Indeed, while we might want to avoid
332+
// storing the size when an array cookie is available, knowing whether an array cookie is available
333+
// requires the type stored in the unique_ptr to be complete, while unique_ptr can normally
334+
// accommodate incomplete types.
335+
//
336+
// (1) Implementation where we rely on the array cookie to know the size of the allocation, if
337+
// an array cookie exists.
338+
struct __unique_ptr_array_bounds_stateless {
339+
__unique_ptr_array_bounds_stateless() = default;
340+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __unique_ptr_array_bounds_stateless(size_t) {}
341+
342+
template <class _Tp, __enable_if_t<__has_array_cookie<_Tp>::value, int> = 0>
343+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool __in_bounds(_Tp* __ptr, size_t __index) const {
344+
// In constant expressions, we can't check the array cookie so we just pretend that the index
345+
// is in-bounds. The compiler catches invalid accesses anyway.
346+
if (__libcpp_is_constant_evaluated())
347+
return true;
348+
size_t __cookie = std::__get_array_cookie(__ptr);
349+
return __index < __cookie;
350+
}
351+
352+
template <class _Tp, __enable_if_t<!__has_array_cookie<_Tp>::value, int> = 0>
353+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool __in_bounds(_Tp*, size_t) const {
354+
return true; // If we don't have an array cookie, we assume the access is in-bounds
355+
}
356+
};
357+
358+
// (2) Implementation where we store the size in the class whenever we have it.
359+
//
360+
// Semantically, we'd need to store the size as an optional<size_t>. However, since that
361+
// is really heavy weight, we instead store a size_t and use SIZE_MAX as a magic value
362+
// meaning that we don't know the size.
363+
struct __unique_ptr_array_bounds_stored {
364+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __unique_ptr_array_bounds_stored() : __size_(SIZE_MAX) {}
365+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __unique_ptr_array_bounds_stored(size_t __size) : __size_(__size) {}
366+
367+
// Use the array cookie if there's one
368+
template <class _Tp, __enable_if_t<__has_array_cookie<_Tp>::value, int> = 0>
369+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool __in_bounds(_Tp* __ptr, size_t __index) const {
370+
if (__libcpp_is_constant_evaluated())
371+
return true;
372+
size_t __cookie = std::__get_array_cookie(__ptr);
373+
return __index < __cookie;
374+
}
375+
376+
// Otherwise, fall back on the stored size (if any)
377+
template <class _Tp, __enable_if_t<!__has_array_cookie<_Tp>::value, int> = 0>
378+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool __in_bounds(_Tp*, size_t __index) const {
379+
return __index < __size_;
380+
}
381+
382+
private:
383+
size_t __size_;
384+
};
385+
295386
template <class _Tp, class _Dp>
296387
class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
297388
public:
@@ -300,8 +391,9 @@ class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp>
300391
typedef typename __pointer<_Tp, deleter_type>::type pointer;
301392

302393
// A unique_ptr contains the following members which may be trivially relocatable:
303-
// - pointer : this may be trivially relocatable, so it's checked
394+
// - pointer: this may be trivially relocatable, so it's checked
304395
// - deleter_type: this may be trivially relocatable, so it's checked
396+
// - (optionally) size: this is trivially relocatable
305397
//
306398
// This unique_ptr implementation only contains a pointer to the unique object and a deleter, so there are no
307399
// references to itself. This means that the entire structure is trivially relocatable if its members are.
@@ -311,7 +403,16 @@ class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp>
311403
void>;
312404

313405
private:
406+
template <class _Up, class _OtherDeleter>
407+
friend class unique_ptr;
408+
314409
_LIBCPP_COMPRESSED_PAIR(pointer, __ptr_, deleter_type, __deleter_);
410+
#ifdef _LIBCPP_ABI_BOUNDED_UNIQUE_PTR
411+
using _BoundsChecker = __unique_ptr_array_bounds_stored;
412+
#else
413+
using _BoundsChecker = __unique_ptr_array_bounds_stateless;
414+
#endif
415+
_LIBCPP_NO_UNIQUE_ADDRESS _BoundsChecker __checker_;
315416

316417
template <class _From>
317418
struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
@@ -373,6 +474,12 @@ class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp>
373474
: __ptr_(__p),
374475
__deleter_() {}
375476

477+
// Private constructor used by make_unique & friends to pass the size that was allocated
478+
template <class _Tag, class _Ptr, __enable_if_t<is_same<_Tag, __private_constructor_tag>::value, int> = 0>
479+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit unique_ptr(_Tag, _Ptr __ptr, size_t __size) _NOEXCEPT
480+
: __ptr_(__ptr),
481+
__checker_(__size) {}
482+
376483
template <class _Pp,
377484
bool _Dummy = true,
378485
class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
@@ -411,11 +518,13 @@ class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp>
411518

412519
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(unique_ptr&& __u) _NOEXCEPT
413520
: __ptr_(__u.release()),
414-
__deleter_(std::forward<deleter_type>(__u.get_deleter())) {}
521+
__deleter_(std::forward<deleter_type>(__u.get_deleter())),
522+
__checker_(std::move(__u.__checker_)) {}
415523

416524
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
417525
reset(__u.release());
418526
__deleter_ = std::forward<deleter_type>(__u.get_deleter());
527+
__checker_ = std::move(std::move(__u.__checker_));
419528
return *this;
420529
}
421530

@@ -425,7 +534,8 @@ class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp>
425534
class = _EnableIfDeleterConvertible<_Ep> >
426535
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
427536
: __ptr_(__u.release()),
428-
__deleter_(std::forward<_Ep>(__u.get_deleter())) {}
537+
__deleter_(std::forward<_Ep>(__u.get_deleter())),
538+
__checker_(std::move(__u.__checker_)) {}
429539

430540
template <class _Up,
431541
class _Ep,
@@ -434,6 +544,7 @@ class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp>
434544
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
435545
reset(__u.release());
436546
__deleter_ = std::forward<_Ep>(__u.get_deleter());
547+
__checker_ = std::move(__u.__checker_);
437548
return *this;
438549
}
439550

@@ -451,6 +562,8 @@ class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp>
451562
}
452563

453564
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __add_lvalue_reference_t<_Tp> operator[](size_t __i) const {
565+
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__checker_.__in_bounds(std::__to_address(__ptr_), __i),
566+
"unique_ptr<T[]>::operator[](index): index out of range");
454567
return __ptr_[__i];
455568
}
456569
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 pointer get() const _NOEXCEPT { return __ptr_; }
@@ -467,20 +580,24 @@ class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp>
467580
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 pointer release() _NOEXCEPT {
468581
pointer __t = __ptr_;
469582
__ptr_ = pointer();
583+
// The deleter and the optional bounds-checker are left unchanged. The bounds-checker
584+
// will be reinitialized appropriately when/if the unique_ptr gets assigned-to or reset.
470585
return __t;
471586
}
472587

473588
template <class _Pp, __enable_if_t<_CheckArrayPointerConversion<_Pp>::value, int> = 0>
474589
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void reset(_Pp __p) _NOEXCEPT {
475590
pointer __tmp = __ptr_;
476591
__ptr_ = __p;
592+
__checker_ = _BoundsChecker();
477593
if (__tmp)
478594
__deleter_(__tmp);
479595
}
480596

481597
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void reset(nullptr_t = nullptr) _NOEXCEPT {
482598
pointer __tmp = __ptr_;
483599
__ptr_ = nullptr;
600+
__checker_ = _BoundsChecker();
484601
if (__tmp)
485602
__deleter_(__tmp);
486603
}
@@ -489,6 +606,7 @@ class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp>
489606
using std::swap;
490607
swap(__ptr_, __u.__ptr_);
491608
swap(__deleter_, __u.__deleter_);
609+
swap(__checker_, __u.__checker_);
492610
}
493611
};
494612

@@ -645,7 +763,7 @@ template <class _Tp>
645763
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 typename __unique_if<_Tp>::__unique_array_unknown_bound
646764
make_unique(size_t __n) {
647765
typedef __remove_extent_t<_Tp> _Up;
648-
return unique_ptr<_Tp>(new _Up[__n]());
766+
return unique_ptr<_Tp>(__private_constructor_tag(), new _Up[__n](), __n);
649767
}
650768

651769
template <class _Tp, class... _Args>
@@ -664,7 +782,7 @@ make_unique_for_overwrite() {
664782
template <class _Tp>
665783
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 typename __unique_if<_Tp>::__unique_array_unknown_bound
666784
make_unique_for_overwrite(size_t __n) {
667-
return unique_ptr<_Tp>(new __remove_extent_t<_Tp>[__n]);
785+
return unique_ptr<_Tp>(__private_constructor_tag(), new __remove_extent_t<_Tp>[__n], __n);
668786
}
669787

670788
template <class _Tp, class... _Args>

libcxx/include/module.modulemap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,7 @@ module std_private_memory_allocator [system] { header "__m
14981498
module std_private_memory_allocator_arg_t [system] { header "__memory/allocator_arg_t.h" }
14991499
module std_private_memory_allocator_destructor [system] { header "__memory/allocator_destructor.h" }
15001500
module std_private_memory_allocator_traits [system] { header "__memory/allocator_traits.h" }
1501+
module std_private_memory_array_cookie [system] { header "__memory/array_cookie.h" }
15011502
module std_private_memory_assume_aligned [system] { header "__memory/assume_aligned.h" }
15021503
module std_private_memory_auto_ptr [system] { header "__memory/auto_ptr.h" }
15031504
module std_private_memory_builtin_new_allocator [system] {

libcxx/test/libcxx/containers/associative/unord.map/abi.compile.pass.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
// UNSUPPORTED: libcpp-has-abi-fix-unordered-container-size-type, libcpp-abi-no-compressed-pair-padding
1010

11+
// std::unique_ptr is used as an implementation detail of the unordered containers, so the layout of
12+
// unordered containers changes when bounded unique_ptr is enabled.
13+
// UNSUPPORTED: libcpp-has-abi-bounded-unique_ptr
14+
1115
#include <cstdint>
1216
#include <unordered_map>
1317

libcxx/test/libcxx/containers/associative/unord.set/abi.compile.pass.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
// UNSUPPORTED: libcpp-has-abi-fix-unordered-container-size-type, libcpp-abi-no-compressed-pair-padding
1010

11+
// std::unique_ptr is used as an implementation detail of the unordered containers, so the layout of
12+
// unordered containers changes when bounded unique_ptr is enabled.
13+
// UNSUPPORTED: libcpp-has-abi-bounded-unique_ptr
14+
1115
#include <cstdint>
1216
#include <unordered_set>
1317

0 commit comments

Comments
 (0)