diff --git a/nogo.yaml b/nogo.yaml index 911db71c9..e62639cf6 100644 --- a/nogo.yaml +++ b/nogo.yaml @@ -162,7 +162,7 @@ analyzers: exclude: - ".*_test.go" # Exclude tests. - "pkg/flipcall/.*_unsafe.go" # Special case. - - pkg/gohacks/gohacks_unsafe.go # Special case. + - pkg/gohacks/noescape_unsafe.go # Special case. - pkg/ring0/pagetables/allocator_unsafe.go # Special case. - pkg/sentry/fsutil/host_file_mapper_unsafe.go # Special case. - pkg/sentry/platform/kvm/bluepill_unsafe.go # Special case. @@ -207,7 +207,7 @@ analyzers: SA4016: # Useless bitwise operations. internal: exclude: - - pkg/gohacks/gohacks_unsafe.go # x ^ 0 always equals x. + - pkg/gohacks/noescape_unsafe.go # x ^ 0 always equals x. ST1019: # Multiple imports of the same package. generated: exclude: diff --git a/pkg/gohacks/BUILD b/pkg/gohacks/BUILD index 4d88bc1bb..bb483fee5 100644 --- a/pkg/gohacks/BUILD +++ b/pkg/gohacks/BUILD @@ -5,7 +5,8 @@ package(licenses = ["notice"]) go_library( name = "gohacks", srcs = [ - "gohacks_unsafe.go", + "linkname_go113_unsafe.go", + "noescape_unsafe.go", "slice_go113_unsafe.go", "slice_go120_unsafe.go", "string_go113_unsafe.go", diff --git a/pkg/gohacks/gohacks_unsafe.go b/pkg/gohacks/linkname_go113_unsafe.go similarity index 69% rename from pkg/gohacks/gohacks_unsafe.go rename to pkg/gohacks/linkname_go113_unsafe.go index c4186763c..2e8c46529 100644 --- a/pkg/gohacks/gohacks_unsafe.go +++ b/pkg/gohacks/linkname_go113_unsafe.go @@ -1,4 +1,4 @@ -// Copyright 2020 The gVisor Authors. +// Copyright 2023 The gVisor Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,17 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build go1.13 && !go1.21 -// +build go1.13,!go1.21 +//go:build go1.13 // //go:linkname directives type-checked by checklinkname. Any other // non-linkname assumptions outside the Go 1 compatibility guarantee should // have an accompanied vet check or version guard build tag. -// Check type signatures and Noescape when updating Go version. -// -// TODO(b/165820485): add these checks to checklinkname. - // Package gohacks contains utilities for subverting the Go compiler. package gohacks @@ -30,19 +25,6 @@ import ( "unsafe" ) -// Noescape hides a pointer from escape analysis. Noescape is the identity -// function but escape analysis doesn't think the output depends on the input. -// Noescape is inlined and currently compiles down to zero instructions. -// USE CAREFULLY! -// -// (Noescape is copy/pasted from Go's runtime/stubs.go:noescape().) -// -//go:nosplit -func Noescape(p unsafe.Pointer) unsafe.Pointer { - x := uintptr(p) - return unsafe.Pointer(x ^ 0) -} - // Note that go:linkname silently doesn't work if the local name is exported, // necessitating an indirection for exported functions. diff --git a/pkg/gohacks/noescape_unsafe.go b/pkg/gohacks/noescape_unsafe.go new file mode 100644 index 000000000..e6470e33d --- /dev/null +++ b/pkg/gohacks/noescape_unsafe.go @@ -0,0 +1,34 @@ +// Copyright 2023 The gVisor Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package gohacks + +import ( + "unsafe" +) + +// Noescape hides a pointer from escape analysis. Noescape is the identity +// function but escape analysis doesn't think the output depends on the input. +// Noescape is inlined and currently compiles down to zero instructions. +// USE CAREFULLY! +// +// Noescape is copy/pasted from Go's runtime/stubs.go:noescape(), and is valid +// as of Go 1.20. It is possible that this approach stops working in future +// versions of the toolchain, at which point `p` may still escape. +// +//go:nosplit +func Noescape(p unsafe.Pointer) unsafe.Pointer { + x := uintptr(p) + return unsafe.Pointer(x ^ 0) +}