From 81e199f71969b97c80cbb4473ddd53adeed3b4d4 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 2 Oct 2017 21:48:24 +0200 Subject: sys/syz-extract: support freebsd --- sys/freebsd/amd64.go | 35 ++++++++++++++-------- sys/freebsd/sys.txt | 8 +++++ sys/freebsd/sys_amd64.const | 14 +++++---- sys/syz-extract/extract.go | 1 + sys/syz-extract/freebsd.go | 71 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 18 deletions(-) create mode 100644 sys/syz-extract/freebsd.go (limited to 'sys') diff --git a/sys/freebsd/amd64.go b/sys/freebsd/amd64.go index 01ff37870..81422e6c7 100644 --- a/sys/freebsd/amd64.go +++ b/sys/freebsd/amd64.go @@ -7,28 +7,39 @@ func init() { RegisterTarget(&Target{OS: "freebsd", Arch: "amd64", Revision: revision_amd64, PtrSize: 8, Syscalls: syscalls_amd64, Resources: resources_amd64, Structs: structDescs_amd64, Consts: consts_amd64}, initTarget) } -var resources_amd64 = []*ResourceDesc(nil) +var resources_amd64 = []*ResourceDesc{ + {Name: "fd", Type: &IntType{IntTypeCommon: IntTypeCommon{TypeCommon: TypeCommon{TypeName: "int32", TypeSize: 4}}}, Kind: []string{"fd"}, Values: []uint64{18446744073709551615}}, +} -var structDescs_amd64 = []*KeyedStruct(nil) +var structDescs_amd64 = []*KeyedStruct{ + {Key: StructKey{Name: "pipefd", Dir: 1}, Desc: &StructDesc{TypeCommon: TypeCommon{TypeName: "pipefd", TypeSize: 8, ArgDir: 1}, Fields: []Type{ + &ResourceType{TypeCommon: TypeCommon{TypeName: "fd", FldName: "rfd", TypeSize: 4, ArgDir: 1}}, + &ResourceType{TypeCommon: TypeCommon{TypeName: "fd", FldName: "wfd", TypeSize: 4, ArgDir: 1}}, + }}}, +} var syscalls_amd64 = []*Syscall{ - {NR: 1, Name: "mmap", CallName: "mmap", Args: []Type{ + {NR: 477, Name: "mmap", CallName: "mmap", Args: []Type{ &VmaType{TypeCommon: TypeCommon{TypeName: "vma", FldName: "addr", TypeSize: 8}}, &LenType{IntTypeCommon: IntTypeCommon{TypeCommon: TypeCommon{TypeName: "len", FldName: "len", TypeSize: 8}}, Buf: "addr"}, - &FlagsType{IntTypeCommon: IntTypeCommon{TypeCommon: TypeCommon{TypeName: "mmap_prot", FldName: "prot", TypeSize: 8}}, Vals: []uint64{2, 3}}, - &FlagsType{IntTypeCommon: IntTypeCommon{TypeCommon: TypeCommon{TypeName: "mmap_flags", FldName: "flags", TypeSize: 8}}, Vals: []uint64{4, 5, 6}}, + &FlagsType{IntTypeCommon: IntTypeCommon{TypeCommon: TypeCommon{TypeName: "mmap_prot", FldName: "prot", TypeSize: 8}}, Vals: []uint64{1, 2}}, + &FlagsType{IntTypeCommon: IntTypeCommon{TypeCommon: TypeCommon{TypeName: "mmap_flags", FldName: "flags", TypeSize: 8}}, Vals: []uint64{2, 4096, 16}}, &ConstType{IntTypeCommon: IntTypeCommon{TypeCommon: TypeCommon{TypeName: "const", FldName: "fd", TypeSize: 8}}, Val: 18446744073709551615}, &ConstType{IntTypeCommon: IntTypeCommon{TypeCommon: TypeCommon{TypeName: "const", FldName: "offset", TypeSize: 8}}}, }, Ret: &VmaType{TypeCommon: TypeCommon{TypeName: "vma", FldName: "ret", TypeSize: 8, ArgDir: 1}}}, + {ID: 1, NR: 42, Name: "pipe", CallName: "pipe", Args: []Type{ + &PtrType{TypeCommon: TypeCommon{TypeName: "ptr", FldName: "pipefd", TypeSize: 8}, Type: &StructType{Key: StructKey{Name: "pipefd", Dir: 1}}}, + }}, } var consts_amd64 = []ConstValue{ - {Name: "MAP_ANONYMOUS", Value: 5}, - {Name: "MAP_FIXED", Value: 6}, - {Name: "MAP_PRIVATE", Value: 4}, - {Name: "PROT_READ", Value: 2}, - {Name: "PROT_WRITE", Value: 3}, - {Name: "SYS_mmap", Value: 1}, + {Name: "MAP_ANONYMOUS", Value: 4096}, + {Name: "MAP_FIXED", Value: 16}, + {Name: "MAP_PRIVATE", Value: 2}, + {Name: "PROT_READ", Value: 1}, + {Name: "PROT_WRITE", Value: 2}, + {Name: "SYS_mmap", Value: 477}, + {Name: "SYS_pipe", Value: 42}, } -const revision_amd64 = "8e89c92ba6210bfbb714647598abe10b8cbb4edf" +const revision_amd64 = "7c737d486a33a6a0817ce924247b4b67428f7a07" diff --git a/sys/freebsd/sys.txt b/sys/freebsd/sys.txt index 6bb524299..5910d26c0 100644 --- a/sys/freebsd/sys.txt +++ b/sys/freebsd/sys.txt @@ -3,7 +3,15 @@ include +resource fd[int32]: 0xffffffffffffffff + mmap(addr vma, len len[addr], prot flags[mmap_prot], flags flags[mmap_flags], fd const[0xffffffffffffffff], offset const[0]) vma +pipe(pipefd ptr[out, pipefd]) mmap_prot = PROT_READ, PROT_WRITE mmap_flags = MAP_PRIVATE, MAP_ANONYMOUS, MAP_FIXED + +pipefd { + rfd fd + wfd fd +} diff --git a/sys/freebsd/sys_amd64.const b/sys/freebsd/sys_amd64.const index 16b860253..327dfb024 100644 --- a/sys/freebsd/sys_amd64.const +++ b/sys/freebsd/sys_amd64.const @@ -1,6 +1,8 @@ -SYS_mmap = 1 -PROT_READ = 2 -PROT_WRITE = 3 -MAP_PRIVATE = 4 -MAP_ANONYMOUS = 5 -MAP_FIXED = 6 +# AUTOGENERATED FILE +MAP_ANONYMOUS = 4096 +MAP_FIXED = 16 +MAP_PRIVATE = 2 +PROT_READ = 1 +PROT_WRITE = 2 +SYS_mmap = 477 +SYS_pipe = 42 diff --git a/sys/syz-extract/extract.go b/sys/syz-extract/extract.go index 523a47d64..17b4728ab 100644 --- a/sys/syz-extract/extract.go +++ b/sys/syz-extract/extract.go @@ -53,6 +53,7 @@ type OS interface { var oses = map[string]OS{ "linux": new(linux), + "freebsd": new(freebsd), "android": new(linux), "fuchsia": new(fuchsia), "windows": new(windows), diff --git a/sys/syz-extract/freebsd.go b/sys/syz-extract/freebsd.go new file mode 100644 index 000000000..66c1e9207 --- /dev/null +++ b/sys/syz-extract/freebsd.go @@ -0,0 +1,71 @@ +// 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 freebsd struct{} + +func (*freebsd) prepare(sourcedir string, build bool, arches []string) error { + if sourcedir == "" { + return fmt.Errorf("provide path to kernel checkout via -sourcedir flag (or make extract SOURCEDIR)") + } + if !build { + return fmt.Errorf("freebsd requires -build flag") + } + return nil +} + +func (*freebsd) prepareArch(arch *Arch) error { + if err := os.Symlink(filepath.Join(arch.sourceDir, "sys", "x86", "include"), + filepath.Join(arch.buildDir, "machine")); err != nil { + return fmt.Errorf("failed to create link: %v", err) + } + return nil +} + +func (*freebsd) processFile(arch *Arch, info *compiler.ConstInfo) (map[string]uint64, map[string]bool, error) { + args := []string{ + "-fmessage-length=0", + "-nostdinc", + "-I", filepath.Join(arch.sourceDir, "sys"), + "-I", filepath.Join(arch.sourceDir, "sys", "sys"), + "-I", arch.buildDir, + } + for _, incdir := range info.Incdirs { + args = append(args, "-I"+filepath.Join(arch.sourceDir, incdir)) + } + // Syscall consts on freebsd have weird prefixes sometimes, + // try to extract consts with these prefixes as well. + compatNames := make(map[string][]string) + for _, val := range info.Consts { + const SYS = "SYS_" + if strings.HasPrefix(val, SYS) { + for _, prefix := range []string{"__", "freebsd11_", "freebsd10_", "freebsd7_"} { + compat := SYS + prefix + val[len(SYS):] + compatNames[val] = append(compatNames[val], compat) + info.Consts = append(info.Consts, compat) + } + } + } + res, undeclared, err := extract(info, "gcc", args, "#include ") + 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 +} -- cgit mrf-deployment