aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/host/host_linux_test.go
blob: e32fe09cc5315e20268252784b2a27bd97eb7566 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2015 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

// +build linux

package host

import (
	"runtime"
	"syscall"
	"testing"

	"github.com/google/syzkaller/prog"
	_ "github.com/google/syzkaller/sys"
)

func TestLog(t *testing.T) {
	t.Parallel()
	target, err := prog.GetTarget("linux", runtime.GOARCH)
	if err != nil {
		t.Fatal(err)
	}
	// Dump for manual inspection.
	supp, err := DetectSupportedSyscalls(target)
	if err != nil {
		t.Skipf("skipping: %v", err)
	}
	t.Logf("unsupported:")
	for _, c := range target.Syscalls {
		s, ok := supp[c]
		if ok && !s {
			t.Fatalf("map contains false value")
		}
		if !s {
			t.Logf("\t%v", c.Name)
		}
	}
	trans := target.TransitivelyEnabledCalls(supp)
	t.Logf("transitively unsupported:")
	for _, c := range target.Syscalls {
		s, ok := trans[c]
		if ok && !s {
			t.Fatalf("map contains false value")
		}
		if !s && supp[c] {
			t.Logf("\t%v", c.Name)
		}
	}
}

func TestSupportedSyscalls(t *testing.T) {
	t.Parallel()
	target, err := prog.GetTarget("linux", runtime.GOARCH)
	if err != nil {
		t.Fatal(err)
	}
	supp, err := DetectSupportedSyscalls(target)
	if err != nil {
		t.Skipf("skipping: %v", err)
	}
	// These are safe to execute with invalid arguments.
	safe := []string{
		"memfd_create",
		"sendfile",
		"bpf$MAP_CREATE",
		"open",
		"openat",
		"read",
		"write",
		"stat",
	}
	for _, name := range safe {
		c := target.SyscallMap[name]
		if c == nil {
			t.Fatalf("can't find syscall '%v'", name)
		}
		a := ^uintptr(0) - 4097 // hopefully invalid
		_, _, err := syscall.Syscall6(uintptr(c.NR), a, a, a, a, a, a)
		if err == 0 {
			t.Fatalf("%v did not fail", name)
		}
		if ok := err != syscall.ENOSYS; ok != supp[c] {
			t.Fatalf("syscall %v: perse=%v kallsyms=%v", name, ok, supp[c])
		}
	}
}