Split remaining gohacks functions into separate files

Functions using linkname are checked via checklinkname. Noescape has no
automatic check anymore, but if a future version of Go invalidates the
implementation, the result will be values escaping to the heap (performance
loss), rather than memory corruption, so I think having no check is OK.

PiperOrigin-RevId: 505173611
This commit is contained in:
Michael Pratt
2023-01-27 11:59:38 -08:00
committed by gVisor bot
parent ee3a8735b5
commit cb58ce5414
4 changed files with 40 additions and 23 deletions
+2 -2
View File
@@ -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:
+2 -1
View File
@@ -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",
@@ -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.
+34
View File
@@ -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)
}