mzyy94
12/18/2013 - 2:26 AM

Bus error: 10 の再現性を確認するためのテスト

Bus error: 10 の再現性を確認するためのテスト

#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>

int mode = 0;
jmp_buf jb;
static const char* label[] = {"auto/set", "auto/read",
	"const/read", "static/set", "static/read",
   	"register/set", "register/read", "volatile/set",
   	"volatile/read", "normal/set", "normal/read"};

void jump(char t) {
	printf("mode: [%s]\n",(label[mode]));
	if(t == 10)
		puts("Bus error: 10");
	else
		puts("Segmentation fault: 11");
	if(label[++mode] == NULL)
		exit(0);
	longjmp(jb,2);
}

void sigsegv() {
	jump(11);
}

void sigbus() {
	jump(10);
}

int main(int argc, char* argv[]) {
	int jp; // Jump point
	unsigned char t; //temp
	auto unsigned char a[1];
	static unsigned char s[1];
	const unsigned char c[1];
	register unsigned char r[1];
	volatile unsigned char v[1];
	unsigned char n[1];
	puts("ARRAY");
	signal(SIGSEGV, sigsegv);
	signal(SIGBUS, sigbus);

	jp = setjmp(jb);
	for(int i = 0; ; i++){
		switch (mode) {
			case 0: 
				a[i] = '\0';
				break;
			case 1:
				t = a[i];
				break;
			case 2:
				t = c[i];
				break;
			case 3:
				s[i] = '\0';
				break;
			case 4:
				t = s[i];
				break;
			case 5:
				r[i] = '\0';
				break;
			case 6: 
				t = r[i];
				break;
			case 7:
				v[i] = '\0';
				break;
			case 8: 
				t = v[i];
				break;
			case 9:
				n[i] = '\0';
				break;
			case 10:
				t = n[i];
				break;
			default:
				exit(1);
		}
	}
	return 0;
}