github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/sentry/arch/stack_unsafe.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 arch
    16  
    17  import (
    18  	"unsafe"
    19  
    20  	"github.com/nicocha30/gvisor-ligolo/pkg/hostarch"
    21  	"github.com/nicocha30/gvisor-ligolo/pkg/marshal/primitive"
    22  )
    23  
    24  // pushAddrSliceAndTerminator copies a slices of addresses to the stack, and
    25  // also pushes an extra null address element at the end of the slice.
    26  //
    27  // Internally, we unsafely transmute the slice type from the arch-dependent
    28  // []hostarch.Addr type, to a slice of fixed-sized ints so that we can pass it to
    29  // go-marshal.
    30  //
    31  // On error, the contents of the stack and the bottom cursor are undefined.
    32  func (s *Stack) pushAddrSliceAndTerminator(src []hostarch.Addr) (int, error) {
    33  	// Note: Stack grows upwards, so push the terminator first.
    34  	switch s.Arch.Width() {
    35  	case 8:
    36  		nNull, err := primitive.CopyUint64Out(s, StackBottomMagic, 0)
    37  		if err != nil {
    38  			return 0, err
    39  		}
    40  		srcAsUint64 := *(*[]uint64)(unsafe.Pointer(&src))
    41  		n, err := primitive.CopyUint64SliceOut(s, StackBottomMagic, srcAsUint64)
    42  		return n + nNull, err
    43  	case 4:
    44  		nNull, err := primitive.CopyUint32Out(s, StackBottomMagic, 0)
    45  		if err != nil {
    46  			return 0, err
    47  		}
    48  		srcAsUint32 := *(*[]uint32)(unsafe.Pointer(&src))
    49  		n, err := primitive.CopyUint32SliceOut(s, StackBottomMagic, srcAsUint32)
    50  		return n + nNull, err
    51  	default:
    52  		panic("Unsupported arch width")
    53  	}
    54  }