]> git.kaiwu.me - njs.git/commitdiff
Added nxt_memzero() and nxt_explicit_memzero().
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 2 Oct 2018 17:28:10 +0000 (20:28 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Tue, 2 Oct 2018 17:28:10 +0000 (20:28 +0300)
Thanks to David CARLIER.

19 files changed:
njs/njs.c
njs/njs_crypto.c
njs/njs_date.c
njs/njs_function.c
njs/njs_shell.c
njs/test/njs_benchmark.c
njs/test/njs_interactive_test.c
njs/test/njs_unit_test.c
nxt/auto/configure
nxt/auto/explicit_bzero [new file with mode: 0644]
nxt/nxt_array.c
nxt/nxt_lvlhsh.c
nxt/nxt_lvlhsh.h
nxt/nxt_md5.c
nxt/nxt_mem_cache_pool.c
nxt/nxt_sha1.c
nxt/nxt_sha2.c
nxt/nxt_string.h
nxt/test/lvlhsh_unit_test.c

index 3771a81ef92bd890b15976d2eb87466f73810431..e63e12f79966d152ffee7a72fcd47eaf8ff2df40 100644 (file)
--- a/njs/njs.c
+++ b/njs/njs.c
@@ -28,7 +28,7 @@ njs_zalloc(void *mem, size_t size)
     p = nxt_malloc(size);
 
     if (p != NULL) {
-        memset(p, 0, size);
+        nxt_memzero(p, size);
     }
 
     return p;
@@ -392,7 +392,7 @@ njs_vm_init(njs_vm_t *vm)
         return NXT_ERROR;
     }
 
-    memset(frame, 0, NJS_GLOBAL_FRAME_SIZE);
+    nxt_memzero(frame, NJS_GLOBAL_FRAME_SIZE);
 
     vm->top_frame = &frame->native;
     vm->active_frame = frame;
index ef1318b8d15cd3815dfa4b3c02395a7e7816cb35..74e680a00e2e0b065ab848ed6c5cfd2f9b39ea02 100644 (file)
@@ -422,11 +422,12 @@ njs_crypto_create_hmac(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
         alg->final(digest, &ctx->u);
 
         memcpy(key_buf, digest, alg->size);
-        memset(key_buf + alg->size, 0, sizeof(key_buf) - alg->size);
+        nxt_explicit_memzero(key_buf + alg->size, sizeof(key_buf) - alg->size);
 
     } else {
         memcpy(key_buf, key.start, key.length);
-        memset(key_buf + key.length, 0, sizeof(key_buf) - key.length);
+        nxt_explicit_memzero(key_buf + key.length,
+                             sizeof(key_buf) - key.length);
     }
 
     for (i = 0; i < 64; i++) {
index b08c75c9bab8419b2c26ea3632d7880b19431225..591e46b825198b5c3e207ff58224ed348be85ddf 100644 (file)
@@ -81,7 +81,7 @@ njs_date_constructor(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
             time = njs_date_string_parse(&args[1]);
 
         } else {
-            memset(values, 0, 8 * sizeof(int64_t));
+            nxt_memzero(values, 8 * sizeof(int64_t));
             /* Month. */
             values[2] = 1;
 
@@ -165,7 +165,7 @@ njs_date_utc(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
     time = NAN;
 
     if (nargs > 2) {
-        memset(values, 0, 8 * sizeof(int32_t));
+        nxt_memzero(values, 8 * sizeof(int32_t));
 
         n = nxt_min(8, nargs);
 
index 96d50d8a9b47aa179ddcb290b5769c25c2ae7099..48269fe12957d481fa493db28273e8f25ad09285 100644 (file)
@@ -262,7 +262,7 @@ njs_function_frame_alloc(njs_vm_t *vm, size_t size)
         vm->stack_size += spare_size;
     }
 
-    memset(frame, 0, sizeof(njs_native_frame_t));
+    nxt_memzero(frame, sizeof(njs_native_frame_t));
 
     frame->size = chunk_size;
     frame->free_size = spare_size - size;
index 7915b8bf75f57f3f5294feb2a59891f332c45cf4..dc083ee0820fc55265d4bd188e64e871146a95d5 100644 (file)
@@ -130,7 +130,7 @@ main(int argc, char **argv)
     njs_opts_t    opts;
     njs_vm_opt_t  vm_options;
 
-    memset(&opts, 0, sizeof(njs_opts_t));
+    nxt_memzero(&opts, sizeof(njs_opts_t));
     opts.interactive = 1;
 
     ret = njs_get_options(&opts, argc, argv);
@@ -143,7 +143,7 @@ main(int argc, char **argv)
         return EXIT_SUCCESS;
     }
 
-    memset(&vm_options, 0, sizeof(njs_vm_opt_t));
+    nxt_memzero(&vm_options, sizeof(njs_vm_opt_t));
 
     vm_options.accumulative = 1;
     vm_options.backtrace = 1;
index 45c625a4520f89520633af4ca89c31520d100edd..f28eeda45dfd07547349915a3bbcd656a287b404 100644 (file)
@@ -26,7 +26,7 @@ njs_unit_test_benchmark(nxt_str_t *script, nxt_str_t *result, const char *msg,
     njs_vm_opt_t   options;
     struct rusage  usage;
 
-    memset(&options, 0, sizeof(njs_vm_opt_t));
+    nxt_memzero(&options, sizeof(njs_vm_opt_t));
 
     vm = NULL;
     nvm = NULL;
index 04069abfe4c721cf537ea98fdf1b94b833e8fd63..f70787f41e1e0dab8b0fa4981b364d16dc46a6a1 100644 (file)
@@ -245,7 +245,7 @@ njs_interactive_test(nxt_bool_t verbose)
             fflush(stdout);
         }
 
-        memset(&options, 0, sizeof(njs_vm_opt_t));
+        nxt_memzero(&options, sizeof(njs_vm_opt_t));
 
         options.accumulative = 1;
         options.backtrace = 1;
index ada29e238389d5be88c8f70b945bb71d9a8014f9..259f0bd74d9d9a38c31aee0e00f87c011d48fd6f 100644 (file)
@@ -10233,7 +10233,7 @@ njs_unit_test(nxt_bool_t disassemble, nxt_bool_t verbose)
             fflush(stdout);
         }
 
-        memset(&options, 0, sizeof(njs_vm_opt_t));
+        nxt_memzero(&options, sizeof(njs_vm_opt_t));
 
         vm = njs_vm_create(&options);
         if (vm == NULL) {
@@ -10380,7 +10380,7 @@ njs_api_test(nxt_bool_t disassemble, nxt_bool_t verbose)
     rc = NXT_ERROR;
 
     vm = NULL;
-    memset(&options, 0, sizeof(njs_vm_opt_t));
+    nxt_memzero(&options, sizeof(njs_vm_opt_t));
 
     for (i = 0; i < nxt_nitems(njs_api_test); i++) {
         test = &njs_api_test[i];
index 9ffb24d4c2ed7825298b74fdfa4ef1993d7fe141..e0ad632808db376c86de6acb4fe89d29e8c01a40 100755 (executable)
@@ -54,6 +54,7 @@ END
 . ${NXT_AUTO}time
 . ${NXT_AUTO}memalign
 . ${NXT_AUTO}getrandom
+. ${NXT_AUTO}explicit_bzero
 . ${NXT_AUTO}pcre
 . ${NXT_AUTO}editline
 . ${NXT_AUTO}expect
diff --git a/nxt/auto/explicit_bzero b/nxt/auto/explicit_bzero
new file mode 100644 (file)
index 0000000..d26f0cc
--- /dev/null
@@ -0,0 +1,40 @@
+
+# Copyright (C) Igor Sysoev
+# Copyright (C) NGINX, Inc.
+
+
+# Linux (glibc and musl from 1.1.20), OpenBSD, FreeBSD and NetBSD.
+
+nxt_feature="explicit_bzero()"
+nxt_feature_name=NXT_HAVE_EXPLICIT_BZERO
+nxt_feature_run=yes
+nxt_feature_incs=
+nxt_feature_libs=
+nxt_feature_test="#include <strings.h>
+                  #include <string.h>
+
+                  int main(void) {
+                      int r;
+
+                      explicit_bzero(&r, sizeof(r));
+                      return 0;
+                  }"
+. ${NXT_AUTO}feature
+
+
+if [ $nxt_found = no ]; then
+
+    # NetBSD has explicit_memset instead.
+
+    nxt_feature="explicit_memset()"
+    nxt_feature_name=NXT_HAVE_EXPLICIT_MEMSET
+    nxt_feature_test="#include <string.h>
+
+                      int main(void) {
+                          int r;
+
+                          explicit_memset(&r, 0, sizeof(r));
+                          return 0;
+                      }"
+    . ${NXT_AUTO}feature
+fi
index ea1cd3cc0c7cdca6b47a36dd0a2fb1b0023c2bcd..6d4ecbdd64ef7f6e81873010113246978b8a9eb4 100644 (file)
@@ -9,6 +9,7 @@
 #include <nxt_clang.h>
 #include <nxt_stub.h>
 #include <nxt_array.h>
+#include <nxt_string.h>
 #include <string.h>
 
 
@@ -140,7 +141,7 @@ nxt_array_zero_add(nxt_array_t *array, const nxt_mem_proto_t *proto, void *pool)
     item = nxt_array_add(array, proto, pool);
 
     if (nxt_fast_path(item != NULL)) {
-        memset(item, 0, array->item_size);
+        nxt_memzero(item, array->item_size);
     }
 
     return item;
index b0f57473ed3d4e5e0bf3391b5133316f9f8b0624..0b554e5526dddfb9149fde7db99f2f0a403c8b9d 100644 (file)
@@ -465,7 +465,7 @@ nxt_lvlhsh_convert_bucket_to_level(nxt_lvlhsh_query_t *lhq, void **slot,
         return NXT_ERROR;
     }
 
-    memset(lvl, 0, size * (sizeof(void *)));
+    nxt_memzero(lvl, size * (sizeof(void *)));
 
     level = lvl;
     shift = 0;
index d5d8e7093eb2a2212d5e8461baee71ee93e923ec..7bcc8314bee7be57ac63e1e158f8dbb4a35458d4 100644 (file)
@@ -176,7 +176,7 @@ typedef struct {
 
 #define nxt_lvlhsh_each_init(lhe, _proto)                                     \
     do {                                                                      \
-        memset(lhe, 0, sizeof(nxt_lvlhsh_each_t));                            \
+        nxt_memzero(lhe, sizeof(nxt_lvlhsh_each_t));                          \
         (lhe)->proto = _proto;                                                \
     } while (0)
 
index 9957b68b9858ccc3a543285bf392ef8ba162c2d1..5382021f448b21cc18d47bebf277732f901b0d95 100644 (file)
@@ -10,6 +10,7 @@
 #include <nxt_types.h>
 #include <nxt_clang.h>
 #include <nxt_md5.h>
+#include <nxt_string.h>
 #include <string.h>
 
 
@@ -72,13 +73,13 @@ nxt_md5_final(u_char result[16], nxt_md5_t *ctx)
     free = 64 - used;
 
     if (free < 8) {
-        memset(&ctx->buffer[used], 0, free);
+        nxt_memzero(&ctx->buffer[used], free);
         (void) nxt_md5_body(ctx, ctx->buffer, 64);
         used = 0;
         free = 64;
     }
 
-    memset(&ctx->buffer[used], 0, free - 8);
+    nxt_memzero(&ctx->buffer[used], free - 8);
 
     ctx->bytes <<= 3;
     ctx->buffer[56] = (u_char)  ctx->bytes;
@@ -109,7 +110,7 @@ nxt_md5_final(u_char result[16], nxt_md5_t *ctx)
     result[14] = (u_char) (ctx->d >> 16);
     result[15] = (u_char) (ctx->d >> 24);
 
-    memset(ctx, 0, sizeof(*ctx));
+    nxt_memzero(ctx, sizeof(*ctx));
 }
 
 
index 3deeeb413ccf492500c8ca35776b7fcf876f45a2..c8757ad71114997f2c80ceabe03e912466351876 100644 (file)
@@ -9,6 +9,7 @@
 #include <nxt_clang.h>
 #include <nxt_alignment.h>
 #include <nxt_stub.h>
+#include <nxt_string.h>
 #include <nxt_queue.h>
 #include <nxt_rbtree.h>
 #include <nxt_mem_cache_pool.h>
@@ -322,7 +323,7 @@ nxt_mem_cache_zalloc(nxt_mem_cache_pool_t *pool, size_t size)
     p = nxt_mem_cache_alloc(pool, size);
 
     if (nxt_fast_path(p != NULL)) {
-        memset(p, 0, size);
+        nxt_memzero(p, size);
     }
 
     return p;
@@ -368,7 +369,7 @@ nxt_mem_cache_zalign(nxt_mem_cache_pool_t *pool, size_t alignment, size_t size)
     p = nxt_mem_cache_align(pool, alignment, size);
 
     if (nxt_fast_path(p != NULL)) {
-        memset(p, 0, size);
+        nxt_memzero(p, size);
     }
 
     return p;
index 6c35e82c91e74f0bfd03b1843e2895c083d0e92b..2b96cdc33d9bed430e7ebc3537e09c6b7cf7dfce 100644 (file)
@@ -11,6 +11,7 @@
 #include <nxt_types.h>
 #include <nxt_clang.h>
 #include <nxt_sha1.h>
+#include <nxt_string.h>
 #include <string.h>
 
 
@@ -74,13 +75,13 @@ nxt_sha1_final(u_char result[20], nxt_sha1_t *ctx)
     free = 64 - used;
 
     if (free < 8) {
-        memset(&ctx->buffer[used], 0, free);
+        nxt_memzero(&ctx->buffer[used], free);
         (void) nxt_sha1_body(ctx, ctx->buffer, 64);
         used = 0;
         free = 64;
     }
 
-    memset(&ctx->buffer[used], 0, free - 8);
+    nxt_memzero(&ctx->buffer[used], free - 8);
 
     ctx->bytes <<= 3;
     ctx->buffer[56] = (u_char) (ctx->bytes >> 56);
@@ -115,7 +116,7 @@ nxt_sha1_final(u_char result[20], nxt_sha1_t *ctx)
     result[18] = (u_char) (ctx->e >> 8);
     result[19] = (u_char)  ctx->e;
 
-    memset(ctx, 0, sizeof(*ctx));
+    nxt_memzero(ctx, sizeof(*ctx));
 }
 
 
index ecf4c936cac4d318d54acdd51d0304ec247bfe0d..9a52ae4dc3d0665bb8ae5bb78563dbbf0905e69c 100644 (file)
@@ -11,6 +11,7 @@
 #include <nxt_types.h>
 #include <nxt_clang.h>
 #include <nxt_sha2.h>
+#include <nxt_string.h>
 #include <string.h>
 
 
@@ -77,13 +78,13 @@ nxt_sha2_final(u_char result[32], nxt_sha2_t *ctx)
     free = 64 - used;
 
     if (free < 8) {
-        memset(&ctx->buffer[used], 0, free);
+        nxt_memzero(&ctx->buffer[used], free);
         (void) nxt_sha2_body(ctx, ctx->buffer, 64);
         used = 0;
         free = 64;
     }
 
-    memset(&ctx->buffer[used], 0, free - 8);
+    nxt_memzero(&ctx->buffer[used], free - 8);
 
     ctx->bytes <<= 3;
     ctx->buffer[56] = (u_char) (ctx->bytes >> 56);
@@ -130,7 +131,7 @@ nxt_sha2_final(u_char result[32], nxt_sha2_t *ctx)
     result[30] = (u_char) (ctx->h >> 8);
     result[31] = (u_char)  ctx->h;
 
-    memset(ctx, 0, sizeof(*ctx));
+    nxt_memzero(ctx, sizeof(*ctx));
 }
 
 
index f1996262c3f691d91ab6c83b31e39d9493c9486c..f3b1b147d8f8c9feab8517af652cea7c69c581da 100644 (file)
@@ -44,6 +44,21 @@ nxt_upper_case(u_char c)
 #define nxt_cpymem(dst, src, n)   (((u_char *) memcpy(dst, src, n)) + (n))
 
 
+#define nxt_memzero(buf, length)   (void) (memset(buf, 0, length))
+
+
+#if (NXT_HAVE_EXPLICIT_BZERO)
+#define nxt_explicit_memzero(buf, length)                                     \
+    (void) (explicit_bzero(buf, length))
+#elif (NXT_HAVE_EXPLICIT_MEMSET)
+#define nxt_explicit_memzero(buf, length)                                     \
+    (void) (explicit_memset(buf, 0, length))
+#else
+#define nxt_explicit_memzero(buf, length)                                     \
+    nxt_memzero(buf, length)
+#endif
+
+
 #define nxt_strstr_eq(s1, s2)                                                 \
     (((s1)->length == (s2)->length)                                           \
      && (memcmp((s1)->start, (s2)->start, (s1)->length) == 0))
index dbad6d653f45913ba0c51be1cbe7c8db9d9d31a1..7ed6d6ee78e46402f0bf353b82f701f6c1070d9c 100644 (file)
@@ -146,7 +146,7 @@ lvlhsh_zalloc(void *mem, size_t size)
     p = nxt_malloc(size);
 
     if (p != NULL) {
-        memset(p, 0, size);
+        nxt_memzero(p, size);
     }
 
     return p;
@@ -216,7 +216,7 @@ lvlhsh_unit_test(nxt_uint_t n)
 
     printf("lvlhsh unit test started: %ld items\n", (long) n);
 
-    memset(&lh, 0, sizeof(nxt_lvlhsh_t));
+    nxt_memzero(&lh, sizeof(nxt_lvlhsh_t));
 
     key = 0;
     for (i = 0; i < n; i++) {