LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-27-2012, 08:43 AM   #1
lwoh
LQ Newbie
 
Registered: Feb 2012
Posts: 4

Rep: Reputation: Disabled
Segmentation Fault [in __strncpy_ssse3 () from /lib64/libc.so.6 on gcc]


Hi,

I did my porting from UNIX to LINUX and was able to compile successfuly without many critical warnings.

However, when i tried to execute the binary, it did not run properly. When i executed the binary with GDB, it showed error as such:

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 139641094334208 (LWP 30988)]
0x0000003be2130e3c in __strncpy_ssse3 () from /lib64/libc.so.6
(gdb) WHERE
#0 0x0000003be2130e3c in __strncpy_ssse3 () from /lib64/libc.so.6
#1 0x0000000000468833 in ?? ()
#2 0x0000000000000023 in ?? ()

It was compiled with gcc (GCC) 4.4.5 on Red Hat Enterprise Linux Server release 6.1. The same specs applied for the run environment. Has anyone encountered this before? Any advice on how to solve this?
 
Old 10-27-2012, 09:11 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by lwoh View Post
I did my porting from UNIX to LINUX
Was that also a port from 32 bit to 64 bit?

You are implying the code ran, without symptoms of this bug, on the Unix system. Correct me if I'm misunderstanding that. Assuming it ran correctly, was that a 32 bit system?

Quote:
was able to compile successfuly without many critical warnings.
It is possible those warnings include an indication of the bug in your program.

Quote:
When i executed the binary with GDB, it showed error as such:
Did you compile with debug info, so that GDB would show you the name of the calling function if it understood the call stack?

This will be a little harder to debug since you can't see the name of the calling function. There are three likely possibilities for why you can't see the name of the calling function:

1) You didn't compile with debug info.
2) __strncpy_ssse3 has a strange stack frame that confuses GDB (I think that one is most likely)
3) The bug in your program caused __strncpy_ssse3 to corrupt its return information.

Quote:
It was compiled with gcc (GCC) 4.4.5 on Red Hat Enterprise Linux Server release 6.1. The same specs applied for the run environment. Has anyone encountered this before? Any advice on how to solve this?
You seem to be assuming the problem might be in GCC or in Red Hat or in the libc code where the symptom occurred, so that someone else might have seen the same problem before.

That is so unlikely it is not worth considering. The problem is a bug in the program you ported. The fact that it ran on some other system does not mean the bug wasn't there. It could mean the bug had no symptom. If the other system was 32 bit, it could mean the bug was a non portable operation that fails in 64-bit but works in 32-bit.

If you have the right 32-bit development package(s) installed, you can build the program to run as 32-bit even though the Red Hat is 64 bit. Any 64 bit vs. 32 bit portability bug in your program would depend only on whether the program is built 32 bit, not on whether the OS is 32 bit.

Add the option -m32 to your gcc commands and see whether that:
A) Fails to build, indicating you need to install some 32-bit packages.
B) Makes the program run symptom free, which strongly hints but does not prove that the bug was a 32 bit portability issue.
C) Changes the symptom, possibly to one easier to diagnose, which is likely for most other kinds of program bugs.
D) Gives essentially the same symptom (which is very unlikely) so the effort was wasted and you must try something else.

BTW, assuming this program has a similar memory map to the one you posted in an earlier thread:
http://www.linuxquestions.org/questi...8/#post4668395
We can estimate that 0x0000000000468833 is the correct address for the caller of strncpy (so GDB wasn't confused by the strncpy stack frame) making it very likely that this caller of strncpy corrupted its own stack frame by this call to strncpy.

If that is correct, we can deduce that the destination of the strncpy was a local variable of the function calling strncpy and that the size was too large for the allocation of that variable (so you didn't use the n correctly to limit the strncpy).

So one way to find the bug is to find all the strncpy calls in your code and look closely at any whose destinations are local variables and see whether the n value might be too high for the allocation of that destination.

Last edited by johnsfine; 10-27-2012 at 09:30 AM.
 
Old 10-29-2012, 11:38 PM   #3
gothrog
Member
 
Registered: Jun 2004
Distribution: Yellow Dog, Fedora, RedHat, Centos, Ubuntu, Suse Linux
Posts: 106

Rep: Reputation: 15
I usually use "up" to go into the stack trace. I haven't used "where" before, however on a seg fault you should be able to go up the stack to at least step through the code to see what line of code it crashed on.

If you can't then maybe you don't have your debug flags enabled.

Or you are having a stack crash that is showing up as segfault, only to show you a corrupt stack.

@johnsfine I just read what you wrote. Cools beans. Can you explain a little more about how to read the memory address to determine the nature of the crash...???
 
Old 10-30-2012, 06:35 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by gothrog View Post
Can you explain a little more about how to read the memory address to determine the nature of the crash...???
Quote:
Originally Posted by lwoh View Post
#0 0x0000003be2130e3c in __strncpy_ssse3 () from /lib64/libc.so.6
#1 0x0000000000468833 in ?? ()
#2 0x0000000000000023 in ?? ()
I was just trying to estimate whether the 0x468833 was the actual return address of the function where the seg fault occurred. Clearly the #2 line is worthless garbage. But #1 is less obvious.

In the other thread, I saw

Quote:
Originally Posted by lwoh View Post
======= Memory map: ========
00400000-0051d000 r-xp 00000000 fd:02 131081 /home/s
audit/SAM360/sam/bin/samupd.apis
0071c000-00744000 rw-p 0011c000 fd:02 131081 /home/s
audit/SAM360/sam/bin/samupd.apis
So the range 0x400000-0x51d000 contains the executable code of the top level module of that process. That is a rather narrow range (within 64 bit addressing) that contains the 0x468833 that might be a return address in the current thread. So it seems very likely that is a return address.

So the lack of a function name on line #1 indicates lack of deg info, rather than a misunderstood or corrupt stack. But the garbage return address on #2 indicates a misunderstood or corrupt stack. At that level, I was estimating that corrupt is more likely than misunderstood. So I was estimating that function corrupted its own stack frame.

It is all guess work with this little info.

If I were debugging it myself, I would look at the asm instructions around the point of failure and look at the register values at the point of failure, as well as the stack. Then I could know for sure whether/what part of the stack was corrupted and probably know why.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SIGSEGV segmentation fault at strftime_l lib64/libc.so.6 smilebhuff Linux - Kernel 8 05-15-2012 09:24 AM
vsnprintf crashes (Segmentation fault) from /lib64/tls/libc.so.6 Ron_09 Linux - Enterprise 0 09-15-2010 02:34 AM
[SOLVED] SEGMENTATION FAULT using gcc 4.4.4 -O2 , works with gcc 4.1.0 -O2 or gcc 4.4.4 -O1 amir1981 Programming 36 07-26-2010 06:07 PM
compiling in gcc goes well, gives segmentation fault jshine Programming 6 12-19-2004 01:08 AM
gcc / segmentation fault skeletal29 Linux - Software 0 05-05-2002 04:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration