gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/tools/checkescape/test1/test1.go (about)

     1  // Copyright 2019 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package test1 is a test package.
    16  package test1
    17  
    18  import (
    19  	"fmt"
    20  )
    21  
    22  // Interface is a generic interface.
    23  type Interface interface {
    24  	Foo()
    25  }
    26  
    27  // Type is a concrete implementation of Interface.
    28  type Type struct {
    29  	A uint64
    30  	B uint64
    31  }
    32  
    33  // Foo implements Interface.Foo.
    34  //
    35  //go:nosplit
    36  func (t Type) Foo() {
    37  	fmt.Printf("%v", t) // Never executed.
    38  }
    39  
    40  // InterfaceFunction is passed an interface argument.
    41  // +checkescape:all,hard
    42  //
    43  //go:nosplit
    44  func InterfaceFunction(i Interface) {
    45  	// Do nothing; exported for tests.
    46  }
    47  
    48  // TypeFunction is passed a concrete pointer argument.
    49  // +checkesacape:all,hard
    50  //
    51  //go:nosplit
    52  func TypeFunction(t *Type) {
    53  }
    54  
    55  // BuiltinMap creates a new map.
    56  // +mustescape:local,builtin
    57  //
    58  //go:noinline
    59  //go:nosplit
    60  func BuiltinMap(x int) map[string]bool {
    61  	return make(map[string]bool)
    62  }
    63  
    64  // +mustescape:builtin
    65  //
    66  //go:noinline
    67  //go:nosplit
    68  func builtinMapRec(x int) map[string]bool {
    69  	return BuiltinMap(x)
    70  }
    71  
    72  // BuiltinClosure returns a closure around x.
    73  // +mustescape:local,builtin
    74  //
    75  //go:noinline
    76  //go:nosplit
    77  func BuiltinClosure(x int) func() {
    78  	return func() {
    79  		fmt.Printf("%v", x)
    80  	}
    81  }
    82  
    83  // +mustescape:builtin
    84  //
    85  //go:noinline
    86  //go:nosplit
    87  func builtinClosureRec(x int) func() {
    88  	return BuiltinClosure(x)
    89  }
    90  
    91  // BuiltinMakeSlice makes a new slice.
    92  // +mustescape:local,builtin
    93  //
    94  //go:noinline
    95  //go:nosplit
    96  func BuiltinMakeSlice(x int) []byte {
    97  	return make([]byte, x)
    98  }
    99  
   100  // +mustescape:builtin
   101  //
   102  //go:noinline
   103  //go:nosplit
   104  func builtinMakeSliceRec(x int) []byte {
   105  	return BuiltinMakeSlice(x)
   106  }
   107  
   108  // BuiltinAppend calls append on a slice.
   109  // +mustescape:local,builtin
   110  //
   111  //go:noinline
   112  //go:nosplit
   113  func BuiltinAppend(x []byte) []byte {
   114  	return append(x, 0)
   115  }
   116  
   117  // +mustescape:builtin
   118  //
   119  //go:noinline
   120  //go:nosplit
   121  func builtinAppendRec() []byte {
   122  	return BuiltinAppend(nil)
   123  }
   124  
   125  // BuiltinChan makes a channel.
   126  // +mustescape:local,builtin
   127  //
   128  //go:noinline
   129  //go:nosplit
   130  func BuiltinChan() chan int {
   131  	return make(chan int)
   132  }
   133  
   134  // +mustescape:builtin
   135  //
   136  //go:noinline
   137  //go:nosplit
   138  func builtinChanRec() chan int {
   139  	return BuiltinChan()
   140  }
   141  
   142  // Heap performs an explicit heap allocation.
   143  // +mustescape:local,heap
   144  //
   145  //go:noinline
   146  //go:nosplit
   147  func Heap() *Type {
   148  	var t Type
   149  	return &t
   150  }
   151  
   152  // +mustescape:heap
   153  //
   154  //go:noinline
   155  //go:nosplit
   156  func heapRec() *Type {
   157  	return Heap()
   158  }
   159  
   160  // Dispatch dispatches via an interface.
   161  // +mustescape:local,interface
   162  //
   163  //go:noinline
   164  //go:nosplit
   165  func Dispatch(i Interface) {
   166  	i.Foo()
   167  }
   168  
   169  // +mustescape:interface
   170  //
   171  //go:noinline
   172  //go:nosplit
   173  func dispatchRec(i Interface) {
   174  	Dispatch(i)
   175  }
   176  
   177  // Dynamic invokes a dynamic function.
   178  // +mustescape:local,dynamic
   179  //
   180  //go:noinline
   181  //go:nosplit
   182  func Dynamic(f func()) {
   183  	f()
   184  }
   185  
   186  // +mustescape:dynamic
   187  //
   188  //go:noinline
   189  //go:nosplit
   190  func dynamicRec(f func()) {
   191  	Dynamic(f)
   192  }
   193  
   194  //go:noinline
   195  //go:nosplit
   196  func internalFunc() {
   197  }
   198  
   199  // Split includes a guaranteed stack split.
   200  // +mustescape:local,stack
   201  //
   202  //go:noinline
   203  func Split() {
   204  	internalFunc()
   205  }
   206  
   207  // +mustescape:stack
   208  //
   209  //go:noinline
   210  //go:nosplit
   211  func splitRec() {
   212  	Split()
   213  }