From ab9f27b2279f3bd28afb65050a9c4339d0b9961d Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Fri, 12 Sep 2008 12:35:06 -0700 Subject: [PATCH] fix up linux trap handling - INTB 5 gives SEGV R=rsc OCL=15244 CL=15244 --- src/runtime/rt1_amd64_darwin.c | 34 +++++++++++++++++++++++++++++++--- src/runtime/rt1_amd64_linux.c | 33 +++++++++++++++++++++++++++++---- src/runtime/rt2_amd64.c | 29 ----------------------------- src/runtime/runtime.h | 2 -- 4 files changed, 60 insertions(+), 38 deletions(-) diff --git a/src/runtime/rt1_amd64_darwin.c b/src/runtime/rt1_amd64_darwin.c index 2cbc7f61346..f6f49ccbcdb 100644 --- a/src/runtime/rt1_amd64_darwin.c +++ b/src/runtime/rt1_amd64_darwin.c @@ -6,9 +6,6 @@ #include "amd64_darwin.h" #include "signals.h" -extern void _rt0_amd64_darwin(); -byte* startsym = (byte*)_rt0_amd64_darwin; - typedef uint64 __uint64_t; /* From /usr/include/mach/i386/_structs.h */ @@ -133,6 +130,37 @@ typedef struct sigaction { int32 sa_flags; /* see signal options below */ } sigaction; +/* + * For trace traps, disassemble instruction to see if it's INTB of known type. + */ +int32 +inlinetrap(int32 sig, byte* pc) +{ + extern void etext(); + extern void _rt0_amd64_darwin(); + + if(sig != 5) /* INTB 5 looks like TRAP */ + return 0; + pc -= 2; // mac steps across INTB + if(pc < (byte*)_rt0_amd64_darwin || pc+2 >= (byte*)etext) + return 0; + if(pc[0] != 0xcd) /* INTB */ + return 0; + switch(pc[1]) { + case 5: + prints("\nTRAP: array out of bounds\n"); + break; + case 6: + prints("\nTRAP: leaving function with returning a value\n"); + break; + default: + prints("\nTRAP: unknown run-time trap "); + sys·printint(pc[1]); + prints("\n"); + } + return 1; +} + void sighandler(int32 sig, siginfo *info, void *context) { diff --git a/src/runtime/rt1_amd64_linux.c b/src/runtime/rt1_amd64_linux.c index 14cdf63e0b6..8c131d29202 100644 --- a/src/runtime/rt1_amd64_linux.c +++ b/src/runtime/rt1_amd64_linux.c @@ -6,9 +6,6 @@ #include "amd64_linux.h" #include "signals.h" -extern void _rt0_amd64_linux(); -byte* startsym = (byte*)_rt0_amd64_linux; - /* From /usr/include/asm-x86_64/sigcontext.h */ struct _fpstate { uint16 cwd; @@ -137,6 +134,35 @@ typedef struct sigaction { void (*sa_restorer) (void); /* unused here; needed to return from trap? */ } sigaction; +/* + * For trace traps, disassemble instruction to see if it's INTB of known type. + */ +int32 +inlinetrap(int32 sig, byte* pc) +{ + extern void etext(); + extern void _rt0_amd64_linux(); + + if(sig != 5 && sig != 11) /* 5 is for trap, but INTB 5 looks like SEGV */ + return 0; + if(pc < (byte*)_rt0_amd64_linux || pc+2 >= (byte*)etext) + return 0; + if(pc[0] != 0xcd) /* INTB */ + return 0; + switch(pc[1]) { + case 5: + prints("\nTRAP: array out of bounds\n"); + break; + case 6: + prints("\nTRAP: leaving function with returning a value\n"); + break; + default: + prints("\nTRAP: unknown run-time trap "); + sys·printint(pc[1]); + prints("\n"); + } + return 1; +} void sighandler(int32 sig, siginfo* info, void** context) @@ -167,7 +193,6 @@ sighandler(int32 sig, siginfo* info, void** context) sys·exit(2); } - static sigaction a; void diff --git a/src/runtime/rt2_amd64.c b/src/runtime/rt2_amd64.c index b7c3b11109e..91bef543406 100644 --- a/src/runtime/rt2_amd64.c +++ b/src/runtime/rt2_amd64.c @@ -86,32 +86,3 @@ traceback(uint8 *pc, uint8 *sp, void* r15) prints(", ...)\n"); } } - -/* - * For trace traps, disassemble instruction to see if it's INTB of known type. - */ -int32 -inlinetrap(int32 sig, byte* pc) -{ - extern void etext(); - - if(sig != 5) /* SIGTRAP */ - return 0; - if(pc-2 < startsym || pc >= (byte*)etext) - return 0; - if(pc[-2] != 0xcd) /* INTB */ - return 0; - switch(pc[-1]) { - case 5: - prints("\nTRAP: array out of bounds\n"); - break; - case 6: - prints("\nTRAP: leaving function with returning a value\n"); - break; - default: - prints("\nTRAP: unknown run-time trap "); - sys·printint(pc[-1]); - prints("\n"); - } - return 1; -} diff --git a/src/runtime/runtime.h b/src/runtime/runtime.h index e786b78ecff..022789234d2 100644 --- a/src/runtime/runtime.h +++ b/src/runtime/runtime.h @@ -208,7 +208,6 @@ G* allg; int32 goidgen; extern int32 gomaxprocs; extern int32 panicking; -extern byte *startsym; /* * common functions and data @@ -238,7 +237,6 @@ uint32 cmpstring(string, string); void initsig(void); void traceback(uint8 *pc, uint8 *sp, G* gp); void tracebackothers(G*); -int32 inlinetrap(int32 sig, byte* pc); int32 open(byte*, int32, ...); int32 read(int32, void*, int32); int32 write(int32, void*, int32);