Skip to content

Commit

Permalink
cmd/compile: avoid memmove -> SSA move rewrite when size is negative
Browse files Browse the repository at this point in the history
We should panic in this situation. Rewriting to a SSA op just leads
to a compiler panic.

Fixes #36259

Change-Id: I6e0bccbed7dd0fdac7ebae76b98a211947947386
Reviewed-on: https://go-review.googlesource.com/c/go/+/212405
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
  • Loading branch information
randall77 committed Feb 24, 2020
1 parent e092fc3 commit bc98e35
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/cmd/compile/internal/ssa/gen/generic.rules
Expand Up @@ -1938,6 +1938,7 @@

// Inline small or disjoint runtime.memmove calls with constant length.
(StaticCall {sym} s1:(Store _ (Const(64|32) [sz]) s2:(Store _ src s3:(Store {t} _ dst mem))))
&& sz >= 0
&& isSameSym(sym,"runtime.memmove")
&& t.(*types.Type).IsPtr() // avoids TUINTPTR, see issue 30061
&& s1.Uses == 1 && s2.Uses == 1 && s3.Uses == 1
Expand Down
8 changes: 4 additions & 4 deletions src/cmd/compile/internal/ssa/rewritegeneric.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions test/fixedbugs/issue36259.go
@@ -0,0 +1,28 @@
// compile

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

func rotate(s []int, m int) {
l := len(s)
m = m % l
buf := make([]int, m)

copy(buf, s)
copy(s, s[m:])
copy(s[l-m:], buf)
}

func main() {
a0 := [...]int{1,2,3,4,5}
println(a0[0])

rotate(a0[:], 1)
println(a0[0])

rotate(a0[:], -3)
println(a0[0])
}

0 comments on commit bc98e35

Please sign in to comment.