github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/sys/syz-extract/netbsd.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 netbsd struct{}
    16  
    17  func (*netbsd) prepare(sourcedir string, build bool, arches []*Arch) error {
    18  	if !build {
    19  		return fmt.Errorf("netbsd requires -build flag")
    20  	}
    21  	return nil
    22  }
    23  
    24  func (*netbsd) prepareArch(arch *Arch) error {
    25  	links := [][2]string{
    26  		{"amd64", "machine"},
    27  		{"amd64", "amd64"},
    28  		{"x86", "x86"},
    29  	}
    30  	for _, v := range links {
    31  		if err := machineLink(arch, v[0], v[1]); err != nil {
    32  			return err
    33  		}
    34  	}
    35  	return nil
    36  }
    37  
    38  func machineLink(arch *Arch, machine, dest string) error {
    39  	if err := os.Symlink(machineInclude(arch, machine),
    40  		filepath.Join(arch.buildDir, dest)); err != nil {
    41  		return fmt.Errorf("failed to create link: %w", err)
    42  	}
    43  	return nil
    44  }
    45  
    46  func machineInclude(arch *Arch, machine string) string {
    47  	return filepath.Join(arch.sourceDir, "sys", "arch", machine, "include")
    48  }
    49  
    50  func (*netbsd) processFile(arch *Arch, info *compiler.ConstInfo) (map[string]uint64, map[string]bool, error) {
    51  	args := []string{
    52  		"-fmessage-length=0",
    53  		"-nostdinc",
    54  		"-D_KERNEL",
    55  		"-D__BSD_VISIBLE=1",
    56  		"-I", filepath.Join(arch.sourceDir, "sys"),
    57  		"-I", filepath.Join(arch.sourceDir, "sys", "sys"),
    58  		"-I", filepath.Join(arch.sourceDir, "sys", "arch", "amd64"),
    59  		"-I", filepath.Join(arch.sourceDir, "common", "include"),
    60  		"-I", filepath.Join(arch.sourceDir, "sys", "compat", "linux", "common"),
    61  		"-I", filepath.Join(arch.sourceDir, "include"),
    62  		"-I", arch.buildDir,
    63  	}
    64  	for _, incdir := range info.Incdirs {
    65  		args = append(args, "-I"+filepath.Join(arch.sourceDir, incdir))
    66  	}
    67  	if arch.includeDirs != "" {
    68  		for _, dir := range strings.Split(arch.includeDirs, ",") {
    69  			args = append(args, "-I"+dir)
    70  		}
    71  	}
    72  	// Syscall consts on netbsd have weird prefixes sometimes,
    73  	// try to extract consts with these prefixes as well.
    74  	compatNames := make(map[string][]string)
    75  	for _, def := range info.Consts {
    76  		const SYS = "SYS_"
    77  		if strings.HasPrefix(def.Name, SYS) {
    78  			for _, prefix := range []string{"_", "__", "___"} {
    79  				for _, suffix := range []string{"30", "50"} {
    80  					compat := SYS + prefix + def.Name[len(SYS):] + suffix
    81  					compatNames[def.Name] = append(compatNames[def.Name], compat)
    82  					info.Consts = append(info.Consts, &compiler.Const{Name: compat})
    83  				}
    84  			}
    85  		} else {
    86  			compat := "LINUX_" + def.Name
    87  			compatNames[def.Name] = append(compatNames[def.Name], compat)
    88  			info.Consts = append(info.Consts, &compiler.Const{Name: compat})
    89  		}
    90  	}
    91  	params := &extractParams{
    92  		AddSource:    "#include <sys/syscall.h>",
    93  		TargetEndian: arch.target.HostEndian,
    94  	}
    95  	res, undeclared, err := extract(info, "gcc", args, params)
    96  	for orig, compats := range compatNames {
    97  		for _, compat := range compats {
    98  			if undeclared[orig] && !undeclared[compat] {
    99  				res[orig] = res[compat]
   100  				delete(res, compat)
   101  				delete(undeclared, orig)
   102  			}
   103  			delete(undeclared, compat)
   104  		}
   105  	}
   106  	return res, undeclared, err
   107  }