Skip to content

Commit

Permalink
cmd/compile: guard against loads with negative offset from readonly c…
Browse files Browse the repository at this point in the history
…onstants

CL 154057 adds guards agaist out-of-bound reads from readonly
constants. It turns out that in dead code, the offset can also
be negative. Guard against negative offset as well.

Fixes golang#30257.

Change-Id: I47c2a2e434dd466c08ae6f50f213999a358c796e
Reviewed-on: https://go-review.googlesource.com/c/162819
Reviewed-by: Keith Randall <khr@golang.org>
  • Loading branch information
cherrymui authored and nebulabox committed Feb 18, 2019
1 parent 62af18e commit 1c7c8f7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/cmd/compile/internal/ssa/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,7 @@ func symIsRO(sym interface{}) bool {
// read8 reads one byte from the read-only global sym at offset off.
func read8(sym interface{}, off int64) uint8 {
lsym := sym.(*obj.LSym)
if off >= int64(len(lsym.P)) {
if off >= int64(len(lsym.P)) || off < 0 {
// Invalid index into the global sym.
// This can happen in dead code, so we don't want to panic.
// Just return any value, it will eventually get ignored.
Expand All @@ -1155,7 +1155,7 @@ func read8(sym interface{}, off int64) uint8 {
// read16 reads two bytes from the read-only global sym at offset off.
func read16(sym interface{}, off int64, bigEndian bool) uint16 {
lsym := sym.(*obj.LSym)
if off >= int64(len(lsym.P))-1 {
if off >= int64(len(lsym.P))-1 || off < 0 {
return 0
}
if bigEndian {
Expand All @@ -1168,7 +1168,7 @@ func read16(sym interface{}, off int64, bigEndian bool) uint16 {
// read32 reads four bytes from the read-only global sym at offset off.
func read32(sym interface{}, off int64, bigEndian bool) uint32 {
lsym := sym.(*obj.LSym)
if off >= int64(len(lsym.P))-3 {
if off >= int64(len(lsym.P))-3 || off < 0 {
return 0
}
if bigEndian {
Expand All @@ -1181,7 +1181,7 @@ func read32(sym interface{}, off int64, bigEndian bool) uint32 {
// read64 reads eight bytes from the read-only global sym at offset off.
func read64(sym interface{}, off int64, bigEndian bool) uint64 {
lsym := sym.(*obj.LSym)
if off >= int64(len(lsym.P))-7 {
if off >= int64(len(lsym.P))-7 || off < 0 {
return 0
}
if bigEndian {
Expand Down
17 changes: 17 additions & 0 deletions test/fixedbugs/issue29215.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,20 @@ func f() {
}
_ = s == "bbb"
}

// Another case: load from negative offset of a symbol
// in dead code (issue 30257).
func g() {
var i int
var s string

if true {
s = "a"
}

if f := 0.0; -f < 0 {
i = len(s[:4])
}

_ = s[i-1:0] != "bb" && true
}

0 comments on commit 1c7c8f7

Please sign in to comment.