[njs] Added nxt_memzero() and nxt_explicit_memzero().
Dmitry Volyntsev
xeioex at nginx.com
Tue Oct 2 17:35:12 UTC 2018
details: http://hg.nginx.org/njs/rev/21858b20db0c
branches:
changeset: 615:21858b20db0c
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Tue Oct 02 20:28:10 2018 +0300
description:
Added nxt_memzero() and nxt_explicit_memzero().
Thanks to David CARLIER.
diffstat:
njs/njs.c | 4 ++--
njs/njs_crypto.c | 5 +++--
njs/njs_date.c | 4 ++--
njs/njs_function.c | 2 +-
njs/njs_shell.c | 4 ++--
njs/test/njs_benchmark.c | 2 +-
njs/test/njs_interactive_test.c | 2 +-
njs/test/njs_unit_test.c | 4 ++--
nxt/auto/configure | 1 +
nxt/auto/explicit_bzero | 40 ++++++++++++++++++++++++++++++++++++++++
nxt/nxt_array.c | 3 ++-
nxt/nxt_lvlhsh.c | 2 +-
nxt/nxt_lvlhsh.h | 2 +-
nxt/nxt_md5.c | 7 ++++---
nxt/nxt_mem_cache_pool.c | 5 +++--
nxt/nxt_sha1.c | 7 ++++---
nxt/nxt_sha2.c | 7 ++++---
nxt/nxt_string.h | 15 +++++++++++++++
nxt/test/lvlhsh_unit_test.c | 4 ++--
19 files changed, 91 insertions(+), 29 deletions(-)
diffs (420 lines):
diff -r c4035cc63370 -r 21858b20db0c njs/njs.c
--- a/njs/njs.c Thu Sep 27 17:37:55 2018 +0300
+++ b/njs/njs.c Tue Oct 02 20:28:10 2018 +0300
@@ -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;
diff -r c4035cc63370 -r 21858b20db0c njs/njs_crypto.c
--- a/njs/njs_crypto.c Thu Sep 27 17:37:55 2018 +0300
+++ b/njs/njs_crypto.c Tue Oct 02 20:28:10 2018 +0300
@@ -422,11 +422,12 @@ njs_crypto_create_hmac(njs_vm_t *vm, njs
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++) {
diff -r c4035cc63370 -r 21858b20db0c njs/njs_date.c
--- a/njs/njs_date.c Thu Sep 27 17:37:55 2018 +0300
+++ b/njs/njs_date.c Tue Oct 02 20:28:10 2018 +0300
@@ -81,7 +81,7 @@ njs_date_constructor(njs_vm_t *vm, njs_v
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 *
time = NAN;
if (nargs > 2) {
- memset(values, 0, 8 * sizeof(int32_t));
+ nxt_memzero(values, 8 * sizeof(int32_t));
n = nxt_min(8, nargs);
diff -r c4035cc63370 -r 21858b20db0c njs/njs_function.c
--- a/njs/njs_function.c Thu Sep 27 17:37:55 2018 +0300
+++ b/njs/njs_function.c Tue Oct 02 20:28:10 2018 +0300
@@ -262,7 +262,7 @@ njs_function_frame_alloc(njs_vm_t *vm, s
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;
diff -r c4035cc63370 -r 21858b20db0c njs/njs_shell.c
--- a/njs/njs_shell.c Thu Sep 27 17:37:55 2018 +0300
+++ b/njs/njs_shell.c Tue Oct 02 20:28:10 2018 +0300
@@ -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;
diff -r c4035cc63370 -r 21858b20db0c njs/test/njs_benchmark.c
--- a/njs/test/njs_benchmark.c Thu Sep 27 17:37:55 2018 +0300
+++ b/njs/test/njs_benchmark.c Tue Oct 02 20:28:10 2018 +0300
@@ -26,7 +26,7 @@ njs_unit_test_benchmark(nxt_str_t *scrip
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;
diff -r c4035cc63370 -r 21858b20db0c njs/test/njs_interactive_test.c
--- a/njs/test/njs_interactive_test.c Thu Sep 27 17:37:55 2018 +0300
+++ b/njs/test/njs_interactive_test.c Tue Oct 02 20:28:10 2018 +0300
@@ -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;
diff -r c4035cc63370 -r 21858b20db0c njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Thu Sep 27 17:37:55 2018 +0300
+++ b/njs/test/njs_unit_test.c Tue Oct 02 20:28:10 2018 +0300
@@ -10233,7 +10233,7 @@ njs_unit_test(nxt_bool_t disassemble, nx
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
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];
diff -r c4035cc63370 -r 21858b20db0c nxt/auto/configure
--- a/nxt/auto/configure Thu Sep 27 17:37:55 2018 +0300
+++ b/nxt/auto/configure Tue Oct 02 20:28:10 2018 +0300
@@ -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 -r c4035cc63370 -r 21858b20db0c nxt/auto/explicit_bzero
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nxt/auto/explicit_bzero Tue Oct 02 20:28:10 2018 +0300
@@ -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
diff -r c4035cc63370 -r 21858b20db0c nxt/nxt_array.c
--- a/nxt/nxt_array.c Thu Sep 27 17:37:55 2018 +0300
+++ b/nxt/nxt_array.c Tue Oct 02 20:28:10 2018 +0300
@@ -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, c
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;
diff -r c4035cc63370 -r 21858b20db0c nxt/nxt_lvlhsh.c
--- a/nxt/nxt_lvlhsh.c Thu Sep 27 17:37:55 2018 +0300
+++ b/nxt/nxt_lvlhsh.c Tue Oct 02 20:28:10 2018 +0300
@@ -465,7 +465,7 @@ nxt_lvlhsh_convert_bucket_to_level(nxt_l
return NXT_ERROR;
}
- memset(lvl, 0, size * (sizeof(void *)));
+ nxt_memzero(lvl, size * (sizeof(void *)));
level = lvl;
shift = 0;
diff -r c4035cc63370 -r 21858b20db0c nxt/nxt_lvlhsh.h
--- a/nxt/nxt_lvlhsh.h Thu Sep 27 17:37:55 2018 +0300
+++ b/nxt/nxt_lvlhsh.h Tue Oct 02 20:28:10 2018 +0300
@@ -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)
diff -r c4035cc63370 -r 21858b20db0c nxt/nxt_md5.c
--- a/nxt/nxt_md5.c Thu Sep 27 17:37:55 2018 +0300
+++ b/nxt/nxt_md5.c Tue Oct 02 20:28:10 2018 +0300
@@ -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
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
result[14] = (u_char) (ctx->d >> 16);
result[15] = (u_char) (ctx->d >> 24);
- memset(ctx, 0, sizeof(*ctx));
+ nxt_memzero(ctx, sizeof(*ctx));
}
diff -r c4035cc63370 -r 21858b20db0c nxt/nxt_mem_cache_pool.c
--- a/nxt/nxt_mem_cache_pool.c Thu Sep 27 17:37:55 2018 +0300
+++ b/nxt/nxt_mem_cache_pool.c Tue Oct 02 20:28:10 2018 +0300
@@ -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_
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_
p = nxt_mem_cache_align(pool, alignment, size);
if (nxt_fast_path(p != NULL)) {
- memset(p, 0, size);
+ nxt_memzero(p, size);
}
return p;
diff -r c4035cc63370 -r 21858b20db0c nxt/nxt_sha1.c
--- a/nxt/nxt_sha1.c Thu Sep 27 17:37:55 2018 +0300
+++ b/nxt/nxt_sha1.c Tue Oct 02 20:28:10 2018 +0300
@@ -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_sh
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_sh
result[18] = (u_char) (ctx->e >> 8);
result[19] = (u_char) ctx->e;
- memset(ctx, 0, sizeof(*ctx));
+ nxt_memzero(ctx, sizeof(*ctx));
}
diff -r c4035cc63370 -r 21858b20db0c nxt/nxt_sha2.c
--- a/nxt/nxt_sha2.c Thu Sep 27 17:37:55 2018 +0300
+++ b/nxt/nxt_sha2.c Tue Oct 02 20:28:10 2018 +0300
@@ -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_sh
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_sh
result[30] = (u_char) (ctx->h >> 8);
result[31] = (u_char) ctx->h;
- memset(ctx, 0, sizeof(*ctx));
+ nxt_memzero(ctx, sizeof(*ctx));
}
diff -r c4035cc63370 -r 21858b20db0c nxt/nxt_string.h
--- a/nxt/nxt_string.h Thu Sep 27 17:37:55 2018 +0300
+++ b/nxt/nxt_string.h Tue Oct 02 20:28:10 2018 +0300
@@ -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))
diff -r c4035cc63370 -r 21858b20db0c nxt/test/lvlhsh_unit_test.c
--- a/nxt/test/lvlhsh_unit_test.c Thu Sep 27 17:37:55 2018 +0300
+++ b/nxt/test/lvlhsh_unit_test.c Tue Oct 02 20:28:10 2018 +0300
@@ -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++) {
More information about the nginx-devel
mailing list