github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/sys/syz-extract/openbsd.go (about)

     1  // Copyright 2017 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  
    12  	"github.com/google/syzkaller/pkg/compiler"
    13  )
    14  
    15  type openbsd struct{}
    16  
    17  func (*openbsd) prepare(sourcedir string, build bool, arches []*Arch) error {
    18  	if !build {
    19  		return fmt.Errorf("openbsd requires -build flag")
    20  	}
    21  	return nil
    22  }
    23  
    24  func (*openbsd) prepareArch(arch *Arch) error {
    25  	if err := os.Symlink(filepath.Join(arch.sourceDir, "sys", "arch", "amd64", "include"),
    26  		filepath.Join(arch.buildDir, "amd64")); err != nil {
    27  		return fmt.Errorf("failed to create link: %w", err)
    28  	}
    29  	if err := os.Symlink(filepath.Join(arch.sourceDir, "sys", "arch", "amd64", "include"),
    30  		filepath.Join(arch.buildDir, "machine")); err != nil {
    31  		return fmt.Errorf("failed to create link: %w", err)
    32  	}
    33  	return nil
    34  }
    35  
    36  func (*openbsd) processFile(arch *Arch, info *compiler.ConstInfo) (map[string]uint64, map[string]bool, error) {
    37  	args := []string{
    38  		"-fmessage-length=0",
    39  		"-nostdinc",
    40  		"-D_KERNEL",
    41  		"-D__BSD_VISIBLE=1",
    42  		"-I", filepath.Join(arch.sourceDir, "sys"),
    43  		"-I", filepath.Join(arch.sourceDir, "sys", "sys"),
    44  		"-I", filepath.Join(arch.sourceDir, "sys", "arch", "amd64"),
    45  		"-I", filepath.Join(arch.sourceDir, "common", "include"),
    46  		"-I", filepath.Join(arch.sourceDir, "sys", "compat", "linux", "common"),
    47  		"-I", arch.buildDir,
    48  	}
    49  	for _, incdir := range info.Incdirs {
    50  		args = append(args, "-I"+filepath.Join(arch.sourceDir, incdir))
    51  	}
    52  	if arch.includeDirs != "" {
    53  		for _, dir := range strings.Split(arch.includeDirs, ",") {
    54  			args = append(args, "-I"+dir)
    55  		}
    56  	}
    57  	// Some syscalls on OpenBSD are prefixed with `SYS___' as opposed of the
    58  	// more common `SYS_' prefix.
    59  	syscallsQuirks := map[string]bool{
    60  		"SYS_get_tcb":      true,
    61  		"SYS_getcwd":       true,
    62  		"SYS_realpath":     true,
    63  		"SYS_semctl":       true,
    64  		"SYS_set_tcb":      true,
    65  		"SYS_syscall":      true,
    66  		"SYS_tfork":        true,
    67  		"SYS_threxit":      true,
    68  		"SYS_thrsigdivert": true,
    69  		"SYS_thrsleep":     true,
    70  		"SYS_thrwakeup":    true,
    71  		"SYS_tmpfd":        true,
    72  	}
    73  	compatNames := make(map[string][]string)
    74  	for _, def := range info.Consts {
    75  		if _, ok := syscallsQuirks[def.Name]; ok {
    76  			compat := "SYS___" + def.Name[len("SYS_"):]
    77  			compatNames[def.Name] = append(compatNames[def.Name], compat)
    78  			info.Consts = append(info.Consts, &compiler.Const{Name: compat})
    79  		}
    80  	}
    81  	params := &extractParams{
    82  		AddSource:    "#include <sys/syscall.h>",
    83  		TargetEndian: arch.target.HostEndian,
    84  	}
    85  	res, undeclared, err := extract(info, "cc", args, params)
    86  	for orig, compats := range compatNames {
    87  		for _, compat := range compats {
    88  			if undeclared[orig] && !undeclared[compat] {
    89  				res[orig] = res[compat]
    90  				delete(res, compat)
    91  				delete(undeclared, orig)
    92  			}
    93  			delete(undeclared, compat)
    94  		}
    95  	}
    96  	return res, undeclared, err
    97  }