aboutsummaryrefslogtreecommitdiffstats
path: root/sys/syz-extract/openbsd.go
blob: 9d9a7026ee95629789c5bad294a044a11022de9a (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
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2017 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.

package main

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/google/syzkaller/pkg/compiler"
)

type openbsd struct{}

func (*openbsd) prepare(sourcedir string, build bool, arches []*Arch) error {
	if !build {
		return fmt.Errorf("openbsd requires -build flag")
	}
	return nil
}

func (*openbsd) prepareArch(arch *Arch) error {
	if err := os.Symlink(filepath.Join(arch.sourceDir, "sys", "arch", "amd64", "include"),
		filepath.Join(arch.buildDir, "amd64")); err != nil {
		return fmt.Errorf("failed to create link: %w", err)
	}
	if err := os.Symlink(filepath.Join(arch.sourceDir, "sys", "arch", "amd64", "include"),
		filepath.Join(arch.buildDir, "machine")); err != nil {
		return fmt.Errorf("failed to create link: %w", err)
	}
	return nil
}

func (*openbsd) processFile(arch *Arch, info *compiler.ConstInfo) (map[string]uint64, map[string]bool, error) {
	args := []string{
		"-fmessage-length=0",
		"-nostdinc",
		"-D_KERNEL",
		"-D__BSD_VISIBLE=1",
		"-I", filepath.Join(arch.sourceDir, "sys"),
		"-I", filepath.Join(arch.sourceDir, "sys", "sys"),
		"-I", filepath.Join(arch.sourceDir, "sys", "arch", "amd64"),
		"-I", filepath.Join(arch.sourceDir, "common", "include"),
		"-I", filepath.Join(arch.sourceDir, "sys", "compat", "linux", "common"),
		"-I", arch.buildDir,
	}
	for _, incdir := range info.Incdirs {
		args = append(args, "-I"+filepath.Join(arch.sourceDir, incdir))
	}
	if arch.includeDirs != "" {
		for _, dir := range strings.Split(arch.includeDirs, ",") {
			args = append(args, "-I"+dir)
		}
	}
	// Some syscalls on OpenBSD are prefixed with `SYS___' as opposed of the
	// more common `SYS_' prefix.
	syscallsQuirks := map[string]bool{
		"SYS_get_tcb":      true,
		"SYS_getcwd":       true,
		"SYS_realpath":     true,
		"SYS_semctl":       true,
		"SYS_set_tcb":      true,
		"SYS_syscall":      true,
		"SYS_tfork":        true,
		"SYS_threxit":      true,
		"SYS_thrsigdivert": true,
		"SYS_thrsleep":     true,
		"SYS_thrwakeup":    true,
		"SYS_tmpfd":        true,
	}
	compatNames := make(map[string][]string)
	for _, def := range info.Consts {
		if _, ok := syscallsQuirks[def.Name]; ok {
			compat := "SYS___" + def.Name[len("SYS_"):]
			compatNames[def.Name] = append(compatNames[def.Name], compat)
			info.Consts = append(info.Consts, &compiler.Const{Name: compat})
		}
	}
	params := &extractParams{
		AddSource:    "#include <sys/syscall.h>",
		TargetEndian: arch.target.HostEndian,
	}
	res, undeclared, err := extract(info, "cc", args, params)
	for orig, compats := range compatNames {
		for _, compat := range compats {
			if undeclared[orig] && !undeclared[compat] {
				res[orig] = res[compat]
				delete(res, compat)
				delete(undeclared, orig)
			}
			delete(undeclared, compat)
		}
	}
	return res, undeclared, err
}