[前][次][番号順一覧][スレッド一覧]

ruby-changes:40760

From: kosaki <ko1@a...>
Date: Wed, 2 Dec 2015 06:52:47 +0900 (JST)
Subject: [ruby-changes:40760] kosaki:r52839 (trunk): * missing/explicit_bzero.c: add ruby_explicit_bzero_hook_unused

kosaki	2015-12-02 06:52:02 +0900 (Wed, 02 Dec 2015)

  New Revision: 52839

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=52839

  Log:
    * missing/explicit_bzero.c: add ruby_explicit_bzero_hook_unused
      for preventing optimization. Inspired from OpenBSD.

  Modified files:
    trunk/ChangeLog
    trunk/configure.in
    trunk/missing/explicit_bzero.c
Index: configure.in
===================================================================
--- configure.in	(revision 52838)
+++ configure.in	(revision 52839)
@@ -1752,6 +1752,10 @@ RUBY_FUNC_ATTRIBUTE(deprecated, DEPRECAT https://github.com/ruby/ruby/blob/trunk/configure.in#L1752
 RUBY_FUNC_ATTRIBUTE(deprecated("by "@%:@n), DEPRECATED_BY(n,x), rb_cv_func_deprecated_by)
 RUBY_TYPE_ATTRIBUTE(deprecated mesg, DEPRECATED_TYPE(mesg,x), rb_cv_type_deprecated)
 RUBY_FUNC_ATTRIBUTE(noinline, NOINLINE)
+RUBY_FUNC_ATTRIBUTE(weak, WEAK, rb_cv_func_weak)
+if test "$rb_cv_func_weak" != x; then
+   AC_DEFINE(HAVE_FUNC_WEAK)
+fi
 
 if_i386=${universal_binary+[defined __i386__]}
 RUBY_FUNC_ATTRIBUTE(stdcall,  [], [], ${if_i386})
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 52838)
+++ ChangeLog	(revision 52839)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Dec  2 06:47:25 2015  KOSAKI Motohiro  <kosaki.motohiro@g...>
+
+	* missing/explicit_bzero.c: add ruby_explicit_bzero_hook_unused
+	  for preventing optimization. Inspired from OpenBSD.
+
 Tue Dec  1 23:36:39 2015  Nobuyoshi Nakada  <nobu@r...>
 
 	* thread.c (rb_thread_setname): allow to reset thread name.
Index: missing/explicit_bzero.c
===================================================================
--- missing/explicit_bzero.c	(revision 52838)
+++ missing/explicit_bzero.c	(revision 52839)
@@ -5,11 +5,14 @@ https://github.com/ruby/ruby/blob/trunk/missing/explicit_bzero.c#L5
 #include <windows.h>
 #endif
 
-/*
- *BSD have explicit_bzero().
- Windows, OS-X have memset_s().
- Linux has none. *Sigh*
-*/
+/* Similar to bzero(), but have a guarantee not to be eliminated from compiler
+   optimization. */
+
+/* OS support note:
+ * BSD have explicit_bzero().
+ * Windows, OS-X have memset_s().
+ * Linux has none. *Sigh*
+ */
 
 /*
  * Following URL explain why memset_s is added to the standard.
@@ -21,35 +24,56 @@ https://github.com/ruby/ruby/blob/trunk/missing/explicit_bzero.c#L24
 #endif
 
 #ifndef HAVE_EXPLICIT_BZERO
-/* Similar to bzero(), but have a guarantee not to be eliminated from compiler
-   optimization. */
+ #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 optimization away. Even if nobody use 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, len);
+    ruby_explicit_bzero_hook_unused(b, len);
+}
+
+ #else /* Your OS have no capability. Sigh. */
 
-#ifndef HAVE_MEMSET_S
 FUNC_UNOPTIMIZED(void explicit_bzero(void *b, size_t len));
-#endif
 #undef explicit_bzero
 
 void
 explicit_bzero(void *b, size_t len)
 {
-#ifdef HAVE_MEMSET_S
-    memset_s(b, len, 0, len);
-#elif defined SecureZeroMemory
-    SecureZeroMemory(b, len);
-#else
-    {
-	/*
-	 * TODO: volatile is not enough if compiler have a LTO (link time
-	 * optimization)
-	 */
-	volatile char* p = (volatile char*)b;
-
-	while(len) {
-	    *p = 0;
-	    p++;
-	    len--;
-	}
+    /*
+     * volatile is not enough if compiler have a LTO (link time
+     * optimization). At least, the standard provide no guarantee.
+     * However, gcc and major other compiler never optimization a volatile
+     * variable away. So, using volatile is practically ok.
+     */
+    volatile char* p = (volatile char*)b;
+
+    while(len) {
+	*p = 0;
+	p++;
+	len--;
     }
-#endif
 }
-#endif
+ #endif
+#endif /* HAVE_EXPLICIT_BZERO */

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]