From f2cb035c8f931efff4a020b164e657f16f51934b Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Fri, 17 Jan 2025 10:39:47 +0100 Subject: pkg/declextract: remove unused includes and defines This is nice on its own, but this will also help to prevent lots of problems when we export more info from the clang tool in future. The clang tool does not know what will end up in the final descriptions, so it exports info about all consts that it encounters. As the result we pull in lots of includes/defines, and lots of kernel includes/defines are broken or create problems. So the fewer we have, the better. --- tools/syz-declextract/declextract.go | 21 ++++++++--- tools/syz-declextract/testdata/file_operations.c | 14 ++++++++ .../testdata/file_operations.c.json | 41 ++++++++++++++++++++++ .../syz-declextract/testdata/file_operations.c.txt | 1 - tools/syz-declextract/testdata/functions.c.txt | 4 --- .../testdata/include/uapi/unused_ioctl.h | 6 ++++ tools/syz-declextract/testdata/io_uring.c.txt | 5 --- tools/syz-declextract/testdata/netlink.c.txt | 3 -- tools/syz-declextract/testdata/syscall.c.txt | 4 --- tools/syz-declextract/testdata/types.c.txt | 4 --- 10 files changed, 77 insertions(+), 26 deletions(-) create mode 100644 tools/syz-declextract/testdata/include/uapi/unused_ioctl.h (limited to 'tools/syz-declextract') diff --git a/tools/syz-declextract/declextract.go b/tools/syz-declextract/declextract.go index 0853756bf..7d85adaf8 100644 --- a/tools/syz-declextract/declextract.go +++ b/tools/syz-declextract/declextract.go @@ -62,7 +62,7 @@ func run(autoFile string, loadProbeInfo func() (*ifaceprobe.Info, error), cfg *c if err != nil { return err } - descriptions, interfaces, err := declextract.Run(out, probeInfo, syscallRename, cfg.DebugTrace) + descriptions, interfaces, includeUse, err := declextract.Run(out, probeInfo, syscallRename, cfg.DebugTrace) if err != nil { return err } @@ -94,6 +94,20 @@ func run(autoFile string, loadProbeInfo func() (*ifaceprobe.Info, error), cfg *c if err := osutil.WriteFile(autoFile+".info", serialize(interfaces)); err != nil { return err } + removeUnused(desc, "", unusedNodes) + // Second pass to remove unused defines/includes. This needs to be done after removing + // other garbage b/c they may be used by other garbage. + unusedConsts, err := compiler.CollectUnusedConsts(desc.Clone(), target, includeUse, eh) + if err != nil { + return fmt.Errorf("failed to typecheck descriptions: %w\n%s", err, errors.Bytes()) + } + removeUnused(desc, autoFile, unusedConsts) + // We need re-parse them again b/c new lines are fixed up during parsing. + formatted := ast.Format(ast.Parse(ast.Format(desc), autoFile, nil)) + return osutil.WriteFile(autoFile, formatted) +} + +func removeUnused(desc *ast.Description, autoFile string, unusedNodes []ast.Node) { unused := make(map[string]bool) for _, n := range unusedNodes { _, typ, name := n.Info() @@ -101,11 +115,8 @@ func run(autoFile string, loadProbeInfo func() (*ifaceprobe.Info, error), cfg *c } desc.Nodes = slices.DeleteFunc(desc.Nodes, func(n ast.Node) bool { pos, typ, name := n.Info() - return pos.File != autoFile || unused[typ+name] + return autoFile != "" && pos.File != autoFile || unused[typ+name] }) - // We need re-parse them again b/c new lines are fixed up during parsing. - formatted := ast.Format(ast.Parse(ast.Format(desc), autoFile, nil)) - return osutil.WriteFile(autoFile, formatted) } func prepare(loadProbeInfo func() (*ifaceprobe.Info, error), cfg *clangtool.Config) ( diff --git a/tools/syz-declextract/testdata/file_operations.c b/tools/syz-declextract/testdata/file_operations.c index 04d548f98..0dd8b9b21 100644 --- a/tools/syz-declextract/testdata/file_operations.c +++ b/tools/syz-declextract/testdata/file_operations.c @@ -3,6 +3,7 @@ #include "include/fs.h" #include "include/uapi/file_operations.h" +#include "include/uapi/unused_ioctl.h" static void foo_open() {} static void foo_read() {} @@ -43,3 +44,16 @@ const struct file_operations proc_ops[] = { .unlocked_ioctl = proc_ioctl, }, }; + +#define UNUSED_IOCTL2 _IO('c', 2) + +static void unused_ioctl(unsigned int cmd) { + switch (cmd) { + case UNUSED_IOCTL1: + case UNUSED_IOCTL2: + } +} + +const struct file_operations unused = { + .unlocked_ioctl = unused_ioctl, +}; diff --git a/tools/syz-declextract/testdata/file_operations.c.json b/tools/syz-declextract/testdata/file_operations.c.json index df1c9a20f..716dbec38 100644 --- a/tools/syz-declextract/testdata/file_operations.c.json +++ b/tools/syz-declextract/testdata/file_operations.c.json @@ -45,6 +45,12 @@ "name": "proc_write", "file": "file_operations.c", "is_static": true + }, + { + "name": "unused_ioctl", + "file": "file_operations.c", + "is_static": true, + "loc": 4 } ], "consts": [ @@ -72,6 +78,16 @@ "name": "FOO_IOCTL5", "filename": "include/uapi/file_operations.h", "value": 3221775109 + }, + { + "name": "UNUSED_IOCTL1", + "filename": "include/uapi/unused_ioctl.h", + "value": 25345 + }, + { + "name": "UNUSED_IOCTL2", + "filename": "file_operations.c", + "value": 25346 } ], "structs": [ @@ -185,6 +201,31 @@ "mmap": "proc_open", "ioctl": "proc_ioctl", "source_file": "file_operations.c" + }, + { + "name": "unused_file_operations", + "ioctl": "unused_ioctl", + "ioctl_cmds": [ + { + "name": "UNUSED_IOCTL2", + "type": { + "int": { + "byte_size": 1, + "is_const": true + } + } + }, + { + "name": "UNUSED_IOCTL1", + "type": { + "int": { + "byte_size": 1, + "is_const": true + } + } + } + ], + "source_file": "file_operations.c" } ] } \ No newline at end of file diff --git a/tools/syz-declextract/testdata/file_operations.c.txt b/tools/syz-declextract/testdata/file_operations.c.txt index f2a0455bc..e94856980 100644 --- a/tools/syz-declextract/testdata/file_operations.c.txt +++ b/tools/syz-declextract/testdata/file_operations.c.txt @@ -6,7 +6,6 @@ type auto_todo int8 include include -include include resource fd_foo_file_operations[fd] diff --git a/tools/syz-declextract/testdata/functions.c.txt b/tools/syz-declextract/testdata/functions.c.txt index 6dc51303b..8bd11c8fc 100644 --- a/tools/syz-declextract/testdata/functions.c.txt +++ b/tools/syz-declextract/testdata/functions.c.txt @@ -4,8 +4,4 @@ meta automatic type auto_todo int8 -include -include -include - functions$auto(x fd) fd diff --git a/tools/syz-declextract/testdata/include/uapi/unused_ioctl.h b/tools/syz-declextract/testdata/include/uapi/unused_ioctl.h new file mode 100644 index 000000000..6c3dbb036 --- /dev/null +++ b/tools/syz-declextract/testdata/include/uapi/unused_ioctl.h @@ -0,0 +1,6 @@ +// Copyright 2024 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. + +#include "ioctl.h" + +#define UNUSED_IOCTL1 _IO('c', 1) diff --git a/tools/syz-declextract/testdata/io_uring.c.txt b/tools/syz-declextract/testdata/io_uring.c.txt index 77183198f..2829bcbf1 100644 --- a/tools/syz-declextract/testdata/io_uring.c.txt +++ b/tools/syz-declextract/testdata/io_uring.c.txt @@ -3,8 +3,3 @@ meta automatic type auto_todo int8 - -include -include -include -include diff --git a/tools/syz-declextract/testdata/netlink.c.txt b/tools/syz-declextract/testdata/netlink.c.txt index e61244406..6ceb9111e 100644 --- a/tools/syz-declextract/testdata/netlink.c.txt +++ b/tools/syz-declextract/testdata/netlink.c.txt @@ -4,9 +4,6 @@ meta automatic type auto_todo int8 -include -include -include include resource genl_bar_family_id$auto[int16] diff --git a/tools/syz-declextract/testdata/syscall.c.txt b/tools/syz-declextract/testdata/syscall.c.txt index de87f1866..bb0cad8c4 100644 --- a/tools/syz-declextract/testdata/syscall.c.txt +++ b/tools/syz-declextract/testdata/syscall.c.txt @@ -4,9 +4,5 @@ meta automatic type auto_todo int8 -include -include -include - chmod$auto(filename ptr[in, filename], mode int32) open$auto(filename ptr[in, filename], flags int32, mode int32) diff --git a/tools/syz-declextract/testdata/types.c.txt b/tools/syz-declextract/testdata/types.c.txt index 45d451d8a..04a160771 100644 --- a/tools/syz-declextract/testdata/types.c.txt +++ b/tools/syz-declextract/testdata/types.c.txt @@ -4,10 +4,6 @@ meta automatic type auto_todo int8 -include -include -include - bitfield_enum$auto = a, b, c types_syscall$auto(p ptr[inout, anon_struct$auto], y ptr[inout, void], b ptr[inout, bitfields$auto], pid pid, f int32, v ptr[inout, various$auto]) -- cgit mrf-deployment