Floating point exception in modulo

Bug #391083 reported by Gwen Weinholt
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Ikarus Scheme
Fix Committed
Medium
Abdulaziz Ghuloum

Bug Description

$ ikarus
Ikarus Scheme version 0.0.4-rc1+, 64-bit (revision 1806, build 2009-06-20)
Copyright (c) 2006-2009 Abdulaziz Ghuloum

> (modulo #xfffffffffffffff #x100000000)
4294967295
> (modulo #xffffffffffffffff #x100000000)
Floating point exception (core dumped)

Related branches

Revision history for this message
leppie (leppie) wrote :

Works on 32-bit Cygwin. revision 1799

Revision history for this message
Andreas Rottmann (rotty) wrote :

Works on 64bit Debian GNU/Linux as well - revision 1806.

Revision history for this message
Gwen Weinholt (weinholt) wrote :

I found the bug on Debian GNU/Linux 5.0.1 amd64 and I've now reproduced it on another 64-bit machine with the same OS. Here's some extra info from ikarus compiled with -O0 -g3 (and not -DNDEBUG):

Ikarus Scheme version 0.0.4-rc1+, 64-bit (revision 1806, build 2009-06-24)
Copyright (c) 2006-2009 Abdulaziz Ghuloum

> (modulo #xffffffffffffffff #x100000000)
MMAP 0x00007f3bc84a7000 .. 0x00007f3bc84a7fff

Program received signal SIGFPE, Arithmetic exception.
0x00007f3bc98d058b in __gmpn_mod_1 () from /usr/lib/libgmp.so.3
(gdb) bt
#0 0x00007f3bc98d058b in __gmpn_mod_1 () from /usr/lib/libgmp.so.3
#1 0x0000000000412907 in ikrt_bnfx_modulo (x=139894750946565, y=34359738368) at ikarus-numerics.c:1821
#2 0x0000000000417dd0 in L_zero_args () at ikarus-enter.S:196
#3 0x0000000000000000 in ?? ()
(gdb) f 1
#1 0x0000000000412907 in ikrt_bnfx_modulo (x=139894750946565, y=34359738368) at ikarus-numerics.c:1821
1821 mp_limb_t m = mpn_mod_1(s2p, s2n, yint);
(gdb) p s2p
$1 = (mp_limb_t *) 0x7f3bc8efa508
(gdb) p *s2p
$2 = 18446744073709551615
(gdb) p s2n
$3 = 1
(gdb) p yint
$4 = 0

Looks like a division by zero. Here's an easier way to reproduce it that even works on the 32-bit build:
$ ikarus
Ikarus Scheme version 0.0.4-rc1+ (revision 1806, build 2009-06-23)
Copyright (c) 2006-2009 Abdulaziz Ghuloum

> (modulo 1 0)
Floating point exception (core dumped)

Revision history for this message
Gwen Weinholt (weinholt) wrote :

Looks like unfix() casts its value to 32 bits, which might be the trigger for the original problem on the 64-bit build:
#1 0x0000000000412907 in ikrt_bnfx_modulo (x=139894750946565, y=34359738368) at ikarus-numerics.c:1821
1798 ikptr
1799 ikrt_bnfx_modulo(ikptr x, ikptr y /*, ikpcb* pcb */){
1800 int yint = unfix(y);

#define wordshift ((wordsize == 4)?2:3)
#define fx_shift wordshift
#define unfix(x) (((long int)(x)) >> fx_shift)

(gdb) p (((long int) (34359738368)) >> 3)
$1 = 0
(gdb) p (((long long int) (34359738368)) >> 3)
$2 = 4294967296

Revision history for this message
Gwen Weinholt (weinholt) wrote :

Ok, it seems like gdb treats (long int) as 32 bits sometimes, depending on what program has been loaded possibly... so ignore that.
The problem is this:
1800 int yint = unfix(y);
If I change that to long int, then (modulo #xffffffffffffffff #x100000000) works correctly. But the division-by-zero can still be triggered if y==0, so a check needs to be added.

Revision history for this message
Abdulaziz Ghuloum (aghuloum) wrote :

Fixed in revision 1811. Thank you all for investigating the causes of this bug.

Changed in ikarus:
assignee: nobody → Abdulaziz Ghuloum (aghuloum)
importance: Undecided → Medium
status: New → Fix Committed
Revision history for this message
Gwen Weinholt (weinholt) wrote :

This bug is not completely fixed yet:

$ ikarus
Ikarus Scheme version 0.0.4-rc1+, 64-bit (revision 1815, build 2009-06-26)
Copyright (c) 2006-2009 Abdulaziz Ghuloum

> (modulo 1 0)
Floating point exception (core dumped)

Changed in ikarus:
status: Fix Committed → Confirmed
Revision history for this message
Abdulaziz Ghuloum (aghuloum) wrote :

Fixed (again) in 1819. Thanks.

Changed in ikarus:
status: Confirmed → Fix Committed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.