Skip to content

Commit

Permalink
go/ssa: fix miscompilation of <<= and >>= statements
Browse files Browse the repository at this point in the history
Unlike other binary operations, the RHS operand of a shift operation
does not need to have the same type as the LHS operand. In particular,
for "x <<= y" and "x >>= y" statements, coercing y to have the same
type as x can lead to miscompilation.

Fixes golang/go#52342.

Change-Id: I3ff139afa18f5637d527bd4a0b10d6b40ce53ab4
Reviewed-on: https://go-review.googlesource.com/c/tools/+/400394
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Tim King <taking@google.com>
  • Loading branch information
mdempsky committed Apr 19, 2022
1 parent c02adcc commit e854e02
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
3 changes: 1 addition & 2 deletions go/ssa/builder.go
Expand Up @@ -1026,8 +1026,7 @@ func (b *builder) setCall(fn *Function, e *ast.CallExpr, c *CallCommon) {

// assignOp emits to fn code to perform loc <op>= val.
func (b *builder) assignOp(fn *Function, loc lvalue, val Value, op token.Token, pos token.Pos) {
oldv := loc.load(fn)
loc.store(fn, emitArith(fn, op, oldv, emitConv(fn, val, oldv.Type()), loc.typ(), pos))
loc.store(fn, emitArith(fn, op, loc.load(fn), val, loc.typ(), pos))
}

// localValueSpec emits to fn code to define all of the vars in the
Expand Down
2 changes: 2 additions & 0 deletions go/ssa/interp/interp_test.go
Expand Up @@ -124,6 +124,8 @@ var testdataTests = []string{
"reflect.go",
"static.go",
"width32.go",

"fixedbugs/issue52342.go",
}

// Specific GOARCH to use for a test case in go.tools/go/ssa/interp/testdata/.
Expand Down
17 changes: 17 additions & 0 deletions go/ssa/interp/testdata/fixedbugs/issue52342.go
@@ -0,0 +1,17 @@
package main

func main() {
var d byte

d = 1
d <<= 256
if d != 0 {
panic(d)
}

d = 1
d >>= 256
if d != 0 {
panic(d)
}
}

0 comments on commit e854e02

Please sign in to comment.