github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/runsc/boot/autosave.go (about) 1 // Copyright 2024 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 boot 16 17 import ( 18 "fmt" 19 "os" 20 21 "github.com/metacubex/gvisor/pkg/abi/linux" 22 "github.com/metacubex/gvisor/pkg/log" 23 "github.com/metacubex/gvisor/pkg/sentry/arch" 24 "github.com/metacubex/gvisor/pkg/sentry/kernel" 25 "github.com/metacubex/gvisor/pkg/sentry/state" 26 "github.com/metacubex/gvisor/pkg/sentry/strace" 27 "github.com/metacubex/gvisor/pkg/sync" 28 ) 29 30 // EnableAutosave enables auto save restore in syscall tests. 31 func EnableAutosave(l *Loader, f *os.File) error { 32 var once sync.Once // Used by target. 33 target := func(k *kernel.Kernel) { 34 once.Do(func() { 35 t, _ := state.CPUTime() 36 log.Infof("Before save CPU usage: %s", t.String()) 37 saveOpts := state.SaveOpts{ 38 Destination: f, 39 Key: nil, 40 Callback: func(err error) { 41 t1, _ := state.CPUTime() 42 log.Infof("Save CPU usage: %s", (t1 - t).String()) 43 if err == nil { 44 log.Infof("Save succeeded: exiting...") 45 k.SetSaveSuccess(true) 46 } else { 47 log.Warningf("Save failed: exiting... %v", err) 48 k.SetSaveError(err) 49 } 50 51 // Kill the sandbox. 52 k.Kill(linux.WaitStatusExit(0)) 53 }, 54 } 55 saveOpts.Save(k.SupervisorContext(), k, l.watchdog) 56 }) 57 } 58 59 for _, table := range kernel.SyscallTables() { 60 sys, ok := strace.Lookup(table.OS, table.Arch) 61 if !ok { 62 continue 63 } 64 if err := configureInitSyscall(table, sys, "init_module", kernel.ExternalAfterEnable); err != nil { 65 return err 66 } 67 // Set external args to our closure above. 68 table.External = target 69 } 70 71 return nil 72 } 73 74 // configureInitSyscall sets the trigger for the S/R syscall tests and the callback 75 // method to be called after the sycall is executed. 76 func configureInitSyscall(table *kernel.SyscallTable, sys strace.SyscallMap, initSyscall string, syscallFlag uint32) error { 77 sl := make(map[uintptr]bool) 78 sysno, ok := sys.ConvertToSysno(initSyscall) 79 if !ok { 80 return fmt.Errorf("syscall %q not found", initSyscall) 81 } 82 sl[sysno] = true 83 log.Infof("sysno %v name %v", sysno, initSyscall) 84 table.FeatureEnable.Enable(syscallFlag, sl, false) 85 table.ExternalFilterBefore = func(*kernel.Task, uintptr, arch.SyscallArguments) bool { 86 return false 87 } 88 // Sets ExternalFilterAfter to true which calls the closure assigned to 89 // External after the syscall is executed. 90 table.ExternalFilterAfter = func(*kernel.Task, uintptr, arch.SyscallArguments) bool { 91 return true 92 } 93 return nil 94 }