公告
财富商城
积分规则
提问
发文
2019-02-16 07:19发布
Bombasti
I am trying very hard to get a bus error.
One way is misaligned access and I have tried the examples given here and here, but no error for me - the programs execute just fine.
Is there some situation which is sure to produce a bus error?
int main(int argc, char **argv) { char *bus_error = new char[1]; for (int i=0; i<1000000000;i++) { bus_error += 0xFFFFFFFFFFFFFFF; *(bus_error + 0xFFFFFFFFFFFFFF) = 'X'; } }
Bus error: 10 (core dumped)
Simple, write to memory that isn't yours:
int main() { char *bus_error = 0; *bus_error = 'X'; }
Instant bus error on my PowerPC Mac [OS X 10.4, dual 1ghz PPC7455's], not necessarily on your hardware and/or operating system.
There's even a wikipedia article about bus errors, including a program to make one.
Bus errors can only be invoked on hardware platforms that:
You probably do not have access to such a system.
For 0x86 arch:
#include <stdio.h> int main() { #if defined(__GNUC__) # if defined(__i386__) /* Enable Alignment Checking on x86 */ __asm__("pushf\norl $0x40000,(%esp)\npopf"); # elif defined(__x86_64__) /* Enable Alignment Checking on x86_64 */ __asm__("pushf\norl $0x40000,(%rsp)\npopf"); # endif #endif int b = 0; int a = 0xffffff; char *c = (char*)&a; c++; int *p = (int*)c; *p = 10; //Bus error as memory accessed by p is not 4 or 8 byte aligned printf ("%d\n", sizeof(a)); printf ("%x\n", *p); printf ("%x\n", p); printf ("%x\n", &a); }
Note:If asm instructions are removed, code wont generate the SIGBUS error as suggested by others. SIGBUS can occur for other reason too.
Try something along the lines of:
#include <signal.h> int main(void) { raise(SIGBUS); return 0; }
(I know, probably not the answer you want, but it's almost sure to get you a "bus error"!)
How about this? untested.
#include<stdio.h> typedef struct { int a; int b; } busErr; int main() { busErr err; char * cPtr; int *iPtr; cPtr = (char *)&err; cPtr++; iPtr = (int *)cPtr; *iPtr = 10; }
最多设置5个标签!
Bus error: 10 (core dumped)
Simple, write to memory that isn't yours:
Instant bus error on my PowerPC Mac [OS X 10.4, dual 1ghz PPC7455's], not necessarily on your hardware and/or operating system.
There's even a wikipedia article about bus errors, including a program to make one.
Bus errors can only be invoked on hardware platforms that:
You probably do not have access to such a system.
For 0x86 arch:
Note:If asm instructions are removed, code wont generate the SIGBUS error as suggested by others. SIGBUS can occur for other reason too.
Try something along the lines of:
(I know, probably not the answer you want, but it's almost sure to get you a "bus error"!)
How about this? untested.