gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/safecopy/safecopy_amd64_test.go (about)

     1  // Copyright 2023 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  //go:build amd64 || i386
    16  // +build amd64 i386
    17  
    18  package safecopy_x_test
    19  
    20  import (
    21  	"testing"
    22  	"unsafe"
    23  
    24  	"gvisor.dev/gvisor/pkg/cpuid"
    25  	"gvisor.dev/gvisor/pkg/safecopy"
    26  	"gvisor.dev/gvisor/pkg/sentry/arch/fpu"
    27  )
    28  
    29  func TestCheckXstateFault(t *testing.T) {
    30  	cpuid.Initialize()
    31  	state := fpu.NewState()
    32  	state.SetMXCSR(0xffffff) // Invalid value
    33  	err := safecopy.CheckXstate(state.BytePointer())
    34  	if want := (safecopy.SegvError{uintptr(unsafe.Pointer(state.BytePointer()))}); err != want {
    35  		t.Errorf("Unexpected error: got %v, want %v", err, want)
    36  	}
    37  }
    38  
    39  func TestCheckXstateSuccess(t *testing.T) {
    40  	cpuid.Initialize()
    41  	if !cpuid.HostFeatureSet().UseXsave() {
    42  		t.Skip("xsave isn't supported")
    43  	}
    44  	state := fpu.NewState()
    45  	err := safecopy.CheckXstate(state.BytePointer())
    46  	if err != nil {
    47  		t.Errorf("Unexpected error: %v", err)
    48  	}
    49  }