1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-19 18:15:18 -07:00

Add a memleak checker to the benchmark code

Plug the leaks it surfaced in pwhash_argon2* tests
This commit is contained in:
Frank Denis 2017-12-16 14:51:11 +01:00
parent b84e4b9ddf
commit bfc8ec1248
3 changed files with 33 additions and 12 deletions

View File

@ -93,6 +93,23 @@ static __attribute__((malloc)) void *mempool_allocarray(size_t count, size_t siz
return mempool_alloc(count * size); 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_malloc(X) mempool_alloc(X)
#define sodium_free(X) mempool_free(X) #define sodium_free(X) mempool_free(X)
#define sodium_allocarray(X, Y) mempool_allocarray((X), (Y)) #define sodium_allocarray(X, Y) mempool_allocarray((X), (Y))
@ -129,7 +146,10 @@ int main(void)
} }
ts_end = now(); ts_end = now();
printf("%llu\n", 1000000ULL * (ts_end - ts_start) / ITERATIONS); printf("%llu\n", 1000000ULL * (ts_end - ts_start) / ITERATIONS);
if (mempool_free_all() != 0) {
fprintf(stderr, "** memory leaks detected **\n");
return 99;
}
return 0; return 0;
} }

View File

@ -208,6 +208,7 @@ tv3(void)
char *out; char *out;
char *passwd; char *passwd;
size_t i = 0U; size_t i = 0U;
int ret;
do { do {
out = (char *) sodium_malloc(strlen(tests[i].out) + 1U); out = (char *) sodium_malloc(strlen(tests[i].out) + 1U);
@ -216,13 +217,13 @@ tv3(void)
passwd = (char *) sodium_malloc(strlen(tests[i].passwd) + 1U); passwd = (char *) sodium_malloc(strlen(tests[i].passwd) + 1U);
assert(passwd != NULL); assert(passwd != NULL);
memcpy(passwd, tests[i].passwd, strlen(tests[i].passwd) + 1U); memcpy(passwd, tests[i].passwd, strlen(tests[i].passwd) + 1U);
if (crypto_pwhash_str_verify(out, passwd, strlen(passwd)) != 0) { ret = crypto_pwhash_str_verify(out, passwd, strlen(passwd));
printf("[tv3] pwhash_str failure (maybe intentional): [%u]\n",
(unsigned int) i);
continue;
}
sodium_free(out); sodium_free(out);
sodium_free(passwd); 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])); } while (++i < (sizeof tests) / (sizeof tests[0]));
} }

View File

@ -204,6 +204,7 @@ tv3(void)
char *out; char *out;
char *passwd; char *passwd;
size_t i = 0U; size_t i = 0U;
int ret;
do { do {
out = (char *) sodium_malloc(strlen(tests[i].out) + 1U); out = (char *) sodium_malloc(strlen(tests[i].out) + 1U);
@ -212,13 +213,13 @@ tv3(void)
passwd = (char *) sodium_malloc(strlen(tests[i].passwd) + 1U); passwd = (char *) sodium_malloc(strlen(tests[i].passwd) + 1U);
assert(passwd != NULL); assert(passwd != NULL);
memcpy(passwd, tests[i].passwd, strlen(tests[i].passwd) + 1U); memcpy(passwd, tests[i].passwd, strlen(tests[i].passwd) + 1U);
if (crypto_pwhash_str_verify(out, passwd, strlen(passwd)) != 0) { ret = crypto_pwhash_str_verify(out, passwd, strlen(passwd));
printf("[tv3] pwhash_argon2id_str failure (maybe intentional): [%u]\n",
(unsigned int) i);
continue;
}
sodium_free(out); sodium_free(out);
sodium_free(passwd); 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])); } while (++i < (sizeof tests) / (sizeof tests[0]));
} }
@ -230,7 +231,6 @@ str_tests(void)
char *salt; char *salt;
const char *passwd = "Correct Horse Battery Staple"; const char *passwd = "Correct Horse Battery Staple";
salt = (char *) sodium_malloc(crypto_pwhash_argon2id_SALTBYTES); salt = (char *) sodium_malloc(crypto_pwhash_argon2id_SALTBYTES);
str_out = (char *) sodium_malloc(crypto_pwhash_argon2id_STRBYTES); str_out = (char *) sodium_malloc(crypto_pwhash_argon2id_STRBYTES);
str_out2 = (char *) sodium_malloc(crypto_pwhash_argon2id_STRBYTES); str_out2 = (char *) sodium_malloc(crypto_pwhash_argon2id_STRBYTES);