From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <799c659c4e05d2a1956ca669bd48b82a.axp-list@archive.invalid> X-Archive-Original-Message-ID: <199710181920.MAA07898@wasatch.ps.uci.edu> Date: Sat, 18 Oct 1997 12:20:45 -0700 From: Christopher J. Grayce Subject: foo=malloc(),realloc(foo) => SIGSEGV ?! Dear Folks, Can anyone shed light on this? I get SIGSEGV when calling realloc with what I reall think is a legitimate pointer. The source is rather long, and I haven't been able to duplicate the bug with a short program, alas, but it seems to me this behaviour is incorrect no matter what my code does. I'd be glad to stand corrected, of course. The program runs without error on this exact same system when it is compiled by Digital's C compiler instead of gcc. If I run my program under gdb then gdb itself gets SIGSEGV and dies. The system is a PC164 + 128 Mb RAM, more or less stock RH 4.2. Here is a description of the system, the relevant sections of the C source code, the compiling command, the output, and the result of strace. Any help would be, of course, greatly appreciated. Christopher Grayce UCI Dept of Chemistry ------------------ the system ----------------------------------------- % uname -a Linux wasatch.ps.uci.edu 2.0.31 #2 Mon Aug 18 08:45:37 PDT 1997 alpha % rpm -q gcc gcc-2.7.2.1-2 % rpm -q glibc glibc-0.961212-5 % rpm -q binutils binutils-2.7.0.2-4 % cat /proc/cpuinfo cpu : Alpha cpu model : EV5 cpu variation : 0 cpu revision : 0 cpu serial number : Linux_is_Great! system type : EB164 system variation : 0 system revision : 0 system serial number : MILO-0000 cycle frequency [Hz] : 0 timer frequency [Hz] : 1024.00 page size [bytes] : 8192 phys. address bits : 40 max. addr. space # : 127 BogoMIPS : 497.02 kernel unaligned acc : 0 (pc=0,va=0) user unaligned acc : 22931684 (pc=2d,va=1555611c5b0) ------------------ the C source ----------------------------------------- /********************************************************************* file main.c */ #include #include #ifndef XYZ_DEFINED typedef struct { double x ; double y ; double z ; } xyz_triple ; #define XYZ_DEFINED #endif int main(int argc, char *argv[]) { static xyz_triple *fluid = 0 ; int ns = 0 ; double drmax ; int want,check ; /* prototype for smove */ int smove(xyz_triple **,int *, double) ; want = 1 ; printf("&fluid = %p, fluid = %p, want = %d, sizeof(xyz_triple) = %d\n", &fluid,fluid,want,sizeof(xyz_triple)) ; fluid = realloc(fluid,want * sizeof(xyz_triple)) ; printf("&fluid = %p, fluid = %p, want = %d, sizeof(xyz_triple) = %d\n", &fluid, fluid,want,sizeof(xyz_triple)) ; [...] printf("&fluid = %p, fluid = %p, ns = %d, drmax = %f\n", &fluid,fluid,ns,drmax) ; check = smove(&fluid,&ns,drmax) ; [...] } /************************************************************************** file smove.c */ static int nex ; static int check_memory(xyz_triple **fluidp) { static int list_sz = 0 ; int want ; if (nex+2 < list_sz) return(0) ; want = list_sz + 100 ; printf("fluidp = %p, *fluidp = %p, want = %d, sizeof(xyz_triple) = %d\n", fluidp,*fluidp,want,sizeof(xyz_triple)) ; *fluidp = (xyz_triple *)realloc((*fluidp),want * sizeof(xyz_triple)) ; exit(1) ; [...] } int smove(xyz_triple **fluidp, int *ns, double drmax) { nex = *ns ; printf("fluidp = %p, *fluidp = %p, ns = %d, drmax = %f\n", fluidp,*fluidp,*ns,drmax) ; check_memory(fluidp) ; [...] } ------------------ the compiling command --------------------------------------- % cc -c -g -Wall -o /home/cgrayce/bin/bound main.c smove.c ------------------ the standard output --------------------------------------- % strace -o tracefile ~/bin/bound [...] &fluid = 0x1201106e8, fluid = (nil), want = 1, sizeof(xyz_triple) = 24 &fluid = 0x1201106e8, fluid = 0x120111f00, want = 1, sizeof(xyz_triple) = 24 [...] &fluid = 0x1201106e8, fluid = 0x120111f00, ns = 0, drmax = 0.250000 fluidp = 0x1201106e8, *fluidp = 0x120111f00, ns = 0, drmax = 0.250000 fluidp = 0x1201106e8, *fluidp = 0x120111f00, want = 100, sizeof(xyz_triple) = 24 Segmentation fault (core dumped) ------------------ the last line in /var/log/messages ------------------------ Oct 18 11:34:55 wasatch kernel: bound: memory violation at pc=155557f128c rp=120002c2c (bad address = 120112868) ------------------ the output of strace --------------------------- execve("/home/cgrayce/bin/bound", ["/home/cgrayce/bin/bound"], [/* 23 vars */]) = 0 brk(0) = 0x120111ee8 open("/etc/ld.so.preload", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/lib/libm.so.6", O_RDONLY) = -1 ENOENT (No such file or directory) open("/etc/ld.so.cache", O_RDONLY) = 4 fstat(4, {st_mode=S_IFREG|0644, st_size=5354, ...}) = 0 mmap(0, 4831835704, PROT_READ, 0 /* MAP_??? */, 0, 0) = 0x15555564000 close(4) = 0 open("/lib/libm.so.6", O_RDONLY) = 4 mmap(0, 0, PROT_NONE, 0 /* MAP_??? */, 0, 0) = 0x15555566000 munmap(0x15555566000, 8192) = 0 mmap(0, 8192, PROT_NONE, 0 /* MAP_??? */, 0, 0) = 0x15555666000 mprotect(0x1555568e000, 1060372, PROT_NONE) = 0 mmap(0x15555766000, 1060372, PROT_NONE, 0 /* MAP_??? */, 0, 0) = 0x15555766000 close(4) = 0 open("/usr/lib/libc.so.6", O_RDONLY) = -1 ENOENT (No such file or directory) open("/lib/libc.so.6", O_RDONLY) = 4 mmap(0, 0, PROT_NONE, 0 /* MAP_??? */, 0, 0) = 0x15555566000 munmap(0x15555566000, 8192) = 0 mmap(0, 8192, PROT_NONE, 0 /* MAP_??? */, 0, 0) = 0x15555792000 mprotect(0x1555584e000, 1119448, PROT_NONE) = 0 mmap(0x15555892000, 1119448, PROT_NONE, 0 /* MAP_??? */, 0, 0) = 0x15555892000 mmap(0x15555958000, 1119448, PROT_NONE, 0 /* MAP_??? */, 0, 0) = 0x15555958000 close(4) = 0 mprotect(0x15555792000, 770048, PROT_READ|PROT_WRITE) = 0 mprotect(0x15555792000, 770048, PROT_READ|PROT_EXEC) = 0 personality(PER_LINUX) = 0 osf_getsysinfo(0x2d, 0x11ffffae8, 0x11ffffb50, 0, 0x2) = 0 osf_setsysinfo(0xe, 0x11ffffae8, 0x11ffffb50, 0, 0x2) = 0 fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(3, 1), ...}) = 0 mmap(0, 4831834440, 0x1ffffb50, 0 /* MAP_??? */, 2, 0) = 0x15555566000 ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0 write(1, " This is bound version 1.\n", 27) = 27 write(1, " main() source file bound_spt3."..., 63) = 63 write(1, " solute: ``flexible 3mer\'\' 1-"..., 50) = 50 write(1, " solvent: 0 1-mers (diam 1.0000"..., 34) = 34 write(1, " box: length = 10.000000\n", 26) = 26 write(1, " density: 0.200000 (compiled) 0"..., 67) = 67 write(1, "> calculating p2_us(r)\n", 23) = 23 write(1, "> no solute moves\n", 18) = 18 gettimeofday({877199691, 70312}, NULL) = 0 write(1, " seeded RNG with 877199691.\n", 29) = 29 write(1, " building new configuration. . "..., 34) = 34 write(1, "&fluid = 0x1201106e8, fluid = (n"..., 71) = 71 brk(0) = 0x120111ee8 brk(0x120111f28) = 0x120111f28 brk(0x120112000) = 0x120112000 write(1, "&fluid = 0x1201106e8, fluid = 0x"..., 77) = 77 write(1, "> sinit_dummy: no solvent initia"..., 39) = 39 write(1, " checking initial configuration"..., 53) = 53 write(1, " ensemble size (10000): ", 25) = 25 fstat(0, {st_mode=S_IFCHR|0620, st_rdev=makedev(3, 1), ...}) = 0 mmap(0, 4831836056, PROT_READ|0x18, 0 /* MAP_??? */, 2, 0) = 0x15555568000 ioctl(0, TCGETS, {B38400 opost isig icanon echo ...}) = 0 read(0, "\n", 8192) = 1 write(1, " sampling interval (1): ", 25) = 25 read(0, "\n", 8192) = 1 write(1, " solvent max trial move distanc"..., 46) = 46 read(0, "\n", 8192) = 1 write(1, " p2_us(r) binsize (0.025000): ", 31) = 31 read(0, "\n", 8192) = 1 sigaction(SIGINT, {0x120006c28, [], SA_RESTART}, {SIG_DFL}) = 0 write(1, "> catch_signals: program will ca"..., 49) = 49 sigaction(SIGHUP, {0x120006d00, [], SA_RESTART}, {SIG_DFL}) = 0 write(1, "> catch_signals: program will ig"..., 47) = 47 write(1, "&fluid = 0x1201106e8, fluid = 0x"..., 68) = 68 write(1, "fluidp = 0x1201106e8, *fluidp = "..., 78) = 78 write(1, "fluidp = 0x1201106e8, want = 100"..., 58) = 58 --- SIGSEGV (Segmentation fault) --- +++ killed by SIGSEGV +++ -- To unsubscribe: send e-mail to axp-list-request@redhat.com with 'unsubscribe' as the subject. Do not send it to axp-list@redhat.com