diff options
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/syz-declextract/clangtool/declextract.cpp | 28 | ||||
| -rw-r--r-- | tools/syz-declextract/clangtool/output.h | 26 | ||||
| -rw-r--r-- | tools/syz-declextract/testdata/file_operations.c.json | 28 | ||||
| -rw-r--r-- | tools/syz-declextract/testdata/io_uring.c.json | 23 | ||||
| -rw-r--r-- | tools/syz-declextract/testdata/netlink.c.json | 62 | ||||
| -rw-r--r-- | tools/syz-declextract/testdata/types.c.json | 11 |
6 files changed, 126 insertions, 52 deletions
diff --git a/tools/syz-declextract/clangtool/declextract.cpp b/tools/syz-declextract/clangtool/declextract.cpp index c41904d8b..4012900d6 100644 --- a/tools/syz-declextract/clangtool/declextract.cpp +++ b/tools/syz-declextract/clangtool/declextract.cpp @@ -113,7 +113,7 @@ private: template <typename T> const T* getResult(StringRef ID) const; FieldType extractRecord(QualType QT, const RecordType* Typ, const std::string& BackupName); std::string extractEnum(const EnumDecl* Decl); - void noteConstUse(const std::string& Name, int64_t Val, const SourceRange& Range); + void emitConst(const std::string& Name, int64_t Val, SourceLocation Loc); std::string getDeclName(const Expr* Expr); const ValueDecl* getValueDecl(const Expr* Expr); std::string getDeclFileID(const Decl* Decl); @@ -304,7 +304,7 @@ std::string Extractor::extractEnum(const EnumDecl* Decl) { std::vector<std::string> Values; for (const auto* Enumerator : Decl->enumerators()) { const std::string& Name = Enumerator->getNameAsString(); - noteConstUse(Name, Enumerator->getInitVal().getExtValue(), Decl->getSourceRange()); + emitConst(Name, Enumerator->getInitVal().getExtValue(), Decl->getBeginLoc()); Values.push_back(Name); } Output.emit(Enum{ @@ -314,19 +314,11 @@ std::string Extractor::extractEnum(const EnumDecl* Decl) { return Name; } -void Extractor::noteConstUse(const std::string& Name, int64_t Val, const SourceRange& Range) { - const std::string& Filename = std::filesystem::relative(SourceManager->getFilename(Range.getBegin()).str()); - // Include only uapi headers. Some ioctl commands defined in internal headers, or even in .c files. - // They have high chances of breaking compilation during const extract. - // If it's not defined in uapi, emit define with concrete value. - // Note: the value may be wrong for other arches. - if (Filename.find("/uapi/") != std::string::npos && Filename.back() == 'h') { - Output.emit(Include{Filename}); - return; - } - Output.emit(Define{ +void Extractor::emitConst(const std::string& Name, int64_t Val, SourceLocation Loc) { + Output.emit(ConstInfo{ .Name = Name, - .Value = std::to_string(Val), + .Filename = std::filesystem::relative(SourceManager->getFilename(Loc).str()), + .Value = Val, }); } @@ -445,8 +437,8 @@ std::vector<std::pair<int, std::string>> Extractor::extractDesignatedInitConsts( for (auto* Match : Matches) { const int64_t Val = *Match->getAPValueResult().getInt().getRawData(); const auto& Name = Match->getEnumConstantDecl()->getNameAsString(); - const auto& SR = Match->getEnumConstantDecl()->getSourceRange(); - noteConstUse(Name, Val, SR); + const auto& Loc = Match->getEnumConstantDecl()->getBeginLoc(); + emitConst(Name, Val, Loc); Inits.emplace_back(Val, Name); } return Inits; @@ -523,7 +515,7 @@ void Extractor::matchNetlinkFamily() { if (!CmdInit) continue; const std::string& OpName = CmdInit->getNameAsString(); - noteConstUse(OpName, CmdInit->getInitVal().getExtValue(), CmdInit->getSourceRange()); + emitConst(OpName, CmdInit->getInitVal().getExtValue(), CmdInit->getBeginLoc()); std::string Policy; if (OpsFields.count("policy") != 0) { if (const auto* PolicyDecl = OpInit->getInit(OpsFields["policy"])->getAsBuiltinConstantDeclRef(*Context)) @@ -818,7 +810,7 @@ std::vector<IoctlCmd> Extractor::extractIoctlCommands(const std::string& Ioctl) if (MacroDef == Macros.end()) continue; int64_t CmdVal = evaluate(Cmd); - noteConstUse(CmdStr, CmdVal, MacroDef->second.SourceRange); + emitConst(CmdStr, CmdVal, MacroDef->second.SourceRange.getBegin()); FieldType CmdType; const auto Dir = _IOC_DIR(CmdVal); if (Dir == _IOC_NONE) { diff --git a/tools/syz-declextract/clangtool/output.h b/tools/syz-declextract/clangtool/output.h index 24ab82f61..d89d64afb 100644 --- a/tools/syz-declextract/clangtool/output.h +++ b/tools/syz-declextract/clangtool/output.h @@ -62,13 +62,10 @@ struct BufferType { bool IsNonTerminated = false; }; -struct Include { - std::string Filename; -}; - -struct Define { +struct ConstInfo { std::string Name; - std::string Value; + std::string Filename; + int64_t Value; }; struct Field { @@ -201,9 +198,10 @@ struct NetlinkPolicy { std::vector<NetlinkAttr> Attrs; }; -inline void print(JSONPrinter& Printer, const Define& V) { +inline void print(JSONPrinter& Printer, const ConstInfo& V) { JSONPrinter::Scope Scope(Printer); Printer.Field("name", V.Name); + Printer.Field("filename", V.Filename); Printer.Field("value", V.Value, true); } @@ -412,13 +410,8 @@ inline FieldType TodoType() { class Output { public: - void emit(Include&& Inc) { - if (IncludesDedup.insert(Inc.Filename).second) - Includes.push_back(Inc.Filename); - } - void emit(Function&& V) { Functions.push_back(std::move(V)); } - void emit(Define&& V) { Defines.push_back(std::move(V)); } + void emit(ConstInfo&& V) { Consts.push_back(std::move(V)); } void emit(Struct&& V) { Structs.push_back(std::move(V)); } void emit(Enum&& V) { Enums.push_back(std::move(V)); } void emit(Syscall&& V) { Syscalls.push_back(std::move(V)); } @@ -430,8 +423,7 @@ public: void print() const { JSONPrinter Printer; Printer.Field("functions", Functions); - Printer.Field("includes", Includes); - Printer.Field("defines", Defines); + Printer.Field("consts", Consts); Printer.Field("enums", Enums); Printer.Field("structs", Structs); Printer.Field("syscalls", Syscalls); @@ -443,9 +435,7 @@ public: private: std::vector<Function> Functions; - std::vector<std::string> Includes; - std::unordered_set<std::string> IncludesDedup; - std::vector<Define> Defines; + std::vector<ConstInfo> Consts; std::vector<Enum> Enums; std::vector<Struct> Structs; std::vector<Syscall> Syscalls; diff --git a/tools/syz-declextract/testdata/file_operations.c.json b/tools/syz-declextract/testdata/file_operations.c.json index 94ad7bef5..df1c9a20f 100644 --- a/tools/syz-declextract/testdata/file_operations.c.json +++ b/tools/syz-declextract/testdata/file_operations.c.json @@ -47,8 +47,32 @@ "is_static": true } ], - "includes": [ - "include/uapi/file_operations.h" + "consts": [ + { + "name": "FOO_IOCTL1", + "filename": "include/uapi/file_operations.h", + "value": 25345 + }, + { + "name": "FOO_IOCTL2", + "filename": "include/uapi/file_operations.h", + "value": 2147771138 + }, + { + "name": "FOO_IOCTL3", + "filename": "include/uapi/file_operations.h", + "value": 2148033283 + }, + { + "name": "FOO_IOCTL4", + "filename": "include/uapi/file_operations.h", + "value": 1074291460 + }, + { + "name": "FOO_IOCTL5", + "filename": "include/uapi/file_operations.h", + "value": 3221775109 + } ], "structs": [ { diff --git a/tools/syz-declextract/testdata/io_uring.c.json b/tools/syz-declextract/testdata/io_uring.c.json index 3f0a74a95..da91ce1b6 100644 --- a/tools/syz-declextract/testdata/io_uring.c.json +++ b/tools/syz-declextract/testdata/io_uring.c.json @@ -29,8 +29,27 @@ "file": "io_uring.c" } ], - "includes": [ - "include/uapi/io_uring.h" + "consts": [ + { + "name": "IORING_OP_NOP", + "filename": "include/uapi/io_uring.h", + "value": 0 + }, + { + "name": "IORING_OP_NOT_SUPPORTED", + "filename": "include/uapi/io_uring.h", + "value": 3 + }, + { + "name": "IORING_OP_READV", + "filename": "include/uapi/io_uring.h", + "value": 1 + }, + { + "name": "IORING_OP_WRITEV", + "filename": "include/uapi/io_uring.h", + "value": 2 + } ], "iouring_ops": [ { diff --git a/tools/syz-declextract/testdata/netlink.c.json b/tools/syz-declextract/testdata/netlink.c.json index 4233eab7d..e1c2754b1 100644 --- a/tools/syz-declextract/testdata/netlink.c.json +++ b/tools/syz-declextract/testdata/netlink.c.json @@ -37,25 +37,71 @@ "is_static": true } ], - "includes": [ - "include/uapi/netlink_family.h" - ], - "defines": [ + "consts": [ { "name": "NETLINK_BAR_CMD_FOO", - "value": "0" + "filename": "netlink.c", + "value": 0 + }, + { + "name": "NETLINK_FOO_ATTR1", + "filename": "include/uapi/netlink_family.h", + "value": 0 + }, + { + "name": "NETLINK_FOO_ATTR2", + "filename": "include/uapi/netlink_family.h", + "value": 1 + }, + { + "name": "NETLINK_FOO_ATTR3", + "filename": "include/uapi/netlink_family.h", + "value": 4 + }, + { + "name": "NETLINK_FOO_ATTR4", + "filename": "include/uapi/netlink_family.h", + "value": 5 + }, + { + "name": "NETLINK_FOO_ATTR5", + "filename": "include/uapi/netlink_family.h", + "value": 6 + }, + { + "name": "NETLINK_FOO_ATTR6", + "filename": "include/uapi/netlink_family.h", + "value": 7 + }, + { + "name": "NETLINK_FOO_ATTR7", + "filename": "include/uapi/netlink_family.h", + "value": 8 + }, + { + "name": "NETLINK_FOO_CMD_BAR", + "filename": "include/uapi/netlink_family.h", + "value": 1 + }, + { + "name": "NETLINK_FOO_CMD_FOO", + "filename": "include/uapi/netlink_family.h", + "value": 0 }, { "name": "NETLINK_FOO_NESTED_ATTR1", - "value": "0" + "filename": "netlink.c", + "value": 0 }, { "name": "NETLINK_FOO_NESTED_ATTR2", - "value": "1" + "filename": "netlink.c", + "value": 1 }, { "name": "NETLINK_NOPOLICY_CMD", - "value": "0" + "filename": "netlink.c", + "value": 0 } ], "structs": [ diff --git a/tools/syz-declextract/testdata/types.c.json b/tools/syz-declextract/testdata/types.c.json index a5a7088db..82d45e3fe 100644 --- a/tools/syz-declextract/testdata/types.c.json +++ b/tools/syz-declextract/testdata/types.c.json @@ -111,18 +111,21 @@ ] } ], - "defines": [ + "consts": [ { "name": "a", - "value": "0" + "filename": "types.c", + "value": 0 }, { "name": "b", - "value": "1" + "filename": "types.c", + "value": 1 }, { "name": "c", - "value": "2" + "filename": "types.c", + "value": 2 } ], "enums": [ |
