github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/platform/kvm/virtual_map_test.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 kvm
    16  
    17  import (
    18  	"testing"
    19  
    20  	"golang.org/x/sys/unix"
    21  	"github.com/SagerNet/gvisor/pkg/hostarch"
    22  )
    23  
    24  type checker struct {
    25  	ok         bool
    26  	accessType hostarch.AccessType
    27  }
    28  
    29  func (c *checker) Containing(addr uintptr) func(virtualRegion) {
    30  	c.ok = false // Reset for below calls.
    31  	return func(vr virtualRegion) {
    32  		if vr.virtual <= addr && addr < vr.virtual+vr.length {
    33  			c.ok = true
    34  			c.accessType = vr.accessType
    35  		}
    36  	}
    37  }
    38  
    39  func TestParseMaps(t *testing.T) {
    40  	c := new(checker)
    41  
    42  	// Simple test.
    43  	if err := applyVirtualRegions(c.Containing(0)); err != nil {
    44  		t.Fatalf("unexpected error: %v", err)
    45  	}
    46  
    47  	// MMap a new page.
    48  	addr, _, errno := unix.RawSyscall6(
    49  		unix.SYS_MMAP, 0, hostarch.PageSize,
    50  		unix.PROT_READ|unix.PROT_WRITE,
    51  		unix.MAP_ANONYMOUS|unix.MAP_PRIVATE, 0, 0)
    52  	if errno != 0 {
    53  		t.Fatalf("unexpected map error: %v", errno)
    54  	}
    55  
    56  	// Re-parse maps.
    57  	if err := applyVirtualRegions(c.Containing(addr)); err != nil {
    58  		unix.RawSyscall(unix.SYS_MUNMAP, addr, hostarch.PageSize, 0)
    59  		t.Fatalf("unexpected error: %v", err)
    60  	}
    61  
    62  	// Assert that it now does contain the region.
    63  	if !c.ok {
    64  		unix.RawSyscall(unix.SYS_MUNMAP, addr, hostarch.PageSize, 0)
    65  		t.Fatalf("updated map does not contain 0x%08x, expected true", addr)
    66  	}
    67  
    68  	// Map the region as PROT_NONE.
    69  	newAddr, _, errno := unix.RawSyscall6(
    70  		unix.SYS_MMAP, addr, hostarch.PageSize,
    71  		unix.PROT_NONE,
    72  		unix.MAP_ANONYMOUS|unix.MAP_FIXED|unix.MAP_PRIVATE, 0, 0)
    73  	if errno != 0 {
    74  		t.Fatalf("unexpected map error: %v", errno)
    75  	}
    76  	if newAddr != addr {
    77  		t.Fatalf("unable to remap address: got 0x%08x, wanted 0x%08x", newAddr, addr)
    78  	}
    79  
    80  	// Re-parse maps.
    81  	if err := applyVirtualRegions(c.Containing(addr)); err != nil {
    82  		t.Fatalf("unexpected error: %v", err)
    83  	}
    84  	if !c.ok {
    85  		t.Fatalf("final map does not contain 0x%08x, expected true", addr)
    86  	}
    87  	if c.accessType.Read || c.accessType.Write || c.accessType.Execute {
    88  		t.Fatalf("final map has incorrect permissions for 0x%08x", addr)
    89  	}
    90  
    91  	// Unmap the region.
    92  	unix.RawSyscall(unix.SYS_MUNMAP, addr, hostarch.PageSize, 0)
    93  }