From 9567bbe65fc64c870ea5e7925c809ba70c4a896b Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Tue, 21 May 2019 10:50:44 +0200 Subject: [PATCH] Limit resources when running tests The default memory limit matches the limit already used when running the javascript and webassembly tests. Original diff by @pilou- Fixes #837 --- configure.ac | 3 ++- test/default/cmptest.h | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index e1fceada..9a8fa74a 100644 --- a/configure.ac +++ b/configure.ac @@ -554,7 +554,7 @@ AC_SUBST(CFLAGS_AESNI) AC_SUBST(CFLAGS_PCLMUL) AC_SUBST(CFLAGS_RDRAND) -AC_CHECK_HEADERS([sys/mman.h sys/random.h intrin.h]) +AC_CHECK_HEADERS([sys/mman.h sys/random.h sys/resource.h intrin.h]) AC_MSG_CHECKING([if _xgetbv() is available]) AC_LINK_IFELSE( @@ -796,6 +796,7 @@ AC_FUNC_ALLOCA AS_IF([test "x$EMSCRIPTEN" = "x"],[ AC_CHECK_FUNCS([arc4random arc4random_buf]) AC_CHECK_FUNCS([mmap mlock madvise mprotect]) + AC_CHECK_FUNCS([setrlimit]) AC_MSG_CHECKING(for getrandom with a standard API) AC_LINK_IFELSE([AC_LANG_PROGRAM([[ diff --git a/test/default/cmptest.h b/test/default/cmptest.h index 1ecc5cd9..6721f41a 100644 --- a/test/default/cmptest.h +++ b/test/default/cmptest.h @@ -14,9 +14,19 @@ #include #include +#if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_SETRLIMIT) +# include +# include +# include +#endif + #include "sodium.h" #include "quirks.h" +#ifndef TOTAL_MEMORY_TESTS +# define TOTAL_MEMORY_TESTS 16777216 +#endif + #ifdef __EMSCRIPTEN__ # undef TEST_SRCDIR # define TEST_SRCDIR "/test-data" @@ -37,6 +47,25 @@ int xmain(void); static unsigned char *guard_page; +static int set_resource_limits(void) +{ + int res = 0; + +#if defined(RLIM_INFINITY) && defined(HAVE_SETRLIMIT) + struct rlimit limits; + + limits.rlim_cur = limits.rlim_max = TOTAL_MEMORY_TESTS; +# ifdef RLIMIT_AS + res |= setrlimit(RLIMIT_AS, &limits); +# endif +# ifdef RLIMIT_DATA + res |= setrlimit(RLIMIT_DATA, &limits); +# endif +#endif + + return res; +} + #ifdef BENCHMARKS # include @@ -136,6 +165,8 @@ int main(void) unsigned long long ts_end; unsigned int i; + (void) set_resource_limits(); + if (sodium_init() != 0) { return 99; } @@ -171,6 +202,8 @@ int main(void) unsigned char *_guard_page; int c; + (void) set_resource_limits(); + if ((fp_res = fopen(TEST_NAME_RES, "w+")) == NULL) { perror("fopen(" TEST_NAME_RES ")"); return 99;