Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

explicit_bzero.c
Go to the documentation of this file.
1 #ifndef __STDC_WANT_LIB_EXT1__
2 #define __STDC_WANT_LIB_EXT1__ 1
3 #endif
4
5 #include "ruby/missing.h"
6 #include <string.h>
7 #ifdef HAVE_MEMSET_S
8 # include <string.h>
9 #endif
10
11 #ifdef _WIN32
12 #include <windows.h>
13 #endif
14
15 /* Similar to bzero(), but has a guarantee not to be eliminated from compiler
16 optimization. */
17
18 /* OS support note:
19 * BSDs have explicit_bzero().
20 * OS-X has memset_s().
21 * Windows has SecureZeroMemory() since XP.
22 * Linux has none. *Sigh*
23 */
24
25 /*
26 * Following URL explains why memset_s is added to the standard.
27 * http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1381.pdf
28 */
29
30 #ifndef FUNC_UNOPTIMIZED
31 # define FUNC_UNOPTIMIZED(x) x
32 #endif
33
34 #undef explicit_bzero
35 #ifndef HAVE_EXPLICIT_BZERO
36 #ifdef HAVE_EXPLICIT_MEMSET
37 void
38 explicit_bzero(void *b, size_t len)
39 {
40 (void)explicit_memset(b, 0, len);
41 }
42 #elif defined HAVE_MEMSET_S
43 void
44 explicit_bzero(void *b, size_t len)
45 {
46 memset_s(b, len, 0, len);
47 }
48 #elif defined SecureZeroMemory
49 void
50 explicit_bzero(void *b, size_t len)
51 {
52 SecureZeroMemory(b, len);
53 }
54
55 #elif defined HAVE_FUNC_WEAK
56
57 /* A weak function never be optimized away. Even if nobody uses it. */
58 WEAK(void ruby_explicit_bzero_hook_unused(void *buf, size_t len));
59 void
60 ruby_explicit_bzero_hook_unused(void *buf, size_t len)
61 {
62 }
63
64 void
65 explicit_bzero(void *b, size_t len)
66 {
67 memset(b, 0, len);
68 ruby_explicit_bzero_hook_unused(b, len);
69 }
70
71 #else /* Your OS have no capability. Sigh. */
72
73 FUNC_UNOPTIMIZED(void explicit_bzero(void *b, size_t len));
74 #undef explicit_bzero
75
76 void
77 explicit_bzero(void *b, size_t len)
78 {
79 /*
80 * volatile is not enough if the compiler has an LTO (link time
81 * optimization). At least, the standard provides no guarantee.
82 * However, gcc and major other compilers never optimize a volatile
83 * variable away. So, using volatile is practically ok.
84 */
85 volatile char* p = (volatile char*)b;
86
87 while(len) {
88 *p = 0;
89 p++;
90 len--;
91 }
92 }
93 #endif
94 #endif /* HAVE_EXPLICIT_BZERO */
void
void
Definition: rb_mjit_min_header-2.7.0.h:13273
memset
void * memset(void *, int, size_t)
WEAK
#define WEAK(x)
Definition: rb_mjit_min_header-2.7.0.h:174
FUNC_UNOPTIMIZED
#define FUNC_UNOPTIMIZED(x)
Definition: explicit_bzero.c:31
buf
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4322
missing.h
len
uint8_t len
Definition: escape.c:17
explicit_bzero
void explicit_bzero(void *b, size_t len)
Definition: explicit_bzero.c:77