Commit 8d71a9e33ba385d176809e99b659f66484481974

Daniel Mendler 2019-05-11T00:19:28

travis: add -fsanitize=undefined

diff --git a/.travis.yml b/.travis.yml
index bde3d08..d1ad089 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -86,7 +86,7 @@ matrix:
     # GCC for the x64_32 architecture (32-bit longs and 32-bit pointers)
     # TODO: Probably not possible to run anything in x32 in Travis
     #       but needs to be checked to be sure.
-    - env: BUILDOPTIONS='--with-cc=gcc-5 --with-mx32'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --with-mx32'
       addons:
         apt:
           packages:
@@ -94,7 +94,7 @@ matrix:
             - gcc-multilib
 
     # GCC for the x86-64 architecture (64-bit longs and 64-bit pointers)
-    - env: BUILDOPTIONS='--with-cc=gcc-5 --with-m64 --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --with-m64 --with-valgrind'
     - env: BUILDOPTIONS='--with-cc=gcc-4.7 --with-m64 --with-valgrind'
       addons:
         apt:
@@ -112,13 +112,13 @@ matrix:
             - gcc-4.9
 
     # clang for x86-64 architecture (64-bit longs and 64-bit pointers)
-    - env: CONV_WARNINGS=1 BUILDOPTIONS='--with-cc=clang-7 --with-m64 --with-valgrind'
-    - env: BUILDOPTIONS='--with-cc=clang-6.0 --with-m64 --with-valgrind'
+    - env: SANITIZER=1 CONV_WARNINGS=1 BUILDOPTIONS='--with-cc=clang-7 --with-m64 --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-6.0 --with-m64 --with-valgrind'
       addons:
         apt:
           packages:
             - clang-6.0
-    - env: BUILDOPTIONS='--with-cc=clang-5.0 --with-m64 --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-5.0 --with-m64 --with-valgrind'
       addons:
         apt:
           packages:
@@ -132,34 +132,34 @@ matrix:
     # GCC for the x86-64 architecture with restricted limb sizes
     # formerly started with the option "--with-low-mp" to testme.sh
     # but testing all three in one run took to long and timed out.
-    - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_8BIT  --with-valgrind'
-    - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_16BIT --with-valgrind'
-    - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_32BIT --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_8BIT  --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_16BIT --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_32BIT --with-valgrind'
 
     # clang for the x86-64 architecture with restricted limb sizes
-    - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_8BIT  --with-valgrind'
-    - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_16BIT --with-valgrind'
-    - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_32BIT --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_8BIT  --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_16BIT --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_32BIT --with-valgrind'
 
     # Test "autotuning", the automatic evaluation and setting of the Toom-Cook cut-offs.
-    - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_8BIT  --with-valgrind --make-option=tune'
-    - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_16BIT --with-valgrind --make-option=tune'
-    - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_32BIT --with-valgrind --make-option=tune'
-    - env: BUILDOPTIONS='--with-cc=gcc-5 --with-valgrind --make-option=tune'
-    - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_8BIT  --with-valgrind --make-option=tune'
-    - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_16BIT --with-valgrind --make-option=tune'
-    - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_32BIT --with-valgrind --make-option=tune'
-    - env: BUILDOPTIONS='--with-cc=clang-7 --with-valgrind --make-option=tune'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_8BIT  --with-valgrind --make-option=tune'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_16BIT --with-valgrind --make-option=tune'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_32BIT --with-valgrind --make-option=tune'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --with-valgrind --make-option=tune'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_8BIT  --with-valgrind --make-option=tune'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_16BIT --with-valgrind --make-option=tune'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_32BIT --with-valgrind --make-option=tune'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --with-valgrind --make-option=tune'
 
     # GCC for the x86-64 architecture testing against a different Bigint-implementation
     # with 333333 different inputs.
-    - env: BUILDOPTIONS='--with-cc=gcc-5 --test-vs-mtest=333333 --with-valgrind'
-    - env: BUILDOPTIONS='--with-cc=clang-7 --test-vs-mtest=333333 --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --test-vs-mtest=333333 --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --test-vs-mtest=333333 --with-valgrind'
 
     # clang for the x86-64 architecture testing against a different Bigint-implementation
     # with a better random source.
-    - env: BUILDOPTIONS='--with-cc=gcc-5 --test-vs-mtest=333333 --mtest-real-rand --with-valgrind'
-    - env: BUILDOPTIONS='--with-cc=clang-7 --test-vs-mtest=333333 --mtest-real-rand --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --test-vs-mtest=333333 --mtest-real-rand --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --test-vs-mtest=333333 --mtest-real-rand --with-valgrind'
 
 
 # Notifications go to
diff --git a/demo/test.c b/demo/test.c
index 2d0348f..00716e0 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -260,7 +260,7 @@ static int test_mp_complement(void)
    }
 
    for (i = 0; i < 1000; ++i) {
-      long l = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+      long l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
       mp_set_long(&a, (unsigned long)labs(l));
       if (l < 0)
          mp_neg(&a, &a);
@@ -297,7 +297,7 @@ static int test_mp_tc_div_2d(void)
       long l;
       int em;
 
-      l = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+      l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
       mp_set_long(&a, (unsigned long)labs(l));
       if (l < 0)
          mp_neg(&a, &a);
@@ -335,12 +335,12 @@ static int test_mp_tc_xor(void)
    for (i = 0; i < 1000; ++i) {
       int l, em;
 
-      l = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+      l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
       mp_set_int(&a, (unsigned long)labs(l));
       if (l < 0)
          mp_neg(&a, &a);
 
-      em = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+      em = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
       mp_set_int(&b, (unsigned long)labs(em));
       if (em < 0)
          mp_neg(&b, &b);
@@ -376,12 +376,12 @@ static int test_mp_tc_or(void)
    for (i = 0; i < 1000; ++i) {
       long l, em;
 
-      l = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+      l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
       mp_set_long(&a, (unsigned long)labs(l));
       if (l < 0)
          mp_neg(&a, &a);
 
-      em = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+      em = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
       mp_set_long(&b, (unsigned long)labs(em));
       if (em < 0)
          mp_neg(&b, &b);
@@ -416,12 +416,12 @@ static int test_mp_tc_and(void)
    for (i = 0; i < 1000; ++i) {
       long l, em;
 
-      l = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+      l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
       mp_set_long(&a, (unsigned long)labs(l));
       if (l < 0)
          mp_neg(&a, &a);
 
-      em = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+      em = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
       mp_set_long(&b, (unsigned long)labs(em));
       if (em < 0)
          mp_neg(&b, &b);
@@ -558,7 +558,7 @@ static int test_mp_get_int(void)
    }
 
    for (i = 0; i < 1000; ++i) {
-      t = (unsigned long)(rand() * rand() + 1) & 0xFFFFFFFFuL;
+      t = ((unsigned long)rand() * (unsigned long)rand() + 1uL) & 0xFFFFFFFFuL;
       mp_set_int(&a, t);
       if (t != mp_get_int(&a)) {
          printf("\nmp_get_int() bad result!");
diff --git a/makefile_include.mk b/makefile_include.mk
index 161321a..d7d4cba 100644
--- a/makefile_include.mk
+++ b/makefile_include.mk
@@ -49,6 +49,10 @@ endif
 
 CFLAGS += -I./ -Wall -Wsign-compare -Wextra -Wshadow
 
+ifdef SANITIZER
+CFLAGS += -fsanitize=undefined -fno-sanitize-recover=all -fno-sanitize=float-divide-by-zero
+endif
+
 ifndef NO_ADDTL_WARNINGS
 # additional warnings
 CFLAGS += -Wdeclaration-after-statement -Wbad-function-cast -Wcast-align