Skip to content

Commit

Permalink
tools/syz-declextract: restructure for better scalability
Browse files Browse the repository at this point in the history
Move AST matcher setup to each interface extractor.
This keeps related things together.
  • Loading branch information
dvyukov committed Nov 7, 2024
1 parent 66362cb commit 78a1cd1
Showing 1 changed file with 36 additions and 35 deletions.
71 changes: 36 additions & 35 deletions tools/syz-declextract/syz-declextract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

//go:build ignore

#include "clang/AST/APValue.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Attr.h"
Expand Down Expand Up @@ -471,15 +472,20 @@ class EnumMatcher : public MatchFinder::MatchCallback {
};

class SyscallMatcher : public MatchFinder::MatchCallback {
public:
SyscallMatcher(MatchFinder &Finder) {
Finder.addMatcher(functionDecl(isExpandedFromMacro("SYSCALL_DEFINEx"), matchesName("__do_sys_.*")).bind("syscall"),
this);
}

private:
const std::string swapIfReservedKeyword(const std::string &word) {
if (word == "resource")
return "rsrc";
return word;
}

public:
void virtual run(const MatchFinder::MatchResult &Result) override {
void run(const MatchFinder::MatchResult &Result) override {
ASTContext *context = Result.Context;
const auto *syscall = Result.Nodes.getNodeAs<FunctionDecl>("syscall");
RecordExtractor recordExtractor(Result.SourceManager);
Expand All @@ -496,13 +502,35 @@ class SyscallMatcher : public MatchFinder::MatchCallback {
}
printf(") (automatic)\n");
recordExtractor.print();

return;
}
};

class NetlinkPolicyMatcher : public MatchFinder::MatchCallback {
public:
NetlinkPolicyMatcher(MatchFinder &Finder) {
Finder.addMatcher(
translationUnitDecl(
hasDescendant(enumDecl(has(enumConstantDecl(hasName("__NLA_TYPE_MAX")))).bind("NLA_ENUM")),
forEachDescendant(
varDecl(hasType(constantArrayType(hasElementType(hasDeclaration(
recordDecl(hasName("nla_policy")).bind("nla_policy"))))
.bind("nla_policy_array")),
isDefinition())
.bind("netlink"))),
this);
Finder.addMatcher(varDecl(hasType(recordDecl(hasName("genl_family")).bind("genl_family")),
has(initListExpr().bind("genl_family_init")))
.bind("genl_family_decl"),
this);
}

private:
void run(const MatchFinder::MatchResult &Result) override {
nlaEnum(Result); // NOTE: Must be executed first, as it generates maps that are used in the following methods.
netlink(Result);
genlFamily(Result);
}

// u8ToNlaEnum stores the Enum values to string conversions. This is later used to transfer types from an unnamed
// integer to a readable form. E.g. 1 -> NLA_U8
// See: https://elixir.bootlin.com/linux/v6.10/source/include/net/netlink.h#L172
Expand Down Expand Up @@ -819,13 +847,6 @@ class NetlinkPolicyMatcher : public MatchFinder::MatchCallback {
printf("syz_genetlink_get_family_id$auto_%s(name ptr[in, string[\"%s\"]], fd sock_nl_generic) %s (automatic)\n",
name.c_str(), name.c_str(), resourceName.c_str());
}

public:
virtual void run(const MatchFinder::MatchResult &Result) override {
nlaEnum(Result); // NOTE: Must be executed first, as it generates maps that are used in the following methods.
netlink(Result);
genlFamily(Result);
};
};

int main(int argc, const char **argv) {
Expand All @@ -836,31 +857,11 @@ int main(int argc, const char **argv) {
return 1;
}

clang::tooling::CommonOptionsParser &OptionsParser = ExpectedParser.get();
clang::tooling::ClangTool Tool(OptionsParser.getCompilations(), OptionsParser.getSourcePathList());

NetlinkPolicyMatcher NetlinkPolicyMatcher;
SyscallMatcher SyscallMatcher;
MatchFinder Finder;
SyscallMatcher SyscallMatcher(Finder);
NetlinkPolicyMatcher NetlinkPolicyMatcher(Finder);

Finder.addMatcher(functionDecl(isExpandedFromMacro("SYSCALL_DEFINEx"), matchesName("__do_sys_.*")).bind("syscall"),
&SyscallMatcher);

Finder.addMatcher(
translationUnitDecl(
hasDescendant(enumDecl(has(enumConstantDecl(hasName("__NLA_TYPE_MAX")))).bind("NLA_ENUM")),
forEachDescendant(
varDecl(hasType(constantArrayType(
hasElementType(hasDeclaration(recordDecl(hasName("nla_policy")).bind("nla_policy"))))
.bind("nla_policy_array")),
isDefinition())
.bind("netlink"))),
&NetlinkPolicyMatcher);

Finder.addMatcher(varDecl(hasType(recordDecl(hasName("genl_family")).bind("genl_family")),
has(initListExpr().bind("genl_family_init")))
.bind("genl_family_decl"),
&NetlinkPolicyMatcher);

clang::tooling::CommonOptionsParser &OptionsParser = ExpectedParser.get();
clang::tooling::ClangTool Tool(OptionsParser.getCompilations(), OptionsParser.getSourcePathList());
return Tool.run(clang::tooling::newFrontendActionFactory(&Finder).get());
}

0 comments on commit 78a1cd1

Please sign in to comment.