From ef26021d30ffb7edf8d1da5a3155e66d6e845a94 Mon Sep 17 00:00:00 2001 From: Ben Shi Date: Tue, 20 Jun 2017 03:03:12 +0000 Subject: [PATCH] cmd/internal/obj/arm: check illegal base registers in ARM instructions Wrong instructions "MOVW 8(F0), R1" and "MOVW R0<<0(F1), R1" are silently accepted, and all Fx are treated as Rx. The patch checks all those illegal base registers. fixes #20724 Change-Id: I05d41bb43fe774b023205163b7daf4a846e9dc88 Reviewed-on: https://go-review.googlesource.com/46132 Run-TryBot: Cherry Zhang TryBot-Result: Gobot Gobot Reviewed-by: Cherry Zhang --- src/cmd/asm/internal/asm/testdata/armerror.s | 26 ++++++++++++++++++++ src/cmd/internal/obj/arm/asm5.go | 21 ++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/cmd/asm/internal/asm/testdata/armerror.s b/src/cmd/asm/internal/asm/testdata/armerror.s index deda9cf4947..2959a2f47bc 100644 --- a/src/cmd/asm/internal/asm/testdata/armerror.s +++ b/src/cmd/asm/internal/asm/testdata/armerror.s @@ -3,6 +3,32 @@ // license that can be found in the LICENSE file. TEXT errors(SB),$0 + MOVW (F0), R1 // ERROR "illegal base register" + MOVB (F0), R1 // ERROR "illegal base register" + MOVH (F0), R1 // ERROR "illegal base register" + MOVF (F0), F1 // ERROR "illegal base register" + MOVD (F0), F1 // ERROR "illegal base register" + MOVW R1, (F0) // ERROR "illegal base register" + MOVB R2, (F0) // ERROR "illegal base register" + MOVH R3, (F0) // ERROR "illegal base register" + MOVF F4, (F0) // ERROR "illegal base register" + MOVD F5, (F0) // ERROR "illegal base register" + MOVM.IA (F1), [R0-R4] // ERROR "illegal base register" + MOVM.DA (F1), [R0-R4] // ERROR "illegal base register" + MOVM.IB (F1), [R0-R4] // ERROR "illegal base register" + MOVM.DB (F1), [R0-R4] // ERROR "illegal base register" + MOVM.IA [R0-R4], (F1) // ERROR "illegal base register" + MOVM.DA [R0-R4], (F1) // ERROR "illegal base register" + MOVM.IB [R0-R4], (F1) // ERROR "illegal base register" + MOVM.DB [R0-R4], (F1) // ERROR "illegal base register" + MOVW R0<<0(F1), R1 // ERROR "illegal base register" + MOVB R0<<0(F1), R1 // ERROR "illegal base register" + MOVW R1, R0<<0(F1) // ERROR "illegal base register" + MOVB R2, R0<<0(F1) // ERROR "illegal base register" + MOVF 0x00ffffff(F2), F1 // ERROR "illegal base register" + MOVD 0x00ffffff(F2), F1 // ERROR "illegal base register" + MOVF F2, 0x00ffffff(F2) // ERROR "illegal base register" + MOVD F2, 0x00ffffff(F2) // ERROR "illegal base register" MULS.S R1, R2, R3, R4 // ERROR "invalid .S suffix" ADD.P R1, R2, R3 // ERROR "invalid .P suffix" SUB.W R2, R3 // ERROR "invalid .W suffix" diff --git a/src/cmd/internal/obj/arm/asm5.go b/src/cmd/internal/obj/arm/asm5.go index 0836a7fa4ea..cfda99f6028 100644 --- a/src/cmd/internal/obj/arm/asm5.go +++ b/src/cmd/internal/obj/arm/asm5.go @@ -1343,6 +1343,27 @@ func (c *ctxt5) oplook(p *obj.Prog) *Optab { } } + // check illegal base register + switch a1 { + case C_SHIFT: + if p.From.Reg == 0 { // no base register + break + } + fallthrough + case C_SOREG, C_LOREG, C_HOREG, C_FOREG, C_ROREG, C_HFOREG, C_SROREG: + if p.From.Reg < REG_R0 || REG_R15 < p.From.Reg { + c.ctxt.Diag("illegal base register: %v", p) + } + default: + } + switch a3 { + case C_SOREG, C_LOREG, C_HOREG, C_FOREG, C_ROREG, C_HFOREG, C_SROREG, C_SHIFT: + if p.To.Reg < REG_R0 || REG_R15 < p.To.Reg { + c.ctxt.Diag("illegal base register: %v", p) + } + default: + } + // If current instruction has a .S suffix (flags update), // we must use the constant pool instead of splitting it. if (a1 == C_RCON2A || a1 == C_RCON2S) && p.Scond&C_SBIT != 0 {