1 /* Erase sensitive data from memory.
2 Copyright 2022-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 /* memset_s need this define */
21 # define __STDC_WANT_LIB_EXT1__ 1
26 /* Set S's bytes to C, where S has LEN bytes. The compiler will not
27 optimize effects away, even if S is dead after the call. */
29 memset_explicit (void *s
, int c
, size_t len
)
31 #if HAVE_EXPLICIT_MEMSET
32 return explicit_memset (s
, c
, len
);
34 (void) memset_s (s
, len
, c
, len
);
36 #elif defined __GNUC__ && !defined __clang__
38 /* Compiler barrier. */
39 __asm__
volatile ("" ::: "memory");
41 #elif defined __clang__
43 /* Compiler barrier. */
44 /* With asm ("" ::: "memory") LLVM analyzes uses of 's' and finds that the
45 whole thing is dead and eliminates it. Use 'g' to work around this
46 problem. See <https://bugs.llvm.org/show_bug.cgi?id=15495#c11>. */
47 __asm__
volatile ("" : : "g"(s
) : "memory");
50 /* Invoke memset through a volatile function pointer. This defeats compiler
52 void * (* const volatile volatile_memset
) (void *, int, size_t) = memset
;
53 return volatile_memset (s
, c
, len
);