2019-04-29 21:25:05 +00:00
|
|
|
// Copyright 2018 The gVisor Authors.
|
2018-04-27 17:37:02 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package pagetables
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gvisor.googlesource.com/gvisor/pkg/sentry/usermem"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mapping struct {
|
2018-05-30 22:13:36 +00:00
|
|
|
start uintptr
|
|
|
|
length uintptr
|
|
|
|
addr uintptr
|
|
|
|
opts MapOpts
|
2018-04-27 17:37:02 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 01:14:22 +00:00
|
|
|
type checkVisitor struct {
|
|
|
|
expected []mapping // Input.
|
|
|
|
current int // Temporary.
|
|
|
|
found []mapping // Output.
|
|
|
|
failed string // Output.
|
|
|
|
}
|
2018-04-27 17:37:02 +00:00
|
|
|
|
2018-06-12 01:14:22 +00:00
|
|
|
func (v *checkVisitor) visit(start uintptr, pte *PTE, align uintptr) {
|
|
|
|
v.found = append(v.found, mapping{
|
|
|
|
start: start,
|
|
|
|
length: align + 1,
|
|
|
|
addr: pte.Address(),
|
|
|
|
opts: pte.Opts(),
|
2018-04-27 17:37:02 +00:00
|
|
|
})
|
2018-06-12 01:14:22 +00:00
|
|
|
if v.failed != "" {
|
|
|
|
// Don't keep looking for errors.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.current >= len(v.expected) {
|
|
|
|
v.failed = "more mappings than expected"
|
|
|
|
} else if v.expected[v.current].start != start {
|
|
|
|
v.failed = "start didn't match expected"
|
|
|
|
} else if v.expected[v.current].length != (align + 1) {
|
|
|
|
v.failed = "end didn't match expected"
|
|
|
|
} else if v.expected[v.current].addr != pte.Address() {
|
|
|
|
v.failed = "address didn't match expected"
|
|
|
|
} else if v.expected[v.current].opts != pte.Opts() {
|
|
|
|
v.failed = "opts didn't match"
|
|
|
|
}
|
|
|
|
v.current++
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*checkVisitor) requiresAlloc() bool { return false }
|
|
|
|
|
|
|
|
func (*checkVisitor) requiresSplit() bool { return false }
|
|
|
|
|
|
|
|
func checkMappings(t *testing.T, pt *PageTables, m []mapping) {
|
|
|
|
// Iterate over all the mappings.
|
|
|
|
w := checkWalker{
|
|
|
|
pageTables: pt,
|
|
|
|
visitor: checkVisitor{
|
|
|
|
expected: m,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
w.iterateRange(0, ^uintptr(0))
|
2018-04-27 17:37:02 +00:00
|
|
|
|
|
|
|
// Were we expected additional mappings?
|
2018-06-12 01:14:22 +00:00
|
|
|
if w.visitor.failed == "" && w.visitor.current != len(w.visitor.expected) {
|
|
|
|
w.visitor.failed = "insufficient mappings found"
|
2018-04-27 17:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit a meaningful error message on failure.
|
2018-06-12 01:14:22 +00:00
|
|
|
if w.visitor.failed != "" {
|
|
|
|
t.Errorf("%s; got %#v, wanted %#v", w.visitor.failed, w.visitor.found, w.visitor.expected)
|
2018-04-27 17:37:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmap(t *testing.T) {
|
2018-06-07 05:51:58 +00:00
|
|
|
pt := New(NewRuntimeAllocator())
|
2018-04-27 17:37:02 +00:00
|
|
|
|
|
|
|
// Map and unmap one entry.
|
2018-05-30 22:13:36 +00:00
|
|
|
pt.Map(0x400000, pteSize, MapOpts{AccessType: usermem.ReadWrite}, pteSize*42)
|
2018-04-27 17:37:02 +00:00
|
|
|
pt.Unmap(0x400000, pteSize)
|
|
|
|
|
|
|
|
checkMappings(t, pt, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadOnly(t *testing.T) {
|
2018-06-07 05:51:58 +00:00
|
|
|
pt := New(NewRuntimeAllocator())
|
2018-04-27 17:37:02 +00:00
|
|
|
|
|
|
|
// Map one entry.
|
2018-05-30 22:13:36 +00:00
|
|
|
pt.Map(0x400000, pteSize, MapOpts{AccessType: usermem.Read}, pteSize*42)
|
2018-04-27 17:37:02 +00:00
|
|
|
|
|
|
|
checkMappings(t, pt, []mapping{
|
2018-05-30 22:13:36 +00:00
|
|
|
{0x400000, pteSize, pteSize * 42, MapOpts{AccessType: usermem.Read}},
|
2018-04-27 17:37:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadWrite(t *testing.T) {
|
2018-06-07 05:51:58 +00:00
|
|
|
pt := New(NewRuntimeAllocator())
|
2018-04-27 17:37:02 +00:00
|
|
|
|
|
|
|
// Map one entry.
|
2018-05-30 22:13:36 +00:00
|
|
|
pt.Map(0x400000, pteSize, MapOpts{AccessType: usermem.ReadWrite}, pteSize*42)
|
2018-04-27 17:37:02 +00:00
|
|
|
|
|
|
|
checkMappings(t, pt, []mapping{
|
2018-05-30 22:13:36 +00:00
|
|
|
{0x400000, pteSize, pteSize * 42, MapOpts{AccessType: usermem.ReadWrite}},
|
2018-04-27 17:37:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSerialEntries(t *testing.T) {
|
2018-06-07 05:51:58 +00:00
|
|
|
pt := New(NewRuntimeAllocator())
|
2018-04-27 17:37:02 +00:00
|
|
|
|
|
|
|
// Map two sequential entries.
|
2018-05-30 22:13:36 +00:00
|
|
|
pt.Map(0x400000, pteSize, MapOpts{AccessType: usermem.ReadWrite}, pteSize*42)
|
|
|
|
pt.Map(0x401000, pteSize, MapOpts{AccessType: usermem.ReadWrite}, pteSize*47)
|
2018-04-27 17:37:02 +00:00
|
|
|
|
|
|
|
checkMappings(t, pt, []mapping{
|
2018-05-30 22:13:36 +00:00
|
|
|
{0x400000, pteSize, pteSize * 42, MapOpts{AccessType: usermem.ReadWrite}},
|
|
|
|
{0x401000, pteSize, pteSize * 47, MapOpts{AccessType: usermem.ReadWrite}},
|
2018-04-27 17:37:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSpanningEntries(t *testing.T) {
|
2018-06-07 05:51:58 +00:00
|
|
|
pt := New(NewRuntimeAllocator())
|
2018-04-27 17:37:02 +00:00
|
|
|
|
|
|
|
// Span a pgd with two pages.
|
2018-05-30 22:13:36 +00:00
|
|
|
pt.Map(0x00007efffffff000, 2*pteSize, MapOpts{AccessType: usermem.Read}, pteSize*42)
|
2018-04-27 17:37:02 +00:00
|
|
|
|
|
|
|
checkMappings(t, pt, []mapping{
|
2018-05-30 22:13:36 +00:00
|
|
|
{0x00007efffffff000, pteSize, pteSize * 42, MapOpts{AccessType: usermem.Read}},
|
|
|
|
{0x00007f0000000000, pteSize, pteSize * 43, MapOpts{AccessType: usermem.Read}},
|
2018-04-27 17:37:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSparseEntries(t *testing.T) {
|
2018-06-07 05:51:58 +00:00
|
|
|
pt := New(NewRuntimeAllocator())
|
2018-04-27 17:37:02 +00:00
|
|
|
|
|
|
|
// Map two entries in different pgds.
|
2018-05-30 22:13:36 +00:00
|
|
|
pt.Map(0x400000, pteSize, MapOpts{AccessType: usermem.ReadWrite}, pteSize*42)
|
|
|
|
pt.Map(0x00007f0000000000, pteSize, MapOpts{AccessType: usermem.Read}, pteSize*47)
|
2018-04-27 17:37:02 +00:00
|
|
|
|
|
|
|
checkMappings(t, pt, []mapping{
|
2018-05-30 22:13:36 +00:00
|
|
|
{0x400000, pteSize, pteSize * 42, MapOpts{AccessType: usermem.ReadWrite}},
|
|
|
|
{0x00007f0000000000, pteSize, pteSize * 47, MapOpts{AccessType: usermem.Read}},
|
2018-04-27 17:37:02 +00:00
|
|
|
})
|
|
|
|
}
|