1// Copyright 2024 The Go Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5package abi 6 7import "unsafe" 8 9// NoEscape hides the pointer p from escape analysis, preventing it 10// from escaping to the heap. It compiles down to nothing. 11// 12// WARNING: This is very subtle to use correctly. The caller must 13// ensure that it's truly safe for p to not escape to the heap by 14// maintaining runtime pointer invariants (for example, that globals 15// and the heap may not generally point into a stack). 16// 17//go:nosplit 18//go:nocheckptr 19func NoEscape(p unsafe.Pointer) unsafe.Pointer { 20 x := uintptr(p) 21 return unsafe.Pointer(x ^ 0) 22} 23 24var alwaysFalse bool 25var escapeSink any 26 27// Escape forces any pointers in x to escape to the heap. 28func Escape[T any](x T) T { 29 if alwaysFalse { 30 escapeSink = x 31 } 32 return x 33} 34