1 #ifndef __STDC_WANT_LIB_EXT1__
2 #define __STDC_WANT_LIB_EXT1__ 1 /* for memset_s() */
5 #include "ruby/missing.h"
12 /* Similar to bzero(), but has a guarantee not to be eliminated from compiler
16 * BSDs have explicit_bzero().
17 * macOS has memset_s().
18 * Windows has SecureZeroMemory() since XP.
19 * Linux has explicit_bzero() since glibc 2.25, musl libc 1.1.20.
23 * Following URL explains why memset_s is added to the standard.
24 * http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1381.pdf
27 #ifndef FUNC_UNOPTIMIZED
28 # define FUNC_UNOPTIMIZED(x) x
32 #ifndef HAVE_EXPLICIT_BZERO
33 #ifdef HAVE_EXPLICIT_MEMSET
35 explicit_bzero(void *b
, size_t len
)
37 (void)explicit_memset(b
, 0, len
);
39 #elif defined HAVE_MEMSET_S
41 explicit_bzero(void *b
, size_t len
)
43 memset_s(b
, len
, 0, len
);
45 #elif defined SecureZeroMemory
47 explicit_bzero(void *b
, size_t len
)
49 SecureZeroMemory(b
, len
);
52 #elif defined HAVE_FUNC_WEAK
54 /* A weak function never be optimized away. Even if nobody uses it. */
55 WEAK(void ruby_explicit_bzero_hook_unused(void *buf
, size_t len
));
57 ruby_explicit_bzero_hook_unused(void *buf
, size_t len
)
62 explicit_bzero(void *b
, size_t len
)
65 ruby_explicit_bzero_hook_unused(b
, len
);
68 #else /* Your OS have no capability. Sigh. */
70 FUNC_UNOPTIMIZED(void explicit_bzero(void *b
, size_t len
));
74 explicit_bzero(void *b
, size_t len
)
77 * volatile is not enough if the compiler has an LTO (link time
78 * optimization). At least, the standard provides no guarantee.
79 * However, gcc and major other compilers never optimize a volatile
80 * variable away. So, using volatile is practically ok.
82 volatile char* p
= (volatile char*)b
;
91 #endif /* HAVE_EXPLICIT_BZERO */