Skip to content

Commit

Permalink
Move bind filter to different package
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Adrian Samfira <[email protected]>
  • Loading branch information
gabriel-samfira committed Feb 6, 2023
1 parent 9a22fe1 commit fc72bee
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 79 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ jobs:
- uses: actions/setup-go@v3
with:
go-version: ${{ env.GO_VERSION }}
- run: go test -gcflags=all=-d=checkptr -v ./...
- name: Run tests on ltsc 2019
if: matrix.os == 'windows-2019'
run: go test -gcflags=all=-d=checkptr -v --test.run="[^TestEnsureOnlyOneTargetCanBeMounted|^TestGetBindMappings|^TestRemoveFileBinding]" ./...
- name: Run tests
if: matrix.os != 'windows-2019'
run: go test -gcflags=all=-d=checkptr -v ./...

build:
name: Build Repo
Expand Down
10 changes: 7 additions & 3 deletions bind_filter.go → pkg/bindfilter/bind_filter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build windows
// +build windows

package winio
package bindfilter

import (
"bytes"
Expand All @@ -18,12 +18,15 @@ import (
"golang.org/x/sys/windows"
)

//go:generate go run github.com/Microsoft/go-winio/tools/mkwinsyscall -output zsyscall_windows.go ./bind_filter.go
//sys bfSetupFilter(jobHandle windows.Handle, flags uint32, virtRootPath *uint16, virtTargetPath *uint16, virtExceptions **uint16, virtExceptionPathCount uint32) (hr error) = bindfltapi.BfSetupFilter?
//sys bfRemoveMapping(jobHandle windows.Handle, virtRootPath *uint16) (hr error) = bindfltapi.BfRemoveMapping?
//sys bfGetMappings(flags uint32, jobHandle windows.Handle, virtRootPath *uint16, sid *windows.SID, bufferSize *uint32, outBuffer uintptr) (hr error) = bindfltapi.BfGetMappings?

// BfSetupFilter flags. See:
// https://github.com/microsoft/BuildXL/blob/a6dce509f0d4f774255e5fbfb75fa6d5290ed163/Public/Src/Utilities/Native/Processes/Windows/NativeContainerUtilities.cs#L193-L240
//
//nolint:revive // var-naming: ALL_CAPS
const (
BINDFLT_FLAG_READ_ONLY_MAPPING uint32 = 0x00000001
// Generates a merged binding, mapping target entries to the virtualization root.
Expand All @@ -48,6 +51,7 @@ const (
BINDFLT_FLAG_BATCHED_REMOVE_MAPPINGS uint32 = 0x20000000
)

//nolint:revive // var-naming: ALL_CAPS
const (
BINDFLT_GET_MAPPINGS_FLAG_VOLUME uint32 = 0x00000001
BINDFLT_GET_MAPPINGS_FLAG_SILO uint32 = 0x00000002
Expand Down Expand Up @@ -128,7 +132,7 @@ func GetBindMappings(volumePath string) ([]BindMapping, error) {
return nil, err
}

var flags uint32 = BINDFLT_GET_MAPPINGS_FLAG_VOLUME
flags := BINDFLT_GET_MAPPINGS_FLAG_VOLUME
// allocate a large buffer for results
var outBuffSize uint32 = 256 * 1024
buf := make([]byte, outBuffSize)
Expand Down Expand Up @@ -224,7 +228,7 @@ func getTargetsFromBuffer(buffer []byte, offset, count int) ([]string, error) {
if len(buffer) < int(tgt.TargetRootOffset)+int(tgt.TargetRootLength) {
return nil, fmt.Errorf("invalid buffer")
}
decoded, err := decodeEntry(buffer[tgt.TargetRootOffset : uint32(tgt.TargetRootOffset)+uint32(tgt.TargetRootLength)])
decoded, err := decodeEntry(buffer[tgt.TargetRootOffset : tgt.TargetRootOffset+tgt.TargetRootLength])
if err != nil {
return nil, fmt.Errorf("decoding name: %w", err)
}
Expand Down
49 changes: 27 additions & 22 deletions bind_filter_test.go → pkg/bindfilter/bind_filter_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build windows
// +build windows

package winio
package bindfilter

import (
"errors"
Expand All @@ -23,11 +23,11 @@ func TestApplyFileBinding(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer RemoveFileBinding(destination)
defer removeFileBinding(t, destination)

data := []byte("bind filter test")

if err := os.WriteFile(srcFile, data, 0755); err != nil {
if err := os.WriteFile(srcFile, data, 0600); err != nil {
t.Fatal(err)
}

Expand All @@ -51,6 +51,12 @@ func TestApplyFileBinding(t *testing.T) {
}
}

func removeFileBinding(t *testing.T, mountpoint string) {
if err := RemoveFileBinding(mountpoint); err != nil {
t.Logf("failed to remove file binding from %s: %q", mountpoint, err)
}
}

func TestApplyFileBindingReadOnly(t *testing.T) {
source := t.TempDir()
destination := t.TempDir()
Expand All @@ -62,11 +68,11 @@ func TestApplyFileBindingReadOnly(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer RemoveFileBinding(destination)
defer removeFileBinding(t, destination)

data := []byte("bind filter test")

if err := os.WriteFile(srcFile, data, 0755); err != nil {
if err := os.WriteFile(srcFile, data, 0600); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -99,10 +105,11 @@ func TestEnsureOnlyOneTargetCanBeMounted(t *testing.T) {
t.Fatal(err)
}

defer RemoveFileBinding(destination)
defer removeFileBinding(t, destination)

err = ApplyFileBinding(destination, secondarySource, false)
if err == nil {
RemoveFileBinding(destination)
removeFileBinding(t, destination)
t.Fatalf("we should not be able to mount multiple targets in the same destination")
}
}
Expand Down Expand Up @@ -150,7 +157,7 @@ func TestGetBindMappings(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer RemoveFileBinding(destination)
defer removeFileBinding(t, destination)

hasMapping, err := checkSourceIsMountedOnDestination(source, destination)
if err != nil {
Expand All @@ -177,32 +184,30 @@ func TestRemoveFileBinding(t *testing.T) {
t.Fatalf("failed to get long path")
}

err = ApplyFileBinding(destination, source, false)
if err != nil {
fileName := "testFile.txt"
srcFile := filepath.Join(source, fileName)
dstFile := filepath.Join(destination, fileName)
data := []byte("bind filter test")

if err := os.WriteFile(srcFile, data, 0600); err != nil {
t.Fatal(err)
}

hasMapping, err := checkSourceIsMountedOnDestination(source, destination)
err = ApplyFileBinding(destination, source, false)
if err != nil {
RemoveFileBinding(destination)
t.Fatal(err)
}
defer removeFileBinding(t, destination)

if !hasMapping {
RemoveFileBinding(destination)
t.Fatalf("expected to find %s mounted on %s, but could not", source, destination)
if _, err := os.Stat(dstFile); err != nil {
t.Fatalf("expected to find %s, but did not", dstFile)
}

if err := RemoveFileBinding(destination); err != nil {
t.Fatal(err)
}

hasMapping, err = checkSourceIsMountedOnDestination(source, destination)
if err != nil {
t.Fatal(err)
}

if hasMapping {
t.Fatalf("expected to find %s unmounted from %s, but it seems to still be mounted", source, destination)
if _, err := os.Stat(dstFile); err == nil {
t.Fatalf("expected %s to be gone, but it not", dstFile)
}
}
93 changes: 93 additions & 0 deletions pkg/bindfilter/zsyscall_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 4 additions & 53 deletions zsyscall_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fc72bee

Please sign in to comment.