Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build+test bn_mp_set_double.c on more platforms #476

Merged
merged 3 commits into from
Mar 5, 2020
Merged

Conversation

minad
Copy link
Member

@minad minad commented Feb 17, 2020

Not all platforms/environments/architectures that support enough of
IEEE 754 for the purposes of mp_set_double() actually support enough
to legitimately define STDC_IEC_559, so only relying on that is
too strict. Fixes #159

Not all platforms/environments/architectures that support enough of
IEEE 754 for the purposes of mp_set_double() actually support enough
to legitimately define __STDC_IEC_559__, so only relying on that is
too strict. Fixes #159
Copy link
Member

@sjaeckel sjaeckel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx

mp_set_double.c Outdated
@@ -3,7 +3,7 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

#if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559)
#if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559) || defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64) || defined(__i386__) || defined(_M_X86) || defined(__aarch64__) || defined(__arm__)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe have this in tommath_private.h instead?

#if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559) || defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64) || defined(__i386__) || defined(_M_X86) || defined(__aarch64__) || defined(__arm__)
#define MP_HAS_SET_DOUBLE

and check for MP_HAS_SET_DOUBLE?

@sjaeckel
Copy link
Member

after fixing the build this should be fine...

@minad
Copy link
Member Author

minad commented Feb 19, 2020

after fixing the build this should be fine...

Why is the build suddenly failing?

@sjaeckel
Copy link
Member

I guess build30866717.log:335 says it all ;-)

@MasterDuke17
Copy link
Collaborator

I don't get divide by zero errors, but git checkout develop && git pull upstream develop && make test && ./test does have two fails: test_mp_radix_size, line 1843: EXPECT(size == results[radix]) failed and test_s_mp_radix_size_overestimate, line 1989: EXPECT(size >= size2) failed

@czurnieden
Copy link
Contributor

[…] two fails: test_mp_radix_size, line 1843: EXPECT(size == results[radix]) failed and test_s_mp_radix_size_overestimate, line 1989: EXPECT(size >= size2) failed

(Ow, that's my part)

Cannot repeat.
Can you get more details out of it, please?

@MasterDuke17
Copy link
Collaborator

Adding some fprintfs:

diff --git a/demo/test.c b/demo/test.c
index 998f14b..66a9635 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -1338,6 +1338,7 @@ static int test_mp_log_n(void)
       DO(s_rs(&a,(int)base, &size));
       /* radix_size includes the memory needed for '\0', too*/
       size -= 2;
+      fprintf(stderr, "lb = %i, size = %i\n", lb, size);
       EXPECT(lb == size);
    }
 
@@ -1350,6 +1351,7 @@ static int test_mp_log_n(void)
       DO(mp_log_n(&a, base, &lb));
       DO(s_rs(&a,(int)base, &size));
       size -= 2;
+      fprintf(stderr, "lb = %i, size = %i\n", lb, size);
       EXPECT(lb == size);
    }
 
@@ -1840,9 +1842,11 @@ static int test_mp_radix_size(void)
 
    for (radix = 2; radix < 65; radix++) {
       DO(mp_radix_size(&a, radix, &size));
+      fprintf(stderr, "size = %li, results[radix] = %li\n", size, results[radix]);
       EXPECT(size == results[radix]);
       a.sign = MP_NEG;
       DO(mp_radix_size(&a, radix, &size));
+      fprintf(stderr, "size = %li, results[radix] + 1 = %li\n", size, results[radix] + 1);
       EXPECT(size == (results[radix] + 1));
       a.sign = MP_ZPOS;
    }
@@ -1970,10 +1974,12 @@ static int test_s_mp_radix_size_overestimate(void)
 
    for (radix = 2; radix < 65; radix++) {
       DO(s_mp_radix_size_overestimate(&a, radix, &size));
+      fprintf(stderr, "size = %lu, results[radix] = %lu\n", size, results[radix]);
       EXPECT(size >= results[radix]);
       EXPECT(size < results[radix] + 20); /* some error bound */
       a.sign = MP_NEG;
       DO(s_mp_radix_size_overestimate(&a, radix, &size));
+      fprintf(stderr, "size = %lu, results[radix] = %lu\n", size, results[radix]);
       EXPECT(size >= results[radix]);
       EXPECT(size < results[radix] + 20); /* some error bound */
       a.sign = MP_ZPOS;
@@ -1986,11 +1992,13 @@ static int test_s_mp_radix_size_overestimate(void)
       for (radix = 2; radix < 65; radix++) {
          DO(s_mp_radix_size_overestimate(&a, radix, &size));
          DO(mp_radix_size(&a, radix, &size2));
+      fprintf(stderr, "size = %li, size2 = %li\n", size, size2);
          EXPECT(size >= size2);
          EXPECT(size < size2 + 20); /* some error bound */
          a.sign = MP_NEG;
          DO(s_mp_radix_size_overestimate(&a, radix, &size));
          DO(mp_radix_size(&a, radix, &size2));
+      fprintf(stderr, "size = %li, size2 = %li\n", size, size2);
          EXPECT(size >= size2);
          EXPECT(size < size2 + 20); /* some error bound */
          a.sign = MP_ZPOS;

gives

TEST mp_radix_size
size = 140045998622299, results[radix] = 1627
test_mp_radix_size, line 1846: EXPECT(size == results[radix]) failed


FAIL mp_radix_size
TEST s_mp_radix_size_overestimate
size = 1628, results[radix] = 1627
size = 1628, results[radix] = 1627
size = 1028, results[radix] = 1027
size = 1028, results[radix] = 1027
size = 815, results[radix] = 814
size = 815, results[radix] = 814
size = 703, results[radix] = 702
size = 703, results[radix] = 702
size = 632, results[radix] = 630
size = 632, results[radix] = 630
size = 582, results[radix] = 581
size = 582, results[radix] = 581
size = 544, results[radix] = 543
size = 544, results[radix] = 543
size = 515, results[radix] = 514
size = 515, results[radix] = 514
size = 492, results[radix] = 491
size = 492, results[radix] = 491
size = 473, results[radix] = 471
size = 473, results[radix] = 471
size = 456, results[radix] = 455
size = 456, results[radix] = 455
size = 442, results[radix] = 441
size = 442, results[radix] = 441
size = 430, results[radix] = 428
size = 430, results[radix] = 428
size = 419, results[radix] = 418
size = 419, results[radix] = 418
size = 409, results[radix] = 408
size = 409, results[radix] = 408
size = 400, results[radix] = 399
size = 400, results[radix] = 399
size = 392, results[radix] = 391
size = 392, results[radix] = 391
size = 385, results[radix] = 384
size = 385, results[radix] = 384
size = 379, results[radix] = 378
size = 379, results[radix] = 378
size = 373, results[radix] = 372
size = 373, results[radix] = 372
size = 367, results[radix] = 366
size = 367, results[radix] = 366
size = 362, results[radix] = 361
size = 362, results[radix] = 361
size = 357, results[radix] = 356
size = 357, results[radix] = 356
size = 353, results[radix] = 352
size = 353, results[radix] = 352
size = 348, results[radix] = 347
size = 348, results[radix] = 347
size = 344, results[radix] = 343
size = 344, results[radix] = 343
size = 341, results[radix] = 340
size = 341, results[radix] = 340
size = 337, results[radix] = 336
size = 337, results[radix] = 336
size = 334, results[radix] = 333
size = 334, results[radix] = 333
size = 331, results[radix] = 330
size = 331, results[radix] = 330
size = 328, results[radix] = 327
size = 328, results[radix] = 327
size = 325, results[radix] = 324
size = 325, results[radix] = 324
size = 322, results[radix] = 321
size = 322, results[radix] = 321
size = 320, results[radix] = 318
size = 320, results[radix] = 318
size = 317, results[radix] = 316
size = 317, results[radix] = 316
size = 315, results[radix] = 314
size = 315, results[radix] = 314
size = 312, results[radix] = 311
size = 312, results[radix] = 311
size = 310, results[radix] = 309
size = 310, results[radix] = 309
size = 308, results[radix] = 307
size = 308, results[radix] = 307
size = 306, results[radix] = 305
size = 306, results[radix] = 305
size = 304, results[radix] = 303
size = 304, results[radix] = 303
size = 302, results[radix] = 301
size = 302, results[radix] = 301
size = 300, results[radix] = 299
size = 300, results[radix] = 299
size = 299, results[radix] = 298
size = 299, results[radix] = 298
size = 297, results[radix] = 296
size = 297, results[radix] = 296
size = 295, results[radix] = 294
size = 295, results[radix] = 294
size = 294, results[radix] = 293
size = 294, results[radix] = 293
size = 292, results[radix] = 291
size = 292, results[radix] = 291
size = 291, results[radix] = 290
size = 291, results[radix] = 290
size = 289, results[radix] = 288
size = 289, results[radix] = 288
size = 288, results[radix] = 287
size = 288, results[radix] = 287
size = 286, results[radix] = 285
size = 286, results[radix] = 285
size = 285, results[radix] = 284
size = 285, results[radix] = 284
size = 284, results[radix] = 283
size = 284, results[radix] = 283
size = 282, results[radix] = 281
size = 282, results[radix] = 281
size = 281, results[radix] = 280
size = 281, results[radix] = 280
size = 280, results[radix] = 279
size = 280, results[radix] = 279
size = 279, results[radix] = 278
size = 279, results[radix] = 278
size = 278, results[radix] = 277
size = 278, results[radix] = 277
size = 277, results[radix] = 276
size = 277, results[radix] = 276
size = 276, results[radix] = 275
size = 276, results[radix] = 275
size = 275, results[radix] = 273
size = 275, results[radix] = 273
size = 273, results[radix] = 272
size = 273, results[radix] = 272
size = 54, size2 = 140045998620725
test_s_mp_radix_size_overestimate, line 1996: EXPECT(size >= size2) failed


FAIL s_mp_radix_size_overestimate

@czurnieden
Copy link
Contributor

Mmh…interesting.
(BTW: the type for the sizes is size_t and the length modifier for size_t for printf is z, so please use %zu next time, if you don't mind. Although I doubt that the exact values would matter in this case.)

It seems as if the calculation for powers of two failed here. (Or more? Could you do me a favour and comment out the exit-on-failure parts and print the actual radix, too, please?)
Who wrote s_mp_log_2expt.c?

czurnieden ~/GITHUB/libtommath O (develop)$ git blame s_mp_log_2expt.c
f6a7bedb (Daniel Mendler 2019-10-29 20:52:29 +0100  1) #include "tommath_private.h"
f6a7bedb (Daniel Mendler 2019-10-29 20:52:29 +0100  2) #ifdef S_MP_LOG_2EXPT_C
f6a7bedb (Daniel Mendler 2019-10-29 20:52:29 +0100  3) /* LibTomMath, multiple-precision integer library -- Tom St Denis */
f6a7bedb (Daniel Mendler 2019-10-29 20:52:29 +0100  4) /* SPDX-License-Identifier: Unlicense */
f6a7bedb (Daniel Mendler 2019-10-29 20:52:29 +0100  5) 
f6a7bedb (Daniel Mendler 2019-10-29 20:52:29 +0100  6) int s_mp_log_2expt(const mp_int *a, mp_digit base)
f6a7bedb (Daniel Mendler 2019-10-29 20:52:29 +0100  7) {
f6a7bedb (Daniel Mendler 2019-10-29 20:52:29 +0100  8)    int y;
f6a7bedb (Daniel Mendler 2019-10-29 20:52:29 +0100  9)    for (y = 0; (base & 1) == 0; y++, base >>= 1) {}
f6a7bedb (Daniel Mendler 2019-10-29 20:52:29 +0100 10)    return (mp_count_bits(a) - 1) / y;
f6a7bedb (Daniel Mendler 2019-10-29 20:52:29 +0100 11) }
f6a7bedb (Daniel Mendler 2019-10-29 20:52:29 +0100 12) #endif

Oh, not me? Phew! ;-)

But lame jokes aside: that's a really curious one. An uninitialized value somewhere (it worked the second time with the same number and base)? Compiler optimization gone too wild?

Can you try a different compiler/optimization?

I know I'm a bit demanding here, but as I cannot repeat it, I'm restricted to poking around in the hope to get bitten at some point and be able to analyse the venom.

Mmh…interesting, indeed.

@minad
Copy link
Member Author

minad commented Feb 19, 2020

@czurnieden thanks for looking into this! Uninitialized values should be caught by the compiler statically and if not, then by valgrind. I find it weird that this problem suddenly came up. Maybe git bisect could help?

@czurnieden
Copy link
Contributor

@czurnieden thanks for looking into this!

Well, I have to, it's (mainly) my code!

Uninitialized values should be caught by the compiler statically and if not, then by valgrind.

Yepp, most likely, but as I said: I'm poking in the dark here, cannot assume anything and static/dynamic analysing can fail, too.

Maybe git bisect could help?

We haven't changed much in the last couple of commits, so yes, that could be helpful.

@MasterDuke17 please find a commit in develop that worked for you. You don't have to try all, just guess a date and choose a commit or two before that date and use git bisect with the start´, good, and bad` commands to find the last working version as described at https://git-scm.com/docs/git-bisect (even I understood it and that says something ;-) )
Thank you very much for your patience!

@MasterDuke17
Copy link
Collaborator

Hm, I just tried again with both clang version 9.0.1 and gcc (Arch Linux 9.2.1+20200130-2) 9.2.1 20200130 and it doesn't repro after doing a make clean, in fact there are no failures.

@MasterDuke17
Copy link
Collaborator

And no complaints from valgrind after building with each compiler.

@czurnieden
Copy link
Contributor

it doesn't repro after doing a make clean, in fact there are no failures.

Ow, you're killing me! ;-)

Nuh, doesn't really matter, shit happens.

@sjaeckel
Copy link
Member

@MasterDuke17 can you please do a git fetch upstream && git checkout -b fix-double-check upstream/fix-double-check && make clean && make check

@MasterDuke17
Copy link
Collaborator

@MasterDuke17 can you please do a git fetch upstream && git checkout -b fix-double-check upstream/fix-double-check && make clean && make check

Works just fine with gcc and clang, no errors.

@sjaeckel
Copy link
Member

And with MSVC?

@MasterDuke17
Copy link
Collaborator

And with MSVC?

Dunno, I don't have a windows machine to test on.

@sjaeckel sjaeckel force-pushed the fix-double-check branch 2 times, most recently from 75e7cb6 to 68ce8c1 Compare March 5, 2020 13:05
@sjaeckel sjaeckel merged commit ce4e6ae into develop Mar 5, 2020
@sjaeckel sjaeckel deleted the fix-double-check branch March 5, 2020 13:25
rozhuk-im pushed a commit to rozhuk-im/freebsd-ports that referenced this pull request Sep 20, 2021
Reported by: Sébastien Santoro <dereckson@espace-win.org>
Obtained from: libtom/libtommath#476
evanmiller added a commit to evanmiller/macports-ports that referenced this pull request Mar 23, 2022
mascguy pushed a commit to macports/macports-ports that referenced this pull request Mar 25, 2022
@chenrui333
Copy link

any ideas about when this change can be released out (did not see in 1.3.0 release tarball)

@sjaeckel
Copy link
Member

Yeah, I missed this change :-\

Give a few days for other issues to maybe pop up and I'll make a 1.3.1.

@sjaeckel
Copy link
Member

@chenrui333 can you please check whether https://github.com/libtom/libtommath/releases/tag/v1.3.1-rc1 fixes this for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bn_mp_set_double.c does not compile under MacOS plus
5 participants