mpz_probab_prime_p crashes?

2023-06-08 Thread Philip Riebold
Hi,


I think I may have come across an error in mpz_probab_prime_p() It seems to

occasionally cause a segmentation fault either when repeatedly run with the

same arguments or with increasing values of the first argument.


I initially came across the bug when I was trying to find the closest primes

> 10^n, 2*10^n or 5*10^n when the program would crash after listing the
first

few dozen/hundred results.


The platform is a 16GB M2 Mac Air.


The test program below, pt3.c is the simplest I could produce to demonstrate

the problem.


The first argument 2011 is the smallest value I found which

caused the error.


When I ran pt3 100 times the for() loop executed between 4 and 4684 times

before crashing.


GMP version 6.2.1

=

Philip:gmp_error philip$ cat pt3.c

#include 

#include 

#include 


int main(void)

{

setbuf(stdout,NULL);

mpz_t test;


for(int n = 1; n <= 1000; n++) {

printf("%4d\n", n);

mpz_init(test);

mpz_set_str(test, "2011", 10);

int res = mpz_probab_prime_p(test, 100);

mpz_clear(test);

if(res != 1)

printf("res = %d\n", res);

}

printf("hmmm, it hasn't failed\n");

}

Philip:gmp_error philip$

=

Philip:gmp_error philip$ cc -v

Apple clang version 14.0.3 (clang-1403.0.22.14.1)

Target: arm64-apple-darwin22.5.0

Thread model: posix

InstalledDir:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Philip:gmp_error philip$

=

Philip:gmp_error philip$ uname -a

Darwin Philip 22.5.0 Darwin Kernel Version 22.5.0: Mon Apr 24 20:52:43 PDT
2023; root:xnu-8796.121.2~5/RELEASE_ARM64_T8112 arm64

Philip:gmp_error philip$

=

Philip:gmp-6.2.1 philip$ ./config.guess

aarch64-apple-darwin22.5.0

Philip:gmp-6.2.1 philip$

=

Philip:gmp-6.2.1 philip$ ./configfsf.guess

aarch64-apple-darwin22.5.0

Philip:gmp-6.2.1 philip$

=

Philip:gmp_error philip$ cc -o pt3 pt3.c -lgmp

Philip:gmp_error philip$ ./pt3

   1

   2

   3

   4

   ⋮

 143

 144

 145

 146

Segmentation fault: 11

Philip:gmp_error philip$

=


If there's anything I can do to help please ask.


TTFN

Philip Riebold
Philip:gmp-6.2.1 philip$ ./configure
checking build system type... aarch64-apple-darwin22.5.0
checking host system type... aarch64-apple-darwin22.5.0
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... ./install-sh -c -d
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking whether to enable maintainer-specific portions of Makefiles... no
checking ABI=64
checking compiler gcc -O2 -pedantic ... yes
checking compiler gcc -O2 -pedantic has sizeof(void*)==8... yes
checking compiler gcc -O2 -pedantic  -march=armv8-a... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking for gcc option to accept ISO C99... none needed
checking how to run the C preprocessor... gcc -E
checking build system compiler gcc... yes
checking for build system preprocessor... gcc -E
checking for build system executable suffix... 
checking whether build system compiler is ANSI... yes
checking for build system compiler math library... -lm
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
using ABI="64"
  CC="gcc"
  CFLAGS="-O2 -pedantic -march=armv8-a"
  CPPFLAGS=""
  MPN_PATH=" arm64 generic"
checking whether assembler supports --noexecstack option... no
checking for ar... ar
checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B
checking the name lister (/usr/bin/nm -B) interface... BSD nm
checking how to print strings... printf
checking for a sed that does not truncate output... /opt/local/bin/gsed
checking for fgrep... /usr/bin/grep -F
checking for ld used by gcc... 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld
checking if the linker 
(/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld)
 is GNU ld... no
checking whether ln -s works... yes
checking the maximum length of command line arguments... 786432
checking how to convert aarch64-apple-darwin22.5.0 file names to 
aarch64-apple-darwin22.5.0 format... func_convert_file_noop
checking how to convert aarch64-apple-darwin22.5.0 file names to toolchain 
format... f

Re: mpz_probab_prime_p crashes?

2023-06-08 Thread Torbjörn Granlund
Philip Riebold  writes:

  The test program below, pt3.c is the simplest I could produce to demonstrate
  the problem.

If that simple program would trigger a genuine GMP bug, that would be
astonishing indeed.

I have no idea why your computer gets a segfault.  Perhaps the malloc
implementation is completely broken?  Perhaps you hit bad RAM cells?

-- 
Torbjörn
Please encrypt, key id 0xC8601622
___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs


Re: mpz_probab_prime_p crashes?

2023-06-08 Thread Marc Glisse

Hello,

could you try with a recent snapshot from 
https://gmplib.org/download/snapshot/ ?


If you built GMP yourself, did you run `make check` afterwards?


On Thu, 8 Jun 2023, Philip Riebold wrote:


Hi,


I think I may have come across an error in mpz_probab_prime_p() It seems to

occasionally cause a segmentation fault either when repeatedly run with the

same arguments or with increasing values of the first argument.


I initially came across the bug when I was trying to find the closest primes


10^n, 2*10^n or 5*10^n when the program would crash after listing the

first

few dozen/hundred results.


The platform is a 16GB M2 Mac Air.


The test program below, pt3.c is the simplest I could produce to demonstrate

the problem.


The first argument 2011 is the smallest value I found which

caused the error.


When I ran pt3 100 times the for() loop executed between 4 and 4684 times

before crashing.


GMP version 6.2.1

=

Philip:gmp_error philip$ cat pt3.c

#include 

#include 

#include 


int main(void)

{

   setbuf(stdout,NULL);

   mpz_t test;


   for(int n = 1; n <= 1000; n++) {

   printf("%4d\n", n);

   mpz_init(test);

   mpz_set_str(test, "2011", 10);

   int res = mpz_probab_prime_p(test, 100);

   mpz_clear(test);

   if(res != 1)

   printf("res = %d\n", res);

   }

   printf("hmmm, it hasn't failed\n");

}

Philip:gmp_error philip$

=

Philip:gmp_error philip$ cc -v

Apple clang version 14.0.3 (clang-1403.0.22.14.1)

Target: arm64-apple-darwin22.5.0

Thread model: posix

InstalledDir:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Philip:gmp_error philip$

=

Philip:gmp_error philip$ uname -a

Darwin Philip 22.5.0 Darwin Kernel Version 22.5.0: Mon Apr 24 20:52:43 PDT
2023; root:xnu-8796.121.2~5/RELEASE_ARM64_T8112 arm64

Philip:gmp_error philip$

=

Philip:gmp-6.2.1 philip$ ./config.guess

aarch64-apple-darwin22.5.0

Philip:gmp-6.2.1 philip$

=

Philip:gmp-6.2.1 philip$ ./configfsf.guess

aarch64-apple-darwin22.5.0

Philip:gmp-6.2.1 philip$

=

Philip:gmp_error philip$ cc -o pt3 pt3.c -lgmp

Philip:gmp_error philip$ ./pt3

  1

  2

  3

  4

  ⋮

143

144

145

146

Segmentation fault: 11

Philip:gmp_error philip$

=


If there's anything I can do to help please ask.


TTFN

Philip Riebold



--
Marc Glisse
___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs