From: Sebastian <sc...@nb...> - 2003-04-15 07:08:56
|
Hi, On Mon, Apr 14, 2003 at 06:16:18PM -0700, Jeremy Fitzhardinge wrote: > Well, the behaviour you're seeing is consistent with be_random() returning > an undefined value in %eax. FP instructions like fildll immediately check > the defined-ness of their values (unlike integer instructions, which only > check for defined-ness when the value is used as a pointer or in a > conditional instruction). It may be that be_random() has a bug, or it is > returning an undefined value as a result of an undefined input. unsigned int be_random (unsigned int max) { unsigned int tmp; if (rand_fd == -1) be_randinit (); read (rand_fd, &tmp, sizeof (tmp)); /* 0 denotes special 0 to 2^32 - 1 range */ if (max == 0) return (tmp); return (tmp % max); } This code looks correct to me. 'rand_fd' leads to /dev/urandom, so to valgrind it should just look like if 'tmp' is defined by the read(), and the return value defined by the modulo operation. 0x804aa89 <be_random>: push %ebp 0x804aa8a <be_random+1>: mov %esp,%ebp 0x804aa8c <be_random+3>: sub $0x8,%esp 0x804aa8f <be_random+6>: cmpl $0xffffffff,0x8065668 0x804aa96 <be_random+13>: jne 0x804aa9d <be_random+20> 0x804aa98 <be_random+15>: call 0x804aa4e <be_randinit> 0x804aa9d <be_random+20>: sub $0x4,%esp 0x804aaa0 <be_random+23>: push $0x4 0x804aaa2 <be_random+25>: lea 0xfffffffc(%ebp),%eax 0x804aaa5 <be_random+28>: push %eax 0x804aaa6 <be_random+29>: pushl 0x8065668 0x804aaac <be_random+35>: call 0x8048a28 <read> 0x804aab1 <be_random+40>: add $0x10,%esp 0x804aab4 <be_random+43>: cmpl $0x0,0x8(%ebp) 0x804aab8 <be_random+47>: jne 0x804aac2 <be_random+57> 0x804aaba <be_random+49>: mov 0xfffffffc(%ebp),%eax 0x804aabd <be_random+52>: mov %eax,0xfffffff8(%ebp) 0x804aac0 <be_random+55>: jmp 0x804aad0 <be_random+71> 0x804aac2 <be_random+57>: mov 0xfffffffc(%ebp),%eax 0x804aac5 <be_random+60>: mov $0x0,%edx 0x804aaca <be_random+65>: divl 0x8(%ebp) 0x804aacd <be_random+68>: mov %edx,0xfffffff8(%ebp) 0x804aad0 <be_random+71>: mov 0xfffffff8(%ebp),%eax 0x804aad3 <be_random+74>: leave 0x804aad4 <be_random+75>: ret (Unoptimized GCC 3.2.3 code). In any case, there is no way for be_random to return without defining %eax. > J ciao, Sebastian -- -. sc...@nb... -. + http://segfault.net/~scut/ `--------------------. -' segfault.net/~scut/pgp `' 5453 AC95 1E02 FDA7 50D2 A42D 427E 6DEF 745A 8E07 `- 4 BLU-82/MOAB articles offered, payment due. hi echelon! -----------------' |