From: Josh Bleecher Snyder Date: Sat, 16 Feb 2019 00:28:05 +0000 (-0800) Subject: cmd/asm: improve DATA size operand validation X-Git-Tag: go1.13beta1~1350 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=f31305b71bcbfe85b918466aa6dd18e19a4b94a1;p=gostls13.git cmd/asm: improve DATA size operand validation Prior to this change, DATA instructions accepted the values 1, 2, 4, and 8 as sizes. The acceptable sizes were further restricted to 4 and 8 for float constants. This was both too restrictive and not restrictive enough: string constants may reasonably have any length, and address constants should really only accept pointer-length sizes. Fixes #30269 Change-Id: I06e44ecdf5909eca7b19553861aec1fa39655c2b Reviewed-on: https://go-review.googlesource.com/c/163747 Run-TryBot: Josh Bleecher Snyder TryBot-Result: Gobot Gobot Reviewed-by: Keith Randall --- diff --git a/src/cmd/asm/internal/asm/asm.go b/src/cmd/asm/internal/asm/asm.go index 5da64f135a..3d99af6889 100644 --- a/src/cmd/asm/internal/asm/asm.go +++ b/src/cmd/asm/internal/asm/asm.go @@ -7,6 +7,7 @@ package asm import ( "bytes" "fmt" + "strconv" "text/scanner" "cmd/asm/internal/arch" @@ -200,7 +201,11 @@ func (p *Parser) asmData(operands [][]lex.Token) { p.errorf("expect /size for DATA argument") return } - scale := p.parseScale(op[n-1].String()) + szop := op[n-1].String() + sz, err := strconv.Atoi(szop) + if err != nil { + p.errorf("bad size for DATA argument: %q", szop) + } op = op[:n-2] nameAddr := p.address(op) if !p.validSymbol("DATA", &nameAddr, true) { @@ -223,24 +228,33 @@ func (p *Parser) asmData(operands [][]lex.Token) { p.errorf("overlapping DATA entry for %s", name) return } - p.dataAddr[name] = nameAddr.Offset + int64(scale) + p.dataAddr[name] = nameAddr.Offset + int64(sz) switch valueAddr.Type { case obj.TYPE_CONST: - nameAddr.Sym.WriteInt(p.ctxt, nameAddr.Offset, int(scale), valueAddr.Offset) + switch sz { + case 1, 2, 4, 8: + nameAddr.Sym.WriteInt(p.ctxt, nameAddr.Offset, int(sz), valueAddr.Offset) + default: + p.errorf("bad int size for DATA argument: %d", sz) + } case obj.TYPE_FCONST: - switch scale { + switch sz { case 4: nameAddr.Sym.WriteFloat32(p.ctxt, nameAddr.Offset, float32(valueAddr.Val.(float64))) case 8: nameAddr.Sym.WriteFloat64(p.ctxt, nameAddr.Offset, valueAddr.Val.(float64)) default: - panic("bad float scale") + p.errorf("bad float size for DATA argument: %d", sz) } case obj.TYPE_SCONST: - nameAddr.Sym.WriteString(p.ctxt, nameAddr.Offset, int(scale), valueAddr.Val.(string)) + nameAddr.Sym.WriteString(p.ctxt, nameAddr.Offset, int(sz), valueAddr.Val.(string)) case obj.TYPE_ADDR: - nameAddr.Sym.WriteAddr(p.ctxt, nameAddr.Offset, int(scale), valueAddr.Sym, valueAddr.Offset) + if sz == p.arch.PtrSize { + nameAddr.Sym.WriteAddr(p.ctxt, nameAddr.Offset, int(sz), valueAddr.Sym, valueAddr.Offset) + } else { + p.errorf("bad addr size for DATA argument: %d", sz) + } } } diff --git a/src/cmd/asm/internal/asm/pseudo_test.go b/src/cmd/asm/internal/asm/pseudo_test.go index 52c98b4056..100bef91cf 100644 --- a/src/cmd/asm/internal/asm/pseudo_test.go +++ b/src/cmd/asm/internal/asm/pseudo_test.go @@ -43,6 +43,13 @@ func TestErroneous(t *testing.T) { {"DATA", "0", "expect two operands for DATA"}, {"DATA", "(0), 1", "expect /size for DATA argument"}, {"DATA", "@B(SB)/4,0", "expected '(', found B"}, // Issue 23580. + {"DATA", "·A(SB)/4,0", "DATA value must be an immediate constant or address"}, + {"DATA", "·B(SB)/4,$0", ""}, + {"DATA", "·C(SB)/5,$0", "bad int size for DATA argument: 5"}, + {"DATA", "·D(SB)/5,$0.0", "bad float size for DATA argument: 5"}, + {"DATA", "·E(SB)/4,$·A(SB)", "bad addr size for DATA argument: 4"}, + {"DATA", "·F(SB)/8,$·A(SB)", ""}, + {"DATA", "·G(SB)/5,$\"abcde\"", ""}, {"GLOBL", "", "expect two or three operands for GLOBL"}, {"GLOBL", "0,1", "GLOBL symbol \"\" must be a symbol(SB)"}, {"GLOBL", "@B(SB), 0", "expected '(', found B"}, // Issue 23580.