-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathguest_linux.go
More file actions
52 lines (47 loc) · 1.02 KB
/
Copy pathguest_linux.go
File metadata and controls
52 lines (47 loc) · 1.02 KB
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
//go:build linux && (arm64 || amd64) && cgo
package sandbox
import (
"context"
"errors"
"fmt"
"os"
"runtime"
"github.com/xgo-dev/sandbox/internal/state"
"golang.org/x/sys/unix"
)
// guestEntry replaces main.main in the guest after Go package initialization.
// Its address is passed by Run, keeping this entry and its dependencies linked.
//
//go:noinline
func guestEntry() {
if err := runGuest(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func runGuest() (err error) {
defer func() {
if value := recover(); value != nil {
err = fmt.Errorf("sandbox guest panicked: %v", value)
}
}()
defer unix.Close(3)
data, unmap, err := readStateImage(3, 0)
if err != nil {
return err
}
defer func() { err = errors.Join(err, unmap()) }()
var graph state.State
var fn func()
ctx := context.Background()
if _, err := graph.Load(ctx, data, &fn); err != nil {
return err
}
runtime.GC()
fn()
_, err = writeStateImage(3, int64(len(data))+8, &graph, &fn)
if err != nil {
return err
}
return nil
}