1 /* Poison symbols at compile time.
3 Copyright (C) 2017-2018 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
25 /* Poison memset of non-POD types. The idea is catching invalid
26 initialization of non-POD structs that is easy to be introduced as
27 side effect of refactoring. For example, say this:
29 struct S { VEC(foo_s) *m_data; };
31 is converted to this at some point:
34 S() { m_data.reserve (10); }
35 std::vector<foo> m_data;
38 and old code was initializing S objects like this:
41 memset (&s, 0, sizeof (S)); // whoops, now wipes vector.
43 Declaring memset as deleted for non-POD types makes the memset above
44 be a compile-time error. */
46 /* Helper for SFINAE. True if "T *" is memsettable. I.e., if T is
47 either void, or POD. */
50 : gdb::Or
<std::is_void
<T
>,
55 typename
= gdb::Requires
<gdb::Not
<IsMemsettable
<T
>>>>
56 void *memset (T
*s
, int c
, size_t n
) = delete;
58 #if HAVE_IS_TRIVIALLY_COPYABLE
60 /* Similarly, poison memcpy and memmove of non trivially-copyable
61 types, which is undefined. */
63 /* True if "T *" is relocatable. I.e., copyable with memcpy/memmove.
64 I.e., T is either trivially copyable, or void. */
67 : gdb::Or
<std::is_void
<T
>,
68 std::is_trivially_copyable
<T
>>
71 /* True if both source and destination are relocatable. */
73 template <typename D
, typename S
>
74 using BothAreRelocatable
75 = gdb::And
<IsRelocatable
<D
>, IsRelocatable
<S
>>;
77 template <typename D
, typename S
,
78 typename
= gdb::Requires
<gdb::Not
<BothAreRelocatable
<D
, S
>>>>
79 void *memcpy (D
*dest
, const S
*src
, size_t n
) = delete;
81 template <typename D
, typename S
,
82 typename
= gdb::Requires
<gdb::Not
<BothAreRelocatable
<D
, S
>>>>
83 void *memmove (D
*dest
, const S
*src
, size_t n
) = delete;
85 #endif /* HAVE_IS_TRIVIALLY_COPYABLE */
87 /* Poison XNEW and friends to catch usages of malloc-style allocations on
88 objects that require new/delete. */
91 using IsMallocable
= std::is_pod
<T
>;
94 using IsFreeable
= gdb::Or
<std::is_trivially_destructible
<T
>, std::is_void
<T
>>;
96 template <typename T
, typename
= gdb::Requires
<gdb::Not
<IsFreeable
<T
>>>>
97 void free (T
*ptr
) = delete;
103 static_assert (IsMallocable
<T
>::value
, "Trying to use XNEW with a non-POD \
104 data type. Use operator new instead.");
109 #define XNEW(T) xnew<T>()
115 static_assert (IsMallocable
<T
>::value
, "Trying to use XCNEW with a non-POD \
116 data type. Use operator new instead.");
121 #define XCNEW(T) xcnew<T>()
127 static_assert (IsFreeable
<T
>::value
, "Trying to use XDELETE with a non-POD \
128 data type. Use operator delete instead.");
133 #define XDELETE(P) xdelete (P)
139 static_assert (IsMallocable
<T
>::value
, "Trying to use XNEWVEC with a \
140 non-POD data type. Use operator new[] (or std::vector) instead.");
141 return XNEWVEC (T
, n
);
145 #define XNEWVEC(T, N) xnewvec<T> (N)
151 static_assert (IsMallocable
<T
>::value
, "Trying to use XCNEWVEC with a \
152 non-POD data type. Use operator new[] (or std::vector) instead.");
153 return XCNEWVEC (T
, n
);
157 #define XCNEWVEC(T, N) xcnewvec<T> (N)
161 xresizevec (T
*p
, size_t n
)
163 static_assert (IsMallocable
<T
>::value
, "Trying to use XRESIZEVEC with a \
164 non-POD data type.");
165 return XRESIZEVEC (T
, p
, n
);
169 #define XRESIZEVEC(T, P, N) xresizevec<T> (P, N)
175 static_assert (IsFreeable
<T
>::value
, "Trying to use XDELETEVEC with a \
176 non-POD data type. Use operator delete[] (or std::vector) instead.");
181 #define XDELETEVEC(P) xdeletevec (P)
187 static_assert (IsMallocable
<T
>::value
, "Trying to use XNEWVAR with a \
188 non-POD data type.");
189 return XNEWVAR (T
, s
);;
193 #define XNEWVAR(T, S) xnewvar<T> (S)
199 static_assert (IsMallocable
<T
>::value
, "Trying to use XCNEWVAR with a \
200 non-POD data type.");
201 return XCNEWVAR (T
, s
);
205 #define XCNEWVAR(T, S) xcnewvar<T> (S)
209 xresizevar (T
*p
, size_t s
)
211 static_assert (IsMallocable
<T
>::value
, "Trying to use XRESIZEVAR with a \
212 non-POD data type.");
213 return XRESIZEVAR (T
, p
, s
);
217 #define XRESIZEVAR(T, P, S) xresizevar<T> (P, S)
219 #endif /* COMMON_POISON_H */