Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: generates invalid instruction when using bit shifts on amd64 #38746

Closed
kynrai opened this issue Apr 29, 2020 · 7 comments
Closed

cmd/compile: generates invalid instruction when using bit shifts on amd64 #38746

kynrai opened this issue Apr 29, 2020 · 7 comments
Milestone

Comments

@kynrai
Copy link

@kynrai kynrai commented Apr 29, 2020

What version of Go are you using (go version)?

$ go version
go version go1.14.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN="/Users/steve/go/bin"
GOCACHE="/Users/steve/Library/Caches/go-build"
GOENV="/Users/steve/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/steve/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.14.2_1/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.14.2_1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/steve/Projects/kynrai/invalid_instruction/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/sm/hgxsvgvx3h93cb_0cwyj39g80000gn/T/go-build850937365=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Using bit shifts to store data when a sequence of code causes a invalid instruction error:
invalid instruction: 00080 (invalid_test.go:22) ANDQ $9223372034707292159, CX

I have minimised the code example to the smallest amount of code that will reproduce the error.

package invalid

import (
	"testing"
)

type ValuesMask uint64

func (v *ValuesMask) PrintBits() {
	for i := 0; i < 64; i++ {
	}
}

func TestMask(t *testing.T) {
	v := ValuesMask(0)
	v.PrintBits()

	v &^= (1 << 31)

	v |= 1 << 63

	v &^= (1 << 63)
}

What did you expect to see?

No error? or a pointer related error at worst

What did you see instead?

invalid instruction: 00080 (/Users/steve/Projects/kynrai/invalid_instruction/invalid_test.go:22) ANDQ $9223372034707292159, CX

@marigonzes
Copy link

@marigonzes marigonzes commented Apr 29, 2020

This code stopped compiling between go1.11 and go1.12, so I did a bisection and commit c6bf9a8 popped up, which seems right.

@ALTree
Copy link
Member

@ALTree ALTree commented Apr 29, 2020

Thank for reporting this, and thanks @marigonzes for the bisection.

cc @randall77 @josharian

@ALTree ALTree changed the title Invalid instruction when using bit shifts cmd/compile: generates invalid instruction when using bit shifts on amd64 Apr 29, 2020
@ALTree ALTree added this to the Go1.15 milestone Apr 29, 2020
@josharian
Copy link
Contributor

@josharian josharian commented Apr 29, 2020

I suspect converting those rules to be strongly typed will fix this. Is anyone currently working on typing the amd64 rules? (We’ll need a test added either way.)

@ALTree
Copy link
Member

@ALTree ALTree commented Apr 29, 2020

It appears that just making this rule typed:

(BTR(L|Q)const [c] (BTR(L|Q)const [d] x)) => (AND(L|Q)const [^(1<<uint32(c) | 1<<uint32(d))] x)

fixes at least the reproducer above. I don't think anyone is working on these amd64 rules at the moment. Will send a change to see if that's enough.

@randall77
Copy link
Contributor

@randall77 randall77 commented Apr 29, 2020

(BTR(L|Q)const [c] (BTR(L|Q)const [d] x)) -> (AND(L|Q)const [^(1<<uint32(c) | 1<<uint32(d))] x)

That rule is just wrong. We need c<32 and d<32 as preconditions.
Just typing it won't help - it will still lose the upper bits of the mask (for the Q version, at least).

@ALTree
Copy link
Member

@ALTree ALTree commented Apr 29, 2020

Thanks. Holding the CL then.

@gopherbot
Copy link

@gopherbot gopherbot commented May 4, 2020

Change https://golang.org/cl/231977 mentions this issue: cmd/compile: restrict bit test rewrite rules

@gopherbot gopherbot closed this in b4ecafc May 5, 2020
xujianhai666 added a commit to xujianhai666/go-1 that referenced this issue May 21, 2020
The {AND,OR,XOR}const ops can only take an int32 as an argument.
Make sure that when rewriting a BTx op to one of these, the result
has no high-order bits.

Fixes golang#38746

Change-Id: Ia7c5f76952329f60974bc033c29a5433610f3b28
Reviewed-on: https://go-review.googlesource.com/c/go/+/231977
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.