1 /* Poison symbols at compile time.
3 Copyright (C) 2017-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #ifndef COMMON_POISON_H
21 #define COMMON_POISON_H
26 /* Poison memset of non-POD types. The idea is catching invalid
27 initialization of non-POD structs that is easy to be introduced as
28 side effect of refactoring. For example, say this:
30 struct S { VEC(foo_s) *m_data; };
32 is converted to this at some point:
35 S() { m_data.reserve (10); }
36 std::vector<foo> m_data;
39 and old code was initializing S objects like this:
42 memset (&s, 0, sizeof (S)); // whoops, now wipes vector.
44 Declaring memset as deleted for non-POD types makes the memset above
45 be a compile-time error. */
47 /* Helper for SFINAE. True if "T *" is memsettable. I.e., if T is
48 either void, or POD. */
51 : gdb::Or
<std::is_void
<T
>,
52 gdb::And
<std::is_standard_layout
<T
>, std::is_trivial
<T
>>>
56 typename
= gdb::Requires
<gdb::Not
<IsMemsettable
<T
>>>>
57 void *memset (T
*s
, int c
, size_t n
) = delete;
59 /* Similarly, poison memcpy and memmove of non trivially-copyable
60 types, which is undefined. */
62 /* True if "T *" is relocatable. I.e., copyable with memcpy/memmove.
63 I.e., T is either trivially copyable, or void. */
66 : gdb::Or
<std::is_void
<T
>,
67 std::is_trivially_copyable
<T
>>
70 /* True if both source and destination are relocatable. */
72 template <typename D
, typename S
>
73 using BothAreRelocatable
74 = gdb::And
<IsRelocatable
<D
>, IsRelocatable
<S
>>;
76 template <typename D
, typename S
,
77 typename
= gdb::Requires
<gdb::Not
<BothAreRelocatable
<D
, S
>>>>
78 void *memcpy (D
*dest
, const S
*src
, size_t n
) = delete;
80 template <typename D
, typename S
,
81 typename
= gdb::Requires
<gdb::Not
<BothAreRelocatable
<D
, S
>>>>
82 void *memmove (D
*dest
, const S
*src
, size_t n
) = delete;
84 /* Poison XNEW and friends to catch usages of malloc-style allocations on
85 objects that require new/delete. */
88 using IsMallocable
= std::is_trivially_constructible
<T
>;
91 using IsFreeable
= gdb::Or
<std::is_trivially_destructible
<T
>, std::is_void
<T
>>;
93 template <typename T
, typename
= gdb::Requires
<gdb::Not
<IsFreeable
<T
>>>>
94 void free (T
*ptr
) = delete;
100 static_assert (IsMallocable
<T
>::value
, "Trying to use XNEW with a non-POD \
101 data type. Use operator new instead.");
106 #define XNEW(T) xnew<T>()
112 static_assert (IsMallocable
<T
>::value
, "Trying to use XCNEW with a non-POD \
113 data type. Use operator new instead.");
118 #define XCNEW(T) xcnew<T>()
124 static_assert (IsFreeable
<T
>::value
, "Trying to use XDELETE with a non-POD \
125 data type. Use operator delete instead.");
130 #define XDELETE(P) xdelete (P)
136 static_assert (IsMallocable
<T
>::value
, "Trying to use XNEWVEC with a \
137 non-POD data type. Use operator new[] (or std::vector) instead.");
138 return XNEWVEC (T
, n
);
142 #define XNEWVEC(T, N) xnewvec<T> (N)
148 static_assert (IsMallocable
<T
>::value
, "Trying to use XCNEWVEC with a \
149 non-POD data type. Use operator new[] (or std::vector) instead.");
150 return XCNEWVEC (T
, n
);
154 #define XCNEWVEC(T, N) xcnewvec<T> (N)
158 xresizevec (T
*p
, size_t n
)
160 static_assert (IsMallocable
<T
>::value
, "Trying to use XRESIZEVEC with a \
161 non-POD data type.");
162 return XRESIZEVEC (T
, p
, n
);
166 #define XRESIZEVEC(T, P, N) xresizevec<T> (P, N)
172 static_assert (IsFreeable
<T
>::value
, "Trying to use XDELETEVEC with a \
173 non-POD data type. Use operator delete[] (or std::vector) instead.");
178 #define XDELETEVEC(P) xdeletevec (P)
184 static_assert (IsMallocable
<T
>::value
, "Trying to use XNEWVAR with a \
185 non-POD data type.");
186 return XNEWVAR (T
, s
);;
190 #define XNEWVAR(T, S) xnewvar<T> (S)
196 static_assert (IsMallocable
<T
>::value
, "Trying to use XCNEWVAR with a \
197 non-POD data type.");
198 return XCNEWVAR (T
, s
);
202 #define XCNEWVAR(T, S) xcnewvar<T> (S)
206 xresizevar (T
*p
, size_t s
)
208 static_assert (IsMallocable
<T
>::value
, "Trying to use XRESIZEVAR with a \
209 non-POD data type.");
210 return XRESIZEVAR (T
, p
, s
);
214 #define XRESIZEVAR(T, P, S) xresizevar<T> (P, S)
220 static_assert (IsMallocable
<T
>::value
, "Trying to use XOBNEW with a \
221 non-POD data type.");
222 return XOBNEW (ob
, T
);
226 #define XOBNEW(O, T) xobnew<T> (O)
230 xobnewvec (obstack
*ob
, size_t n
)
232 static_assert (IsMallocable
<T
>::value
, "Trying to use XOBNEWVEC with a \
233 non-POD data type.");
234 return XOBNEWVEC (ob
, T
, n
);
238 #define XOBNEWVEC(O, T, N) xobnewvec<T> (O, N)
240 #endif /* COMMON_POISON_H */