From bfc8ec1248b3d6b72167f414c9f668eeef096492 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sat, 16 Dec 2017 14:51:11 +0100 Subject: [PATCH] Add a memleak checker to the benchmark code Plug the leaks it surfaced in pwhash_argon2* tests --- test/default/cmptest.h | 22 +++++++++++++++++++++- test/default/pwhash_argon2i.c | 11 ++++++----- test/default/pwhash_argon2id.c | 12 ++++++------ 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/test/default/cmptest.h b/test/default/cmptest.h index c6bd7fc9..79de928e 100644 --- a/test/default/cmptest.h +++ b/test/default/cmptest.h @@ -93,6 +93,23 @@ static __attribute__((malloc)) void *mempool_allocarray(size_t count, size_t siz return mempool_alloc(count * size); } +static int mempool_free_all(void) +{ + size_t i; + int ret = 0; + + for (i = 0U; i < mempool_idx; i++) { + if ((mempool[i].size & (size_t) 0x80000000) == (size_t) 0x0) { + ret = -1; + } + free(mempool[i].pnt); + mempool[i].pnt = NULL; + } + mempool_idx = (size_t) 0U; + + return ret; +} + #define sodium_malloc(X) mempool_alloc(X) #define sodium_free(X) mempool_free(X) #define sodium_allocarray(X, Y) mempool_allocarray((X), (Y)) @@ -129,7 +146,10 @@ int main(void) } ts_end = now(); printf("%llu\n", 1000000ULL * (ts_end - ts_start) / ITERATIONS); - + if (mempool_free_all() != 0) { + fprintf(stderr, "** memory leaks detected **\n"); + return 99; + } return 0; } diff --git a/test/default/pwhash_argon2i.c b/test/default/pwhash_argon2i.c index fa811b88..3e1195e1 100644 --- a/test/default/pwhash_argon2i.c +++ b/test/default/pwhash_argon2i.c @@ -208,6 +208,7 @@ tv3(void) char *out; char *passwd; size_t i = 0U; + int ret; do { out = (char *) sodium_malloc(strlen(tests[i].out) + 1U); @@ -216,13 +217,13 @@ tv3(void) passwd = (char *) sodium_malloc(strlen(tests[i].passwd) + 1U); assert(passwd != NULL); memcpy(passwd, tests[i].passwd, strlen(tests[i].passwd) + 1U); - if (crypto_pwhash_str_verify(out, passwd, strlen(passwd)) != 0) { - printf("[tv3] pwhash_str failure (maybe intentional): [%u]\n", - (unsigned int) i); - continue; - } + ret = crypto_pwhash_str_verify(out, passwd, strlen(passwd)); sodium_free(out); sodium_free(passwd); + if (ret != 0) { + printf("[tv3] pwhash_str failure (maybe intentional): [%u]\n", + (unsigned int) i); + } } while (++i < (sizeof tests) / (sizeof tests[0])); } diff --git a/test/default/pwhash_argon2id.c b/test/default/pwhash_argon2id.c index cbb982fa..5940c9ca 100644 --- a/test/default/pwhash_argon2id.c +++ b/test/default/pwhash_argon2id.c @@ -204,6 +204,7 @@ tv3(void) char *out; char *passwd; size_t i = 0U; + int ret; do { out = (char *) sodium_malloc(strlen(tests[i].out) + 1U); @@ -212,13 +213,13 @@ tv3(void) passwd = (char *) sodium_malloc(strlen(tests[i].passwd) + 1U); assert(passwd != NULL); memcpy(passwd, tests[i].passwd, strlen(tests[i].passwd) + 1U); - if (crypto_pwhash_str_verify(out, passwd, strlen(passwd)) != 0) { - printf("[tv3] pwhash_argon2id_str failure (maybe intentional): [%u]\n", - (unsigned int) i); - continue; - } + ret = crypto_pwhash_str_verify(out, passwd, strlen(passwd)); sodium_free(out); sodium_free(passwd); + if (ret != 0) { + printf("[tv3] pwhash_argon2id_str failure (maybe intentional): [%u]\n", + (unsigned int) i); + } } while (++i < (sizeof tests) / (sizeof tests[0])); } @@ -230,7 +231,6 @@ str_tests(void) char *salt; const char *passwd = "Correct Horse Battery Staple"; - salt = (char *) sodium_malloc(crypto_pwhash_argon2id_SALTBYTES); str_out = (char *) sodium_malloc(crypto_pwhash_argon2id_STRBYTES); str_out2 = (char *) sodium_malloc(crypto_pwhash_argon2id_STRBYTES);