Skip to content

Commit 7131569

Browse files
[clang] Warn about memset/memcpy to NonTriviallyCopyable types (#111434)
This implements a warning that's similar to what GCC does in that context: both memcpy and memset require their first and second operand to be trivially copyable, let's warn if that's not the case.
1 parent 70d61f6 commit 7131569

File tree

8 files changed

+104
-6
lines changed

8 files changed

+104
-6
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,11 @@ Modified Compiler Flags
322322
to utilize these vector libraries. The behavior for all other vector function
323323
libraries remains unchanged.
324324

325+
- The ``-Wnontrivial-memaccess`` warning has been updated to also warn about
326+
passing non-trivially-copyable destrination parameter to ``memcpy``,
327+
``memset`` and similar functions for which it is a documented undefined
328+
behavior.
329+
325330
Removed Compiler Flags
326331
-------------------------
327332

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,10 @@ def warn_cstruct_memaccess : Warning<
795795
"%1 call is a pointer to record %2 that is not trivial to "
796796
"%select{primitive-default-initialize|primitive-copy}3">,
797797
InGroup<NonTrivialMemaccess>;
798+
def warn_cxxstruct_memaccess : Warning<
799+
"first argument in call to "
800+
"%0 is a pointer to non-trivially copyable type %1">,
801+
InGroup<NonTrivialMemaccess>;
798802
def note_nontrivial_field : Note<
799803
"field is non-trivial to %select{copy|default-initialize}0">;
800804
def err_non_trivial_c_union_in_invalid_context : Error<

clang/lib/Sema/SemaChecking.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8899,18 +8899,36 @@ void Sema::CheckMemaccessArguments(const CallExpr *Call,
88998899
<< ArgIdx << FnName << PointeeTy
89008900
<< Call->getCallee()->getSourceRange());
89018901
else if (const auto *RT = PointeeTy->getAs<RecordType>()) {
8902+
8903+
bool IsTriviallyCopyableCXXRecord =
8904+
RT->desugar().isTriviallyCopyableType(Context);
8905+
89028906
if ((BId == Builtin::BImemset || BId == Builtin::BIbzero) &&
89038907
RT->getDecl()->isNonTrivialToPrimitiveDefaultInitialize()) {
89048908
DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
89058909
PDiag(diag::warn_cstruct_memaccess)
89068910
<< ArgIdx << FnName << PointeeTy << 0);
89078911
SearchNonTrivialToInitializeField::diag(PointeeTy, Dest, *this);
8912+
} else if ((BId == Builtin::BImemset || BId == Builtin::BIbzero) &&
8913+
!IsTriviallyCopyableCXXRecord && ArgIdx == 0) {
8914+
// FIXME: Limiting this warning to dest argument until we decide
8915+
// whether it's valid for source argument too.
8916+
DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
8917+
PDiag(diag::warn_cxxstruct_memaccess)
8918+
<< FnName << PointeeTy);
89088919
} else if ((BId == Builtin::BImemcpy || BId == Builtin::BImemmove) &&
89098920
RT->getDecl()->isNonTrivialToPrimitiveCopy()) {
89108921
DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
89118922
PDiag(diag::warn_cstruct_memaccess)
89128923
<< ArgIdx << FnName << PointeeTy << 1);
89138924
SearchNonTrivialToCopyField::diag(PointeeTy, Dest, *this);
8925+
} else if ((BId == Builtin::BImemcpy || BId == Builtin::BImemmove) &&
8926+
!IsTriviallyCopyableCXXRecord && ArgIdx == 0) {
8927+
// FIXME: Limiting this warning to dest argument until we decide
8928+
// whether it's valid for source argument too.
8929+
DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
8930+
PDiag(diag::warn_cxxstruct_memaccess)
8931+
<< FnName << PointeeTy);
89148932
} else {
89158933
continue;
89168934
}

clang/test/SemaCXX/constexpr-string.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,8 @@ namespace MemcpyEtc {
670670
constexpr bool test_address_of_incomplete_struct_type() { // expected-error {{never produces a constant}}
671671
struct Incomplete;
672672
extern Incomplete x, y;
673+
// expected-warning@+2 {{first argument in call to '__builtin_memcpy' is a pointer to non-trivially copyable type 'Incomplete'}}
674+
// expected-note@+1 {{explicitly cast the pointer to silence this warning}}
673675
__builtin_memcpy(&x, &x, 4);
674676
// expected-note@-1 2{{cannot constant evaluate 'memcpy' between objects of incomplete type 'Incomplete'}}
675677
return true;

clang/test/SemaCXX/warn-memaccess.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wnontrivial-memaccess %s
2+
3+
extern "C" void *bzero(void *, unsigned);
4+
extern "C" void *memset(void *, int, unsigned);
5+
extern "C" void *memmove(void *s1, const void *s2, unsigned n);
6+
extern "C" void *memcpy(void *s1, const void *s2, unsigned n);
7+
8+
class TriviallyCopyable {};
9+
class NonTriviallyCopyable { NonTriviallyCopyable(const NonTriviallyCopyable&);};
10+
11+
void test_bzero(TriviallyCopyable* tc,
12+
NonTriviallyCopyable *ntc) {
13+
// OK
14+
bzero(tc, sizeof(*tc));
15+
16+
// expected-warning@+2{{first argument in call to 'bzero' is a pointer to non-trivially copyable type 'NonTriviallyCopyable'}}
17+
// expected-note@+1{{explicitly cast the pointer to silence this warning}}
18+
bzero(ntc, sizeof(*ntc));
19+
20+
// OK
21+
bzero((void*)ntc, sizeof(*ntc));
22+
}
23+
24+
void test_memset(TriviallyCopyable* tc,
25+
NonTriviallyCopyable *ntc) {
26+
// OK
27+
memset(tc, 0, sizeof(*tc));
28+
29+
// expected-warning@+2{{first argument in call to 'memset' is a pointer to non-trivially copyable type 'NonTriviallyCopyable'}}
30+
// expected-note@+1{{explicitly cast the pointer to silence this warning}}
31+
memset(ntc, 0, sizeof(*ntc));
32+
33+
// OK
34+
memset((void*)ntc, 0, sizeof(*ntc));
35+
}
36+
37+
38+
void test_memcpy(TriviallyCopyable* tc0, TriviallyCopyable* tc1,
39+
NonTriviallyCopyable *ntc0, NonTriviallyCopyable *ntc1) {
40+
// OK
41+
memcpy(tc0, tc1, sizeof(*tc0));
42+
43+
// expected-warning@+2{{first argument in call to 'memcpy' is a pointer to non-trivially copyable type 'NonTriviallyCopyable'}}
44+
// expected-note@+1{{explicitly cast the pointer to silence this warning}}
45+
memcpy(ntc0, ntc1, sizeof(*ntc0));
46+
47+
// ~ OK
48+
memcpy((void*)ntc0, ntc1, sizeof(*ntc0));
49+
50+
// OK
51+
memcpy((void*)ntc0, (void*)ntc1, sizeof(*ntc0));
52+
}
53+
54+
void test_memmove(TriviallyCopyable* tc0, TriviallyCopyable* tc1,
55+
NonTriviallyCopyable *ntc0, NonTriviallyCopyable *ntc1) {
56+
// OK
57+
memmove(tc0, tc1, sizeof(*tc0));
58+
59+
// expected-warning@+2{{first argument in call to 'memmove' is a pointer to non-trivially copyable type 'NonTriviallyCopyable'}}
60+
// expected-note@+1{{explicitly cast the pointer to silence this warning}}
61+
memmove(ntc0, ntc1, sizeof(*ntc0));
62+
63+
// ~ OK
64+
memmove((void*)ntc0, ntc1, sizeof(*ntc0));
65+
66+
// OK
67+
memmove((void*)ntc0, (void*)ntc1, sizeof(*ntc0));
68+
}

libcxx/include/__memory/uninitialized_algorithms.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,8 @@ __uninitialized_allocator_relocate(_Alloc& __alloc, _Tp* __first, _Tp* __last, _
638638
__guard.__complete();
639639
std::__allocator_destroy(__alloc, __first, __last);
640640
} else {
641-
__builtin_memcpy(__result, __first, sizeof(_Tp) * (__last - __first));
641+
// Casting to void* to suppress clang complaining that this is technically UB.
642+
__builtin_memcpy(static_cast<void*>(__result), __first, sizeof(_Tp) * (__last - __first));
642643
}
643644
}
644645

libcxx/test/std/utilities/expected/types.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ template <int Constant>
162162
struct TailClobberer {
163163
constexpr TailClobberer() noexcept {
164164
if (!std::is_constant_evaluated()) {
165-
std::memset(this, Constant, sizeof(*this));
165+
std::memset(static_cast<void*>(this), Constant, sizeof(*this));
166166
}
167167
// Always set `b` itself to `false` so that the comparison works.
168168
b = false;
@@ -245,7 +245,7 @@ struct BoolWithPadding {
245245
constexpr explicit BoolWithPadding() noexcept : BoolWithPadding(false) {}
246246
constexpr BoolWithPadding(bool val) noexcept {
247247
if (!std::is_constant_evaluated()) {
248-
std::memset(this, 0, sizeof(*this));
248+
std::memset(static_cast<void*>(this), 0, sizeof(*this));
249249
}
250250
val_ = val;
251251
}
@@ -268,7 +268,7 @@ struct IntWithoutPadding {
268268
constexpr explicit IntWithoutPadding() noexcept : IntWithoutPadding(0) {}
269269
constexpr IntWithoutPadding(int val) noexcept {
270270
if (!std::is_constant_evaluated()) {
271-
std::memset(this, 0, sizeof(*this));
271+
std::memset(static_cast<void*>(this), 0, sizeof(*this));
272272
}
273273
val_ = val;
274274
}

libcxx/test/support/min_allocator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,14 +465,14 @@ class safe_allocator {
465465
TEST_CONSTEXPR_CXX20 T* allocate(std::size_t n) {
466466
T* memory = std::allocator<T>().allocate(n);
467467
if (!TEST_IS_CONSTANT_EVALUATED)
468-
std::memset(memory, 0, sizeof(T) * n);
468+
std::memset(static_cast<void*>(memory), 0, sizeof(T) * n);
469469

470470
return memory;
471471
}
472472

473473
TEST_CONSTEXPR_CXX20 void deallocate(T* p, std::size_t n) {
474474
if (!TEST_IS_CONSTANT_EVALUATED)
475-
DoNotOptimize(std::memset(p, 0, sizeof(T) * n));
475+
DoNotOptimize(std::memset(static_cast<void*>(p), 0, sizeof(T) * n));
476476
std::allocator<T>().deallocate(p, n);
477477
}
478478

0 commit comments

Comments
 (0)