github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/tools/go_marshal/test/escape/escape.go (about)

     1  // Copyright 2020 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 escape contains test cases for escape analysis.
    16  package escape
    17  
    18  import (
    19  	"github.com/SagerNet/gvisor/pkg/hostarch"
    20  	"github.com/SagerNet/gvisor/pkg/marshal"
    21  	"github.com/SagerNet/gvisor/tools/go_marshal/test"
    22  )
    23  
    24  // dummyCopyContext implements marshal.CopyContext.
    25  type dummyCopyContext struct {
    26  }
    27  
    28  func (*dummyCopyContext) CopyScratchBuffer(size int) []byte {
    29  	return make([]byte, size)
    30  }
    31  
    32  func (*dummyCopyContext) CopyOutBytes(addr hostarch.Addr, b []byte) (int, error) {
    33  	return len(b), nil
    34  }
    35  
    36  func (*dummyCopyContext) CopyInBytes(addr hostarch.Addr, b []byte) (int, error) {
    37  	return len(b), nil
    38  }
    39  
    40  func (t *dummyCopyContext) MarshalBytes(addr hostarch.Addr, marshallable marshal.Marshallable) {
    41  	buf := t.CopyScratchBuffer(marshallable.SizeBytes())
    42  	marshallable.MarshalBytes(buf)
    43  	t.CopyOutBytes(addr, buf)
    44  }
    45  
    46  func (t *dummyCopyContext) MarshalUnsafe(addr hostarch.Addr, marshallable marshal.Marshallable) {
    47  	buf := t.CopyScratchBuffer(marshallable.SizeBytes())
    48  	marshallable.MarshalUnsafe(buf)
    49  	t.CopyOutBytes(addr, buf)
    50  }
    51  
    52  // +checkescape:all
    53  //go:nosplit
    54  func doCopyIn(t *dummyCopyContext) {
    55  	var stat test.Stat
    56  	stat.CopyIn(t, hostarch.Addr(0xf000ba12))
    57  }
    58  
    59  // +checkescape:all
    60  //go:nosplit
    61  func doCopyOut(t *dummyCopyContext) {
    62  	var stat test.Stat
    63  	stat.CopyOut(t, hostarch.Addr(0xf000ba12))
    64  }
    65  
    66  // +mustescape:builtin
    67  // +mustescape:stack
    68  //go:nosplit
    69  func doMarshalBytesDirect(t *dummyCopyContext) {
    70  	var stat test.Stat
    71  	buf := t.CopyScratchBuffer(stat.SizeBytes())
    72  	stat.MarshalBytes(buf)
    73  	t.CopyOutBytes(hostarch.Addr(0xf000ba12), buf)
    74  }
    75  
    76  // +mustescape:builtin
    77  // +mustescape:stack
    78  //go:nosplit
    79  func doMarshalUnsafeDirect(t *dummyCopyContext) {
    80  	var stat test.Stat
    81  	buf := t.CopyScratchBuffer(stat.SizeBytes())
    82  	stat.MarshalUnsafe(buf)
    83  	t.CopyOutBytes(hostarch.Addr(0xf000ba12), buf)
    84  }
    85  
    86  // +mustescape:local,heap
    87  // +mustescape:stack
    88  //go:nosplit
    89  func doMarshalBytesViaMarshallable(t *dummyCopyContext) {
    90  	var stat test.Stat
    91  	t.MarshalBytes(hostarch.Addr(0xf000ba12), &stat)
    92  }
    93  
    94  // +mustescape:local,heap
    95  // +mustescape:stack
    96  //go:nosplit
    97  func doMarshalUnsafeViaMarshallable(t *dummyCopyContext) {
    98  	var stat test.Stat
    99  	t.MarshalUnsafe(hostarch.Addr(0xf000ba12), &stat)
   100  }