Debugging stack corruption detected: aborted

5,269 views
Skip to first unread message

pps

unread,
Jan 4, 2013, 11:06:28 PM1/4/13
to andro...@googlegroups.com
I have a library (a voice codec) that produces that stack corruption error on android. I'm having hard time figuring out value of that stack protection BS if it gives me no clue how to debug it. So, I'd like to ask for possible techniques that I could use to identify and fix the issue.
Perhaps, I could go to functions that allocate lots of stuff on stack and add padding to structures and then re-run it again to see if stack corruption error disappears, but this is not a very practical approach, since the library is quite big. Another possible approach: manually trigger stack checks after each function and abort/assert asap from inside my app and generate usable backrace to see where it aborted. Right now I get totally dumb useless crash with extremely valuable message:

F//system/bin/app_process(19796): stack corruption detected: aborted

There is lots of info on the web, but everything is useless and doesn't even mention what should be done for detection of the code that corrupted the stack.
So, my questions is: what should be done to actually fix stack corruption? I use latest r8d NDK and I don't have any good debugging tools for android (and I obviously not going waste a day of my life trying to build valgrind for android), my main dev platformt is windows/VS2008.... so, please advise how do u fix that kind of problems in the stone age of the NDK development ;)

thanks.

pps

unread,
Jan 4, 2013, 11:10:01 PM1/4/13
to andro...@googlegroups.com
Just to add more info. If I build the codec library with -fstack-protector-strong or -fstack-protector-all it crashes the app that uses the library. If I build it with -fno-stack-protector then it runs fine (even thought the main app is compiled with stack protection).

a1

unread,
Jan 5, 2013, 4:21:29 AM1/5/13
to andro...@googlegroups.com

There is lots of info on the web, but everything is useless and doesn't even mention what should be done for detection of the code that corrupted the stack.
So, my questions is: what should be done to actually fix stack corruption? I use latest r8d NDK and I don't have any good debugging tools for android (and I obviously not going waste a day of my life trying to build valgrind for android), my main dev platformt is windows/VS2008.... so, please advise how do u fix that kind of problems in the stone age of the NDK development ;)
 
Now that's really smart, why "waste" a day with valgrind (which is rather a few hours) which not only will allow to locate this kind of bug in no time but will also help you debug other problems. Anyway if you decide to "waste" a day I can provide a few pointers since I recently setup valgrind for android (and I also mainly develop in vs). 
First of all you need linux box, personally I've used virtualbox to install ubuntu, current valgrind version already has support for android so there is no hacking required. I used virtualized x86 system image as test machine (so I build x86 version of valgrind).

Now a bit of trickery was required to run this setup, there is a readme in valgrind (http://valgrind.org/docs/manual/dist.readme-android.html) which cover building and installing. To run application with valgrind simply call following: adb shell setprop wrap.your.package.name "logwrapper /data/local/Inst/bin/valgrind" (you may add additional parameters there if you like).
Few issues wort mentioning: first of all make sure that valgrind has +x attribute, also there are several tutorials on web that suggest using shell script to wrap application process - I've never were able to setup that that's why I use valgrind directly in setprop, last thing: remember to kill your application process after setting wrap.* property.

Getting exact location of detected error is also a bit tricky, since binaries in APK are stripped valgrind will report only symbols (function names) and addresses, no line numbers, you can of course use addr2line but you need base address of your .so extracted from /proc/<pid>/smaps. 
BTW if someone know how to disable stripping in ndk-build (if possible) that'd great, I tried to do so but ndk-build system is far too complicated for my lowly make skills.

--
Bart

pps

unread,
Jan 5, 2013, 6:15:02 PM1/5/13
to andro...@googlegroups.com
I agree, valgrind is very useful and I was thinking to get it working with andrdoid, but the page that describes how to get it running with android is far too long to to even read it (from one of mozilla/webkit developers who started valgrind porting to android). It looked like I need special build of android for valgrind, that is a big turn off for me. I have like 100 phones to run my builds on and I need tools that work well everywhere. I really miss debugging with VS on windows mobile phones, all that linux/gcc/gdb seems too far inferior to MS tools even though I'm full time on android NDK for the last couple of years. Basically, our android development/bugfixing totally relies on windows port and development tools that are available for windows, but obviously some parts of code simply cannot be tested/debugged on windows port.
So, you say that you used valgrind on android. Is it a standalone valgrind apk/executable that can be installed/copied to an android-arm phone and using some scripts I can get it running? I don't need to dance 10 hour rituals to get it working. The thing is that so far I use visual studio and windows port of our stuff for testing and debugging (thanks microsoft for good tools :), and I only need android specific tools quite rarely, that's why I I'm saying that I don't want to waste 10 hours just to get it barely working. At the same time I agree, it's probably the only tool that I think is better than similar tools available on windows.

to "disable" stripping in ndk, you can manually copy unstripped libs from obj\local\armeabi to libs. I use stripped libs, but then I run addr2line against unstripped ones to get backtraces.

Ray Donnelly

unread,
Jan 5, 2013, 7:39:21 PM1/5/13
to andro...@googlegroups.com
I've got some interest in valgrind for Android but haven't looked into it much.

This page might prove helpful. They don't build a custom ROM but still
get some value from valgrind:

https://staktrace.com/spout/entry.php?id=762

Ray.
> --
> You received this message because you are subscribed to the Google Groups
> "android-ndk" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/android-ndk/-/B9XDQE9y_D4J.
>
> To post to this group, send email to andro...@googlegroups.com.
> To unsubscribe from this group, send email to
> android-ndk...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/android-ndk?hl=en.

a1

unread,
Jan 6, 2013, 4:32:11 AM1/6/13
to andro...@googlegroups.com

I agree, valgrind is very useful and I was thinking to get it working with andrdoid, but the page that describes how to get it running with android is far too long to to even read it (from one of mozilla/webkit developers who started valgrind porting to android).
That's why I linked to detailed build instruction for valgrind for android, out of box it support xoom, nexus s and android emulator, I personally use haxm x86 emulator. No need to build custom system image.
 
So, you say that you used valgrind on android. Is it a standalone valgrind apk/executable that can be installed/copied to an android-arm phone and using some scripts I can get it running?
Yes, whole setup indeed took me around 12h (including virtualbox setup and so on).
 
to "disable" stripping in ndk, you can manually copy unstripped libs from obj\local\armeabi to libs. I use stripped libs, but then I run addr2line against unstripped ones to get backtraces.

I didn't think about that :) I'd of course prefer build system modification but it'll probably work just fine (as I said its not required but with unstripped so you'll get line numbers in valgrind traces).

--
Bart
Reply all
Reply to author
Forward
0 new messages