segv at strcmp
Hi -
The gcc version is gcc-3.2-29 and the flags we pass
to gcc are : -O -ansi -fwritable-strings -fsigned-char -D_GNU_SOURCE -D_REENTRANT
thanks,
Umut
Quoted 31 lines · Tony
Quoted 1 line
We have hit a problem with
strcmp()on UnitedLinux1.0
Quoted 2 lines
with kernel: 2.4.19 and glibc: 2.2.5.
I am wondering if anybody has seen something likeit
Quoted 1 line
or knows if already there is a patch. I do not
have a
Quoted 4 lines
small repro but i will try to explain:
The segv happens at
memcmp()at a line like:
'strcmp(x, "this is 24 chars long")' where x is achar
Quoted 2 lines
pointer with a length of 7 and the constant has a length of 24. x is located at the first 8 bytes of
the
Quoted 1 line
last 16 bytes at the end of a non-contiguous
shared
Quoted 1 line
memory segment.
memcmp()segvs when it tries toload 8
Quoted 1 line
bytes from the "r19" register which initially has
the
Quoted 1 line
address of x and points to the end of the segment
when
Quoted 1 line
the segv happens.
Sounds like your compiler converted the
strcmp(str, const_str)
intomemcmp(str, const_str, strlen(const_str))... and then
the memcmp fell off the end of the page.
Quoted 5 lines
What version of gcc are you using, and what
arguments are you
passing to gcc?-Tony
__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/
Quoted 4 lines
Hi -
The gcc version is gcc-3.2-29 and the flags we pass
to gcc are : -O -ansi -fwritable-strings -fsigned-char -D_GNU_SOURCE -D_REENTRANT
I have gcc 3.2.3 ... but it appears to not do anything strange to my test program with those options.
main()
{
char *x = "7chars!";
strcmp(x, "This is 24 chars long!!!");
}with those options. Do you have any include files that might be redefining strcmp as memcmp?
-Tony
Probably the location of "x" is not at the end of the shared memory segment. In /usr/include/bits/string2.h strcmp is defined as memcmp which must be getting picked up.
thanks,
Umut
Quoted 18 lines · Tony
Quoted 2 lines
Hi -
The gcc version is gcc-3.2-29 and the flags wepass
Quoted 1 line
to gcc are : -O -ansi -fwritable-strings
-fsigned-char
Quoted 1 line
-D_GNU_SOURCE -D_REENTRANT
I have gcc 3.2.3 ... but it appears to not do
anything
strange to my test program with those options.
main(){ char *x = "7chars!"; strcmp(x, "This is 24 chars long!!!"); }
Quoted 5 lines
with those options. Do you have any include files that
might be redefining strcmp as memcmp?-Tony
__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/
The <bits/string2.h> on my machine (came from the glibc-devel-2.3.2-63 package in RedHat EL3). Looks like it will only convert a strcmp to a memcmp if *both* the arguments are constant:
# define strcmp(s1, s2) \
__extension__ \
({ size_t __s1_len, __s2_len; \
(__builtin_constant_p (s1) && __builtin_constant_p (s2) \
&& (__s1_len = strlen (s1), __s2_len = strlen (s2), \
(!__string2_1bptr_p (s1) || __s1_len >= 4) \
&& (!__string2_1bptr_p (s2) || __s2_len >= 4)) \
? memcmp ((__const char *) (s1), (__const char *) (s2), \
(__s1_len < __s2_len ? __s1_len : __s2_len) + 1) \
: (__builtin_constant_p (s1) && __string2_1bptr_p (s1) \
&& (__s1_len = strlen (s1), __s1_len < 4) \
? (__builtin_constant_p (s2) && __string2_1bptr_p (s2) \
? __strcmp_cc (s1, s2, __s1_len) \
: __strcmp_cg (s1, s2, __s1_len)) \
: (__builtin_constant_p (s2) && __string2_1bptr_p (s2) \
&& (__s2_len = strlen (s2), __s2_len < 4) \
? (__builtin_constant_p (s1) && __string2_1bptr_p (s1) \
? __strcmp_cc (s1, s2, __s2_len) \
: __strcmp_gc (s1, s2, __s2_len)) \
: strcmp (s1, s2)))); })(I say "looks like" because I'm getting dizzy trying to match parentheses in that expression). Clearly your test case managed to convert to memcmp when the first argument was not a constant string ... which caused you grief.
-Tony
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line · Tony
Hi - The gcc version is gcc-3.2-29 and the flags we pass to gcc are : -O -ansi -fwritable-strings -fsigned-char -D_GNU_SOURCE -D_REENTRANT
Tony> I have gcc 3.2.3 ... but it appears to not do anything strange Tony> to my test program with those options.
Tony> main() { char *x = "7chars!"; Tony> strcmp(x, "This is 24 chars long!!!"); }
Tony> with those options. Do you have any include files that might Tony> be redefining strcmp as memcmp?
In earlier versions of libc, there were some mem/str-related routines which were too aggressive in prefetching. Perhaps Jes remembers the details, but I'd definitely recommend to make sure you're running the latest libc for your distro.
--david
Ok - I have a small repro that segvs.
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <stdlib.h>
#define SHMBASE 0x200000000
#define AL (1024*1024)
#define MAXSEGMENTS 1
#ifndef SHM_R
#define SHM_R 0400
#endif
#ifndef SHM_W
#define SHM_W 0660
#endif
#define SHM_MODE ( SHM_R | SHM_W | IPC_CREAT )
#define KEY2SUCKS 0x52435200
unsigned long sizes[MAXSEGMENTS] ={720896};
main()
{
int shmid[MAXSEGMENTS];
char *shmptr[MAXSEGMENTS];
long addr,mykey,loop,addr_save,attempts=0;
int i;
char *name;
addr = SHMBASE;
mykey = KEY2SUCKS;
attempts = 0;for( loop=0;loop<MAXSEGMENTS;loop++ )
{
shmid[loop]=shmget((key_t)mykey,sizes[loop],SHM_MODE);
shmptr[loop]=(char
*)shmat(shmid[loop],(void*)addr,0);
/* ALign the size on SHMLBA(16K) */
sizes[loop]=(sizes[loop] + SHMLBA - 1) & ~(SHMLBA -1); addr=shmptr[attempts]+(unsigned long)sizes[loop]; addr_save = addr;
printf("addr1 = %p\n",addr);
/* aLign the Address on 1MB */
addr=(char *)(((unsigned long)addr + AL-1) & ~(AL-1));
printf("addr2 = %p\n",addr);
attempts++;
mykey++;
}
name = ((unsigned long)addr_save - 16);
printf("%p\n",name);
strcpy(name, "sqlexec");
ret = strcmp(name,"aaaaaaaaaaaaaaaaaaaaaaaa");
for( loop=0;loop<attempts;loop++ )
shmctl( shmid[loop],IPC_RMID,0 );
printf( "\n\tRemoved All Segments ... \n\n");
exit( 0 );
}
%gcc -O -o x x.c
%./x
__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/
Didn't compile ... "ret" undefined ... so I fixed that, but then it SEGV'd on the "strcpy" ... it didn't make it to the strcmp().
-Tony
Quoted 93 lines
-----Original Message-----
From: umut aymakoglu [mailto:[email protected]]
Sent: Monday, January 05, 2004 4:04 PM
To: Luck, Tony; [email protected]
Cc: [email protected]
Subject: RE: segv at strcmpOk - I have a small repro that segvs.
#include <
stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <stdlib.h>#define SHMBASE
0x200000000
#define AL (1024*1024)
#define MAXSEGMENTS 1#ifndef
SHM_R
#defineSHM_R0400
#endif
#ifndefSHM_W
#defineSHM_W0660
#endif#define
SHM_MODE(SHM_R|SHM_W|IPC_CREAT)
#define KEY2SUCKS0x52435200unsigned long sizes[MAXSEGMENTS] ={720896};
main()
{int shmid[MAXSEGMENTS]; char *shmptr[MAXSEGMENTS]; long addr,mykey,loop,addr_save,attempts=0; int i; char *name; addr = SHMBASE; mykey = KEY2SUCKS; attempts = 0;for( loop=0;loop<MAXSEGMENTS;loop++ )
{shmid[loop]=shmget((key_t)mykey,sizes[loop],SHM_MODE);
shmptr[loop]=(char
*)shmat(shmid[loop],(void*)addr,0);/* ALign the size on SHMLBA(16K) */
sizes[loop]=(sizes[loop] + SHMLBA - 1) & ~(SHMLBA -1); addr=shmptr[attempts]+(unsigned long)sizes[loop];addr_save = addr; printf("addr1 = %p\n",addr);/* aLign the Address on 1MB */
addr=(char *)(((unsigned long)addr + AL-1) & ~(AL-1)); printf("addr2 = %p\n",addr); attempts++; mykey++; }name = ((unsigned long)addr_save - 16);
printf("%p\n",name);
strcpy(name, "sqlexec");
ret =strcmp(name,"aaaaaaaaaaaaaaaaaaaaaaaa");for( loop=0;loop<attempts;loop++ )
shmctl( shmid[loop],IPC_RMID,0 );printf( "\n\tRemoved All Segments ... \n\n");
exit( 0 );
}%gcc -O -o x
x.c
%./x__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/
probably either shmget or shmat failed. There should not be any shmget or shmat error.
i attached a working one.
thanks,
Umut
Quoted 6 lines · Tony
Didn't compile ... "ret" undefined ... so I fixed that, but
then it SEGV'd on the "strcpy" ... it didn't make it to thestrcmp().
Quoted 57 lines
-Tony
Quoted 54 lines
-----Original Message-----
From: umut aymakoglu [mailto:[email protected]]
Sent: Monday, January 05, 2004 4:04 PM
To: Luck, Tony; [email protected]
Cc: [email protected]
Subject: RE: segv at strcmpOk - I have a small repro that segvs.
#include <
stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <stdlib.h>#define SHMBASE
0x200000000
#define AL (1024*1024)
#define MAXSEGMENTS 1#ifndef
SHM_R
#defineSHM_R0400
#endif
#ifndefSHM_W
#defineSHM_W0660
#endif#define
SHM_MODE(SHM_R|SHM_W|IPC_CREAT)
#define KEY2SUCKS0x52435200unsigned long sizes[MAXSEGMENTS] ={720896};
main()
{int shmid[MAXSEGMENTS]; char *shmptr[MAXSEGMENTS]; long addr,mykey,loop,addr_save,attempts=0; int i; char *name; addr = SHMBASE; mykey = KEY2SUCKS; attempts = 0;for( loop=0;loop<MAXSEGMENTS;loop++ )
{
shmid[loop]=shmget((key_t)mykey,sizes[loop],SHM_MODE);
Quoted 5 lines
Quoted 5 lines
shmptr[loop]=(char
*)shmat(shmid[loop],(void*)addr,0);/* ALign the size on SHMLBA(16K) */
sizes[loop]=(sizes[loop] + SHMLBA - 1) & ~(SHMLBA
Quoted 36 lines
-1);
Quoted 6 lines
addr=shmptr[attempts]+(unsigned long)sizes[loop];
addr_save = addr; printf("addr1 = %p\n",addr);/* aLign the Address on 1MB */
addr=(char *)(((unsigned long)addr + AL-1) &~(AL-1));
Quoted 28 lines
printf("addr2 = %p\n",addr); attempts++; mykey++; }name = ((unsigned long)addr_save - 16);
printf("%p\n",name);
strcpy(name, "sqlexec");
ret =strcmp(name,"aaaaaaaaaaaaaaaaaaaaaaaa");for( loop=0;loop<attempts;loop++ )
shmctl( shmid[loop],IPC_RMID,0 );printf( "\n\tRemoved All Segments ... \n\n");
exit( 0 );
}%gcc -O -o x
x.c
%./x__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/
__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <stdlib.h>
#define SHMBASE 0x200000000
#define AL (1024*1024)
#define MAXSEGMENTS 1
#ifndef SHM_R
#define SHM_R 0400
#endif
#ifndef SHM_W
#define SHM_W 0660
#endif
#define SHM_MODE ( SHM_R | SHM_W | IPC_CREAT ) /* Mode Read Write */ #define KEY2SUCKS 0x52435200 /* Shared Mem Key */
unsigned long sizes[MAXSEGMENTS] ={720896};
main()
{
int shmid[MAXSEGMENTS];
char *shmptr[MAXSEGMENTS];
long addr,mykey,loop,addr_save,attempts=0;
int i,ret;
char *name;
addr = SHMBASE;
mykey = KEY2SUCKS;
attempts = 0; for( loop=0;loop<MAXSEGMENTS;loop++ )
{ if ((shmid[loop]= shmget( (key_t)mykey,sizes[loop],SHM_MODE)) == -1)
{
printf("shmget error\n");
goto bad;
}
if((shmptr[loop]=(char *)shmat(shmid[loop],(void*)addr,0 )) == (void*)-1)
{
printf("shmat error\n");
goto bad;
} /* ALign the size on SHMLBA(16K) */
sizes[loop] = (sizes[loop] + SHMLBA - 1) & ~(SHMLBA - 1);
addr = shmptr[attempts] + (unsigned long)sizes[loop];
addr_save = addr;
printf("addr1 = %p\n",addr); /* aLign the Address on 1MB */
addr = (char *)(((unsigned long)addr + AL-1) & ~(AL-1)); printf("addr2 = %p\n",addr);
attempts++;
mykey++;
} /* end for loop */
name = ((unsigned long)addr_save - 16);
printf("%p\n",name);
strcpy(name, "sqlexec");
ret = strcmp(name,"aaaaaaaaaaaaaaaaaaaaaaaa");bad:
for( loop=0;loop<attempts;loop++ )
shmctl( shmid[loop],IPC_RMID,0 );
printf( "\n\tRemoved All Segments ... \n\n");
exit( 0 );}
The size rounding is wrong:
sizes[loop] = (sizes[loop] + SHMLBA - 1) & ~(SHMLBA - 1);
will make addr falls outside the shared memory segment, which is the reason why segfault on strcpy. Once that is fixed, it doesn't segfault on strcmp either.
- Ken
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of umut aymakoglu
Sent: Monday, January 05, 2004 5:38 PM
To: Luck, Tony; [email protected]
Cc: [email protected]
Subject: RE: segv at strcmp
probably either shmget or shmat failed. There should not be any shmget or shmat error.
i attached a working one.
thanks,
Umut
Quoted 6 lines · Tony
Didn't compile ... "ret" undefined ... so I fixed that, but
then it SEGV'd on the "strcpy" ... it didn't make it to thestrcmp().
Quoted 57 lines
-Tony
Quoted 54 lines
-----Original Message-----
From: umut aymakoglu [mailto:[email protected]]
Sent: Monday, January 05, 2004 4:04 PM
To: Luck, Tony; [email protected]
Cc: [email protected]
Subject: RE: segv at strcmpOk - I have a small repro that segvs.
#include <
stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <stdlib.h>#define SHMBASE
0x200000000
#define AL (1024*1024)
#define MAXSEGMENTS 1#ifndef
SHM_R
#defineSHM_R0400
#endif
#ifndefSHM_W
#defineSHM_W0660
#endif#define
SHM_MODE(SHM_R|SHM_W|IPC_CREAT)
#define KEY2SUCKS0x52435200unsigned long sizes[MAXSEGMENTS] ={720896};
main()
{int shmid[MAXSEGMENTS]; char *shmptr[MAXSEGMENTS]; long addr,mykey,loop,addr_save,attempts=0; int i; char *name; addr = SHMBASE; mykey = KEY2SUCKS; attempts = 0;for( loop=0;loop<MAXSEGMENTS;loop++ )
{
shmid[loop]=shmget((key_t)mykey,sizes[loop],SHM_MODE);
Quoted 5 lines
Quoted 5 lines
shmptr[loop]=(char
*)shmat(shmid[loop],(void*)addr,0);/* ALign the size on SHMLBA(16K) */
sizes[loop]=(sizes[loop] + SHMLBA - 1) & ~(SHMLBA
Quoted 36 lines
-1);
Quoted 6 lines
addr=shmptr[attempts]+(unsigned long)sizes[loop];
addr_save = addr; printf("addr1 = %p\n",addr);/* aLign the Address on 1MB */
addr=(char *)(((unsigned long)addr + AL-1) &~(AL-1));
Quoted 28 lines
printf("addr2 = %p\n",addr); attempts++; mykey++; }name = ((unsigned long)addr_save - 16);
printf("%p\n",name);
strcpy(name, "sqlexec");
ret =strcmp(name,"aaaaaaaaaaaaaaaaaaaaaaaa");for( loop=0;loop<attempts;loop++ )
shmctl( shmid[loop],IPC_RMID,0 );printf( "\n\tRemoved All Segments ... \n\n");
exit( 0 );
}%gcc -O -o x
x.c
%./x__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/
__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/
The problem seems to be the gcc 3.2-29 we have on United Linux 1.0. The binary built with this does not also work on RedHat. The binary built with gcc 3.2.3-20 on Redhat works fine.
The problem with the segv at strcpy() was due to the unaligned size. I have attached the corrected program.
Does anybody know where i can find the latest fixpack on United Linux 1.0?
thanks,
Umut
--- "Chen, Kenneth W" <[email protected]>
Quoted 76 lines · wrote:
The size rounding is wrong:
sizes[loop] = (sizes[loop] + SHMLBA - 1) & ~(SHMLBA
- 1);will make addr falls outside the shared memory segment, which is the
reason why segfault on strcpy. Once that is fixed, it doesn't segfault
on strcmp either.- Ken
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of umut aymakoglu
Sent: Monday, January 05, 2004 5:38 PM
To: Luck, Tony; [email protected]
Cc: [email protected]
Subject: RE: segv at strcmpprobably either shmget or shmat failed. There should not be any shmget or shmat error.
i attached a working one.
thanks,
UmutQuoted 3 lines · Tony
Didn't compile ... "ret" undefined ... so I fixed
that, but
then it SEGV'd on the "strcpy" ... it didn't makeit
Quoted 7 lines
to the
strcmp().-Tony
Quoted 2 lines
-----Original Message-----
From: umut aymakoglu[mailto:[email protected]]
Quoted 29 lines
Quoted 29 lines
Sent: Monday, January 05, 2004 4:04 PM
To: Luck, Tony; [email protected]
Cc: [email protected]
Subject: RE: segv at strcmpOk - I have a small repro that segvs.
#include <
stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <stdlib.h>#define SHMBASE
0x200000000
#define AL (1024*1024)
#define MAXSEGMENTS 1#ifndef
SHM_R
#defineSHM_R0400
#endif
#ifndefSHM_W
#defineSHM_W0660
#endif
Quoted 26 lines
Quoted 1 line
Quoted 1 line
#define
SHM_MODE(SHM_R|SHM_W|IPC_CREAT)
Quoted 23 lines
Quoted 22 lines
#define KEY2SUCKS
0x52435200unsigned long sizes[MAXSEGMENTS] ={720896};
main()
{int shmid[MAXSEGMENTS]; char *shmptr[MAXSEGMENTS]; long addr,mykey,loop,addr_save,attempts=0; int i; char *name; addr = SHMBASE; mykey = KEY2SUCKS; attempts = 0;for( loop=0;loop<MAXSEGMENTS;loop++ )
{
shmid[loop]=shmget((key_t)mykey,sizes[loop],SHM_MODE);
Quoted 42 lines
Quoted 5 lines
Quoted 5 lines
shmptr[loop]=(char
*)shmat(shmid[loop],(void*)addr,0);/* ALign the size on SHMLBA(16K) */
sizes[loop]=(sizes[loop] + SHMLBA - 1) &~(SHMLBA
Quoted 2 lines
-1);
Quoted 1 line
addr=shmptr[attempts]+(unsigned
long)sizes[loop];
Quoted 32 lines
Quoted 5 lines
addr_save = addr; printf("addr1 = %p\n",addr);/* aLign the Address on 1MB */
addr=(char *)(((unsigned long)addr + AL-1) &~(AL-1));
Quoted 26 lines
printf("addr2 = %p\n",addr); attempts++; mykey++; }name = ((unsigned long)addr_save - 16);
printf("%p\n",name);
strcpy(name, "sqlexec");
ret =strcmp(name,"aaaaaaaaaaaaaaaaaaaaaaaa");for( loop=0;loop<attempts;loop++ )
shmctl( shmid[loop],IPC_RMID,0 );printf( "\n\tRemoved All Segments ... \n\n");
exit( 0 );
}%gcc -O -o x
x.c
%./x__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading andsharing.
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/
__________________________________
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <stdlib.h>
#define SHMBASE 0x200000000
#define AL (1024*1024)
#define MAXSEGMENTS 1
#ifndef SHM_R
#define SHM_R 0400
#endif
#ifndef SHM_W
#define SHM_W 0660
#endif
#define SHM_MODE ( SHM_R | SHM_W | IPC_CREAT ) /* Mode Read Write */ #define KEY2SUCKS 0x52435200 /* Shared Mem Key */
unsigned long sizes[MAXSEGMENTS] ={1024*1024};
main()
{
int shmid[MAXSEGMENTS];
char *shmptr[MAXSEGMENTS];
long addr,mykey,loop,addr_save,attempts=0;
int i,ret;
char *name;
addr = SHMBASE;
mykey = KEY2SUCKS;
attempts = 0; for( loop=0;loop<MAXSEGMENTS;loop++ )
{
printf("size before= %d\n",sizes[loop]); if ((shmid[loop]= shmget( (key_t)mykey,sizes[loop],SHM_MODE)) == -1)
{
printf("shmget error\n");
goto bad;
}
if((shmptr[loop]=(char *)shmat(shmid[loop],(void*)addr,0 )) == (void*)-1)
{
printf("shmat error\n");
goto bad;
} sizes[loop] = (sizes[loop] + AL - 1) & ~(AL - 1);
printf("size before= %d\n",sizes[loop]); addr = shmptr[attempts] + (unsigned long)sizes[loop];
printf("addr1 = %p\n",addr); attempts++;
mykey++;
/* aLign the Address on 1MB */
addr = (char *)(((unsigned long)addr + AL-1) & ~(AL-1)); printf("addr2 = %p\n",addr);
} /* end for loop */
name = ((unsigned long)addr - 16);
printf("%p\n",name);
strcpy(name, "sqlexec");
ret = strcmp(name,"aaaaaaaaaaaaaaaaaaaaaaaa");bad:
for( loop=0;loop<attempts;loop++ )
shmctl( shmid[loop],IPC_RMID,0 );
printf( "\n\tRemoved All Segments ... \n\n");
exit( 0 );}