mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	 09bdb7913c
			
		
	
	
		09bdb7913c
		
	
	
	
	
		
			
			* include/ruby/missing.h (explicit_bzero_by_memset_s): remove inline implementation by memset_s, which needs a macro before including headers and can cause problems in extension libraries by the order of the macro and headers. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53493 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
		
			
				
	
	
		
			88 lines
		
	
	
	
		
			1.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			88 lines
		
	
	
	
		
			1.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| #ifndef __STDC_WANT_LIB_EXT1__
 | |
| #define __STDC_WANT_LIB_EXT1__ 1
 | |
| #endif
 | |
| 
 | |
| #include "ruby/missing.h"
 | |
| #include <string.h>
 | |
| #ifdef HAVE_MEMSET_S
 | |
| # include <string.h>
 | |
| #endif
 | |
| 
 | |
| #ifdef _WIN32
 | |
| #include <windows.h>
 | |
| #endif
 | |
| 
 | |
| /* Similar to bzero(), but has a guarantee not to be eliminated from compiler
 | |
|    optimization. */
 | |
| 
 | |
| /* OS support note:
 | |
|  * BSDs have explicit_bzero().
 | |
|  * OS-X has memset_s().
 | |
|  * Windows has SecureZeroMemory() since XP.
 | |
|  * Linux has none. *Sigh*
 | |
|  */
 | |
| 
 | |
| /*
 | |
|  * Following URL explains why memset_s is added to the standard.
 | |
|  * http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1381.pdf
 | |
|  */
 | |
| 
 | |
| #ifndef FUNC_UNOPTIMIZED
 | |
| # define FUNC_UNOPTIMIZED(x) x
 | |
| #endif
 | |
| 
 | |
| #undef explicit_bzero
 | |
| #ifndef HAVE_EXPLICIT_BZERO
 | |
|  #ifdef HAVE_MEMSET_S
 | |
| 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
 | |
| 
 | |
| /* A weak function never be optimized away. Even if nobody uses it. */
 | |
| 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)
 | |
| {
 | |
|     memset(b, 0, len);
 | |
|     ruby_explicit_bzero_hook_unused(b, len);
 | |
| }
 | |
| 
 | |
|  #else /* Your OS have no capability. Sigh. */
 | |
| 
 | |
| FUNC_UNOPTIMIZED(void explicit_bzero(void *b, size_t len));
 | |
| #undef explicit_bzero
 | |
| 
 | |
| void
 | |
| explicit_bzero(void *b, size_t len)
 | |
| {
 | |
|     /*
 | |
|      * volatile is not enough if the compiler has an LTO (link time
 | |
|      * optimization). At least, the standard provides no guarantee.
 | |
|      * However, gcc and major other compilers never optimize a volatile
 | |
|      * variable away. So, using volatile is practically ok.
 | |
|      */
 | |
|     volatile char* p = (volatile char*)b;
 | |
| 
 | |
|     while(len) {
 | |
| 	*p = 0;
 | |
| 	p++;
 | |
| 	len--;
 | |
|     }
 | |
| }
 | |
|  #endif
 | |
| #endif /* HAVE_EXPLICIT_BZERO */
 |