The following simple program segfaults for me:
#include <string.h>
int main()
{
void* voidp = NULL;
char zeroes[sizeof(void*)];
memset(zeroes, 0, sizeof(void*));
int res = memcmp(&voidp, zeroes, sizeof(void*));
return 0;
}
The debugger reveals the segfault is in the memcmp.
My machine is running 64-bit Ubuntu 8.04, gcc version is 4.2.4.
It runs fine on other machines that I've tried (both 32-bit or 64-bit).
Any ideas as to what is going on?
UPDATE: Here's the assembly code:
.file "failed.c"
.text
.globl main
.type main, @function
main:
.LFB2:
pushq %rbp
.LCFI0:
movq %rsp, %rbp
.LCFI1:
subq $32, %rsp
.LCFI2:
movq %fs:40, %rax
movq %rax, -8(%rbp)
xorl %eax, %eax
movq $0, -32(%rbp)
leaq -16(%rbp), %rdi
movl $8, %edx
movl $0, %esi
call memset
leaq -16(%rbp), %rsi
leaq -32(%rbp), %rdi
movl $8, %edx
call memcmp
movl %eax, -20(%rbp)
movl $0, %eax
movq -8(%rbp), %rdx
xorq %fs:40, %rdx
je .L3
call __stack_chk_fail
.L3:
leave
ret
.LFE2:
.size main, .-main
.section .eh_frame,"a",@progbits
.Lframe1:
.long .LECIE1-.LSCIE1
.LSCIE1:
.long 0x0
.byte 0x1
.string "zR"
.uleb128 0x1
.sleb128 -8
.byte 0x10
.uleb128 0x1
.byte 0x3
.byte 0xc
.uleb128 0x7
.uleb128 0x8
.byte 0x90
.uleb128 0x1
.align 8
.LECIE1:
.LSFDE1:
.long .LEFDE1-.LASFDE1
.LASFDE1:
.long .LASFDE1-.Lframe1
.long .LFB2
.long .LFE2-.LFB2
.uleb128 0x0
.byte 0x4
.long .LCFI0-.LFB2
.byte 0xe
.uleb128 0x10
.byte 0x86
.uleb128 0x2
.byte 0x4
.long .LCFI1-.LCFI0
.byte 0xd
.uleb128 0x6
.align 8
.LEFDE1:
.ident "GCC: (GNU) 4.2.4 (Ubuntu 4.2.4-1ubuntu4)"
.section .note.GNU-stack,"",@progbits
UPDATE: (This is supposed to be a comment but doesn't fit into 600 characters). When I try sudo apt-get install libc6
, I get:
Reading package lists... Done
Building dependency tree
Reading state information... Done
libc6 is already the newest version.
You might want to run `apt-get -f install' to correct these:
The following packages have unmet dependencies:
libc6: Depends: libc-bin (= 2.11.1-0ubuntu7.5) but it is not installable
Depends: findutils (>= 4.4.0-2ubuntu2) but 4.2.32-1ubuntu2 is to be installed
libc6-dev: Depends: libc6 (= 2.7-10ubuntu5) but 2.11.1-0ubuntu7.5 is to be installed
libc6-i386: Depends: libc6 (= 2.7-10ubuntu5) but 2.11.1-0ubuntu7.5 is to be installed
locales: Depends: belocs-locales-bin (>= 2.4-2.2ubuntu2) but it is not going to be installed
locales-all: Depends: glibc-2.11-1 but it is not installable
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
Finally, if I try to do sudo apt-get -f install
, I get:
Reading package lists... Done
Building dependency tree
Reading state information... Done
libc6 is already the newest version.
You might want to run `apt-get -f install' to correct these:
The following packages have unmet dependencies:
libc6: Depends: libc-bin (= 2.11.1-0ubuntu7.5) but it is not installable
Depends: findutils (>= 4.4.0-2ubuntu2) but 4.2.32-1ubuntu2 is to be installed
libc6-dev: Depends: libc6 (= 2.7-10ubuntu5) but 2.11.1-0ubuntu7.5 is to be installed
libc6-i386: Depends: libc6 (= 2.7-10ubuntu5) but 2.11.1-0ubuntu7.5 is to be installed
locales: Depends: belocs-locales-bin (>= 2.4-2.2ubuntu2) but it is not going to be installed
locales-all: Depends: glibc-2.11-1 but it is not installable
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
botond@adaptive1:~$ sudo apt-get -f install
Reading package lists... Done
Building dependency tree
Reading state information... Done
Correcting dependencies... Done
The following packages were automatically installed and are no longer required:
vim-runtime linux-libc-dev mysql-common openssh-blacklist
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
libdns35
The following packages will be REMOVED:
adduser apparmor apparmor-utils apt apt-file apt-utils aptitude at atop base-files base-passwd bash bash-completion bind9-host binutils bsdmainutils bsdutils bzip2 command-not-found
console-setup console-terminus console-tools coreutils cpio cpp cpp-4.2 cron curl dash debconf debconf-i18n debianutils dhcp3-client dhcp3-common diff dmidecode dmsetup dnsutils dosfstools dpkg
e2fslibs e2fsprogs ed eject ethtool fdutils file findutils friendly-recovery ftp fuse-utils g++ g++-4.2 g++-4.2-multilib gcc gcc-4.2 gcc-4.2-multilib gdb-minimal gettext-base gnupg gpgv grep
groff-base grub gzip hdparm hostname ifupdown info initramfs-tools initscripts inputattach iproute iptables iputils-arping iputils-ping iputils-tracepath klogd laptop-detect less lib32gcc1
lib32gomp1 lib32stdc++6 libacl1 libapr1 libaprutil1 libapt-pkg-perl libatm1 libattr1 libbind9-30 libblkid1 libboost-dev libbz2-1.0 libc6 libc6-dev libc6-dev-i386 libc6-i386 libcap1
libck-connector0 libcomerr2 libconfig-file-perl libconsole libcurl3 libcurl3-gnutls libcwidget3 libdb-dev libdb4.6 libdb4.6++ libdb4.6++-dev libdb4.6-dev libdbd-mysql-perl libdbi-perl
libdbus-1-3 libdevmapper1.02.1 libedit2 libelfg0 libexpat1 libfribidi0 libfuse2 libgc1c2 libgcc1 libgcrypt11 libgdbm3 libglib2.0-0 libgnutls13 libgomp1 libgpg-error0 libgpmg1
libhtml-parser-perl libhtml-tagset-perl libhtml-tree-perl libidn11 libisc35 libisccc30 libisccfg30 libiw29 libkeyutils1 libkrb53 libldap-2.4-2 liblist-moreutils-perl liblocale-gettext-perl
liblwres30 liblzo2-2 libmagic1 libmysqlclient15off libncurses5 libncursesw5 libneon27 libnet-daemon-perl libnewt0.52 libntfs-3g23 libopencdk10 libpam-modules libpam0g libparted1.7-1 libpcap0.8
libpcre3 libplrpc-perl libpopt0 libpq5 libreadline5 libreadline6 librpc-xml-perl libsasl2-2 libsasl2-modules libselinux1 libsepol1 libsigc++-2.0-0c2a libslang2 libsqlite3-0 libss2 libssl0.9.8
libstdc++6 libstdc++6-4.2-dev libsvn1 libsysfs2 libtasn1-3 libterm-readkey-perl libtext-charwidth-perl libtext-iconv-perl libtext-wrapi18n-perl liburi-perl libusb-0.1-4 libuuid1 libvolume-id0
libwrap0 libwww-perl libx11-6 libx11-data libxau6 libxcb-xlib0 libxcb1 libxdmcp6 libxext6 libxml-parser-perl libxml2 libxmuu1 linux-image-2.6.24-23-server linux-image-server linux-server
linux-ubuntu-modules-2.6.24-23-server locales locales-all login logrotate lsb-base lsb-release lshw lsof ltrace lvm2 lzma make makedev man-db mawk menu mii-diag mktemp mlocate module-init-tools
mount mtr-tiny mysql-client-5.0 mysql-server mysql-server-5.0 nano ncurses-base ncurses-bin net-tools netbase netcat netcat-traditional ntfs-3g ntp ntpdate openssh-client openssh-server parted
passwd pciutils pcmciautils perl perl-base perl-modules popularity-contest ppp pppconfig pppoeconf procps psmisc python python-apt python-central python-cxx python-cxx-dev python-dev
python-gdbm python-gnupginterface python-minimal python-mysqldb python-pkg-resources python-pycurl python-setuptools python-simplejson python-support python2.5 python2.5-dev python2.5-minimal
reiserfsprogs rsync screen sed sshfs startup-tasks strace subversion sudo sysklogd system-services sysvutils tar tasksel tasksel-data tcpd tcpdump telnet time tzdata ubuntu-keyring
ubuntu-minimal ubuntu-standard ucf udev ufw update-inetd update-manager-core upstart upstart-compat-sysv upstart-logd usbutils util-linux util-linux-locales uuid-runtime valgrind vim vim-common
vim-tiny w3m wget whiptail wireless-tools wpasupplicant x11-common xauth zlib1g
The following packages will be upgraded:
libdns35
WARNING: The following essential packages will be removed.
This should NOT be done unless you know exactly what you are doing!
apt libc6 (due to apt) libgcc1 (due to apt) libstdc++6 (due to apt) base-files base-passwd (due to base-files) libpam-modules (due to base-files) bash debianutils (due to bash) libncurses5 (due
to bash) bsdutils coreutils libacl1 (due to coreutils) libselinux1 (due to coreutils) dash mktemp (due to debianutils) diff dpkg lzma (due to dpkg) e2fsprogs e2fslibs (due to e2fsprogs)
libblkid1 (due to e2fsprogs) libcomerr2 (due to e2fsprogs) libss2 (due to e2fsprogs) libuuid1 (due to e2fsprogs) findutils grep gzip hostname login libpam0g (due to login) mount ncurses-base
ncurses-bin perl-base python-minimal python2.5-minimal (due to python-minimal) sed sysvutils libsepol1 (due to sysvutils) tar util-linux lsb-base (due to util-linux) tzdata (due to util-linux)
libslang2 (due to util-linux) zlib1g (due to util-linux)
1 upgraded, 0 newly installed, 316 to remove and 4 not upgraded.
4 not fully installed or removed.
Need to get 11.3kB of archives.
After this operation, 712MB disk space will be freed.
You are about to do something potentially harmful.
To continue type in the phrase 'Yes, do as I say!'
?]
Obviously, I didn't type "Yes, do as I say!". It seems something is really screwed up... any idea how to fix it?
Overview. A segmentation fault (aka segfault) is a common condition that causes programs to crash; they are often associated with a file named core . Segfaults are caused by a program trying to read or write an illegal memory location.
The following are some typical causes of a segmentation fault: Attempting to access a nonexistent memory address (outside process's address space) Attempting to access memory the program does not have rights to (such as kernel structures in process context) Attempting to write read-only memory (such as code segment)
I can affirm that on RHEL 5 with GCC 4.1.2, the code works fine, both 32-bit and 64-bit (AMD x86/64 architecture).
You've (probably) found a bug.
Maybe you should upgrade to GCC 4.5.2 (released in the last 24 hours or so - as of 2010-12-19). Or maybe you need to upgrade the C library, but this is more likely to be a compiler issue than a library issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With