Error: SEGV_ACCERR in Native code

2,398 views
Skip to first unread message

patrick Immling

unread,
Aug 26, 2011, 7:57:37 AM8/26/11
to android-ndk
Dear All,

I am writing a simple tcp Server(native server) and I compiled and it
and did an "adb push" to /data/tmp.

But whenever I run it I get this error:

I/DEBUG ( 34): pid: 352, tid: 352 >>> ./tcpServer <<<
I/DEBUG ( 34): signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault
addr 40008010
I/DEBUG ( 34): r0 00000001 r1 beda9c04 r2 beda9c0c r3 4000800c
I/DEBUG ( 34): r4 beda9c04 r5 00000001 r6 beda9c0c r7 00008594
I/DEBUG ( 34): r8 00000000 r9 00000000 10 00000000 fp 00000000
I/DEBUG ( 34): ip 00000000 sp beda9be8 lr 000085b3 pc
aff14c88 cpsr 00000010
I/DEBUG ( 34): #00 pc 00014c88 /system/lib/libc.so
(pthread_atfork)
I/DEBUG ( 34): #01 lr 000085b3 /system/bin/tcpServer
I/DEBUG ( 34):
I/DEBUG ( 34): libc base address: aff00000
I/DEBUG ( 34):
I/DEBUG ( 34): code around pc:
I/DEBUG ( 34): aff14c68 e5902004 e08f3001 e5862004 e590c000
I/DEBUG ( 34): aff14c78 e15c0000 e59f002c 15826000 058c6000
I/DEBUG ( 34): aff14c88 e5836004 e08f0000 ebffeff7 e3a00000
I/DEBUG ( 34): aff14c98 e8bd81f0 e3a0000c e8bd81f0 0002b950
I/DEBUG ( 34): aff14ca8 0002b940 0002b934 0002b910 e92d4070
I/DEBUG ( 34):
I/DEBUG ( 34): code around lr:
I/DEBUG ( 34): 00008590 eaffffdd ea000046 00009000 00009008
I/DEBUG ( 34): 000085a0 00009010 00009018 e1a00000 e1a00000
I/DEBUG ( 34): 000085b0 43f0e92d 49384c37 447cb08f 58602200
I/DEBUG ( 34): 000085c0 68032101 930d2002 efa6f7ff 46052800
I/DEBUG ( 34): 000085d0 4832da03 f7ff4478 482defa6 f101a909
I/DEBUG ( 34):
I/DEBUG ( 34): stack:
I/DEBUG ( 34): beda9ba8 aff4053c /system/lib/libc.so
I/DEBUG ( 34): beda9bac 00001000
I/DEBUG ( 34): beda9bb0 40008000
I/DEBUG ( 34): beda9bb4 aff257f1 /system/lib/libc.so
I/DEBUG ( 34): beda9bb8 ffffffff
I/DEBUG ( 34): beda9bbc 00000000
I/DEBUG ( 34): beda9bc0 00000000
I/DEBUG ( 34): beda9bc4 aff257f1 /system/lib/libc.so
I/DEBUG ( 34): beda9bc8 00009010 /system/bin/tcpServer
I/DEBUG ( 34): beda9bcc 00000000
I/DEBUG ( 34): beda9bd0 00000000
I/DEBUG ( 34): beda9bd4 beda9c04
I/DEBUG ( 34): beda9bd8 00000001
I/DEBUG ( 34): beda9bdc beda9c0c
I/DEBUG ( 34): beda9be0 df002777
I/DEBUG ( 34): beda9be4 e3a070ad
I/DEBUG ( 34): #00 beda9be8 00008598 /system/bin/tcpServer
I/DEBUG ( 34): beda9bec 00000000
I/DEBUG ( 34): beda9bf0 00000000
I/DEBUG ( 34): beda9bf4 00000000
I/DEBUG ( 34): beda9bf8 00000000
I/DEBUG ( 34): beda9bfc b00045ed /system/bin/linker
I/DEBUG ( 34): beda9c00 00000001
I/DEBUG ( 34): beda9c04 beda9d0c
I/DEBUG ( 34): beda9c08 00000000
I/DEBUG ( 34): beda9c0c beda9d18
I/DEBUG ( 34): beda9c10 beda9d26
I/DEBUG ( 34): beda9c14 beda9d39
I/DEBUG ( 34): beda9c18 beda9d5c
I/DEBUG ( 34): beda9c1c beda9d75
I/DEBUG ( 34): beda9c20 beda9d9f
I/DEBUG ( 34): beda9c24 beda9da9
I/DEBUG ( 34): beda9c28 beda9dc6
I/DEBUG ( 34): beda9c2c beda9dd9


I thought it could be that this /data partition isn't allowed access
to sockets?

So I tried remounting /system with rw permission and did adb push to
this /system/bin partiotion but the error persists.

Can we not run a native TCP server? what does this error mean?

Thanks.

David Turner

unread,
Aug 26, 2011, 12:20:32 PM8/26/11
to andro...@googlegroups.com
The stack trace indicates that pthread_atfork() is crashing for some reason.
You probably shouldn't be calling this function anyway.

Use the ndk-stack tool to see where you did call it.

This doesn't seem to be related to sockets btw. And which data partition you run on doesn't affect your permissions, only your UID


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
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.


patrick Immling

unread,
Aug 29, 2011, 8:53:16 AM8/29/11
to andro...@googlegroups.com
Thanks David,

But I never used an y thread forking calls :(

I just wrote a very basic TCP server which seem to wok without errors on my linux PC but somehow on Android it just stops with this error :( :(

Please advise

David Turner

unread,
Aug 29, 2011, 8:59:07 AM8/29/11
to andro...@googlegroups.com
On Mon, Aug 29, 2011 at 2:53 PM, patrick Immling <pimm...@googlemail.com> wrote:
Thanks David,

But I never used an y thread forking calls :(

there are no "thread forking calls". You either fork a process, but this is only safe if only one thread is running (the main one), or you handle multiple threads and do not fork. Anything else is signifcantly unsafe, even on traditional Linux land. Are you calling pthread_atfork() or not?

patrick Immling

unread,
Aug 29, 2011, 9:20:06 AM8/29/11
to android-ndk
No I am not.

Here is my C code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>

//#define IP_ADDRESS "10.0.2.2"
#define BUFFERLENGTH 128

int main(int argc, char **argv) {
int serv_fd, clnt_fd;
struct sockaddr_in serv_addr;
char ackmsg[32];
// create socket
if((serv_fd = socket(AF_INET, SOCK_STREAM, 0))<0){
printf("socket creation failed\n");
}

// prepare for bind
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8040);

serv_addr.sin_addr.s_addr = INADDR_ANY;
//memset(&serv_addr, 0, sizeof(serv_addr));

memset(&(serv_addr.sin_zero), '\0', 8);
// bind
if((bind(serv_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)))
< 0){
printf("bind failed\n");
return -1;
}

listen(serv_fd, 1);
printf("LISTENING FOR CLIENTS :) :) :)\n");
while (1) {
// accept only 1 client
printf("in while loop\n");
if((clnt_fd = accept(serv_fd, NULL, NULL))< 0) {
printf("failed to accept connection\n");
}

int rxLen = recv(clnt_fd, ackmsg, sizeof(ackmsg), 0);
if(rxLen <= 0) {
printf(" error occoured, close socket\n");
close(clnt_fd);
return -1;
}
printf("%s\n",ackmsg);

}

// close connection from client and stop echo server
close(clnt_fd);
close(serv_fd);
return 0;
}




On Aug 29, 1:59 pm, David Turner <di...@android.com> wrote:
> On Mon, Aug 29, 2011 at 2:53 PM, patrick Immling <pimml...@googlemail.com>wrote:
>
> > Thanks David,
>
> > But I never used an y thread forking calls :(
>
> > there are no "thread forking calls". You either fork a process, but this is
>
> only safe if only one thread is running (the main one), or you handle
> multiple threads and do not fork. Anything else is signifcantly unsafe, even
> on traditional Linux land. Are you calling pthread_atfork() or not?
>
>
>
>
>
>
>
> > I just wrote a very basic TCP server which seem to wok without errors on my
> > linux PC but somehow on Android it just stops with this error :( :(
>
> > Please advise
>
> > On Fri, Aug 26, 2011 at 5:20 PM, David Turner <di...@android.com> wrote:
>
> >> The stack trace indicates that pthread_atfork() is crashing for some
> >> reason.
> >> You probably shouldn't be calling this function anyway.
>
> >> Use the ndk-stack tool to see where you did call it.
>
> >> This doesn't seem to be related to sockets btw. And which data partition
> >> you run on doesn't affect your permissions, only your UID
>
> >> On Fri, Aug 26, 2011 at 1:57 PM, patrick Immling <pimml...@googlemail.com

patrick Immling

unread,
Aug 29, 2011, 9:50:54 AM8/29/11
to android-ndk
When I try a simple tcp client with the tcp Server as above on my host
and the client on android.

I do:
>adb shell
>cd data/tmp/
>ls -la
-rwxrwxrwx root root 5500 2011-08-26 16:51 clientTcp
-rwxrwxrwx root root 5484 2011-08-26 13:28 tcpServer

>./clientTcp

My logcat output:

I/DEBUG ( 34): *** *** *** *** *** *** *** *** *** *** *** *** ***
*** *** ***
I/DEBUG ( 34): Build fingerprint: 'generic/sdk/generic:3.1/MASTER/
123685:eng/test-keys'
I/DEBUG ( 34): pid: 369, tid: 369 >>> ./clientTcp <<<
I/DEBUG ( 34): signal 4 (SIGILL), code 1 (ILL_ILLOPC), fault addr
000086b0
I/DEBUG ( 34): r0 0000011c r1 bea49bc0 r2 00000000 r3 00000000
I/DEBUG ( 34): r4 000090e8 r5 bea49bbc r6 00000003 r7 00008644
I/DEBUG ( 34): r8 00000000 r9 00000000 10 00000000 fp 00000000
I/DEBUG ( 34): ip 00000000 sp bea49bb8 lr 000086ad pc
000086b0 cpsr 80000030
I/DEBUG ( 34): #00 pc 000086b0 /data/tmp/clientTcp
I/DEBUG ( 34): #01 lr 000086ad /data/tmp/clientTcp
I/DEBUG ( 34):
I/DEBUG ( 34): libc base address: aff00000
I/DEBUG ( 34):
I/DEBUG ( 34): code around pc:
I/DEBUG ( 34): 00008690 f7ff4478 e050ef98 ad014833 f7ff4478
I/DEBUG ( 34): 000086a0 2210ef92 46282100 ef92f7ff 1d29482f
I/DEBUG ( 34): 000086b0 0c02f04f f8ad4478 f7ffc004 f646ef90
I/DEBUG ( 34): 000086c0 4630021f 2006f8ad 22104629 ef8cf7ff
I/DEBUG ( 34): 000086d0 28004b27 58e0da08 4a274926 447930a8
I/DEBUG ( 34):
I/DEBUG ( 34): code around lr:
I/DEBUG ( 34): 0000868c 4835da04 f7ff4478 e050ef98 ad014833
I/DEBUG ( 34): 0000869c f7ff4478 2210ef92 46282100 ef92f7ff
I/DEBUG ( 34): 000086ac 1d29482f 0c02f04f f8ad4478 f7ffc004
I/DEBUG ( 34): 000086bc f646ef90 4630021f 2006f8ad 22104629
I/DEBUG ( 34): 000086cc ef8cf7ff 28004b27 58e0da08 4a274926
I/DEBUG ( 34):
I/DEBUG ( 34): stack:
I/DEBUG ( 34): bea49b78 aff4063c /system/lib/libc.so
I/DEBUG ( 34): bea49b7c ffffff24
I/DEBUG ( 34): bea49b80 00008644 /data/tmp/clientTcp
I/DEBUG ( 34): bea49b84 aff19a29 /system/lib/libc.so
I/DEBUG ( 34): bea49b88 b000a488
I/DEBUG ( 34): bea49b8c 000087c2 /data/tmp/clientTcp
I/DEBUG ( 34): bea49b90 00000011
I/DEBUG ( 34): bea49b94 aff374a7 /system/lib/libc.so
I/DEBUG ( 34): bea49b98 00000001
I/DEBUG ( 34): bea49b9c bea49b8c
I/DEBUG ( 34): bea49ba0 bea49bbc
I/DEBUG ( 34): bea49ba4 000090e8 /data/tmp/clientTcp
I/DEBUG ( 34): bea49ba8 bea49bbc
I/DEBUG ( 34): bea49bac 00000003
I/DEBUG ( 34): bea49bb0 df002777
I/DEBUG ( 34): bea49bb4 e3a070ad
I/DEBUG ( 34): #00 bea49bb8 ffffffff
I/DEBUG ( 34): bea49bbc 00000000
I/DEBUG ( 34): bea49bc0 00000000
I/DEBUG ( 34): bea49bc4 00000000
I/DEBUG ( 34): bea49bc8 00000000
I/DEBUG ( 34): bea49bcc a5b31914
I/DEBUG ( 34): bea49bd0 00000000
I/DEBUG ( 34): bea49bd4 bea49c04
I/DEBUG ( 34): bea49bd8 00000001
I/DEBUG ( 34): bea49bdc bea49c0c
I/DEBUG ( 34): bea49be0 00008644 /data/tmp/clientTcp
I/DEBUG ( 34): bea49be4 aff14a31 /system/lib/libc.so
I/DEBUG ( 34): bea49be8 00008648 /data/tmp/clientTcp
I/DEBUG ( 34): bea49bec 00000000
I/DEBUG ( 34): bea49bf0 00000000
I/DEBUG ( 34): bea49bf4 00000000
I/DEBUG ( 34): bea49bf8 00000000
I/DEBUG ( 34): bea49bfc b00045ed /system/bin/linker


However the same two codes work on my PC.

Any help?



On Aug 29, 1:59 pm, David Turner <di...@android.com> wrote:
> On Mon, Aug 29, 2011 at 2:53 PM, patrick Immling <pimml...@googlemail.com>wrote:
>
> > Thanks David,
>
> > But I never used an y thread forking calls :(
>
> > there are no "thread forking calls". You either fork a process, but this is
>
> only safe if only one thread is running (the main one), or you handle
> multiple threads and do not fork. Anything else is signifcantly unsafe, even
> on traditional Linux land. Are you calling pthread_atfork() or not?
>
>
>
>
>
>
>
> > I just wrote a very basic TCP server which seem to wok without errors on my
> > linux PC but somehow on Android it just stops with this error :( :(
>
> > Please advise
>
> > On Fri, Aug 26, 2011 at 5:20 PM, David Turner <di...@android.com> wrote:
>
> >> The stack trace indicates that pthread_atfork() is crashing for some
> >> reason.
> >> You probably shouldn't be calling this function anyway.
>
> >> Use the ndk-stack tool to see where you did call it.
>
> >> This doesn't seem to be related to sockets btw. And which data partition
> >> you run on doesn't affect your permissions, only your UID
>
> >> On Fri, Aug 26, 2011 at 1:57 PM, patrick Immling <pimml...@googlemail.com

David Turner

unread,
Aug 29, 2011, 12:06:59 PM8/29/11
to andro...@googlegroups.com
Ok, it looks like the first and second stack traces are completely different. The first corresponds to a segmentation fault in a function that is never called, the second is for an illegal CPU instruction in your program. It looks like the main's function return address was corrupted, or something.

Your sockaddress initialization function is buggy. You should first memset(&addr, 0, sizeof(addr)), then initialize the fields in it. Never touch sin_zero explicitely, this is not portable, especially if you use hard-coded sizes like 8.

For the record, how did you build this program exactly?

patrick Immling

unread,
Aug 30, 2011, 3:24:12 AM8/30/11
to andro...@googlegroups.com
Thanks David.


I had created "tcpServer" folder under "external" directory of android sources.
I had used an example Android.mk(of that of "ping" program).

I had changed it accordingly and placed it inside the tcpServer. I then did an "mm". It had compiled without warning or errors.

Here is the Android.mk:

ifneq ($(TARGET_SIMULATOR),true)

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)
LOCAL_SRC_FILES:= tcpServer.c
LOCAL_MODULE := tcpServer
LOCAL_MODULE_PATH := $(TARGET_OUT_EXECUTABLES)
LOCAL_MODULE_TAGS := optional

LOCAL_STATIC_LIBRARIES := libcutils libc
LOCAL_LDLIBS := -lsocket
include $(BUILD_EXECUTABLE)

endif  # TARGET_SIMULATOR != true


I already found my executables in  out/target/product/generic/system/bin folder.

However on the emulator, I did not find it on the system/bin folder so I had to do an adb push  of the executable on the /data/tmp/ folder

It was the same procedure with the clientTcp program.
Reply all
Reply to author
Forward
0 new messages