From 7d2c6eb3f53d4fa54587ff52ecb739bd4b13f9ab Mon Sep 17 00:00:00 2001 From: Ilya Tocar Date: Tue, 20 Oct 2015 14:52:08 +0300 Subject: [PATCH] cmd/internal/obj/x86: align functions with trap instruction Align functions with 0xCC (INT $3) - breakpoint instruction, instead of 0x00, which can disassemble into valid instruction. Change-Id: Ieda191886efc4aacb86f58bea1169fd1b3b57636 Reviewed-on: https://go-review.googlesource.com/16102 Run-TryBot: Ilya Tocar Reviewed-by: Gregory Shimansky TryBot-Result: Gobot Gobot Reviewed-by: Keith Randall --- src/cmd/internal/obj/x86/asm6.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/cmd/internal/obj/x86/asm6.go b/src/cmd/internal/obj/x86/asm6.go index f3be6cdfc7..8159e5934f 100644 --- a/src/cmd/internal/obj/x86/asm6.go +++ b/src/cmd/internal/obj/x86/asm6.go @@ -1754,7 +1754,16 @@ func span6(ctxt *obj.Link, s *obj.LSym) { c = naclpad(ctxt, s, c, -c&31) } - c += -c & (FuncAlign - 1) + // Pad functions with trap instruction, to catch invalid jumps + if c&(FuncAlign-1) != 0 { + v = -c & (FuncAlign - 1) + obj.Symgrow(ctxt, s, int64(c)+int64(v)) + for i := c; i < c+v; i++ { + // 0xCC is INT $3 - breakpoint instruction + s.P[i] = uint8(0xCC) + } + c += v + } s.Size = int64(c) if false { /* debug['a'] > 1 */