[compiler-rt] [scudo] Group type traits into a single header (NFC) (PR #118888)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 5 23:15:31 PST 2024
https://github.com/ChiaHungDuan updated https://github.com/llvm/llvm-project/pull/118888
>From 37093fc2aacd4dfe6eaba67f0cebb521b0fb9884 Mon Sep 17 00:00:00 2001
From: Chia-hung Duan <chiahungduan at google.com>
Date: Thu, 5 Dec 2024 22:11:13 +0000
Subject: [PATCH 1/2] [scudo] Group type traits into a single header (NFC)
---
.../lib/scudo/standalone/CMakeLists.txt | 1 +
.../standalone/allocator_config_wrapper.h | 30 +-----------
compiler-rt/lib/scudo/standalone/list.h | 12 +----
.../lib/scudo/standalone/type_traits.h | 49 +++++++++++++++++++
4 files changed, 52 insertions(+), 40 deletions(-)
create mode 100644 compiler-rt/lib/scudo/standalone/type_traits.h
diff --git a/compiler-rt/lib/scudo/standalone/CMakeLists.txt b/compiler-rt/lib/scudo/standalone/CMakeLists.txt
index dc700cec9becbf..3f9ae866a75533 100644
--- a/compiler-rt/lib/scudo/standalone/CMakeLists.txt
+++ b/compiler-rt/lib/scudo/standalone/CMakeLists.txt
@@ -98,6 +98,7 @@ set(SCUDO_HEADERS
tsd_exclusive.h
tsd_shared.h
tsd.h
+ type_traits.h
vector.h
wrappers_c_checks.h
wrappers_c.h
diff --git a/compiler-rt/lib/scudo/standalone/allocator_config_wrapper.h b/compiler-rt/lib/scudo/standalone/allocator_config_wrapper.h
index 5477236ac1f397..ea12a5b1447f69 100644
--- a/compiler-rt/lib/scudo/standalone/allocator_config_wrapper.h
+++ b/compiler-rt/lib/scudo/standalone/allocator_config_wrapper.h
@@ -12,35 +12,7 @@
#include "condition_variable.h"
#include "internal_defs.h"
#include "secondary.h"
-
-namespace {
-
-template <typename T> struct removeConst {
- using type = T;
-};
-template <typename T> struct removeConst<const T> {
- using type = T;
-};
-
-// This is only used for SFINAE when detecting if a type is defined.
-template <typename T> struct voidAdaptor {
- using type = void;
-};
-
-// This is used for detecting the case that defines the flag with wrong type and
-// it'll be viewed as undefined optional flag.
-template <typename L, typename R> struct assertSameType {
- template <typename, typename> struct isSame {
- static constexpr bool value = false;
- };
- template <typename T> struct isSame<T, T> {
- static constexpr bool value = true;
- };
- static_assert(isSame<L, R>::value, "Flag type mismatches");
- using type = R;
-};
-
-} // namespace
+#include "type_traits.h"
namespace scudo {
diff --git a/compiler-rt/lib/scudo/standalone/list.h b/compiler-rt/lib/scudo/standalone/list.h
index c6bd32a8fa3251..5c34cbb049e602 100644
--- a/compiler-rt/lib/scudo/standalone/list.h
+++ b/compiler-rt/lib/scudo/standalone/list.h
@@ -10,17 +10,7 @@
#define SCUDO_LIST_H_
#include "internal_defs.h"
-
-// TODO: Move the helpers to a header.
-namespace {
-template <typename T> struct isPointer {
- static constexpr bool value = false;
-};
-
-template <typename T> struct isPointer<T *> {
- static constexpr bool value = true;
-};
-} // namespace
+#include "type_traits.h"
namespace scudo {
diff --git a/compiler-rt/lib/scudo/standalone/type_traits.h b/compiler-rt/lib/scudo/standalone/type_traits.h
new file mode 100644
index 00000000000000..86aa0431df4347
--- /dev/null
+++ b/compiler-rt/lib/scudo/standalone/type_traits.h
@@ -0,0 +1,49 @@
+//===-- type_traits.h -------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef SCUDO_TYPE_TRAITS_H_
+#define SCUDO_TYPE_TRAITS_H_
+
+namespace scudo {
+
+template <typename T> struct removeConst {
+ using type = T;
+};
+template <typename T> struct removeConst<const T> {
+ using type = T;
+};
+
+// This is only used for SFINAE when detecting if a type is defined.
+template <typename T> struct voidAdaptor {
+ using type = void;
+};
+
+// This is used for detecting the case that defines the flag with wrong type and
+// it'll be viewed as undefined optional flag.
+template <typename L, typename R> struct assertSameType {
+ template <typename, typename> struct isSame {
+ static constexpr bool value = false;
+ };
+ template <typename T> struct isSame<T, T> {
+ static constexpr bool value = true;
+ };
+ static_assert(isSame<L, R>::value, "Flag type mismatches");
+ using type = R;
+};
+
+template <typename T> struct isPointer {
+ static constexpr bool value = false;
+};
+
+template <typename T> struct isPointer<T *> {
+ static constexpr bool value = true;
+};
+
+} // namespace scudo
+
+#endif // SCUDO_TYPE_TRAITS_H_
>From ee9a84f0587f16eb7a3f4697df120119f3ba91ec Mon Sep 17 00:00:00 2001
From: Chia-hung Duan <chiahungduan at google.com>
Date: Fri, 6 Dec 2024 07:11:33 +0000
Subject: [PATCH 2/2] [scudo] Clean the TODO in list.h
* Finished the type and size verification
* Remove the TODO for checking if array size can be fit into LinkTy
because if there's a truncation happens, other DCHECK like offset
checking will catch the failure. In addition, it's supposed to be a
rare case.
---
compiler-rt/lib/scudo/standalone/list.h | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/compiler-rt/lib/scudo/standalone/list.h b/compiler-rt/lib/scudo/standalone/list.h
index 5c34cbb049e602..e7c69e5bb88d67 100644
--- a/compiler-rt/lib/scudo/standalone/list.h
+++ b/compiler-rt/lib/scudo/standalone/list.h
@@ -48,10 +48,11 @@ class LinkOp {
template <class T> class LinkOp<T, /*LinkWithPtr=*/false> {
public:
- using LinkTy = decltype(T::Next);
+ using LinkTy = typename assertSameType<
+ typename removeConst<decltype(T::Next)>::type,
+ typename removeConst<decltype(T::EndOfListVal)>::type>::type;
LinkOp() = default;
- // TODO: Check if the `BaseSize` can fit in `Size`.
LinkOp(T *BaseT, uptr BaseSize)
: Base(BaseT), Size(static_cast<LinkTy>(BaseSize)) {}
void init(T *LinkBase, uptr BaseSize) {
@@ -70,11 +71,12 @@ template <class T> class LinkOp<T, /*LinkWithPtr=*/false> {
}
// Set `X->Next` to `Next`.
void setNext(T *X, T *Next) const {
- // TODO: Check if the offset fits in the size of `LinkTy`.
- if (Next == nullptr)
+ if (Next == nullptr) {
X->Next = getEndOfListVal();
- else
+ } else {
+ DCHECK_LE(static_cast<LinkTy>(Next - Base), Size);
X->Next = static_cast<LinkTy>(Next - Base);
+ }
}
T *getPrev(T *X) const {
@@ -94,7 +96,6 @@ template <class T> class LinkOp<T, /*LinkWithPtr=*/false> {
X->Prev = static_cast<LinkTy>(Prev - Base);
}
- // TODO: `LinkTy` should be the same as decltype(T::EndOfListVal).
LinkTy getEndOfListVal() const { return T::EndOfListVal; }
protected:
More information about the llvm-commits
mailing list