Skip to content

Commit

Permalink
cmd/compile: fix crash when memmove argument is not the right type
Browse files Browse the repository at this point in the history
Make sure the argument to memmove is of pointer type before we try to
get the element type.

This has been noticed for code that uses unsafe+linkname so it can
call runtime.memmove. Probably not the best thing to allow, but the
code is out there and we'd rather not break it unnecessarily.

Fixes golang#30061

Change-Id: I334a8453f2e293959fd742044c43fbe93f0b3d31
Reviewed-on: https://go-review.googlesource.com/c/160826
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
  • Loading branch information
randall77 authored and nebulabox committed Feb 20, 2019
1 parent 3e7f550 commit 5375c13
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/cmd/compile/internal/ssa/gen/generic.rules
Expand Up @@ -1354,6 +1354,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))))
&& isSameSym(sym,"runtime.memmove")
&& t.(*types.Type).IsPtr() // avoids TUINTPTR, see issue 30061
&& s1.Uses == 1 && s2.Uses == 1 && s3.Uses == 1
&& isInlinableMemmove(dst,src,sz,config)
&& clobber(s1) && clobber(s2) && clobber(s3)
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.

20 changes: 20 additions & 0 deletions test/fixedbugs/issue30061.go
@@ -0,0 +1,20 @@
// 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.

// Make sure we can linkname to memmove with an unsafe.Pointer argument.

package p

import "unsafe"

//go:linkname memmove runtime.memmove
func memmove(to, from unsafe.Pointer, n uintptr)

var V1, V2 int

func F() {
memmove(unsafe.Pointer(&V1), unsafe.Pointer(&V2), unsafe.Sizeof(int(0)))
}

0 comments on commit 5375c13

Please sign in to comment.