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: wrong calculation result for bit operation that's inlined and has all constant shifts in rewrite rules #32680

Closed
chanxuehong opened this issue Jun 19, 2019 · 17 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. release-blocker
Milestone

Comments

@chanxuehong
Copy link
Contributor

chanxuehong commented Jun 19, 2019

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

go version go1.12.6 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
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/chan/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/chan/gopath"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
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/qw/ddvmdjjd35xcj6823_nz3z780000gn/T/go-build028699097=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import (
	"fmt"
)

func main() {
	fmt.Println(New1().info)
	fmt.Println(New2().info)
	fmt.Println(New3().info)
}

type interfaceImpl struct {
	info [256]uint32
}

func New1() *interfaceImpl {
	p := interfaceImpl{}
	for k := 0; k < 256; k++ {
		i := k*8 + 4
		p.info[k] = readLittleEndian32_1(data[i], data[i+1], data[i+2], data[i+3])
	}
	return &p
}

func New2() *interfaceImpl {
	p := interfaceImpl{}
	for k := 0; k < 256; k++ {
		i := k*8 + 4
		p.info[k] = readLittleEndian32_2(data[i], data[i+1], data[i+2], data[i+3])
	}
	return &p
}

func New3() *interfaceImpl {
	p := interfaceImpl{}
	for k := 0; k < 256; k++ {
		i := k*8 + 4
		p.info[k] = readLittleEndian32_3(data[i], data[i+1], data[i+2], data[i+3])
	}
	return &p
}

var x = false

func readLittleEndian32_1(a, b, c, d byte) uint32 {
	r := uint32(a) | (uint32(b) << 8) | (uint32(c) << 16) | (uint32(d) << 24)
	if !x {
		x = true
		fmt.Println(a, b, c, d, r)
	}
	return r
}

func readLittleEndian32_2(a, b, c, d byte) uint32 {
	return uint32(a) | (uint32(b) << 8) | (uint32(c) << 16) | (uint32(d) << 24)
}

func readLittleEndian32_3(a, b, c, d byte) uint32 {
	return (uint32(a) & 0xFF) | ((uint32(b) << 8) & 0xFF00) | ((uint32(c) << 16) & 0xFF0000) | ((uint32(d) << 24) & 0xFF000000)
}

var data = []byte{
	112, 1, 250, 10, 146, 254, 150, 46, 114, 115, 40, 141, 42, 144, 93, 5, 124, 166, 33, 115, 46, 36, 167, 13, 104, 240, 64, 6, 72, 10, 56, 108,
	161, 222, 173, 14, 112, 43, 206, 127, 75, 160, 97, 147, 37, 247, 164, 191, 227, 80, 14, 45, 239, 136, 118, 33, 250, 35, 139, 252, 139, 185, 14, 27,
	83, 174, 104, 224, 28, 124, 175, 193, 21, 182, 188, 44, 150, 225, 22, 54, 167, 33, 4, 137, 114, 213, 154, 164, 29, 48, 75, 39, 154, 70, 118, 162,
	35, 154, 52, 215, 141, 209, 130, 210, 110, 155, 130, 3, 44, 179, 118, 252, 142, 159, 95, 59, 33, 234, 242, 111, 204, 119, 84, 133, 154, 126, 134, 127,
	48, 128, 54, 115, 202, 210, 163, 134, 30, 9, 254, 117, 177, 15, 103, 158, 99, 205, 136, 196, 7, 84, 5, 103, 22, 168, 243, 20, 130, 125, 80, 62,
	18, 21, 9, 59, 150, 9, 33, 16, 138, 60, 221, 167, 42, 137, 197, 161, 31, 150, 168, 240, 12, 207, 169, 175, 132, 43, 74, 55, 171, 87, 227, 90,
	84, 161, 47, 121, 47, 171, 203, 149, 48, 120, 36, 180, 252, 137, 192, 40, 228, 125, 24, 220, 28, 146, 173, 190, 179, 81, 220, 240, 255, 66, 71, 189,
	252, 27, 145, 129, 134, 23, 251, 131, 147, 87, 248, 237, 10, 106, 141, 58, 0, 87, 176, 177, 148, 167, 149, 33, 123, 228, 222, 206, 7, 2, 182, 112,
	177, 77, 65, 164, 80, 120, 216, 246, 155, 98, 90, 238, 237, 33, 228, 19, 50, 2, 24, 163, 65, 58, 222, 85, 241, 128, 46, 223, 138, 11, 35, 136,
	51, 170, 220, 139, 241, 167, 86, 9, 77, 132, 74, 200, 118, 198, 86, 181, 156, 216, 89, 26, 64, 219, 45, 94, 130, 250, 69, 172, 107, 196, 70, 40,
	159, 14, 174, 6, 196, 170, 183, 55, 247, 41, 84, 152, 227, 112, 67, 252, 190, 221, 51, 74, 175, 212, 254, 18, 23, 62, 29, 242, 128, 15, 85, 137,
	62, 98, 112, 52, 114, 242, 13, 208, 127, 161, 186, 51, 173, 113, 41, 245, 201, 13, 130, 229, 3, 160, 151, 228, 75, 170, 33, 176, 141, 189, 124, 88,
	220, 195, 227, 40, 196, 217, 81, 202, 166, 193, 38, 136, 129, 38, 188, 5, 216, 123, 141, 214, 130, 246, 186, 92, 34, 28, 166, 95, 160, 48, 19, 224,
	160, 212, 137, 112, 143, 182, 244, 99, 103, 252, 119, 72, 178, 115, 133, 141, 9, 146, 98, 237, 199, 255, 122, 144, 165, 118, 147, 35, 50, 63, 127, 12,
	23, 165, 140, 92, 6, 118, 42, 141, 134, 160, 102, 214, 181, 99, 218, 77, 154, 133, 30, 117, 238, 215, 207, 236, 148, 209, 192, 56, 69, 101, 49, 148,
	40, 7, 51, 1, 218, 188, 87, 28, 54, 17, 228, 17, 124, 95, 201, 206, 57, 215, 216, 133, 162, 15, 14, 43, 67, 137, 249, 190, 84, 52, 197, 84,
	87, 195, 50, 97, 93, 85, 82, 116, 33, 243, 112, 41, 135, 57, 34, 30, 212, 110, 27, 43, 201, 142, 201, 12, 236, 105, 248, 112, 121, 201, 89, 64,
	11, 86, 16, 88, 8, 77, 179, 50, 89, 210, 24, 10, 197, 224, 221, 22, 122, 127, 182, 35, 158, 224, 185, 181, 226, 39, 121, 230, 176, 80, 188, 8,
	206, 183, 46, 198, 249, 72, 243, 63, 160, 115, 14, 24, 167, 91, 172, 162, 52, 91, 2, 198, 129, 56, 245, 182, 179, 229, 176, 26, 18, 82, 25, 228,
	83, 241, 54, 85, 96, 66, 200, 157, 182, 48, 232, 139, 122, 158, 16, 239, 67, 103, 100, 225, 121, 28, 221, 158, 191, 234, 125, 9, 215, 80, 167, 138,
	191, 6, 145, 229, 230, 152, 124, 19, 22, 41, 64, 227, 180, 29, 201, 5, 173, 218, 241, 187, 182, 89, 85, 61, 167, 55, 175, 11, 56, 179, 44, 238,
	97, 125, 160, 80, 141, 114, 6, 194, 113, 194, 164, 249, 205, 124, 103, 15, 77, 7, 88, 19, 6, 30, 17, 49, 249, 129, 246, 63, 217, 222, 159, 161,
	187, 170, 33, 198, 206, 8, 110, 170, 218, 189, 235, 181, 141, 120, 51, 190, 133, 44, 109, 150, 11, 105, 154, 241, 249, 200, 223, 116, 154, 66, 209, 240,
	133, 59, 7, 181, 99, 215, 196, 93, 26, 148, 133, 127, 62, 144, 21, 110, 168, 222, 242, 252, 0, 242, 227, 37, 104, 21, 182, 199, 255, 69, 116, 207,
	75, 32, 93, 150, 192, 207, 173, 29, 81, 139, 245, 116, 126, 93, 7, 196, 107, 205, 192, 214, 175, 25, 54, 246, 7, 226, 64, 85, 166, 28, 205, 89,
	124, 90, 133, 18, 112, 81, 30, 36, 170, 37, 229, 174, 118, 213, 86, 143, 75, 242, 176, 48, 245, 229, 13, 26, 103, 162, 49, 26, 223, 254, 59, 212,
	153, 223, 21, 58, 17, 171, 42, 66, 203, 185, 249, 74, 66, 63, 177, 7, 184, 25, 130, 29, 162, 34, 17, 118, 192, 110, 4, 247, 158, 169, 103, 38,
	91, 90, 16, 105, 205, 70, 230, 224, 94, 37, 14, 27, 175, 171, 23, 67, 180, 44, 79, 2, 142, 130, 205, 72, 208, 99, 78, 77, 185, 137, 70, 160,
	180, 227, 110, 134, 147, 134, 237, 79, 186, 42, 140, 41, 127, 158, 226, 188, 136, 18, 252, 50, 10, 52, 109, 79, 220, 227, 192, 180, 14, 70, 143, 181,
	101, 101, 65, 8, 23, 18, 7, 62, 150, 190, 111, 164, 212, 78, 219, 114, 175, 166, 53, 61, 231, 19, 6, 143, 160, 225, 61, 106, 12, 182, 93, 106,
	14, 186, 59, 80, 134, 221, 38, 90, 152, 15, 132, 7, 58, 89, 191, 140, 173, 49, 140, 200, 59, 149, 110, 49, 139, 97, 24, 242, 110, 209, 13, 252,
	219, 120, 40, 160, 73, 44, 81, 203, 207, 247, 83, 45, 34, 171, 140, 143, 102, 238, 163, 139, 254, 28, 65, 230, 59, 94, 187, 238, 39, 240, 95, 113,
	201, 22, 67, 176, 223, 244, 98, 175, 114, 79, 133, 105, 244, 29, 216, 128, 23, 58, 234, 232, 124, 199, 62, 25, 216, 213, 94, 216, 184, 230, 57, 54,
	81, 206, 1, 156, 58, 231, 134, 16, 32, 39, 63, 97, 148, 159, 245, 146, 218, 227, 87, 170, 191, 233, 147, 37, 62, 62, 172, 106, 214, 103, 64, 125,
	20, 46, 25, 237, 169, 55, 249, 130, 52, 163, 12, 253, 63, 27, 173, 215, 182, 166, 179, 120, 6, 234, 171, 130, 50, 150, 248, 159, 64, 178, 168, 105,
	161, 185, 192, 100, 134, 248, 177, 168, 172, 149, 211, 172, 238, 166, 12, 46, 171, 230, 130, 43, 51, 205, 186, 90, 199, 37, 197, 86, 181, 235, 203, 127,
	128, 105, 137, 180, 67, 99, 202, 144, 235, 208, 207, 248, 189, 5, 86, 254, 40, 148, 119, 118, 143, 11, 115, 160, 84, 98, 15, 52, 33, 174, 125, 24,
	228, 27, 14, 25, 2, 211, 156, 118, 216, 227, 117, 232, 54, 101, 49, 166, 48, 199, 44, 3, 36, 167, 166, 13, 146, 227, 179, 141, 79, 95, 241, 249,
	66, 103, 130, 51, 186, 28, 140, 47, 30, 231, 181, 88, 44, 74, 142, 190, 97, 210, 93, 180, 146, 70, 106, 140, 166, 202, 242, 92, 1, 183, 20, 157,
	171, 142, 71, 250, 58, 183, 240, 120, 15, 151, 181, 85, 30, 17, 43, 75, 134, 15, 36, 224, 249, 105, 150, 59, 47, 112, 19, 178, 119, 108, 85, 107,
	115, 155, 56, 103, 226, 209, 223, 26, 200, 46, 220, 197, 73, 210, 56, 186, 12, 221, 75, 70, 19, 114, 10, 75, 154, 43, 62, 131, 90, 235, 152, 12,
	139, 20, 172, 241, 246, 114, 194, 234, 14, 205, 44, 137, 13, 253, 252, 101, 250, 170, 36, 68, 199, 229, 69, 32, 74, 167, 218, 225, 145, 246, 220, 39,
	235, 177, 42, 25, 20, 209, 83, 246, 196, 212, 98, 65, 11, 103, 47, 14, 163, 96, 5, 124, 180, 204, 70, 243, 56, 7, 164, 14, 206, 142, 109, 190,
	151, 95, 102, 81, 112, 134, 6, 159, 141, 7, 42, 58, 42, 79, 163, 2, 84, 15, 190, 39, 45, 122, 219, 199, 45, 115, 191, 92, 235, 232, 107, 222,
	124, 204, 182, 131, 116, 14, 73, 21, 76, 236, 78, 42, 180, 186, 220, 174, 87, 155, 65, 172, 244, 159, 90, 163, 179, 11, 240, 177, 187, 208, 241, 41,
	182, 170, 111, 40, 245, 12, 253, 26, 161, 73, 189, 137, 181, 105, 1, 203, 49, 34, 165, 124, 233, 53, 114, 254, 210, 219, 13, 204, 67, 151, 6, 104,
	61, 205, 252, 123, 13, 206, 13, 64, 117, 237, 230, 202, 114, 58, 32, 49, 163, 203, 202, 169, 49, 242, 135, 18, 145, 96, 113, 103, 35, 185, 153, 44,
	244, 145, 185, 135, 35, 219, 79, 199, 87, 54, 112, 5, 229, 191, 55, 185, 52, 207, 173, 3, 40, 101, 197, 45, 27, 155, 131, 158, 176, 198, 78, 33,
	67, 140, 237, 5, 241, 120, 72, 115, 85, 236, 144, 64, 62, 248, 187, 62, 196, 143, 140, 111, 108, 148, 129, 200, 180, 163, 14, 169, 76, 168, 5, 197,
	94, 178, 251, 150, 226, 29, 22, 144, 241, 208, 173, 12, 171, 124, 36, 182, 231, 128, 185, 139, 95, 117, 129, 238, 65, 100, 162, 45, 3, 79, 188, 91,
	195, 110, 5, 154, 116, 131, 244, 17, 214, 75, 27, 198, 73, 30, 195, 6, 242, 71, 160, 242, 186, 120, 135, 233, 120, 135, 58, 195, 203, 106, 133, 161,
	246, 104, 118, 31, 105, 198, 94, 17, 79, 244, 213, 106, 125, 214, 45, 1, 149, 36, 252, 51, 196, 105, 43, 24, 237, 231, 54, 89, 91, 187, 148, 54,
	72, 156, 6, 213, 111, 217, 111, 235, 250, 179, 249, 138, 203, 23, 9, 156, 190, 254, 67, 119, 102, 191, 204, 53, 153, 2, 75, 97, 248, 199, 206, 163,
	9, 68, 81, 133, 133, 14, 131, 112, 42, 57, 9, 237, 184, 22, 29, 208, 161, 86, 98, 164, 246, 175, 56, 169, 165, 5, 75, 132, 163, 22, 68, 203,
	48, 190, 9, 144, 169, 26, 86, 20, 198, 193, 33, 52, 148, 42, 52, 33, 18, 132, 132, 207, 96, 76, 196, 214, 208, 233, 126, 191, 4, 224, 107, 199,
	14, 33, 228, 16, 200, 36, 198, 191, 149, 147, 80, 186, 44, 58, 86, 79, 13, 47, 122, 32, 37, 31, 218, 182, 178, 236, 120, 154, 84, 36, 176, 207,
	183, 45, 116, 146, 185, 31, 136, 255, 35, 132, 120, 57, 44, 118, 96, 204, 46, 153, 33, 249, 214, 123, 205, 243, 186, 206, 144, 172, 29, 8, 57, 127,
	169, 190, 163, 243, 72, 135, 221, 116, 149, 82, 90, 233, 65, 44, 15, 141, 181, 3, 128, 136, 212, 220, 118, 86, 40, 50, 123, 188, 167, 42, 22, 131,
	94, 251, 85, 103, 206, 140, 49, 75, 14, 48, 176, 5, 112, 63, 110, 122, 255, 209, 105, 64, 41, 39, 126, 176, 122, 110, 108, 122, 165, 39, 243, 36,
	164, 130, 205, 78, 7, 241, 89, 52, 191, 53, 219, 47, 25, 152, 83, 129, 29, 216, 219, 10, 29, 47, 113, 92, 121, 36, 237, 101, 94, 52, 59, 90,
	167, 88, 164, 137, 42, 161, 242, 140, 63, 193, 10, 10, 142, 42, 6, 54, 0, 155, 153, 247, 214, 75, 172, 141, 196, 126, 255, 27, 24, 237, 130, 57,
	56, 156, 59, 137, 102, 194, 206, 20, 100, 240, 67, 154, 32, 42, 245, 4, 70, 17, 38, 24, 49, 152, 7, 22, 128, 171, 15, 225, 226, 89, 140, 0,
	22, 25, 155, 181, 101, 164, 198, 143, 161, 126, 135, 202, 22, 205, 87, 120, 149, 218, 197, 127, 200, 13, 191, 172, 192, 47, 42, 219, 7, 5, 184, 162,
	183, 130, 193, 113, 26, 126, 136, 72, 195, 81, 54, 238, 92, 243, 114, 9, 219, 226, 51, 2, 210, 226, 184, 166, 105, 57, 172, 152, 175, 139, 47, 108,
}

What did you expect to see?

146 254 150 46 781647506
[781647506 90017834 229057582 1815611976 2144217968 3215259429 561416431 453949835 3249503260 907469206 2761610610 2725660314 3531788685 4235637548 1878190625 2139520666 2258883274 2657554353 1728402439 1045462402 270600598 2714077482 2947141388 1524848555 2513152815 683706876 3199046172 3175564031 2214270854 982346250 563455892 1890976263 4141381712 333717997 1440627265 2283998090 156674033 3042362998 1580063552 675726443 934783684 4232278243 318690479 2304053120 3490574962 4113133997 3835142147 1484569997 3394361796 96216705 1555756674 3759354016 1676981903 2374333362 2423979975 209665842 2368370182 1306158005 3973044206 2486265157 475512026 3469303676 722341794 1422210132 1951552861 505559431 214535881 1079626105 850611464 383639749 3048857758 146559152 1072908537 2729204647 3069524097 3826864658 2647147104 4010843770 2665290873 2326221015 326932710 97066420 1029003702 3995906872 3255202445 258440397 823205382 2711609049 2859337934 3191044237 4053428491 4040245914 1573181283 1846906942 635695616 3480503807 497930176 3288817022 4130740655 1506614438 605966704 2404832630 437118453 3560701663 1110092561 129056578 1980834466 644327838 3773187789 1125624751 1221427854 2688977337 1340966547 3168968319 1332556810 3046065678 1040650775 1926975188 2399540199 1784526348 1512496518 2361350458 829330747 4228764014 3411094601 2408360738 3863026942 1902112807 2942498015 2161647092 423544700 909764280 277276474 2465570708 630450623 2101372886 2197370793 3618446143 2192304646 1772663360 2830235782 772581102 1522191667 2144070581 2429182787 4267050429 2691894159 410889761 1989989122 2788255030 229025572 4193345359 797711546 3196996140 2355775122 2635380481 2029041466 1261113630 999713273 1800760439 450875874 3124286025 1258975763 211348314 3938611958 1711078669 541451719 668792465 4132688148 237987595 4081503412 3194851022 2668004976 44257066 3353049645 3731613931 357109364 2933701300 2740625396 703713467 452791541 3405867445 4268897769 1745262403 1074646541 824195698 310899249 748271907 3343899427 3107438565 767911208 558810800 1934129393 1052506174 3363935340 3305482316 2417368546 3055844523 4001461599 1539067651 301237108 113450569 3917969594 2709875403 291423849 19781245 405498308 915716955 3949975919 2617841611 902610790 2748237816 1887637125 3491567288 2839064566 3410237091 341187241 557066900 3603188832 3345735684 3217433800 1331051052 3067748133 3484427348 4287111097 3428873772 4090330070 2134444061 1960675144 2366581825 1450630356 2199268007 1261538510 2054045552 2961057577 619915173 878309639 2169739289 1550921501 1513829470 2364711210 906373774 2376879062 964881688 349094502 83175968 369596465 9198050 2412160101 2019020054 2898202056 2729968903 1216904730 158528348 2797134546 1815055279]
[781647506 90017834 229057582 1815611976 2144217968 3215259429 561416431 453949835 3249503260 907469206 2761610610 2725660314 3531788685 4235637548 1878190625 2139520666 2258883274 2657554353 1728402439 1045462402 270600598 2714077482 2947141388 1524848555 2513152815 683706876 3199046172 3175564031 2214270854 982346250 563455892 1890976263 4141381712 333717997 1440627265 2283998090 156674033 3042362998 1580063552 675726443 934783684 4232278243 318690479 2304053120 3490574962 4113133997 3835142147 1484569997 3394361796 96216705 1555756674 3759354016 1676981903 2374333362 2423979975 209665842 2368370182 1306158005 3973044206 2486265157 475512026 3469303676 722341794 1422210132 1951552861 505559431 214535881 1079626105 850611464 383639749 3048857758 146559152 1072908537 2729204647 3069524097 3826864658 2647147104 4010843770 2665290873 2326221015 326932710 97066420 1029003702 3995906872 3255202445 258440397 823205382 2711609049 2859337934 3191044237 4053428491 4040245914 1573181283 1846906942 635695616 3480503807 497930176 3288817022 4130740655 1506614438 605966704 2404832630 437118453 3560701663 1110092561 129056578 1980834466 644327838 3773187789 1125624751 1221427854 2688977337 1340966547 3168968319 1332556810 3046065678 1040650775 1926975188 2399540199 1784526348 1512496518 2361350458 829330747 4228764014 3411094601 2408360738 3863026942 1902112807 2942498015 2161647092 423544700 909764280 277276474 2465570708 630450623 2101372886 2197370793 3618446143 2192304646 1772663360 2830235782 772581102 1522191667 2144070581 2429182787 4267050429 2691894159 410889761 1989989122 2788255030 229025572 4193345359 797711546 3196996140 2355775122 2635380481 2029041466 1261113630 999713273 1800760439 450875874 3124286025 1258975763 211348314 3938611958 1711078669 541451719 668792465 4132688148 237987595 4081503412 3194851022 2668004976 44257066 3353049645 3731613931 357109364 2933701300 2740625396 703713467 452791541 3405867445 4268897769 1745262403 1074646541 824195698 310899249 748271907 3343899427 3107438565 767911208 558810800 1934129393 1052506174 3363935340 3305482316 2417368546 3055844523 4001461599 1539067651 301237108 113450569 3917969594 2709875403 291423849 19781245 405498308 915716955 3949975919 2617841611 902610790 2748237816 1887637125 3491567288 2839064566 3410237091 341187241 557066900 3603188832 3345735684 3217433800 1331051052 3067748133 3484427348 4287111097 3428873772 4090330070 2134444061 1960675144 2366581825 1450630356 2199268007 1261538510 2054045552 2961057577 619915173 878309639 2169739289 1550921501 1513829470 2364711210 906373774 2376879062 964881688 349094502 83175968 369596465 9198050 2412160101 2019020054 2898202056 2729968903 1216904730 158528348 2797134546 1815055279]
[781647506 90017834 229057582 1815611976 2144217968 3215259429 561416431 453949835 3249503260 907469206 2761610610 2725660314 3531788685 4235637548 1878190625 2139520666 2258883274 2657554353 1728402439 1045462402 270600598 2714077482 2947141388 1524848555 2513152815 683706876 3199046172 3175564031 2214270854 982346250 563455892 1890976263 4141381712 333717997 1440627265 2283998090 156674033 3042362998 1580063552 675726443 934783684 4232278243 318690479 2304053120 3490574962 4113133997 3835142147 1484569997 3394361796 96216705 1555756674 3759354016 1676981903 2374333362 2423979975 209665842 2368370182 1306158005 3973044206 2486265157 475512026 3469303676 722341794 1422210132 1951552861 505559431 214535881 1079626105 850611464 383639749 3048857758 146559152 1072908537 2729204647 3069524097 3826864658 2647147104 4010843770 2665290873 2326221015 326932710 97066420 1029003702 3995906872 3255202445 258440397 823205382 2711609049 2859337934 3191044237 4053428491 4040245914 1573181283 1846906942 635695616 3480503807 497930176 3288817022 4130740655 1506614438 605966704 2404832630 437118453 3560701663 1110092561 129056578 1980834466 644327838 3773187789 1125624751 1221427854 2688977337 1340966547 3168968319 1332556810 3046065678 1040650775 1926975188 2399540199 1784526348 1512496518 2361350458 829330747 4228764014 3411094601 2408360738 3863026942 1902112807 2942498015 2161647092 423544700 909764280 277276474 2465570708 630450623 2101372886 2197370793 3618446143 2192304646 1772663360 2830235782 772581102 1522191667 2144070581 2429182787 4267050429 2691894159 410889761 1989989122 2788255030 229025572 4193345359 797711546 3196996140 2355775122 2635380481 2029041466 1261113630 999713273 1800760439 450875874 3124286025 1258975763 211348314 3938611958 1711078669 541451719 668792465 4132688148 237987595 4081503412 3194851022 2668004976 44257066 3353049645 3731613931 357109364 2933701300 2740625396 703713467 452791541 3405867445 4268897769 1745262403 1074646541 824195698 310899249 748271907 3343899427 3107438565 767911208 558810800 1934129393 1052506174 3363935340 3305482316 2417368546 3055844523 4001461599 1539067651 301237108 113450569 3917969594 2709875403 291423849 19781245 405498308 915716955 3949975919 2617841611 902610790 2748237816 1887637125 3491567288 2839064566 3410237091 341187241 557066900 3603188832 3345735684 3217433800 1331051052 3067748133 3484427348 4287111097 3428873772 4090330070 2134444061 1960675144 2366581825 1450630356 2199268007 1261538510 2054045552 2961057577 619915173 878309639 2169739289 1550921501 1513829470 2364711210 906373774 2376879062 964881688 349094502 83175968 369596465 9198050 2412160101 2019020054 2898202056 2729968903 1216904730 158528348 2797134546 1815055279]

What did you see instead?

146 254 150 46 781647506
[781647506 90017834 229057582 1815611976 2144217968 3215259429 561416431 453949835 3249503260 907469206 2761610610 2725660314 3531788685 4235637548 1878190625 2139520666 2258883274 2657554353 1728402439 1045462402 270600598 2714077482 2947141388 1524848555 2513152815 683706876 3199046172 3175564031 2214270854 982346250 563455892 1890976263 4141381712 333717997 1440627265 2283998090 156674033 3042362998 1580063552 675726443 934783684 4232278243 318690479 2304053120 3490574962 4113133997 3835142147 1484569997 3394361796 96216705 1555756674 3759354016 1676981903 2374333362 2423979975 209665842 2368370182 1306158005 3973044206 2486265157 475512026 3469303676 722341794 1422210132 1951552861 505559431 214535881 1079626105 850611464 383639749 3048857758 146559152 1072908537 2729204647 3069524097 3826864658 2647147104 4010843770 2665290873 2326221015 326932710 97066420 1029003702 3995906872 3255202445 258440397 823205382 2711609049 2859337934 3191044237 4053428491 4040245914 1573181283 1846906942 635695616 3480503807 497930176 3288817022 4130740655 1506614438 605966704 2404832630 437118453 3560701663 1110092561 129056578 1980834466 644327838 3773187789 1125624751 1221427854 2688977337 1340966547 3168968319 1332556810 3046065678 1040650775 1926975188 2399540199 1784526348 1512496518 2361350458 829330747 4228764014 3411094601 2408360738 3863026942 1902112807 2942498015 2161647092 423544700 909764280 277276474 2465570708 630450623 2101372886 2197370793 3618446143 2192304646 1772663360 2830235782 772581102 1522191667 2144070581 2429182787 4267050429 2691894159 410889761 1989989122 2788255030 229025572 4193345359 797711546 3196996140 2355775122 2635380481 2029041466 1261113630 999713273 1800760439 450875874 3124286025 1258975763 211348314 3938611958 1711078669 541451719 668792465 4132688148 237987595 4081503412 3194851022 2668004976 44257066 3353049645 3731613931 357109364 2933701300 2740625396 703713467 452791541 3405867445 4268897769 1745262403 1074646541 824195698 310899249 748271907 3343899427 3107438565 767911208 558810800 1934129393 1052506174 3363935340 3305482316 2417368546 3055844523 4001461599 1539067651 301237108 113450569 3917969594 2709875403 291423849 19781245 405498308 915716955 3949975919 2617841611 902610790 2748237816 1887637125 3491567288 2839064566 3410237091 341187241 557066900 3603188832 3345735684 3217433800 1331051052 3067748133 3484427348 4287111097 3428873772 4090330070 2134444061 1960675144 2366581825 1450630356 2199268007 1261538510 2054045552 2961057577 619915173 878309639 2169739289 1550921501 1513829470 2364711210 906373774 2376879062 964881688 349094502 83175968 369596465 9198050 2412160101 2019020054 2898202056 2729968903 1216904730 158528348 2797134546 1815055279]
[781582336 90046098 229085226 1815618606 2144209480 3215207280 561444645 453937391 3249518987 907443228 2761613718 2725696882 3531753114 4235645325 1878176556 2139548193 2258861722 2657604298 1728384945 1045451783 270630274 2714044822 2947123498 1524879116 2513131435 683715375 3199044092 3175584284 2214281983 982325126 563440138 1891018644 4141351431 333740112 1440621037 2284010049 156633994 3042355185 1580058230 675732288 934790251 4232293060 318664931 2304103599 3490516864 4113166962 3835130285 1484562435 3394354573 96262596 1555703425 3759404674 1676947616 2374350479 2423944114 209715143 2368356146 1306162694 3973014453 2486294510 475489605 3469327578 722362236 1422200738 1951544404 505566557 214514055 1079611081 850643321 383601928 3048857797 146595998 1072910512 2729199865 3069533095 3826858113 2647151122 4010820192 2665324154 2326207609 326914263 97097958 1028988340 3995883958 3255219000 258437773 823229645 2711559686 2859392729 3191015630 4053432461 4040255755 1573143194 1846925155 635670590 3480547840 497894911 3288846272 4130758014 1506613679 605953190 2404798832 437114230 3560695285 1110114015 129084177 1980841794 644293282 3773213086 1125598925 1221438383 2688975502 1340967353 3168962195 1332584063 3046061066 1040664078 1926959639 2399555284 1784484839 1512486412 2361384326 829315386 4228748603 3411136878 2408328265 3863063330 1902058750 2942496807 2161702111 423501300 909756284 277276344 2465589050 630431636 2101406143 2197383126 3618453417 2192251711 1772677638 2830217792 772601990 1522181870 2144062771 2429217717 4267074371 2691892669 410848143 1989979681 2788283138 229008694 4193363748 797728591 3196984506 2355776044 2635351698 2029041409 1261156154 999690526 1800759801 450849911 3124285922 1259000393 211317267 3938642778 1711043318 541457677 668788167 4132697745 238014740 4081477387 3194866868 2668007118 44271216 3353038634 3731585581 357165291 2933657204 2740632244 703700980 452841659 3405843701 4268911029 1745237481 1074632515 824233485 310852210 748286513 3343890723 3107445539 767934437 558785832 1934149296 1052473585 3363960894 3305477228 2417403980 3055820258 4001463467 1539077471 301223683 113476468 3917946441 2709878970 291400395 19777129 405526141 915696068 3949968219 2617891183 902567883 2748235622 1887684600 3491565189 2839025336 3410276342 341186211 557062825 3603180180 3345697888 3217481732 1331045576 3067755052 3484426021 4287112276 3428851641 4090328620 2134473686 1960642589 2366605128 1450585153 2199313620 1261513383 2054065358 2961063792 619915049 878258085 2169762055 1550948377 1513828125 2364683358 906404138 2376870542 964840406 349105432 83214950 369568288 9214001 2412141026 2019009637 2898251030 2729971144 1216873735 158498330 2797138780 1815077586]
[781647506 90017834 229057582 1815611976 2144217968 3215259429 561416431 453949835 3249503260 907469206 2761610610 2725660314 3531788685 4235637548 1878190625 2139520666 2258883274 2657554353 1728402439 1045462402 270600598 2714077482 2947141388 1524848555 2513152815 683706876 3199046172 3175564031 2214270854 982346250 563455892 1890976263 4141381712 333717997 1440627265 2283998090 156674033 3042362998 1580063552 675726443 934783684 4232278243 318690479 2304053120 3490574962 4113133997 3835142147 1484569997 3394361796 96216705 1555756674 3759354016 1676981903 2374333362 2423979975 209665842 2368370182 1306158005 3973044206 2486265157 475512026 3469303676 722341794 1422210132 1951552861 505559431 214535881 1079626105 850611464 383639749 3048857758 146559152 1072908537 2729204647 3069524097 3826864658 2647147104 4010843770 2665290873 2326221015 326932710 97066420 1029003702 3995906872 3255202445 258440397 823205382 2711609049 2859337934 3191044237 4053428491 4040245914 1573181283 1846906942 635695616 3480503807 497930176 3288817022 4130740655 1506614438 605966704 2404832630 437118453 3560701663 1110092561 129056578 1980834466 644327838 3773187789 1125624751 1221427854 2688977337 1340966547 3168968319 1332556810 3046065678 1040650775 1926975188 2399540199 1784526348 1512496518 2361350458 829330747 4228764014 3411094601 2408360738 3863026942 1902112807 2942498015 2161647092 423544700 909764280 277276474 2465570708 630450623 2101372886 2197370793 3618446143 2192304646 1772663360 2830235782 772581102 1522191667 2144070581 2429182787 4267050429 2691894159 410889761 1989989122 2788255030 229025572 4193345359 797711546 3196996140 2355775122 2635380481 2029041466 1261113630 999713273 1800760439 450875874 3124286025 1258975763 211348314 3938611958 1711078669 541451719 668792465 4132688148 237987595 4081503412 3194851022 2668004976 44257066 3353049645 3731613931 357109364 2933701300 2740625396 703713467 452791541 3405867445 4268897769 1745262403 1074646541 824195698 310899249 748271907 3343899427 3107438565 767911208 558810800 1934129393 1052506174 3363935340 3305482316 2417368546 3055844523 4001461599 1539067651 301237108 113450569 3917969594 2709875403 291423849 19781245 405498308 915716955 3949975919 2617841611 902610790 2748237816 1887637125 3491567288 2839064566 3410237091 341187241 557066900 3603188832 3345735684 3217433800 1331051052 3067748133 3484427348 4287111097 3428873772 4090330070 2134444061 1960675144 2366581825 1450630356 2199268007 1261538510 2054045552 2961057577 619915173 878309639 2169739289 1550921501 1513829470 2364711210 906373774 2376879062 964881688 349094502 83175968 369596465 9198050 2412160101 2019020054 2898202056 2729968903 1216904730 158528348 2797134546 1815055279]
@odeke-em
Copy link
Member

Hello @chanxuehong, thank you for this report!

In order to help isolate the bug, I have made a simple modification to show where the problem is
https://play.golang.org/p/ukK4MABB-Zc which will instead print out checksums so

146 254 150 46 781647506
75f7453db3643a46136d82e46c82f80c
1ef94ebae8013e871c555d8ec5d89ab6
75f7453db3643a46136d82e46c82f80c

on systems that provide inlining and then

146 254 150 46 781647506
75f7453db3643a46136d82e46c82f80c
75f7453db3643a46136d82e46c82f80c
75f7453db3643a46136d82e46c82f80c

on systems such the Go playground and when we turn off inlining with -gcflags='-l'

The issue seems to be in

func readLittleEndian32_2(a, b, c, d byte) uint32 {
	return uint32(a) | (uint32(b) << 8) | (uint32(c) << 16) | (uint32(d) << 24)
}

where there we are inlining. I believe in readLittleEndian32_1 you added the extra if !x check to ensure it wasn't inlined

Here is a minimal repro

package main

import (
	"fmt"
)

var foo = []byte{105, 57, 172, 152, 175, 139, 47, 108}

func main() {
	for i := 0; i < len(foo); i += 4 {
		fmt.Println(readLittleEndian32_1(foo[i], foo[i+1], foo[i+2], foo[i+3]))
		fmt.Println(readLittleEndian32_2(foo[i], foo[i+1], foo[i+2], foo[i+3]))
		fmt.Println(readLittleEndian32_3(foo[i], foo[i+1], foo[i+2], foo[i+3]))
                fmt.Println("\n\n")
	}
}

var x = false

func readLittleEndian32_1(a, b, c, d byte) uint32 {
	r := uint32(a) | (uint32(b) << 8) | (uint32(c) << 16) | (uint32(d) << 24)
	if !x {
		x = true
		fmt.Println(a, b, c, d, r)
	}
	return r
}

func readLittleEndian32_2(a, b, c, d byte) uint32 {
	return uint32(a) | (uint32(b) << 8) | (uint32(c) << 16) | (uint32(d) << 24)
}

func readLittleEndian32_3(a, b, c, d byte) uint32 {
	return (uint32(a) & 0xFF) | ((uint32(b) << 8) & 0xFF00) | ((uint32(c) << 16) & 0xFF0000) | ((uint32(d) << 24) & 0xFF000000)
}

which prints

105 57 172 152 2561423721
2561423721
2561409216
2561423721

1815055279
1815034217
1815055279

and as we can see readLittleEndian32_2 is trippin'.

Kindly paging some compiler/math experts @randall77 @josharian @dr2chase @rasky

@odeke-em
Copy link
Member

odeke-em commented Jun 19, 2019

Seems like this got introduced in https://go-review.googlesource.com/39310 aka git commit 9d5987d

Apologies for the false page @dr2chase @rsc: it was a manual edit from me that falsely produced the bug, and moreover at 1AM so my mind was a little fuzzy

@randall77 seems like https://go-review.googlesource.com/38801 aka 53f8a6a#diff-e5f79fbc16c194021eb0476790c8a606, the commit before it doesn't have the problem, the one after has it, please help me confirm.

Go1.8 and below are safe, from Go1.9 and beyond have this bug thus a couple of backports shall be needed.

@odeke-em odeke-em changed the title Wrong calculation result for bit operation in go1.12.6 cmd/compile: wrong calculation result for bit operation that's inlined and has all constant shifts in rewrite rules Jun 19, 2019
@odeke-em odeke-em added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. NeedsFix The path to resolution is known, but the work has not been done. labels Jun 19, 2019
@gopherbot gopherbot removed the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jun 19, 2019
@dr2chase
Copy link
Contributor

A variant of the test program causes a complier crash:, in both at least go1.11 and tip:

package main

import (
	"fmt"
)

var foo = []byte{105, 57, 172, 152, 175, 139, 47, 108}
var i = 0

func main() {
	fmt.Printf("0x%x\n", readLittleEndian32_2(foo[i], foo[i+1], foo[i+2], foo[i+3]))
}

func readLittleEndian32_2(a, b, c, d byte) uint32 {
	return uint32(a) | (uint32(b) << 8) | (uint32(c) << 16) | (uint32(d) << 24)
}

Also crashes in the playground: https://play.golang.org/p/MzGNDnB7hDF

@dr2chase
Copy link
Contributor

FYI, fix is coming, won't be ready for submit till tomorrow AM (needs a test), will set it up for review tonight.

@gopherbot
Copy link

Change https://golang.org/cl/183059 mentions this issue: cmd/compile: add necessary operand to mergePoint in rewrite rules

@gopherbot
Copy link

Change https://golang.org/cl/183239 mentions this issue: cmd/compile: debug rewrite, enhanced

@gopherbot
Copy link

Change https://golang.org/cl/183240 mentions this issue: cmd/compile: rearrange creation of ssa.Func to allow defer-cleanup

@dr2chase
Copy link
Contributor

@gopherbot please open backport issues. It technically fails the regression policy (been broken too long -- probably since 1.9) but it is a silent bad code problem which is very bad, and the fix is very low risk.

@gopherbot
Copy link

Backport issue(s) opened: #32711 (for 1.11), #32712 (for 1.12).

Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://golang.org/wiki/MinorReleases.

@gopherbot
Copy link

Change https://golang.org/cl/183241 mentions this issue: [release-branch.go1.12] cmd/compile: add necessary operand to mergePoint in rewrite rules

@gopherbot
Copy link

Change https://golang.org/cl/183242 mentions this issue: [release-branch.go1.11] cmd/compile: add necessary operand to mergePoint in rewrite rules

@odeke-em
Copy link
Member

Thank you for the fix @dr2chase! So since this bug goes ways back to Go1.9, is there a way for us to backport the fix to those versions of Go too? Kindly paging @golang/proposal-review

@josharian
Copy link
Contributor

I can answer that one. :) We won’t backport past the last two releases, regardless of how long the bug has existed.

@odeke-em
Copy link
Member

Gotcha, thank you for the answer @josharian!

@shanemhansen
Copy link
Contributor

This appears to have fixed a bug I've been chasing for a couple months (as I attempted to learn more about SSA to fix). Would this crasher be a helpful addition?

// errorcheck
package p

func hashBytesRaw(b0, b1, b2, b3, b7 byte) uint64 {
        return (uint64(b0) | uint64(b1)<<8 | uint64(b2)<<16 | uint64(b3)<<24 )
}

func doStuff(data []byte) uint64 {
        return hashBytesRaw(data[0], data[1], data[2], data[3], data[7]) // ERROR ".*runtime error: index out of range.*"

}

@randall77
Copy link
Contributor

@shanemhansen: Thanks, I'll add that as a test.

@gopherbot
Copy link

Change https://golang.org/cl/185137 mentions this issue: test: add another test for issue 32680

gopherbot pushed a commit that referenced this issue Jul 5, 2019
Update #32680

Change-Id: I0318c22c22c5cd6ab6441d1aa2d1a40d20d71242
Reviewed-on: https://go-review.googlesource.com/c/go/+/185137
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Reviewed-by: David Chase <drchase@google.com>
gopherbot pushed a commit that referenced this issue Jul 8, 2019
…int in rewrite rules

A missing operand to mergePoint caused lower to place values
in the wrong blocks.

Includes test, belt+suspenders to do both ssa check and verify
the output (was is how the bug was originally observed).

The fixed bug here is very likely present in Go versions
1.9-1.12 on amd64 and s390x

Updates #32680.
Fixes #32711.

Change-Id: I63e702c4c40602cb795ef71b1691eb704d38ccc7
Reviewed-on: https://go-review.googlesource.com/c/go/+/183059
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
(cherry picked from commit 769fda2)
Reviewed-on: https://go-review.googlesource.com/c/go/+/183242
gopherbot pushed a commit that referenced this issue Jul 8, 2019
…int in rewrite rules

A missing operand to mergePoint caused lower to place values
in the wrong blocks.

Includes test, belt+suspenders to do both ssa check and verify
the output (was is how the bug was originally observed).

The fixed bug here is very likely present in Go versions
1.9-1.12 on amd64 and s390x

Updates #32680.
Fixes #32712.

Change-Id: I63e702c4c40602cb795ef71b1691eb704d38ccc7
Reviewed-on: https://go-review.googlesource.com/c/go/+/183059
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
(cherry picked from commit 769fda2)
Reviewed-on: https://go-review.googlesource.com/c/go/+/183241
@golang golang locked and limited conversation to collaborators Jul 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. release-blocker
Projects
None yet
Development

No branches or pull requests

8 participants