Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of 'code' in Segmentation Fault

Tags:

linux

android

I see these 2 segmentation faults in android. One said SEGV_MAPERR, the other said SEGV_ACCERR.

Can you please tell me what are the differences between these 2?

signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 41963214

signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 4006e000

Thanks.

like image 220
michael Avatar asked Oct 01 '13 20:10

michael


2 Answers

Per siginfo.h:

SEGV_MAPERR means you tried to access an address that doesn't map to anything.

SEGV_ACCERR means you tried to access an address that you don't have permission to access.

So in both cases you accessed an address you shouldn't have, which is probably the only thing your actual code is guilty of. In the former case there's no memory in that address range anyway. In the latter case there is memory in that address range but you don't own it.

If you were to access a random address then which you get depends on how the OS happens to have your process set up at that moment.

like image 63
Tommy Avatar answered Oct 16 '22 20:10

Tommy


If you access a memory like *((int*)0)=1, you will get SEGV_MAPERR.

If you protected a memory with mprotect(2), e.g., mprotect(buffer, pagesize, PROT_READ), then you modified the memory like *(buffer)=1, you will get SEGV_ACCERR.

Please man mprotect for detail.

like image 38
Qiu Yangfan Avatar answered Oct 16 '22 19:10

Qiu Yangfan