[go] cmd/compile: restrict use of unsafe.{Add,Slice} to go1.17 or newer

521 views
Skip to first unread message

Matthew Dempsky (Gerrit)

unread,
Jun 2, 2021, 3:32:58 PM6/2/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Matthew Dempsky has uploaded this change for review.

View Change

cmd/compile: restrict use of unsafe.{Add,Slice} to go1.17 or newer

This CL only fixes the legacy typechecker, because it's the only one
applicable to Go 1.17: go/types doesn't have a knob for controlling
supported language version, and types2 integration isn't complete in
Go 1.17 anyway. Instead, it's added to types2's known-failures list to
be addressed for Go 1.18.

Fixes #46525.

Change-Id: I1bfe025a672d9f4b929f443064ad1effd38d0363
---
M src/cmd/compile/internal/typecheck/func.go
A test/fixedbugs/issue46525.go
M test/run.go
3 files changed, 21 insertions(+), 0 deletions(-)

diff --git a/src/cmd/compile/internal/typecheck/func.go b/src/cmd/compile/internal/typecheck/func.go
index f381e1d..9fa9537 100644
--- a/src/cmd/compile/internal/typecheck/func.go
+++ b/src/cmd/compile/internal/typecheck/func.go
@@ -981,6 +981,9 @@

// tcUnsafeAdd typechecks an OUNSAFEADD node.
func tcUnsafeAdd(n *ir.BinaryExpr) *ir.BinaryExpr {
+ if !types.AllowsGoVersion(curpkg(), 1, 17) {
+ base.ErrorfVers("go1.17", "unsafe.Add")
+ }
n.X = AssignConv(Expr(n.X), types.Types[types.TUNSAFEPTR], "argument to unsafe.Add")
n.Y = DefaultLit(Expr(n.Y), types.Types[types.TINT])
if n.X.Type() == nil || n.Y.Type() == nil {
@@ -997,6 +1000,9 @@

// tcUnsafeSlice typechecks an OUNSAFESLICE node.
func tcUnsafeSlice(n *ir.BinaryExpr) *ir.BinaryExpr {
+ if !types.AllowsGoVersion(curpkg(), 1, 17) {
+ base.ErrorfVers("go1.17", "unsafe.Slice")
+ }
n.X = Expr(n.X)
n.Y = Expr(n.Y)
if n.X.Type() == nil || n.Y.Type() == nil {
diff --git a/test/fixedbugs/issue46525.go b/test/fixedbugs/issue46525.go
new file mode 100644
index 0000000..164e147
--- /dev/null
+++ b/test/fixedbugs/issue46525.go
@@ -0,0 +1,14 @@
+// errorcheck -lang=go1.16
+
+// Copyright 2021 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 p
+
+import "unsafe"
+
+func main() {
+ _ = unsafe.Add(unsafe.Pointer(nil), 0) // ERROR "unsafe.Add requires go1.17 or later"
+ _ = unsafe.Slice(new(byte), 1) // ERROR "unsafe.Slice requires go1.17 or later"
+}
diff --git a/test/run.go b/test/run.go
index 5e60de7..cc82bd7 100644
--- a/test/run.go
+++ b/test/run.go
@@ -1995,6 +1995,7 @@
"fixedbugs/issue42058b.go": true, // types2 doesn't report "channel element type too large"
"fixedbugs/issue4232.go": true, // types2 reports (correct) extra errors
"fixedbugs/issue4452.go": true, // types2 reports (correct) extra errors
+ "fixedbugs/issue46525.go": true, // types2 doesn't check language version for unsafe.{Add,Slice}
"fixedbugs/issue5609.go": true, // types2 needs a better error message
"fixedbugs/issue6889.go": true, // types2 can handle this without constant overflow
"fixedbugs/issue7525.go": true, // types2 reports init cycle error on different line - ok otherwise

To view, visit change 324369. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I1bfe025a672d9f4b929f443064ad1effd38d0363
Gerrit-Change-Number: 324369
Gerrit-PatchSet: 1
Gerrit-Owner: Matthew Dempsky <mdem...@google.com>
Gerrit-Reviewer: Matthew Dempsky <mdem...@google.com>
Gerrit-MessageType: newchange

Matthew Dempsky (Gerrit)

unread,
Jun 2, 2021, 3:49:11 PM6/2/21
to goph...@pubsubhelper.golang.org, Ian Lance Taylor, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Ian Lance Taylor.

View Change

1 comment:

  • Patchset:

    • Patch Set #1:

      Evidently go/types has language version support in Go 1.17, just not exposed. I'll fix that and update the CL shortly.

To view, visit change 324369. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I1bfe025a672d9f4b929f443064ad1effd38d0363
Gerrit-Change-Number: 324369
Gerrit-PatchSet: 1
Gerrit-Owner: Matthew Dempsky <mdem...@google.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Matthew Dempsky <mdem...@google.com>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Comment-Date: Wed, 02 Jun 2021 19:49:06 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

Matthew Dempsky (Gerrit)

unread,
Jun 2, 2021, 4:58:16 PM6/2/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Ian Lance Taylor, Matthew Dempsky.

Matthew Dempsky uploaded patch set #2 to this change.

View Change

cmd/compile,go/types: restrict use of unsafe.{Add,Slice} to go1.17 or newer

This CL updates cmd/compile (including types2) and go/types to report
errors about using unsafe.Add and unsafe.Slice when language
compatibility is set to Go 1.16 or older.


Fixes #46525.

Change-Id: I1bfe025a672d9f4b929f443064ad1effd38d0363
---
M src/cmd/compile/internal/typecheck/func.go
M src/cmd/compile/internal/types2/builtins.go
M src/go/types/builtins.go
A test/fixedbugs/issue46525.go
4 files changed, 40 insertions(+), 0 deletions(-)

To view, visit change 324369. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I1bfe025a672d9f4b929f443064ad1effd38d0363
Gerrit-Change-Number: 324369
Gerrit-PatchSet: 2
Gerrit-Owner: Matthew Dempsky <mdem...@google.com>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Matthew Dempsky <mdem...@google.com>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Matthew Dempsky <mdem...@google.com>
Gerrit-MessageType: newpatchset

Matthew Dempsky (Gerrit)

unread,
Jun 2, 2021, 5:13:40 PM6/2/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Robert Findley, Robert Griesemer, Ian Lance Taylor.

Matthew Dempsky uploaded patch set #3 to this change.

View Change

cmd/compile,go/types: restrict use of unsafe.{Add,Slice} to go1.17 or newer

This CL updates cmd/compile (including types2) and go/types to report
errors about using unsafe.Add and unsafe.Slice when language
compatibility is set to Go 1.16 or older.

Fixes #46525.

Change-Id: I1bfe025a672d9f4b929f443064ad1effd38d0363
---
M src/cmd/compile/internal/typecheck/func.go
M src/cmd/compile/internal/types2/builtins.go
M src/go/types/builtins.go
A test/fixedbugs/issue46525.go
4 files changed, 46 insertions(+), 0 deletions(-)

To view, visit change 324369. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I1bfe025a672d9f4b929f443064ad1effd38d0363
Gerrit-Change-Number: 324369
Gerrit-PatchSet: 3
Gerrit-Owner: Matthew Dempsky <mdem...@google.com>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Matthew Dempsky <mdem...@google.com>
Gerrit-Reviewer: Robert Findley <rfin...@google.com>
Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
Gerrit-Attention: Robert Findley <rfin...@google.com>
Gerrit-Attention: Robert Griesemer <g...@golang.org>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-MessageType: newpatchset

Robert Findley (Gerrit)

unread,
Jun 2, 2021, 5:24:53 PM6/2/21
to Matthew Dempsky, goph...@pubsubhelper.golang.org, Robert Griesemer, Go Bot, Ian Lance Taylor, golang-co...@googlegroups.com

Attention is currently required from: Robert Griesemer, Ian Lance Taylor, Matthew Dempsky.

Patch set 3:Code-Review +1

View Change

1 comment:

  • Patchset:

    • Patch Set #3:

      LGTM for the go/types change. I'll let Ian and Robert review for the compiler, and for the behavior with respect to -lang.

To view, visit change 324369. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I1bfe025a672d9f4b929f443064ad1effd38d0363
Gerrit-Change-Number: 324369
Gerrit-PatchSet: 3
Gerrit-Owner: Matthew Dempsky <mdem...@google.com>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Matthew Dempsky <mdem...@google.com>
Gerrit-Reviewer: Robert Findley <rfin...@google.com>
Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
Gerrit-Attention: Robert Griesemer <g...@golang.org>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Matthew Dempsky <mdem...@google.com>
Gerrit-Comment-Date: Wed, 02 Jun 2021 21:24:46 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

Robert Griesemer (Gerrit)

unread,
Jun 3, 2021, 7:56:29 PM6/3/21
to Matthew Dempsky, goph...@pubsubhelper.golang.org, Robert Griesemer, Go Bot, Robert Findley, Ian Lance Taylor, golang-co...@googlegroups.com

Attention is currently required from: Ian Lance Taylor, Matthew Dempsky.

Patch set 3:Code-Review +2

View Change

    To view, visit change 324369. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I1bfe025a672d9f4b929f443064ad1effd38d0363
    Gerrit-Change-Number: 324369
    Gerrit-PatchSet: 3
    Gerrit-Owner: Matthew Dempsky <mdem...@google.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Reviewer: Matthew Dempsky <mdem...@google.com>
    Gerrit-Reviewer: Robert Findley <rfin...@google.com>
    Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
    Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Attention: Matthew Dempsky <mdem...@google.com>
    Gerrit-Comment-Date: Thu, 03 Jun 2021 23:56:24 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Matthew Dempsky (Gerrit)

    unread,
    Jun 3, 2021, 9:31:29 PM6/3/21
    to goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Robert Griesemer, Go Bot, Robert Findley, Ian Lance Taylor, golang-co...@googlegroups.com

    Matthew Dempsky submitted this change.

    View Change

    Approvals: Robert Griesemer: Looks good to me, approved Robert Findley: Looks good to me, but someone else must approve Matthew Dempsky: Trusted; Run TryBots Go Bot: TryBots succeeded
    cmd/compile,go/types: restrict use of unsafe.{Add,Slice} to go1.17 or newer

    This CL updates cmd/compile (including types2) and go/types to report
    errors about using unsafe.Add and unsafe.Slice when language
    compatibility is set to Go 1.16 or older.

    Fixes #46525.

    Change-Id: I1bfe025a672d9f4b929f443064ad1effd38d0363
    Reviewed-on: https://go-review.googlesource.com/c/go/+/324369
    Run-TryBot: Matthew Dempsky <mdem...@google.com>
    Reviewed-by: Robert Findley <rfin...@google.com>
    Reviewed-by: Robert Griesemer <g...@golang.org>
    TryBot-Result: Go Bot <go...@golang.org>
    Trust: Matthew Dempsky <mdem...@google.com>

    ---
    M src/cmd/compile/internal/typecheck/func.go
    M src/cmd/compile/internal/types2/builtins.go
    M src/go/types/builtins.go
    A test/fixedbugs/issue46525.go
    4 files changed, 46 insertions(+), 0 deletions(-)

    diff --git a/src/cmd/compile/internal/typecheck/func.go b/src/cmd/compile/internal/typecheck/func.go
    index f381e1d..a6dfbbf 100644
    --- a/src/cmd/compile/internal/typecheck/func.go
    +++ b/src/cmd/compile/internal/typecheck/func.go
    @@ -981,6 +981,12 @@


    // tcUnsafeAdd typechecks an OUNSAFEADD node.
    func tcUnsafeAdd(n *ir.BinaryExpr) *ir.BinaryExpr {
    + if !types.AllowsGoVersion(curpkg(), 1, 17) {
    + base.ErrorfVers("go1.17", "unsafe.Add")
    +		n.SetType(nil)
    + return n
    + }

    +
    n.X = AssignConv(Expr(n.X), types.Types[types.TUNSAFEPTR], "argument to unsafe.Add")
    n.Y = DefaultLit(Expr(n.Y), types.Types[types.TINT])
    if n.X.Type() == nil || n.Y.Type() == nil {
    @@ -997,6 +1003,12 @@


    // tcUnsafeSlice typechecks an OUNSAFESLICE node.
    func tcUnsafeSlice(n *ir.BinaryExpr) *ir.BinaryExpr {
    + if !types.AllowsGoVersion(curpkg(), 1, 17) {
    + base.ErrorfVers("go1.17", "unsafe.Slice")
    +		n.SetType(nil)
    + return n
    + }

    +
    n.X = Expr(n.X)
    n.Y = Expr(n.Y)
    if n.X.Type() == nil || n.Y.Type() == nil {
    diff --git a/src/cmd/compile/internal/types2/builtins.go b/src/cmd/compile/internal/types2/builtins.go
    index b9e178d..f90e06f 100644
    --- a/src/cmd/compile/internal/types2/builtins.go
    +++ b/src/cmd/compile/internal/types2/builtins.go
    @@ -579,6 +579,11 @@

    case _Add:
    // unsafe.Add(ptr unsafe.Pointer, len IntegerType) unsafe.Pointer
    + if !check.allowVersion(check.pkg, 1, 17) {
    + check.error(call.Fun, "unsafe.Add requires go1.17 or later")
    + return
    + }
    +
    check.assignment(x, Typ[UnsafePointer], "argument to unsafe.Add")
    if x.mode == invalid {
    return
    @@ -675,6 +680,11 @@

    case _Slice:
    // unsafe.Slice(ptr *T, len IntegerType) []T
    + if !check.allowVersion(check.pkg, 1, 17) {
    + check.error(call.Fun, "unsafe.Slice requires go1.17 or later")
    + return
    + }
    +
    typ := asPointer(x.typ)
    if typ == nil {
    check.errorf(x, invalidArg+"%s is not a pointer", x)
    diff --git a/src/go/types/builtins.go b/src/go/types/builtins.go
    index 739051c..2a2d54d 100644
    --- a/src/go/types/builtins.go
    +++ b/src/go/types/builtins.go
    @@ -588,6 +588,11 @@

    case _Add:
    // unsafe.Add(ptr unsafe.Pointer, len IntegerType) unsafe.Pointer
    + if !check.allowVersion(check.pkg, 1, 17) {
    + check.errorf(call.Fun, _InvalidUnsafeAdd, "unsafe.Add requires go1.17 or later")
    + return
    + }
    +
    check.assignment(x, Typ[UnsafePointer], "argument to unsafe.Add")
    if x.mode == invalid {
    return
    @@ -684,6 +689,11 @@

    case _Slice:
    // unsafe.Slice(ptr *T, len IntegerType) []T
    + if !check.allowVersion(check.pkg, 1, 17) {
    + check.errorf(call.Fun, _InvalidUnsafeSlice, "unsafe.Slice requires go1.17 or later")
    + return
    + }
    +
    typ := asPointer(x.typ)
    if typ == nil {
    check.invalidArg(x, _InvalidUnsafeSlice, "%s is not a pointer", x)

    diff --git a/test/fixedbugs/issue46525.go b/test/fixedbugs/issue46525.go
    new file mode 100644
    index 0000000..164e147
    --- /dev/null
    +++ b/test/fixedbugs/issue46525.go
    @@ -0,0 +1,14 @@
    +// errorcheck -lang=go1.16
    +
    +// Copyright 2021 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 p
    +
    +import "unsafe"
    +
    +func main() {
    + _ = unsafe.Add(unsafe.Pointer(nil), 0) // ERROR "unsafe.Add requires go1.17 or later"
    + _ = unsafe.Slice(new(byte), 1) // ERROR "unsafe.Slice requires go1.17 or later"
    +}

    To view, visit change 324369. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I1bfe025a672d9f4b929f443064ad1effd38d0363
    Gerrit-Change-Number: 324369
    Gerrit-PatchSet: 4
    Gerrit-Owner: Matthew Dempsky <mdem...@google.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Reviewer: Matthew Dempsky <mdem...@google.com>
    Gerrit-Reviewer: Robert Findley <rfin...@google.com>
    Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
    Gerrit-MessageType: merged
    Reply all
    Reply to author
    Forward
    0 new messages