github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/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  //go:nosplit
    35  func (t Type) Foo() {
    36  	fmt.Printf("%v", t) // Never executed.
    37  }
    38  
    39  // InterfaceFunction is passed an interface argument.
    40  // +checkescape:all,hard
    41  //go:nosplit
    42  func InterfaceFunction(i Interface) {
    43  	// Do nothing; exported for tests.
    44  }
    45  
    46  // TypeFunction is passed a concrete pointer argument.
    47  // +checkesacape:all,hard
    48  //go:nosplit
    49  func TypeFunction(t *Type) {
    50  }
    51  
    52  // BuiltinMap creates a new map.
    53  // +mustescape:local,builtin
    54  //go:noinline
    55  //go:nosplit
    56  func BuiltinMap(x int) map[string]bool {
    57  	return make(map[string]bool)
    58  }
    59  
    60  // +mustescape:builtin
    61  //go:noinline
    62  //go:nosplit
    63  func builtinMapRec(x int) map[string]bool {
    64  	return BuiltinMap(x)
    65  }
    66  
    67  // BuiltinClosure returns a closure around x.
    68  // +mustescape:local,builtin
    69  //go:noinline
    70  //go:nosplit
    71  func BuiltinClosure(x int) func() {
    72  	return func() {
    73  		fmt.Printf("%v", x)
    74  	}
    75  }
    76  
    77  // +mustescape:builtin
    78  //go:noinline
    79  //go:nosplit
    80  func builtinClosureRec(x int) func() {
    81  	return BuiltinClosure(x)
    82  }
    83  
    84  // BuiltinMakeSlice makes a new slice.
    85  // +mustescape:local,builtin
    86  //go:noinline
    87  //go:nosplit
    88  func BuiltinMakeSlice(x int) []byte {
    89  	return make([]byte, x)
    90  }
    91  
    92  // +mustescape:builtin
    93  //go:noinline
    94  //go:nosplit
    95  func builtinMakeSliceRec(x int) []byte {
    96  	return BuiltinMakeSlice(x)
    97  }
    98  
    99  // BuiltinAppend calls append on a slice.
   100  // +mustescape:local,builtin
   101  //go:noinline
   102  //go:nosplit
   103  func BuiltinAppend(x []byte) []byte {
   104  	return append(x, 0)
   105  }
   106  
   107  // +mustescape:builtin
   108  //go:noinline
   109  //go:nosplit
   110  func builtinAppendRec() []byte {
   111  	return BuiltinAppend(nil)
   112  }
   113  
   114  // BuiltinChan makes a channel.
   115  // +mustescape:local,builtin
   116  //go:noinline
   117  //go:nosplit
   118  func BuiltinChan() chan int {
   119  	return make(chan int)
   120  }
   121  
   122  // +mustescape:builtin
   123  //go:noinline
   124  //go:nosplit
   125  func builtinChanRec() chan int {
   126  	return BuiltinChan()
   127  }
   128  
   129  // Heap performs an explicit heap allocation.
   130  // +mustescape:local,heap
   131  //go:noinline
   132  //go:nosplit
   133  func Heap() *Type {
   134  	var t Type
   135  	return &t
   136  }
   137  
   138  // +mustescape:heap
   139  //go:noinline
   140  //go:nosplit
   141  func heapRec() *Type {
   142  	return Heap()
   143  }
   144  
   145  // Dispatch dispatches via an interface.
   146  // +mustescape:local,interface
   147  //go:noinline
   148  //go:nosplit
   149  func Dispatch(i Interface) {
   150  	i.Foo()
   151  }
   152  
   153  // +mustescape:interface
   154  //go:noinline
   155  //go:nosplit
   156  func dispatchRec(i Interface) {
   157  	Dispatch(i)
   158  }
   159  
   160  // Dynamic invokes a dynamic function.
   161  // +mustescape:local,dynamic
   162  //go:noinline
   163  //go:nosplit
   164  func Dynamic(f func()) {
   165  	f()
   166  }
   167  
   168  // +mustescape:dynamic
   169  //go:noinline
   170  //go:nosplit
   171  func dynamicRec(f func()) {
   172  	Dynamic(f)
   173  }
   174  
   175  //go:noinline
   176  //go:nosplit
   177  func internalFunc() {
   178  }
   179  
   180  // Split includes a guaranteed stack split.
   181  // +mustescape:local,stack
   182  //go:noinline
   183  func Split() {
   184  	internalFunc()
   185  }
   186  
   187  // +mustescape:stack
   188  //go:noinline
   189  //go:nosplit
   190  func splitRec() {
   191  	Split()
   192  }