From a1cd6a69c49732b1b24bad2fab34857aaa399a60 Mon Sep 17 00:00:00 2001 From: Aleksandr Nogikh Date: Wed, 12 Jan 2022 12:49:48 +0000 Subject: sys/syz-sysgen: generate possibly missing __NR/SYS defines As the comiling machine may have a kernel version different from the tested one, not all definitions might be present. Generate sequences of ifndef in defs.h to avoid potential issues. Restrict __NR-related style checking rules to only checking common*.h files. --- executor/style_test.go | 1 + sys/syz-sysgen/sysgen.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/executor/style_test.go b/executor/style_test.go index 3bb581886..0bc1c73bd 100644 --- a/executor/style_test.go +++ b/executor/style_test.go @@ -65,6 +65,7 @@ if (foo) { "These should be guarded by #ifndef __NR_foo, but this is dependent on the host " + "and may break on other machines (after pkg/csource processing).\n" + "Define sys_foo constants instead.", + commonOnly: true, tests: []string{ ` #ifndef __NR_io_uring_setup diff --git a/sys/syz-sysgen/sysgen.go b/sys/syz-sysgen/sysgen.go index 4099bfb51..9ebaac382 100644 --- a/sys/syz-sysgen/sysgen.go +++ b/sys/syz-sysgen/sysgen.go @@ -35,6 +35,11 @@ type SyscallData struct { Attrs []uint64 } +type Define struct { + Name string + Value string +} + type ArchData struct { Revision string ForkServer int @@ -44,6 +49,7 @@ type ArchData struct { NumPages uint64 DataOffset uint64 Calls []SyscallData + Defines []Define } type OSData struct { @@ -277,6 +283,7 @@ func generateExecutorSyscalls(target *targets.Target, syscalls []*prog.Syscall, if target.ExecutorUsesShmem { data.Shmem = 1 } + defines := make(map[string]string) for _, c := range syscalls { var attrVals []uint64 attrs := reflect.ValueOf(c.Attrs) @@ -300,10 +307,25 @@ func generateExecutorSyscalls(target *targets.Target, syscalls []*prog.Syscall, } } data.Calls = append(data.Calls, newSyscallData(target, c, attrVals[:last+1])) + // Some syscalls might not be present on the compiling machine, so we + // generate definitions for them. + if target.SyscallNumbers && !strings.HasPrefix(c.CallName, "syz_") && + target.NeedSyscallDefine(c.NR) { + defines[target.SyscallPrefix+c.CallName] = fmt.Sprintf("%d", c.NR) + } } sort.Slice(data.Calls, func(i, j int) bool { return data.Calls[i].Name < data.Calls[j].Name }) + // Get a sorted list of definitions. + defineNames := []string{} + for key := range defines { + defineNames = append(defineNames, key) + } + sort.Strings(defineNames) + for _, key := range defineNames { + data.Defines = append(data.Defines, Define{key, defines[key]}) + } return data } @@ -380,7 +402,10 @@ struct call_props_t { {{range $attr := $.CallProps}} #define SYZ_PAGE_SIZE {{.PageSize}} #define SYZ_NUM_PAGES {{.NumPages}} #define SYZ_DATA_OFFSET {{.DataOffset}} +{{range $c := $arch.Defines}}#ifndef {{$c.Name}} +#define {{$c.Name}} {{$c.Value}} #endif +{{end}}#endif {{end}} #endif {{end}} -- cgit mrf-deployment