2016-01-10 01:45:36 -05:00
|
|
|
#ifndef __STDC_WANT_LIB_EXT1__
|
|
|
|
#define __STDC_WANT_LIB_EXT1__ 1
|
|
|
|
#endif
|
|
|
|
|
2015-11-30 19:07:21 -05:00
|
|
|
#include "ruby/missing.h"
|
2015-11-30 17:53:21 -05:00
|
|
|
#include <string.h>
|
2016-01-10 01:45:36 -05:00
|
|
|
#ifdef HAVE_MEMSET_S
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
2015-11-30 17:53:21 -05:00
|
|
|
|
2015-11-30 22:52:20 -05:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2015-12-01 19:55:50 -05:00
|
|
|
/* Similar to bzero(), but has a guarantee not to be eliminated from compiler
|
2015-12-01 16:52:02 -05:00
|
|
|
optimization. */
|
|
|
|
|
|
|
|
/* OS support note:
|
2015-12-01 19:55:50 -05:00
|
|
|
* BSDs have explicit_bzero().
|
2020-04-25 13:00:45 -04:00
|
|
|
* macOS has memset_s().
|
2015-12-01 19:55:50 -05:00
|
|
|
* Windows has SecureZeroMemory() since XP.
|
2020-04-25 13:00:45 -04:00
|
|
|
* Linux has explicit_bzero() since glibc 2.25, musl libc 1.1.20.
|
2015-12-01 16:52:02 -05:00
|
|
|
*/
|
2015-11-30 19:07:21 -05:00
|
|
|
|
2015-11-30 19:35:59 -05:00
|
|
|
/*
|
2015-12-01 19:55:50 -05:00
|
|
|
* Following URL explains why memset_s is added to the standard.
|
2015-11-30 19:35:59 -05:00
|
|
|
* http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1381.pdf
|
|
|
|
*/
|
|
|
|
|
2015-11-30 20:05:48 -05:00
|
|
|
#ifndef FUNC_UNOPTIMIZED
|
|
|
|
# define FUNC_UNOPTIMIZED(x) x
|
2015-11-30 19:35:59 -05:00
|
|
|
#endif
|
|
|
|
|
2015-12-01 19:03:51 -05:00
|
|
|
#undef explicit_bzero
|
2015-11-30 19:07:21 -05:00
|
|
|
#ifndef HAVE_EXPLICIT_BZERO
|
2019-08-17 01:17:30 -04:00
|
|
|
#ifdef HAVE_EXPLICIT_MEMSET
|
|
|
|
void
|
|
|
|
explicit_bzero(void *b, size_t len)
|
|
|
|
{
|
|
|
|
(void)explicit_memset(b, 0, len);
|
|
|
|
}
|
|
|
|
#elif defined HAVE_MEMSET_S
|
2015-12-01 16:52:02 -05:00
|
|
|
void
|
|
|
|
explicit_bzero(void *b, size_t len)
|
|
|
|
{
|
|
|
|
memset_s(b, len, 0, len);
|
|
|
|
}
|
|
|
|
#elif defined SecureZeroMemory
|
|
|
|
void
|
|
|
|
explicit_bzero(void *b, size_t len)
|
|
|
|
{
|
|
|
|
SecureZeroMemory(b, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined HAVE_FUNC_WEAK
|
|
|
|
|
2015-12-01 19:55:50 -05:00
|
|
|
/* A weak function never be optimized away. Even if nobody uses it. */
|
2015-12-01 16:52:02 -05:00
|
|
|
WEAK(void ruby_explicit_bzero_hook_unused(void *buf, size_t len));
|
|
|
|
void
|
|
|
|
ruby_explicit_bzero_hook_unused(void *buf, size_t len)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
explicit_bzero(void *b, size_t len)
|
|
|
|
{
|
2015-12-01 17:42:00 -05:00
|
|
|
memset(b, 0, len);
|
2015-12-01 16:52:02 -05:00
|
|
|
ruby_explicit_bzero_hook_unused(b, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* Your OS have no capability. Sigh. */
|
2015-11-30 20:24:23 -05:00
|
|
|
|
2015-11-30 20:05:48 -05:00
|
|
|
FUNC_UNOPTIMIZED(void explicit_bzero(void *b, size_t len));
|
2015-11-30 20:24:23 -05:00
|
|
|
#undef explicit_bzero
|
2015-11-30 20:05:48 -05:00
|
|
|
|
2015-11-30 17:53:21 -05:00
|
|
|
void
|
2015-11-30 19:07:21 -05:00
|
|
|
explicit_bzero(void *b, size_t len)
|
2015-11-30 17:53:21 -05:00
|
|
|
{
|
2015-12-01 16:52:02 -05:00
|
|
|
/*
|
2015-12-01 21:36:58 -05:00
|
|
|
* volatile is not enough if the compiler has an LTO (link time
|
2015-12-01 19:55:50 -05:00
|
|
|
* optimization). At least, the standard provides no guarantee.
|
2015-12-01 21:36:58 -05:00
|
|
|
* However, gcc and major other compilers never optimize a volatile
|
2015-12-01 16:52:02 -05:00
|
|
|
* variable away. So, using volatile is practically ok.
|
|
|
|
*/
|
|
|
|
volatile char* p = (volatile char*)b;
|
|
|
|
|
|
|
|
while(len) {
|
|
|
|
*p = 0;
|
|
|
|
p++;
|
|
|
|
len--;
|
2015-11-30 19:07:21 -05:00
|
|
|
}
|
2015-11-30 17:53:21 -05:00
|
|
|
}
|
2015-12-01 16:52:02 -05:00
|
|
|
#endif
|
|
|
|
#endif /* HAVE_EXPLICIT_BZERO */
|