1 /* Poison symbols at compile time.
3 Copyright (C) 2017-2020 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
>,
56 typename
= gdb::Requires
<gdb::Not
<IsMemsettable
<T
>>>>
57 void *memset (T
*s
, int c
, size_t n
) = delete;
59 #if HAVE_IS_TRIVIALLY_COPYABLE
61 /* Similarly, poison memcpy and memmove of non trivially-copyable
62 types, which is undefined. */
64 /* True if "T *" is relocatable. I.e., copyable with memcpy/memmove.
65 I.e., T is either trivially copyable, or void. */
68 : gdb::Or
<std::is_void
<T
>,
69 std::is_trivially_copyable
<T
>>
72 /* True if both source and destination are relocatable. */
74 template <typename D
, typename S
>
75 using BothAreRelocatable
76 = gdb::And
<IsRelocatable
<D
>, IsRelocatable
<S
>>;
78 template <typename D
, typename S
,
79 typename
= gdb::Requires
<gdb::Not
<BothAreRelocatable
<D
, S
>>>>
80 void *memcpy (D
*dest
, const S
*src
, size_t n
) = delete;
82 template <typename D
, typename S
,
83 typename
= gdb::Requires
<gdb::Not
<BothAreRelocatable
<D
, S
>>>>
84 void *memmove (D
*dest
, const S
*src
, size_t n
) = delete;
86 #endif /* HAVE_IS_TRIVIALLY_COPYABLE */
88 /* Poison XNEW and friends to catch usages of malloc-style allocations on
89 objects that require new/delete. */
92 #if HAVE_IS_TRIVIALLY_CONSTRUCTIBLE
93 using IsMallocable
= std::is_trivially_constructible
<T
>;
95 using IsMallocable
= std::true_type
;
99 using IsFreeable
= gdb::Or
<std::is_trivially_destructible
<T
>, std::is_void
<T
>>;
101 template <typename T
, typename
= gdb::Requires
<gdb::Not
<IsFreeable
<T
>>>>
102 void free (T
*ptr
) = delete;
108 static_assert (IsMallocable
<T
>::value
, "Trying to use XNEW with a non-POD \
109 data type. Use operator new instead.");
114 #define XNEW(T) xnew<T>()
120 static_assert (IsMallocable
<T
>::value
, "Trying to use XCNEW with a non-POD \
121 data type. Use operator new instead.");
126 #define XCNEW(T) xcnew<T>()
132 static_assert (IsFreeable
<T
>::value
, "Trying to use XDELETE with a non-POD \
133 data type. Use operator delete instead.");
138 #define XDELETE(P) xdelete (P)
144 static_assert (IsMallocable
<T
>::value
, "Trying to use XNEWVEC with a \
145 non-POD data type. Use operator new[] (or std::vector) instead.");
146 return XNEWVEC (T
, n
);
150 #define XNEWVEC(T, N) xnewvec<T> (N)
156 static_assert (IsMallocable
<T
>::value
, "Trying to use XCNEWVEC with a \
157 non-POD data type. Use operator new[] (or std::vector) instead.");
158 return XCNEWVEC (T
, n
);
162 #define XCNEWVEC(T, N) xcnewvec<T> (N)
166 xresizevec (T
*p
, size_t n
)
168 static_assert (IsMallocable
<T
>::value
, "Trying to use XRESIZEVEC with a \
169 non-POD data type.");
170 return XRESIZEVEC (T
, p
, n
);
174 #define XRESIZEVEC(T, P, N) xresizevec<T> (P, N)
180 static_assert (IsFreeable
<T
>::value
, "Trying to use XDELETEVEC with a \
181 non-POD data type. Use operator delete[] (or std::vector) instead.");
186 #define XDELETEVEC(P) xdeletevec (P)
192 static_assert (IsMallocable
<T
>::value
, "Trying to use XNEWVAR with a \
193 non-POD data type.");
194 return XNEWVAR (T
, s
);;
198 #define XNEWVAR(T, S) xnewvar<T> (S)
204 static_assert (IsMallocable
<T
>::value
, "Trying to use XCNEWVAR with a \
205 non-POD data type.");
206 return XCNEWVAR (T
, s
);
210 #define XCNEWVAR(T, S) xcnewvar<T> (S)
214 xresizevar (T
*p
, size_t s
)
216 static_assert (IsMallocable
<T
>::value
, "Trying to use XRESIZEVAR with a \
217 non-POD data type.");
218 return XRESIZEVAR (T
, p
, s
);
222 #define XRESIZEVAR(T, P, S) xresizevar<T> (P, S)
228 static_assert (IsMallocable
<T
>::value
, "Trying to use XOBNEW with a \
229 non-POD data type.");
230 return XOBNEW (ob
, T
);
234 #define XOBNEW(O, T) xobnew<T> (O)
238 xobnewvec (obstack
*ob
, size_t n
)
240 static_assert (IsMallocable
<T
>::value
, "Trying to use XOBNEWVEC with a \
241 non-POD data type.");
242 return XOBNEWVEC (ob
, T
, n
);
246 #define XOBNEWVEC(O, T, N) xobnewvec<T> (O, N)
248 #endif /* COMMON_POISON_H */