github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/platform/kvm/testutil/testutil.go (about) 1 // Copyright 2018 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 testutil provides common assembly stubs for testing. 16 package testutil 17 18 import ( 19 "fmt" 20 "strings" 21 ) 22 23 // Getpid executes a trivial system call. 24 func Getpid() 25 26 // Touch touches the value in the first register. 27 func Touch() 28 29 // SyscallLoop executes a syscall and loops. 30 func SyscallLoop() 31 32 // SpinLoop spins on the CPU. 33 func SpinLoop() 34 35 // HaltLoop immediately halts and loops. 36 func HaltLoop() 37 38 // TwiddleRegsFault twiddles registers then faults. 39 func TwiddleRegsFault() 40 41 // TwiddleRegsSyscall twiddles registers then executes a syscall. 42 func TwiddleRegsSyscall() 43 44 // FloatingPointWorks is a floating point test. 45 // 46 // It returns true or false. 47 func FloatingPointWorks() bool 48 49 // RegisterMismatchError is used for checking registers. 50 type RegisterMismatchError []string 51 52 // Error returns a human-readable error. 53 func (r RegisterMismatchError) Error() string { 54 return strings.Join([]string(r), ";") 55 } 56 57 // addRegisterMisatch allows simple chaining of register mismatches. 58 func addRegisterMismatch(err error, reg string, got, expected interface{}) error { 59 errStr := fmt.Sprintf("%s got %08x, expected %08x", reg, got, expected) 60 switch r := err.(type) { 61 case nil: 62 // Return a new register mismatch. 63 return RegisterMismatchError{errStr} 64 case RegisterMismatchError: 65 // Append the error. 66 r = append(r, errStr) 67 return r 68 default: 69 // Leave as is. 70 return err 71 } 72 }