Understanding sigbus and SIGSEGV

Source: Internet
Author: User
Understanding sigbus and SIGSEGV
Searched by google


Q: How do I understand SIGSEGV.

A: nkwht @ smth

Nkwht uses Google to obtain such knowledge. There are multiple possible sigbus signals:

1) hardware faults. Needless to say, this is certainly not the case that programmers encounter most often.

2) execute malloc () on the Linux platform. If there is not enough RAM, Linux does not return the result of malloc () failure,
Instead, it distributes sigbus signals to the current process.

Note: You are skeptical about this point and have the opportunity to test and confirm the current system response.

3) Some architectures require alignment when accessing data. For example, you can only read one 4-byte
Data type. The IA-32 architecture does not require rigid alignment, although non-alignment access reduces execution efficiency. In addition
In some architectures, for example, the names of the following two types of architectures.

The sigbus and SIGSEGV signals are the same and can be captured normally. The default behavior of sigbus is to terminate the current process and generate
Generate core dump.

A: Marc rochkind

The differences between sigbus and SIGSEGV signals are as follows:

1) sigbus (Bus Error) means that the address corresponding to the pointer is a valid address, but the bus cannot use this
Pointer. This is usually caused by unaligned data access.

2) SIGSEGV (segment fault) means that the address corresponding to the pointer is invalid and there is no physical memory pair
Address.

A: SCZ 2002-11-20

How to capture the signal of sigbus and SIGSEGV in "2.4 how to program to obtain the stack base address", and use sigsetjmp,
Siglongjmp regain control.

Tests show that on the x86/Linux, x86/Solaris, or iSCSI/Solaris platforms, access from the bottom of the stack
Question causes the SIGSEGV signal. On x86/FreeBSD, x86/NetBSD, and x86/OpenBSD platforms
The sigbus signal is not the SIGSEGV signal.

The following is an example of what is non-aligned data access.

--------------------------------------------------------------------------
/*
* Test
* Gcc-wall-pipe-g-o bus. c
*/
# Include
# Include

Int main (INT argc, char * argv [])
{
Unsigned int I = 0x12345678;
Unsigned short int * q = NULL;
Unsigned char * P = (unsigned char *) & I;

* P = 0x00;
Q = (unsigned short int *) (p + 1 );
* Q = 0x0000;
Return (exit_success );
}/* End of main */
--------------------------------------------------------------------------

$./Bus
Bus Error (core dumped)
$ GDB./bus Core
Gnu gdb 5.0
#0 0x1084c in main (argc = 1, argv = 0xffbefc54) at bus. C: 16
16 * q = 0x0000;
(GDB) disas main
Dump of worker er code for function main:
Zero X 10810 : Save % sp,-128, % sp
Zero X 10814 : ST % I0, [% FP + 0x44]
Zero X 10818 : ST % I1, [% FP + 0x48]
0x00001c : Sethi % Hi (0x12345400), % O1
Zero X 10820 : Or % O1, Zero X 278, % O0! Zero X 12345678
Zero X 10824 : ST % O0, [% FP +-20]
Zero X 10828 : CLR [% FP +-24]
0x00002c : Add % FP,-20, % O0
Zero X 10830 : ST % O0, [% FP +-28]
Zero X 10834 : LD [% FP +-28], % O0
Zero X 10838 : Clrb [% O0]
0x00003c : LD [% FP +-28], % O0
Zero X 10840 : Add % O0, 1, % O1
Zero X 10844 : ST % O1, [% FP +-24]
Zero X 10848 : LD [% FP +-24], % O0
0x1084c : Clrh [% O0]
Zero X 10850 : CLR % I0
Zero X 10854 : B 0x1085c
Zero X 10858 : NOP
0x1085c : Ret
Zero X 10860 : Restore
End of worker er dump.
(GDB) I r PC
PC 0x1084c 67660
(GDB) I r O0
O0 0 xffbefbdd-4260899
(GDB) x/3bx 0 xffbefbdd
0 xffbefbdd: 0x34 0x56 0x78
(GDB)

In the C language, executing "* q = 0x0000;" causes sigbus. Execute "clrh [% O0]" in the Assembly command.
Cause sigbus, register % O0 is 0 xffbefbdd, this address is not aligned on the dual-byte boundary.

Note:-O is not specified during GCC compilation. Optimization, but still use clrh, instead of twice clrb. Similar
The Assembly Commands include LDW and lduh. Some people may encounter read operations that also lead to sigbus, which is incomprehensible,
In fact, reading and writing leads to no essential difference between sigbus. For example, LDW can only read addresses on the 4-byte boundary.

Bus. C is explicitly not aligned. What programmers are most likely to face is implicit non-alignment, mainly from the strong pointer
Type conversion. The following is an example of this situation.

--------------------------------------------------------------------------
/*
* Test
* Gcc-wall-pipe-g-o other_bus other_bus.c
*/
# Include
# Include

Int main (INT argc, char * argv [])
{
Unsigned int I = 0x12345678;
Unsigned short Int J = 0x0000;

J = * (unsigned short int *) (unsigned char *) & I) + 1 ));
Return (exit_success );
}/* End of main */
--------------------------------------------------------------------------

$./Other_bus
Bus Error (core dumped)
$ GDB./other_bus Core
Gnu gdb 5.0
#0 main (argc = 1, argv = 0xffbefc44) at other_bus.c: 13
13 J = * (unsigned short int *) (unsigned char *) & I) + 1 ));
(GDB) disas main
Dump of worker er code for function main:
Zero X 10810 : Save % sp,-120, % sp
Zero X 10814 : ST % I0, [% FP + 0x44]
Zero X 10818 : ST % I1, [% FP + 0x48]
0x00001c : Sethi % Hi (0x12345400), % O1
Zero X 10820 : Or % O1, Zero X 278, % O0! Zero X 12345678
Zero X 10824 : ST % O0, [% FP +-20]
Zero X 10828 : Clrh [% FP +-22]
0x00002c : Lduh [% FP +-19], % O0
Zero X 10830 : Something % O0, [% FP +-22]
Zero X 10834 : CLR % I0
Zero X 10838 : B 0x10840
0x00003c : NOP
Zero X 10840 : Ret
Zero X 10844 : Restore
End of worker er dump.
(GDB) I r PC
PC 0x00002c 67628
(GDB)

Therefore, to program on the structure of the world's disks, you must pay attention to forced type conversion, and be sure to know what you are doing.
No hidden risks.

D: yuhuan@smth.org

Parameter Linux MMAP (2) manual page

--------------------------------------------------------------------------
Using ing may involve the following signals:

SIGSEGV

Try to write the read-only ing area

Sigbus

Try to access a memory area corresponding to the file content, such as the memory area that exceeds the end of the file, or
The file content corresponds to the memory area truncated by another process.
--------------------------------------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.