[syserror] Split usermem package

Split usermem package to help remove syserror dependency in go_marshal.
New hostarch package contains code not dependent on syserror.

PiperOrigin-RevId: 365651233
This commit is contained in:
Zach Koopmans
2021-03-29 13:30:21 -07:00
committed by gVisor bot
parent b125afba41
commit 8a2f7e716d
313 changed files with 2135 additions and 1955 deletions
+2 -2
View File
@@ -18,8 +18,8 @@ type Context interface {
}
type AddressSpace interface {
MapFile(addr usermem.Addr, f File, fr FileRange, at usermem.AccessType, ...) error
Unmap(addr usermem.Addr, length uint64)
MapFile(addr hostarch.Addr, f File, fr FileRange, at hostarch.AccessType, ...) error
Unmap(addr hostarch.Addr, length uint64)
}
```
+1 -1
View File
@@ -7,8 +7,8 @@ go_library(
srcs = ["coverage.go"],
visibility = ["//:sandbox"],
deps = [
"//pkg/hostarch",
"//pkg/sync",
"//pkg/usermem",
"@io_bazel_rules_go//go/tools/coverdata",
],
)
+2 -2
View File
@@ -28,8 +28,8 @@ import (
"sort"
"testing"
"gvisor.dev/gvisor/pkg/hostarch"
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/pkg/usermem"
"github.com/bazelbuild/rules_go/go/tools/coverdata"
)
@@ -141,7 +141,7 @@ func ConsumeCoverageData(w io.Writer) int {
// Non-zero coverage data found; consume it and report as a PC.
counters[index] = 0
pc := globalData.syntheticPCs[fileNum][index]
usermem.ByteOrder.PutUint64(pcBuffer[:], pc)
hostarch.ByteOrder.PutUint64(pcBuffer[:], pc)
n, err := w.Write(pcBuffer[:])
if err != nil {
if err == io.EOF {
+42
View File
@@ -0,0 +1,42 @@
load("//tools:defs.bzl", "go_library", "go_test")
load("//tools/go_generics:defs.bzl", "go_template_instance")
package(licenses = ["notice"])
go_template_instance(
name = "addr_range",
out = "addr_range.go",
package = "hostarch",
prefix = "Addr",
template = "//pkg/segment:generic_range",
types = {
"T": "Addr",
},
)
go_test(
name = "hostarch_test",
size = "small",
srcs = [
"addr_range_seq_test.go",
],
library = ":hostarch",
)
go_library(
name = "hostarch",
srcs = [
"access_type.go",
"addr.go",
"addr_range.go",
"addr_range_seq_unsafe.go",
"hostarch.go",
"hostarch_arm64.go",
"hostarch_x86.go",
],
visibility = ["//:sandbox"],
deps = [
"//pkg/gohacks",
"@org_golang_x_sys//unix:go_default_library",
],
)
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package usermem
package hostarch
import "golang.org/x/sys/unix"
+2 -2
View File
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package usermem
package hostarch
import (
"fmt"
@@ -57,7 +57,7 @@ func (v Addr) RoundUp() (addr Addr, ok bool) {
func (v Addr) MustRoundUp() Addr {
addr, ok := v.RoundUp()
if !ok {
panic(fmt.Sprintf("usermem.Addr(%d).RoundUp() wraps", v))
panic(fmt.Sprintf("hostarch.Addr(%d).RoundUp() wraps", v))
}
return addr
}
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package usermem
package hostarch
import (
"testing"
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package usermem
package hostarch
import (
"bytes"
+7
View File
@@ -0,0 +1,7 @@
// Copyright 2021 The gVisor Authors.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package hostarch contains host arch address operations for user memory.
package hostarch
@@ -14,7 +14,7 @@
// +build arm64
package usermem
package hostarch
import (
"encoding/binary"
@@ -14,7 +14,7 @@
// +build amd64 386
package usermem
package hostarch
import "encoding/binary"
+1 -1
View File
@@ -11,5 +11,5 @@ go_library(
visibility = [
"//:sandbox",
],
deps = ["//pkg/usermem"],
deps = ["//pkg/hostarch"],
)
+10 -10
View File
@@ -23,7 +23,7 @@ package marshal
import (
"io"
"gvisor.dev/gvisor/pkg/usermem"
"gvisor.dev/gvisor/pkg/hostarch"
)
// CopyContext defines the memory operations required to marshal to and from
@@ -36,11 +36,11 @@ type CopyContext interface {
// CopyOutBytes writes the contents of b to the task's memory. See
// kernel.CopyOutBytes.
CopyOutBytes(addr usermem.Addr, b []byte) (int, error)
CopyOutBytes(addr hostarch.Addr, b []byte) (int, error)
// CopyInBytes reads the contents of the task's memory to b. See
// kernel.CopyInBytes.
CopyInBytes(addr usermem.Addr, b []byte) (int, error)
CopyInBytes(addr hostarch.Addr, b []byte) (int, error)
}
// Marshallable represents operations on a type that can be marshalled to and
@@ -108,7 +108,7 @@ type Marshallable interface {
// If the copy-in from the task memory is only partially successful, CopyIn
// should still attempt to deserialize as much data as possible. See comment
// for UnmarshalBytes.
CopyIn(cc CopyContext, addr usermem.Addr) (int, error)
CopyIn(cc CopyContext, addr hostarch.Addr) (int, error)
// CopyOut serializes a Marshallable type to a task's memory. This may only
// be called from a task goroutine. This is more efficient than calling
@@ -119,7 +119,7 @@ type Marshallable interface {
// The copy-out to the task memory may be partially successful, in which
// case CopyOut returns how much data was serialized. See comment for
// MarshalBytes for implications.
CopyOut(cc CopyContext, addr usermem.Addr) (int, error)
CopyOut(cc CopyContext, addr hostarch.Addr) (int, error)
// CopyOutN is like CopyOut, but explicitly requests a partial
// copy-out. Note that this may yield unexpected results for non-packed
@@ -127,7 +127,7 @@ type Marshallable interface {
// comment on MarshalBytes.
//
// The limit must be less than or equal to SizeBytes().
CopyOutN(cc CopyContext, addr usermem.Addr, limit int) (int, error)
CopyOutN(cc CopyContext, addr hostarch.Addr, limit int) (int, error)
}
// go-marshal generates additional functions for a type based on additional
@@ -157,10 +157,10 @@ type Marshallable interface {
// func UnmarshalUnsafeFooSlice(dst []Foo, src []byte) (int, error) { ... }
//
// // CopyFooSliceIn copies in a slice of Foo objects from the task's memory.
// func CopyFooSliceIn(cc marshal.CopyContext, addr usermem.Addr, dst []Foo) (int, error) { ... }
// func CopyFooSliceIn(cc marshal.CopyContext, addr hostarch.Addr, dst []Foo) (int, error) { ... }
//
// // CopyFooSliceIn copies out a slice of Foo objects to the task's memory.
// func CopyFooSliceOut(cc marshal.CopyContext, addr usermem.Addr, src []Foo) (int, error) { ... }
// func CopyFooSliceOut(cc marshal.CopyContext, addr hostarch.Addr, src []Foo) (int, error) { ... }
//
// The name of the functions are of the format "Copy%sIn" and "Copy%sOut", where
// %s is the first argument to the slice clause. This directive is not supported
@@ -175,10 +175,10 @@ type Marshallable interface {
// This is only valid on newtypes on primitives, and causes the generated
// functions to accept slices of the inner type instead:
//
// func CopyInt32SliceIn(cc marshal.CopyContext, addr usermem.Addr, dst []int32) (int, error) { ... }
// func CopyInt32SliceIn(cc marshal.CopyContext, addr hostarch.Addr, dst []int32) (int, error) { ... }
//
// Without "inner", they would instead be:
//
// func CopyInt32SliceIn(cc marshal.CopyContext, addr usermem.Addr, dst []Int32) (int, error) { ... }
// func CopyInt32SliceIn(cc marshal.CopyContext, addr hostarch.Addr, dst []Int32) (int, error) { ... }
//
// This may help avoid a cast depending on how the generated functions are used.
+4 -4
View File
@@ -17,7 +17,7 @@ package marshal
import (
"io"
"gvisor.dev/gvisor/pkg/usermem"
"gvisor.dev/gvisor/pkg/hostarch"
)
// StubMarshallable implements the Marshallable interface.
@@ -63,16 +63,16 @@ func (StubMarshallable) UnmarshalUnsafe(src []byte) {
}
// CopyIn implements Marshallable.CopyIn.
func (StubMarshallable) CopyIn(cc CopyContext, addr usermem.Addr) (int, error) {
func (StubMarshallable) CopyIn(cc CopyContext, addr hostarch.Addr) (int, error) {
panic("Please implement your own CopyIn function")
}
// CopyOut implements Marshallable.CopyOut.
func (StubMarshallable) CopyOut(cc CopyContext, addr usermem.Addr) (int, error) {
func (StubMarshallable) CopyOut(cc CopyContext, addr hostarch.Addr) (int, error) {
panic("Please implement your own CopyOut function")
}
// CopyOutN implements Marshallable.CopyOutN.
func (StubMarshallable) CopyOutN(cc CopyContext, addr usermem.Addr, limit int) (int, error) {
func (StubMarshallable) CopyOutN(cc CopyContext, addr hostarch.Addr, limit int) (int, error) {
panic("Please implement your own CopyOutN function")
}
+1
View File
@@ -13,6 +13,7 @@ go_library(
],
deps = [
"//pkg/context",
"//pkg/hostarch",
"//pkg/marshal",
"//pkg/usermem",
],
+27 -26
View File
@@ -20,6 +20,7 @@ import (
"io"
"gvisor.dev/gvisor/pkg/context"
"gvisor.dev/gvisor/pkg/hostarch"
"gvisor.dev/gvisor/pkg/marshal"
"gvisor.dev/gvisor/pkg/usermem"
)
@@ -102,17 +103,17 @@ func (b *ByteSlice) UnmarshalUnsafe(src []byte) {
}
// CopyIn implements marshal.Marshallable.CopyIn.
func (b *ByteSlice) CopyIn(cc marshal.CopyContext, addr usermem.Addr) (int, error) {
func (b *ByteSlice) CopyIn(cc marshal.CopyContext, addr hostarch.Addr) (int, error) {
return cc.CopyInBytes(addr, *b)
}
// CopyOut implements marshal.Marshallable.CopyOut.
func (b *ByteSlice) CopyOut(cc marshal.CopyContext, addr usermem.Addr) (int, error) {
func (b *ByteSlice) CopyOut(cc marshal.CopyContext, addr hostarch.Addr) (int, error) {
return cc.CopyOutBytes(addr, *b)
}
// CopyOutN implements marshal.Marshallable.CopyOutN.
func (b *ByteSlice) CopyOutN(cc marshal.CopyContext, addr usermem.Addr, limit int) (int, error) {
func (b *ByteSlice) CopyOutN(cc marshal.CopyContext, addr hostarch.Addr, limit int) (int, error) {
return cc.CopyOutBytes(addr, (*b)[:limit])
}
@@ -131,7 +132,7 @@ var _ marshal.Marshallable = (*ByteSlice)(nil)
// CopyInt8In is a convenient wrapper for copying in an int8 from the task's
// memory.
func CopyInt8In(cc marshal.CopyContext, addr usermem.Addr, dst *int8) (int, error) {
func CopyInt8In(cc marshal.CopyContext, addr hostarch.Addr, dst *int8) (int, error) {
var buf Int8
n, err := buf.CopyIn(cc, addr)
if err != nil {
@@ -143,14 +144,14 @@ func CopyInt8In(cc marshal.CopyContext, addr usermem.Addr, dst *int8) (int, erro
// CopyInt8Out is a convenient wrapper for copying out an int8 to the task's
// memory.
func CopyInt8Out(cc marshal.CopyContext, addr usermem.Addr, src int8) (int, error) {
func CopyInt8Out(cc marshal.CopyContext, addr hostarch.Addr, src int8) (int, error) {
srcP := Int8(src)
return srcP.CopyOut(cc, addr)
}
// CopyUint8In is a convenient wrapper for copying in a uint8 from the task's
// memory.
func CopyUint8In(cc marshal.CopyContext, addr usermem.Addr, dst *uint8) (int, error) {
func CopyUint8In(cc marshal.CopyContext, addr hostarch.Addr, dst *uint8) (int, error) {
var buf Uint8
n, err := buf.CopyIn(cc, addr)
if err != nil {
@@ -162,7 +163,7 @@ func CopyUint8In(cc marshal.CopyContext, addr usermem.Addr, dst *uint8) (int, er
// CopyUint8Out is a convenient wrapper for copying out a uint8 to the task's
// memory.
func CopyUint8Out(cc marshal.CopyContext, addr usermem.Addr, src uint8) (int, error) {
func CopyUint8Out(cc marshal.CopyContext, addr hostarch.Addr, src uint8) (int, error) {
srcP := Uint8(src)
return srcP.CopyOut(cc, addr)
}
@@ -171,7 +172,7 @@ func CopyUint8Out(cc marshal.CopyContext, addr usermem.Addr, src uint8) (int, er
// CopyInt16In is a convenient wrapper for copying in an int16 from the task's
// memory.
func CopyInt16In(cc marshal.CopyContext, addr usermem.Addr, dst *int16) (int, error) {
func CopyInt16In(cc marshal.CopyContext, addr hostarch.Addr, dst *int16) (int, error) {
var buf Int16
n, err := buf.CopyIn(cc, addr)
if err != nil {
@@ -183,14 +184,14 @@ func CopyInt16In(cc marshal.CopyContext, addr usermem.Addr, dst *int16) (int, er
// CopyInt16Out is a convenient wrapper for copying out an int16 to the task's
// memory.
func CopyInt16Out(cc marshal.CopyContext, addr usermem.Addr, src int16) (int, error) {
func CopyInt16Out(cc marshal.CopyContext, addr hostarch.Addr, src int16) (int, error) {
srcP := Int16(src)
return srcP.CopyOut(cc, addr)
}
// CopyUint16In is a convenient wrapper for copying in a uint16 from the task's
// memory.
func CopyUint16In(cc marshal.CopyContext, addr usermem.Addr, dst *uint16) (int, error) {
func CopyUint16In(cc marshal.CopyContext, addr hostarch.Addr, dst *uint16) (int, error) {
var buf Uint16
n, err := buf.CopyIn(cc, addr)
if err != nil {
@@ -202,7 +203,7 @@ func CopyUint16In(cc marshal.CopyContext, addr usermem.Addr, dst *uint16) (int,
// CopyUint16Out is a convenient wrapper for copying out a uint16 to the task's
// memory.
func CopyUint16Out(cc marshal.CopyContext, addr usermem.Addr, src uint16) (int, error) {
func CopyUint16Out(cc marshal.CopyContext, addr hostarch.Addr, src uint16) (int, error) {
srcP := Uint16(src)
return srcP.CopyOut(cc, addr)
}
@@ -211,7 +212,7 @@ func CopyUint16Out(cc marshal.CopyContext, addr usermem.Addr, src uint16) (int,
// CopyInt32In is a convenient wrapper for copying in an int32 from the task's
// memory.
func CopyInt32In(cc marshal.CopyContext, addr usermem.Addr, dst *int32) (int, error) {
func CopyInt32In(cc marshal.CopyContext, addr hostarch.Addr, dst *int32) (int, error) {
var buf Int32
n, err := buf.CopyIn(cc, addr)
if err != nil {
@@ -223,14 +224,14 @@ func CopyInt32In(cc marshal.CopyContext, addr usermem.Addr, dst *int32) (int, er
// CopyInt32Out is a convenient wrapper for copying out an int32 to the task's
// memory.
func CopyInt32Out(cc marshal.CopyContext, addr usermem.Addr, src int32) (int, error) {
func CopyInt32Out(cc marshal.CopyContext, addr hostarch.Addr, src int32) (int, error) {
srcP := Int32(src)
return srcP.CopyOut(cc, addr)
}
// CopyUint32In is a convenient wrapper for copying in a uint32 from the task's
// memory.
func CopyUint32In(cc marshal.CopyContext, addr usermem.Addr, dst *uint32) (int, error) {
func CopyUint32In(cc marshal.CopyContext, addr hostarch.Addr, dst *uint32) (int, error) {
var buf Uint32
n, err := buf.CopyIn(cc, addr)
if err != nil {
@@ -242,7 +243,7 @@ func CopyUint32In(cc marshal.CopyContext, addr usermem.Addr, dst *uint32) (int,
// CopyUint32Out is a convenient wrapper for copying out a uint32 to the task's
// memory.
func CopyUint32Out(cc marshal.CopyContext, addr usermem.Addr, src uint32) (int, error) {
func CopyUint32Out(cc marshal.CopyContext, addr hostarch.Addr, src uint32) (int, error) {
srcP := Uint32(src)
return srcP.CopyOut(cc, addr)
}
@@ -251,7 +252,7 @@ func CopyUint32Out(cc marshal.CopyContext, addr usermem.Addr, src uint32) (int,
// CopyInt64In is a convenient wrapper for copying in an int64 from the task's
// memory.
func CopyInt64In(cc marshal.CopyContext, addr usermem.Addr, dst *int64) (int, error) {
func CopyInt64In(cc marshal.CopyContext, addr hostarch.Addr, dst *int64) (int, error) {
var buf Int64
n, err := buf.CopyIn(cc, addr)
if err != nil {
@@ -263,14 +264,14 @@ func CopyInt64In(cc marshal.CopyContext, addr usermem.Addr, dst *int64) (int, er
// CopyInt64Out is a convenient wrapper for copying out an int64 to the task's
// memory.
func CopyInt64Out(cc marshal.CopyContext, addr usermem.Addr, src int64) (int, error) {
func CopyInt64Out(cc marshal.CopyContext, addr hostarch.Addr, src int64) (int, error) {
srcP := Int64(src)
return srcP.CopyOut(cc, addr)
}
// CopyUint64In is a convenient wrapper for copying in a uint64 from the task's
// memory.
func CopyUint64In(cc marshal.CopyContext, addr usermem.Addr, dst *uint64) (int, error) {
func CopyUint64In(cc marshal.CopyContext, addr hostarch.Addr, dst *uint64) (int, error) {
var buf Uint64
n, err := buf.CopyIn(cc, addr)
if err != nil {
@@ -282,14 +283,14 @@ func CopyUint64In(cc marshal.CopyContext, addr usermem.Addr, dst *uint64) (int,
// CopyUint64Out is a convenient wrapper for copying out a uint64 to the task's
// memory.
func CopyUint64Out(cc marshal.CopyContext, addr usermem.Addr, src uint64) (int, error) {
func CopyUint64Out(cc marshal.CopyContext, addr hostarch.Addr, src uint64) (int, error) {
srcP := Uint64(src)
return srcP.CopyOut(cc, addr)
}
// CopyByteSliceIn is a convenient wrapper for copying in a []byte from the
// task's memory.
func CopyByteSliceIn(cc marshal.CopyContext, addr usermem.Addr, dst *[]byte) (int, error) {
func CopyByteSliceIn(cc marshal.CopyContext, addr hostarch.Addr, dst *[]byte) (int, error) {
var buf ByteSlice
n, err := buf.CopyIn(cc, addr)
if err != nil {
@@ -301,14 +302,14 @@ func CopyByteSliceIn(cc marshal.CopyContext, addr usermem.Addr, dst *[]byte) (in
// CopyByteSliceOut is a convenient wrapper for copying out a []byte to the
// task's memory.
func CopyByteSliceOut(cc marshal.CopyContext, addr usermem.Addr, src []byte) (int, error) {
func CopyByteSliceOut(cc marshal.CopyContext, addr hostarch.Addr, src []byte) (int, error) {
srcP := ByteSlice(src)
return srcP.CopyOut(cc, addr)
}
// CopyStringIn is a convenient wrapper for copying in a string from the
// task's memory.
func CopyStringIn(cc marshal.CopyContext, addr usermem.Addr, dst *string) (int, error) {
func CopyStringIn(cc marshal.CopyContext, addr hostarch.Addr, dst *string) (int, error) {
var buf ByteSlice
n, err := buf.CopyIn(cc, addr)
if err != nil {
@@ -320,12 +321,12 @@ func CopyStringIn(cc marshal.CopyContext, addr usermem.Addr, dst *string) (int,
// CopyStringOut is a convenient wrapper for copying out a string to the task's
// memory.
func CopyStringOut(cc marshal.CopyContext, addr usermem.Addr, src string) (int, error) {
func CopyStringOut(cc marshal.CopyContext, addr hostarch.Addr, src string) (int, error) {
srcP := ByteSlice(src)
return srcP.CopyOut(cc, addr)
}
// IOCopyContext wraps an object implementing usermem.IO to implement
// IOCopyContext wraps an object implementing hostarch.IO to implement
// marshal.CopyContext.
type IOCopyContext struct {
Ctx context.Context
@@ -339,11 +340,11 @@ func (i *IOCopyContext) CopyScratchBuffer(size int) []byte {
}
// CopyOutBytes implements marshal.CopyContext.CopyOutBytes.
func (i *IOCopyContext) CopyOutBytes(addr usermem.Addr, b []byte) (int, error) {
func (i *IOCopyContext) CopyOutBytes(addr hostarch.Addr, b []byte) (int, error) {
return i.IO.CopyOut(i.Ctx, addr, b, i.Opts)
}
// CopyInBytes implements marshal.CopyContext.CopyInBytes.
func (i *IOCopyContext) CopyInBytes(addr usermem.Addr, b []byte) (int, error) {
func (i *IOCopyContext) CopyInBytes(addr hostarch.Addr, b []byte) (int, error) {
return i.IO.CopyIn(i.Ctx, addr, b, i.Opts)
}
+2 -2
View File
@@ -8,7 +8,7 @@ go_library(
visibility = ["//pkg/sentry:internal"],
deps = [
"//pkg/abi/linux",
"//pkg/usermem",
"//pkg/hostarch",
],
)
@@ -18,6 +18,6 @@ go_test(
library = ":merkletree",
deps = [
"//pkg/abi/linux",
"//pkg/usermem",
"//pkg/hostarch",
],
)
+3 -2
View File
@@ -24,7 +24,8 @@ import (
"io"
"gvisor.dev/gvisor/pkg/abi/linux"
"gvisor.dev/gvisor/pkg/usermem"
"gvisor.dev/gvisor/pkg/hostarch"
)
const (
@@ -65,7 +66,7 @@ type Layout struct {
// of a tree. dataSize specifies the size of input data in bytes.
func InitLayout(dataSize int64, hashAlgorithms int, dataAndTreeInSameFile bool) (Layout, error) {
layout := Layout{
blockSize: usermem.PageSize,
blockSize: hostarch.PageSize,
}
// TODO(b/156980949): Allow config SHA384.
+86 -85
View File
@@ -24,7 +24,8 @@ import (
"time"
"gvisor.dev/gvisor/pkg/abi/linux"
"gvisor.dev/gvisor/pkg/usermem"
"gvisor.dev/gvisor/pkg/hostarch"
)
func TestLayout(t *testing.T) {
@@ -58,7 +59,7 @@ func TestLayout(t *testing.T) {
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA256,
dataAndTreeInSameFile: true,
expectedDigestSize: 32,
expectedLevelOffset: []int64{usermem.PageSize},
expectedLevelOffset: []int64{hostarch.PageSize},
},
{
name: "SmallSizeSHA512SameFile",
@@ -66,7 +67,7 @@ func TestLayout(t *testing.T) {
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA512,
dataAndTreeInSameFile: true,
expectedDigestSize: 64,
expectedLevelOffset: []int64{usermem.PageSize},
expectedLevelOffset: []int64{hostarch.PageSize},
},
{
name: "MiddleSizeSHA256SeparateFile",
@@ -74,7 +75,7 @@ func TestLayout(t *testing.T) {
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA256,
dataAndTreeInSameFile: false,
expectedDigestSize: 32,
expectedLevelOffset: []int64{0, 2 * usermem.PageSize, 3 * usermem.PageSize},
expectedLevelOffset: []int64{0, 2 * hostarch.PageSize, 3 * hostarch.PageSize},
},
{
name: "MiddleSizeSHA512SeparateFile",
@@ -82,7 +83,7 @@ func TestLayout(t *testing.T) {
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA512,
dataAndTreeInSameFile: false,
expectedDigestSize: 64,
expectedLevelOffset: []int64{0, 4 * usermem.PageSize, 5 * usermem.PageSize},
expectedLevelOffset: []int64{0, 4 * hostarch.PageSize, 5 * hostarch.PageSize},
},
{
name: "MiddleSizeSHA256SameFile",
@@ -90,7 +91,7 @@ func TestLayout(t *testing.T) {
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA256,
dataAndTreeInSameFile: true,
expectedDigestSize: 32,
expectedLevelOffset: []int64{245 * usermem.PageSize, 247 * usermem.PageSize, 248 * usermem.PageSize},
expectedLevelOffset: []int64{245 * hostarch.PageSize, 247 * hostarch.PageSize, 248 * hostarch.PageSize},
},
{
name: "MiddleSizeSHA512SameFile",
@@ -98,39 +99,39 @@ func TestLayout(t *testing.T) {
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA512,
dataAndTreeInSameFile: true,
expectedDigestSize: 64,
expectedLevelOffset: []int64{245 * usermem.PageSize, 249 * usermem.PageSize, 250 * usermem.PageSize},
expectedLevelOffset: []int64{245 * hostarch.PageSize, 249 * hostarch.PageSize, 250 * hostarch.PageSize},
},
{
name: "LargeSizeSHA256SeparateFile",
dataSize: 4096 * int64(usermem.PageSize),
dataSize: 4096 * int64(hostarch.PageSize),
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA256,
dataAndTreeInSameFile: false,
expectedDigestSize: 32,
expectedLevelOffset: []int64{0, 32 * usermem.PageSize, 33 * usermem.PageSize},
expectedLevelOffset: []int64{0, 32 * hostarch.PageSize, 33 * hostarch.PageSize},
},
{
name: "LargeSizeSHA512SeparateFile",
dataSize: 4096 * int64(usermem.PageSize),
dataSize: 4096 * int64(hostarch.PageSize),
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA512,
dataAndTreeInSameFile: false,
expectedDigestSize: 64,
expectedLevelOffset: []int64{0, 64 * usermem.PageSize, 65 * usermem.PageSize},
expectedLevelOffset: []int64{0, 64 * hostarch.PageSize, 65 * hostarch.PageSize},
},
{
name: "LargeSizeSHA256SameFile",
dataSize: 4096 * int64(usermem.PageSize),
dataSize: 4096 * int64(hostarch.PageSize),
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA256,
dataAndTreeInSameFile: true,
expectedDigestSize: 32,
expectedLevelOffset: []int64{4096 * usermem.PageSize, 4128 * usermem.PageSize, 4129 * usermem.PageSize},
expectedLevelOffset: []int64{4096 * hostarch.PageSize, 4128 * hostarch.PageSize, 4129 * hostarch.PageSize},
},
{
name: "LargeSizeSHA512SameFile",
dataSize: 4096 * int64(usermem.PageSize),
dataSize: 4096 * int64(hostarch.PageSize),
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA512,
dataAndTreeInSameFile: true,
expectedDigestSize: 64,
expectedLevelOffset: []int64{4096 * usermem.PageSize, 4160 * usermem.PageSize, 4161 * usermem.PageSize},
expectedLevelOffset: []int64{4096 * hostarch.PageSize, 4160 * hostarch.PageSize, 4161 * hostarch.PageSize},
},
}
@@ -140,8 +141,8 @@ func TestLayout(t *testing.T) {
if err != nil {
t.Fatalf("Failed to InitLayout: %v", err)
}
if l.blockSize != int64(usermem.PageSize) {
t.Errorf("Got blockSize %d, want %d", l.blockSize, usermem.PageSize)
if l.blockSize != int64(hostarch.PageSize) {
t.Errorf("Got blockSize %d, want %d", l.blockSize, hostarch.PageSize)
}
if l.digestSize != tc.expectedDigestSize {
t.Errorf("Got digestSize %d, want %d", l.digestSize, sha256DigestSize)
@@ -202,56 +203,56 @@ func TestGenerate(t *testing.T) {
}{
{
name: "OnePageZeroesSHA256SeparateFile",
data: bytes.Repeat([]byte{0}, usermem.PageSize),
data: bytes.Repeat([]byte{0}, hostarch.PageSize),
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA256,
dataAndTreeInSameFile: false,
expectedHash: []byte{9, 115, 238, 230, 38, 140, 195, 70, 207, 144, 202, 118, 23, 113, 32, 129, 226, 239, 177, 69, 161, 26, 14, 113, 16, 37, 30, 96, 19, 148, 132, 27},
},
{
name: "OnePageZeroesSHA256SameFile",
data: bytes.Repeat([]byte{0}, usermem.PageSize),
data: bytes.Repeat([]byte{0}, hostarch.PageSize),
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA256,
dataAndTreeInSameFile: true,
expectedHash: []byte{9, 115, 238, 230, 38, 140, 195, 70, 207, 144, 202, 118, 23, 113, 32, 129, 226, 239, 177, 69, 161, 26, 14, 113, 16, 37, 30, 96, 19, 148, 132, 27},
},
{
name: "OnePageZeroesSHA512SeparateFile",
data: bytes.Repeat([]byte{0}, usermem.PageSize),
data: bytes.Repeat([]byte{0}, hostarch.PageSize),
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA512,
dataAndTreeInSameFile: false,
expectedHash: []byte{127, 8, 95, 11, 83, 101, 51, 39, 170, 235, 39, 43, 135, 243, 145, 118, 148, 58, 27, 155, 182, 205, 44, 47, 5, 223, 215, 17, 35, 16, 43, 104, 43, 11, 8, 88, 171, 7, 249, 243, 14, 62, 126, 218, 23, 159, 237, 237, 42, 226, 39, 25, 87, 48, 253, 191, 116, 213, 37, 3, 187, 152, 154, 14},
},
{
name: "OnePageZeroesSHA512SameFile",
data: bytes.Repeat([]byte{0}, usermem.PageSize),
data: bytes.Repeat([]byte{0}, hostarch.PageSize),
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA512,
dataAndTreeInSameFile: true,
expectedHash: []byte{127, 8, 95, 11, 83, 101, 51, 39, 170, 235, 39, 43, 135, 243, 145, 118, 148, 58, 27, 155, 182, 205, 44, 47, 5, 223, 215, 17, 35, 16, 43, 104, 43, 11, 8, 88, 171, 7, 249, 243, 14, 62, 126, 218, 23, 159, 237, 237, 42, 226, 39, 25, 87, 48, 253, 191, 116, 213, 37, 3, 187, 152, 154, 14},
},
{
name: "MultiplePageZeroesSHA256SeparateFile",
data: bytes.Repeat([]byte{0}, 128*usermem.PageSize+1),
data: bytes.Repeat([]byte{0}, 128*hostarch.PageSize+1),
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA256,
dataAndTreeInSameFile: false,
expectedHash: []byte{247, 158, 42, 215, 180, 106, 0, 28, 77, 64, 132, 162, 74, 65, 250, 161, 243, 66, 129, 44, 197, 8, 145, 14, 94, 206, 156, 184, 145, 145, 20, 185},
},
{
name: "MultiplePageZeroesSHA256SameFile",
data: bytes.Repeat([]byte{0}, 128*usermem.PageSize+1),
data: bytes.Repeat([]byte{0}, 128*hostarch.PageSize+1),
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA256,
dataAndTreeInSameFile: true,
expectedHash: []byte{247, 158, 42, 215, 180, 106, 0, 28, 77, 64, 132, 162, 74, 65, 250, 161, 243, 66, 129, 44, 197, 8, 145, 14, 94, 206, 156, 184, 145, 145, 20, 185},
},
{
name: "MultiplePageZeroesSHA512SeparateFile",
data: bytes.Repeat([]byte{0}, 128*usermem.PageSize+1),
data: bytes.Repeat([]byte{0}, 128*hostarch.PageSize+1),
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA512,
dataAndTreeInSameFile: false,
expectedHash: []byte{100, 121, 14, 30, 104, 200, 142, 182, 190, 78, 23, 68, 157, 174, 23, 75, 174, 250, 250, 25, 66, 45, 235, 103, 129, 49, 78, 127, 173, 154, 121, 35, 37, 115, 60, 217, 26, 205, 253, 253, 236, 145, 107, 109, 232, 19, 72, 92, 4, 191, 181, 205, 191, 57, 234, 177, 144, 235, 143, 30, 15, 197, 109, 81},
},
{
name: "MultiplePageZeroesSHA512SameFile",
data: bytes.Repeat([]byte{0}, 128*usermem.PageSize+1),
data: bytes.Repeat([]byte{0}, 128*hostarch.PageSize+1),
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA512,
dataAndTreeInSameFile: true,
expectedHash: []byte{100, 121, 14, 30, 104, 200, 142, 182, 190, 78, 23, 68, 157, 174, 23, 75, 174, 250, 250, 25, 66, 45, 235, 103, 129, 49, 78, 127, 173, 154, 121, 35, 37, 115, 60, 217, 26, 205, 253, 253, 236, 145, 107, 109, 232, 19, 72, 92, 4, 191, 181, 205, 191, 57, 234, 177, 144, 235, 143, 30, 15, 197, 109, 81},
@@ -286,28 +287,28 @@ func TestGenerate(t *testing.T) {
},
{
name: "OnePageASHA256SeparateFile",
data: bytes.Repeat([]byte{'a'}, usermem.PageSize),
data: bytes.Repeat([]byte{'a'}, hostarch.PageSize),
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA256,
dataAndTreeInSameFile: false,
expectedHash: []byte{132, 54, 112, 142, 156, 19, 50, 140, 138, 240, 192, 154, 100, 120, 242, 69, 64, 217, 62, 166, 127, 88, 23, 197, 100, 66, 255, 215, 214, 229, 54, 1},
},
{
name: "OnePageASHA256SameFile",
data: bytes.Repeat([]byte{'a'}, usermem.PageSize),
data: bytes.Repeat([]byte{'a'}, hostarch.PageSize),
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA256,
dataAndTreeInSameFile: true,
expectedHash: []byte{132, 54, 112, 142, 156, 19, 50, 140, 138, 240, 192, 154, 100, 120, 242, 69, 64, 217, 62, 166, 127, 88, 23, 197, 100, 66, 255, 215, 214, 229, 54, 1},
},
{
name: "OnePageASHA512SeparateFile",
data: bytes.Repeat([]byte{'a'}, usermem.PageSize),
data: bytes.Repeat([]byte{'a'}, hostarch.PageSize),
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA512,
dataAndTreeInSameFile: false,
expectedHash: []byte{165, 46, 176, 116, 47, 209, 101, 193, 64, 185, 30, 9, 52, 22, 24, 154, 135, 220, 232, 168, 215, 45, 222, 226, 207, 104, 160, 10, 156, 98, 245, 250, 76, 21, 68, 204, 65, 118, 69, 52, 210, 155, 36, 109, 233, 103, 1, 40, 218, 89, 125, 38, 247, 194, 2, 225, 119, 155, 65, 99, 182, 111, 110, 145},
},
{
name: "OnePageASHA512SameFile",
data: bytes.Repeat([]byte{'a'}, usermem.PageSize),
data: bytes.Repeat([]byte{'a'}, hostarch.PageSize),
hashAlgorithms: linux.FS_VERITY_HASH_ALG_SHA512,
dataAndTreeInSameFile: true,
expectedHash: []byte{165, 46, 176, 116, 47, 209, 101, 193, 64, 185, 30, 9, 52, 22, 24, 154, 135, 220, 232, 168, 215, 45, 222, 226, 207, 104, 160, 10, 156, 98, 245, 250, 76, 21, 68, 204, 65, 118, 69, 52, 210, 155, 36, 109, 233, 103, 1, 40, 218, 89, 125, 38, 247, 194, 2, 225, 119, 155, 65, 99, 182, 111, 110, 145},
@@ -415,14 +416,14 @@ func TestVerifyInvalidRange(t *testing.T) {
// Verify range starts outside data range.
{
name: "StartOutsideRange",
verifyStart: usermem.PageSize,
verifyStart: hostarch.PageSize,
verifySize: 1,
},
// Verify range ends outside data range.
{
name: "EndOutsideRange",
verifyStart: 0,
verifySize: 2 * usermem.PageSize,
verifySize: 2 * hostarch.PageSize,
},
// Verify range with negative size.
{
@@ -434,7 +435,7 @@ func TestVerifyInvalidRange(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
_, params := prepareVerify(t, usermem.PageSize /* dataSize */, defaultHashAlgorithm, false /* dataAndTreeInSameFile */, false /* isSymlink */, tc.verifyStart, tc.verifySize, &buf)
_, params := prepareVerify(t, hostarch.PageSize /* dataSize */, defaultHashAlgorithm, false /* dataAndTreeInSameFile */, false /* isSymlink */, tc.verifyStart, tc.verifySize, &buf)
if _, err := Verify(&params); errors.Is(err, nil) {
t.Errorf("Verification succeeded when expected to fail")
}
@@ -467,7 +468,7 @@ func TestVerifyUnmodifiedMetadata(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
_, params := prepareVerify(t, usermem.PageSize /* dataSize */, defaultHashAlgorithm, tc.dataAndTreeInSameFile, tc.isSymlink, 0 /* verifyStart */, 0 /* verifySize */, &buf)
_, params := prepareVerify(t, hostarch.PageSize /* dataSize */, defaultHashAlgorithm, tc.dataAndTreeInSameFile, tc.isSymlink, 0 /* verifyStart */, 0 /* verifySize */, &buf)
if tc.isSymlink {
params.SymlinkTarget = defaultSymlinkPath
}
@@ -495,7 +496,7 @@ func TestVerifyModifiedName(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
_, params := prepareVerify(t, usermem.PageSize /* dataSize */, defaultHashAlgorithm, tc.dataAndTreeInSameFile, false /* isSymlink */, 0 /* verifyStart */, 0 /* verifySize */, &buf)
_, params := prepareVerify(t, hostarch.PageSize /* dataSize */, defaultHashAlgorithm, tc.dataAndTreeInSameFile, false /* isSymlink */, 0 /* verifyStart */, 0 /* verifySize */, &buf)
params.Name += "abc"
if _, err := Verify(&params); errors.Is(err, nil) {
t.Errorf("Verification succeeded when expected to fail")
@@ -521,7 +522,7 @@ func TestVerifyModifiedSize(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
_, params := prepareVerify(t, usermem.PageSize /* dataSize */, defaultHashAlgorithm, tc.dataAndTreeInSameFile, false /* isSymlink */, 0 /* verifyStart */, 0 /* verifySize */, &buf)
_, params := prepareVerify(t, hostarch.PageSize /* dataSize */, defaultHashAlgorithm, tc.dataAndTreeInSameFile, false /* isSymlink */, 0 /* verifyStart */, 0 /* verifySize */, &buf)
params.Size--
if _, err := Verify(&params); errors.Is(err, nil) {
t.Errorf("Verification succeeded when expected to fail")
@@ -547,7 +548,7 @@ func TestVerifyModifiedMode(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
_, params := prepareVerify(t, usermem.PageSize /* dataSize */, defaultHashAlgorithm, tc.dataAndTreeInSameFile, false /* isSymlink */, 0 /* verifyStart */, 0 /* verifySize */, &buf)
_, params := prepareVerify(t, hostarch.PageSize /* dataSize */, defaultHashAlgorithm, tc.dataAndTreeInSameFile, false /* isSymlink */, 0 /* verifyStart */, 0 /* verifySize */, &buf)
params.Mode++
if _, err := Verify(&params); errors.Is(err, nil) {
t.Errorf("Verification succeeded when expected to fail")
@@ -573,7 +574,7 @@ func TestVerifyModifiedUID(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
_, params := prepareVerify(t, usermem.PageSize /* dataSize */, defaultHashAlgorithm, tc.dataAndTreeInSameFile, false /* isSymlink */, 0 /* verifyStart */, 0 /* verifySize */, &buf)
_, params := prepareVerify(t, hostarch.PageSize /* dataSize */, defaultHashAlgorithm, tc.dataAndTreeInSameFile, false /* isSymlink */, 0 /* verifyStart */, 0 /* verifySize */, &buf)
params.UID++
if _, err := Verify(&params); errors.Is(err, nil) {
t.Errorf("Verification succeeded when expected to fail")
@@ -599,7 +600,7 @@ func TestVerifyModifiedGID(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
_, params := prepareVerify(t, usermem.PageSize /* dataSize */, defaultHashAlgorithm, tc.dataAndTreeInSameFile, false /* isSymlink */, 0 /* verifyStart */, 0 /* verifySize */, &buf)
_, params := prepareVerify(t, hostarch.PageSize /* dataSize */, defaultHashAlgorithm, tc.dataAndTreeInSameFile, false /* isSymlink */, 0 /* verifyStart */, 0 /* verifySize */, &buf)
params.GID++
if _, err := Verify(&params); errors.Is(err, nil) {
t.Errorf("Verification succeeded when expected to fail")
@@ -625,7 +626,7 @@ func TestVerifyModifiedChildren(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
_, params := prepareVerify(t, usermem.PageSize /* dataSize */, defaultHashAlgorithm, tc.dataAndTreeInSameFile, false /* isSymlink */, 0 /* verifyStart */, 0 /* verifySize */, &buf)
_, params := prepareVerify(t, hostarch.PageSize /* dataSize */, defaultHashAlgorithm, tc.dataAndTreeInSameFile, false /* isSymlink */, 0 /* verifyStart */, 0 /* verifySize */, &buf)
params.Children["abc"] = struct{}{}
if _, err := Verify(&params); errors.Is(err, nil) {
t.Errorf("Verification succeeded when expected to fail")
@@ -636,7 +637,7 @@ func TestVerifyModifiedChildren(t *testing.T) {
func TestVerifyModifiedSymlink(t *testing.T) {
var buf bytes.Buffer
_, params := prepareVerify(t, usermem.PageSize /* dataSize */, defaultHashAlgorithm, false /* dataAndTreeInSameFile */, true /* isSymlink */, 0 /* verifyStart */, 0 /* verifySize */, &buf)
_, params := prepareVerify(t, hostarch.PageSize /* dataSize */, defaultHashAlgorithm, false /* dataAndTreeInSameFile */, true /* isSymlink */, 0 /* verifyStart */, 0 /* verifySize */, &buf)
params.SymlinkTarget = "merkle_modified_test_link"
if _, err := Verify(&params); err == nil {
t.Errorf("Verification succeeded when expected to fail")
@@ -652,30 +653,30 @@ func TestModifyOutsideVerifyRange(t *testing.T) {
}{
{
name: "BeforeRangeSeparateFile",
modifyByte: 4*usermem.PageSize - 1,
modifyByte: 4*hostarch.PageSize - 1,
dataAndTreeInSameFile: false,
},
{
name: "BeforeRangeSameFile",
modifyByte: 4*usermem.PageSize - 1,
modifyByte: 4*hostarch.PageSize - 1,
dataAndTreeInSameFile: true,
},
{
name: "AfterRangeSeparateFile",
modifyByte: 5 * usermem.PageSize,
modifyByte: 5 * hostarch.PageSize,
dataAndTreeInSameFile: false,
},
{
name: "AfterRangeSameFile",
modifyByte: 5 * usermem.PageSize,
modifyByte: 5 * hostarch.PageSize,
dataAndTreeInSameFile: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
dataSize := int64(8 * usermem.PageSize)
verifyStart := int64(4 * usermem.PageSize)
verifySize := int64(usermem.PageSize)
dataSize := int64(8 * hostarch.PageSize)
verifyStart := int64(4 * hostarch.PageSize)
verifySize := int64(hostarch.PageSize)
var buf bytes.Buffer
// Modified byte is outside verify range. Verify should succeed.
data, params := prepareVerify(t, dataSize, defaultHashAlgorithm, tc.dataAndTreeInSameFile, false /* isSymlink */, verifyStart, verifySize, &buf)
@@ -712,16 +713,16 @@ func TestModifyInsideVerifyRange(t *testing.T) {
// to fail.
{
name: "BlockAlignedRangeSeparateFile",
verifyStart: 4 * usermem.PageSize,
verifySize: usermem.PageSize,
modifyByte: 4 * usermem.PageSize,
verifyStart: 4 * hostarch.PageSize,
verifySize: hostarch.PageSize,
modifyByte: 4 * hostarch.PageSize,
dataAndTreeInSameFile: false,
},
{
name: "BlockAlignedRangeSameFile",
verifyStart: 4 * usermem.PageSize,
verifySize: usermem.PageSize,
modifyByte: 4 * usermem.PageSize,
verifyStart: 4 * hostarch.PageSize,
verifySize: hostarch.PageSize,
modifyByte: 4 * hostarch.PageSize,
dataAndTreeInSameFile: true,
},
// The tests below use a non-block-aligned verify range.
@@ -729,48 +730,48 @@ func TestModifyInsideVerifyRange(t *testing.T) {
// verify to fail.
{
name: "ModifyStartSeparateFile",
verifyStart: 4*usermem.PageSize + 123,
verifySize: 2 * usermem.PageSize,
modifyByte: 4*usermem.PageSize + 123,
verifyStart: 4*hostarch.PageSize + 123,
verifySize: 2 * hostarch.PageSize,
modifyByte: 4*hostarch.PageSize + 123,
dataAndTreeInSameFile: false,
},
{
name: "ModifyStartSameFile",
verifyStart: 4*usermem.PageSize + 123,
verifySize: 2 * usermem.PageSize,
modifyByte: 4*usermem.PageSize + 123,
verifyStart: 4*hostarch.PageSize + 123,
verifySize: 2 * hostarch.PageSize,
modifyByte: 4*hostarch.PageSize + 123,
dataAndTreeInSameFile: true,
},
// Modifying a byte at the end of verify range should cause
// verify to fail.
{
name: "ModifyEndSeparateFile",
verifyStart: 4*usermem.PageSize + 123,
verifySize: 2 * usermem.PageSize,
modifyByte: 6*usermem.PageSize + 123,
verifyStart: 4*hostarch.PageSize + 123,
verifySize: 2 * hostarch.PageSize,
modifyByte: 6*hostarch.PageSize + 123,
dataAndTreeInSameFile: false,
},
{
name: "ModifyEndSameFile",
verifyStart: 4*usermem.PageSize + 123,
verifySize: 2 * usermem.PageSize,
modifyByte: 6*usermem.PageSize + 123,
verifyStart: 4*hostarch.PageSize + 123,
verifySize: 2 * hostarch.PageSize,
modifyByte: 6*hostarch.PageSize + 123,
dataAndTreeInSameFile: true,
},
// Modifying a byte in the middle verified block should cause
// verify to fail.
{
name: "ModifyMiddleSeparateFile",
verifyStart: 4*usermem.PageSize + 123,
verifySize: 2 * usermem.PageSize,
modifyByte: 5*usermem.PageSize + 123,
verifyStart: 4*hostarch.PageSize + 123,
verifySize: 2 * hostarch.PageSize,
modifyByte: 5*hostarch.PageSize + 123,
dataAndTreeInSameFile: false,
},
{
name: "ModifyMiddleSameFile",
verifyStart: 4*usermem.PageSize + 123,
verifySize: 2 * usermem.PageSize,
modifyByte: 5*usermem.PageSize + 123,
verifyStart: 4*hostarch.PageSize + 123,
verifySize: 2 * hostarch.PageSize,
modifyByte: 5*hostarch.PageSize + 123,
dataAndTreeInSameFile: true,
},
// Modifying a byte in the first block in the verified range
@@ -778,16 +779,16 @@ func TestModifyInsideVerifyRange(t *testing.T) {
// out of verify range.
{
name: "ModifyFirstBlockSeparateFile",
verifyStart: 4*usermem.PageSize + 123,
verifySize: 2 * usermem.PageSize,
modifyByte: 4*usermem.PageSize + 122,
verifyStart: 4*hostarch.PageSize + 123,
verifySize: 2 * hostarch.PageSize,
modifyByte: 4*hostarch.PageSize + 122,
dataAndTreeInSameFile: false,
},
{
name: "ModifyFirstBlockSameFile",
verifyStart: 4*usermem.PageSize + 123,
verifySize: 2 * usermem.PageSize,
modifyByte: 4*usermem.PageSize + 122,
verifyStart: 4*hostarch.PageSize + 123,
verifySize: 2 * hostarch.PageSize,
modifyByte: 4*hostarch.PageSize + 122,
dataAndTreeInSameFile: true,
},
// Modifying a byte in the last block in the verified range
@@ -795,22 +796,22 @@ func TestModifyInsideVerifyRange(t *testing.T) {
// out of verify range.
{
name: "ModifyLastBlockSeparateFile",
verifyStart: 4*usermem.PageSize + 123,
verifySize: 2 * usermem.PageSize,
modifyByte: 6*usermem.PageSize + 124,
verifyStart: 4*hostarch.PageSize + 123,
verifySize: 2 * hostarch.PageSize,
modifyByte: 6*hostarch.PageSize + 124,
dataAndTreeInSameFile: false,
},
{
name: "ModifyLastBlockSameFile",
verifyStart: 4*usermem.PageSize + 123,
verifySize: 2 * usermem.PageSize,
modifyByte: 6*usermem.PageSize + 124,
verifyStart: 4*hostarch.PageSize + 123,
verifySize: 2 * hostarch.PageSize,
modifyByte: 6*hostarch.PageSize + 124,
dataAndTreeInSameFile: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
dataSize := int64(8 * usermem.PageSize)
dataSize := int64(8 * hostarch.PageSize)
var buf bytes.Buffer
data, params := prepareVerify(t, dataSize, defaultHashAlgorithm, tc.dataAndTreeInSameFile, false /* isSymlink */, tc.verifyStart, tc.verifySize, &buf)
// Flip a bit in data and checks Verify results.
@@ -854,7 +855,7 @@ func TestVerifyRandom(t *testing.T) {
rand.Seed(time.Now().UnixNano())
// Use a random dataSize. Minimum size 2 so that we can pick a random
// portion from it.
dataSize := rand.Int63n(200*usermem.PageSize) + 2
dataSize := rand.Int63n(200*hostarch.PageSize) + 2
// Pick a random portion of data.
start := rand.Int63n(dataSize - 1)
+1 -1
View File
@@ -77,10 +77,10 @@ go_library(
visibility = ["//pkg/sentry:internal"],
deps = [
"//pkg/cpuid",
"//pkg/hostarch",
"//pkg/ring0/pagetables",
"//pkg/safecopy",
"//pkg/sentry/arch",
"//pkg/sentry/arch/fpu",
"//pkg/usermem",
],
)

Some files were not shown because too many files have changed in this diff Show More