]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix slice-in-bound check on amd64p32
authorCherry Zhang <cherryyz@google.com>
Tue, 27 Jun 2017 20:34:06 +0000 (16:34 -0400)
committerCherry Zhang <cherryyz@google.com>
Wed, 28 Jun 2017 16:20:53 +0000 (16:20 +0000)
Should use CMPL instead of CMPQ.

Fixes #20811.

Change-Id: I610d487949c2c8a08b3743656149069d931a51bb
Reviewed-on: https://go-review.googlesource.com/46870
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/compile/internal/ssa/gen/AMD64.rules
src/cmd/compile/internal/ssa/rewriteAMD64.go
test/fixedbugs/issue20811.go [new file with mode: 0644]

index 5887a9454cf25e4a23b3d8ca222459a168becb51..1900f5e794fbfe547e7951aca69c6a7a19d662b8 100644 (file)
 (Convert <t> x mem) && config.PtrSize == 4 -> (MOVLconvert <t> x mem)
 (IsNonNil p) && config.PtrSize == 8 -> (SETNE (TESTQ p p))
 (IsNonNil p) && config.PtrSize == 4 -> (SETNE (TESTL p p))
-(IsInBounds idx len) -> (SETB (CMPQ idx len))
-(IsSliceInBounds idx len) -> (SETBE (CMPQ idx len))
+(IsInBounds idx len) && config.PtrSize == 8 -> (SETB (CMPQ idx len))
+(IsInBounds idx len) && config.PtrSize == 4 -> (SETB (CMPL idx len))
+(IsSliceInBounds idx len) && config.PtrSize == 8 -> (SETBE (CMPQ idx len))
+(IsSliceInBounds idx len) && config.PtrSize == 4 -> (SETBE (CMPL idx len))
 (NilCheck ptr mem) -> (LoweredNilCheck ptr mem)
 (GetG mem) -> (LoweredGetG mem)
 (GetClosurePtr) -> (LoweredGetClosurePtr)
index 641dc11a1735e4a2deb44c8df7d23fab2413e3f7..f9a94cac3699142ad1ed9a61b010288e256041f8 100644 (file)
@@ -39253,13 +39253,18 @@ func rewriteValueAMD64_OpInterCall_0(v *Value) bool {
 func rewriteValueAMD64_OpIsInBounds_0(v *Value) bool {
        b := v.Block
        _ = b
+       config := b.Func.Config
+       _ = config
        // match: (IsInBounds idx len)
-       // cond:
+       // cond: config.PtrSize == 8
        // result: (SETB (CMPQ idx len))
        for {
                _ = v.Args[1]
                idx := v.Args[0]
                len := v.Args[1]
+               if !(config.PtrSize == 8) {
+                       break
+               }
                v.reset(OpAMD64SETB)
                v0 := b.NewValue0(v.Pos, OpAMD64CMPQ, types.TypeFlags)
                v0.AddArg(idx)
@@ -39267,6 +39272,24 @@ func rewriteValueAMD64_OpIsInBounds_0(v *Value) bool {
                v.AddArg(v0)
                return true
        }
+       // match: (IsInBounds idx len)
+       // cond: config.PtrSize == 4
+       // result: (SETB (CMPL idx len))
+       for {
+               _ = v.Args[1]
+               idx := v.Args[0]
+               len := v.Args[1]
+               if !(config.PtrSize == 4) {
+                       break
+               }
+               v.reset(OpAMD64SETB)
+               v0 := b.NewValue0(v.Pos, OpAMD64CMPL, types.TypeFlags)
+               v0.AddArg(idx)
+               v0.AddArg(len)
+               v.AddArg(v0)
+               return true
+       }
+       return false
 }
 func rewriteValueAMD64_OpIsNonNil_0(v *Value) bool {
        b := v.Block
@@ -39308,13 +39331,18 @@ func rewriteValueAMD64_OpIsNonNil_0(v *Value) bool {
 func rewriteValueAMD64_OpIsSliceInBounds_0(v *Value) bool {
        b := v.Block
        _ = b
+       config := b.Func.Config
+       _ = config
        // match: (IsSliceInBounds idx len)
-       // cond:
+       // cond: config.PtrSize == 8
        // result: (SETBE (CMPQ idx len))
        for {
                _ = v.Args[1]
                idx := v.Args[0]
                len := v.Args[1]
+               if !(config.PtrSize == 8) {
+                       break
+               }
                v.reset(OpAMD64SETBE)
                v0 := b.NewValue0(v.Pos, OpAMD64CMPQ, types.TypeFlags)
                v0.AddArg(idx)
@@ -39322,6 +39350,24 @@ func rewriteValueAMD64_OpIsSliceInBounds_0(v *Value) bool {
                v.AddArg(v0)
                return true
        }
+       // match: (IsSliceInBounds idx len)
+       // cond: config.PtrSize == 4
+       // result: (SETBE (CMPL idx len))
+       for {
+               _ = v.Args[1]
+               idx := v.Args[0]
+               len := v.Args[1]
+               if !(config.PtrSize == 4) {
+                       break
+               }
+               v.reset(OpAMD64SETBE)
+               v0 := b.NewValue0(v.Pos, OpAMD64CMPL, types.TypeFlags)
+               v0.AddArg(idx)
+               v0.AddArg(len)
+               v.AddArg(v0)
+               return true
+       }
+       return false
 }
 func rewriteValueAMD64_OpLeq16_0(v *Value) bool {
        b := v.Block
diff --git a/test/fixedbugs/issue20811.go b/test/fixedbugs/issue20811.go
new file mode 100644 (file)
index 0000000..96b61ec
--- /dev/null
@@ -0,0 +1,23 @@
+// run
+
+// Copyright 2017 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.
+
+// Issue 20811: slice-in-bound check is lowered incorrectly on
+// amd64p32.
+
+package main
+
+func main() {
+       i := g()
+       _ = "x"[int32(i)]
+       j := g()
+       _ = "x"[:int32(j)]
+}
+
+//go:noinline
+func g() int64 {
+       return 4398046511104
+}
+