summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/arch/stack_unsafe.go
blob: a90d297eeb2a7c0994f58e57c3d279b8a613b8d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package arch

import (
	"reflect"
	"runtime"
	"unsafe"

	"gvisor.dev/gvisor/pkg/marshal/primitive"
	"gvisor.dev/gvisor/pkg/usermem"
)

// pushAddrSliceAndTerminator copies a slices of addresses to the stack, and
// also pushes an extra null address element at the end of the slice.
//
// Internally, we unsafely transmute the slice type from the arch-dependent
// []usermem.Addr type, to a slice of fixed-sized ints so that we can pass it to
// go-marshal.
//
// On error, the contents of the stack and the bottom cursor are undefined.
func (s *Stack) pushAddrSliceAndTerminator(src []usermem.Addr) (int, error) {
	// Note: Stack grows upwards, so push the terminator first.
	srcHdr := (*reflect.SliceHeader)(unsafe.Pointer(&src))
	switch s.Arch.Width() {
	case 8:
		nNull, err := primitive.CopyUint64Out(s, StackBottomMagic, 0)
		if err != nil {
			return 0, err
		}
		var dst []uint64
		dstHdr := (*reflect.SliceHeader)(unsafe.Pointer(&dst))
		dstHdr.Data = srcHdr.Data
		dstHdr.Len = srcHdr.Len
		dstHdr.Cap = srcHdr.Cap
		n, err := primitive.CopyUint64SliceOut(s, StackBottomMagic, dst)
		// Ensures src doesn't get GCed until we're done using it through dst.
		runtime.KeepAlive(src)
		return n + nNull, err
	case 4:
		nNull, err := primitive.CopyUint32Out(s, StackBottomMagic, 0)
		if err != nil {
			return 0, err
		}
		var dst []uint32
		dstHdr := (*reflect.SliceHeader)(unsafe.Pointer(&dst))
		dstHdr.Data = srcHdr.Data
		dstHdr.Len = srcHdr.Len
		dstHdr.Cap = srcHdr.Cap
		n, err := primitive.CopyUint32SliceOut(s, StackBottomMagic, dst)
		// Ensure src doesn't get GCed until we're done using it through dst.
		runtime.KeepAlive(src)
		return n + nNull, err
	default:
		panic("Unsupported arch width")
	}
}