gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/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  // AddrOfGetpid returns the address of Getpid.
    27  //
    28  // In Go 1.17+, Go references to assembly functions resolve to an ABIInternal
    29  // wrapper function rather than the function itself. We must reference from
    30  // assembly to get the ABI0 (i.e., primary) address.
    31  func AddrOfGetpid() uintptr
    32  
    33  // AddrOfTouch returns the address of a function that touches the value in the
    34  // first register.
    35  func AddrOfTouch() uintptr
    36  func touch()
    37  
    38  // AddrOfSyscallLoop returns the address of a function that executes a syscall
    39  // and loops.
    40  func AddrOfSyscallLoop() uintptr
    41  func syscallLoop()
    42  
    43  // AddrOfSpinLoop returns the address of a function that spins on the CPU.
    44  func AddrOfSpinLoop() uintptr
    45  func spinLoop()
    46  
    47  // AddrOfHaltLoop returns the address of a function that immediately halts and
    48  // loops.
    49  func AddrOfHaltLoop() uintptr
    50  func haltLoop()
    51  
    52  // AddrOfTwiddleRegsFault returns the address of a function that twiddles
    53  // registers then faults.
    54  func AddrOfTwiddleRegsFault() uintptr
    55  func twiddleRegsFault()
    56  
    57  // AddrOfTwiddleRegsSyscall returns the address of a function that twiddles
    58  // registers then executes a syscall.
    59  func AddrOfTwiddleRegsSyscall() uintptr
    60  func twiddleRegsSyscall()
    61  
    62  // FloatingPointWorks is a floating point test.
    63  //
    64  // It returns true or false.
    65  func FloatingPointWorks() bool
    66  
    67  // RegisterMismatchError is used for checking registers.
    68  type RegisterMismatchError []string
    69  
    70  // Error returns a human-readable error.
    71  func (r RegisterMismatchError) Error() string {
    72  	return strings.Join([]string(r), ";")
    73  }
    74  
    75  // addRegisterMisatch allows simple chaining of register mismatches.
    76  func addRegisterMismatch(err error, reg string, got, expected any) error {
    77  	errStr := fmt.Sprintf("%s got %08x, expected %08x", reg, got, expected)
    78  	switch r := err.(type) {
    79  	case nil:
    80  		// Return a new register mismatch.
    81  		return RegisterMismatchError{errStr}
    82  	case RegisterMismatchError:
    83  		// Append the error.
    84  		r = append(r, errStr)
    85  		return r
    86  	default:
    87  		// Leave as is.
    88  		return err
    89  	}
    90  }