From fd36daf55d7f9e8ab412fcbf0441658a8d8be727 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Thu, 5 Feb 2026 13:59:08 +0100 Subject: tools/clang: compile clang tools into the binary Compiled clang tools into Go binaries using cgo. This significantly simplifies building and deployment. This also enables unit testing of clang tools. Now raw go test for clang tools will build them, run, and verify output. Each clang tool is still started as a subprocess. I've experimented with running them in-process, but this makes stdout/stderr interception extremly complicated, and it seems that clang tools still use unsynchronized global state, which breaks when invoked multiple times. Subprocesses also make it safer in the face of potential memory leaks, or memory corruptions in clang tools. Fixes #6645 --- tools/clang/codesearch/README.md | 9 --------- tools/clang/codesearch/build.go | 1 + tools/clang/codesearch/codesearch.cpp | 21 ++++++++++++++++++--- tools/clang/codesearch/codesearch.go | 6 ++++++ 4 files changed, 25 insertions(+), 12 deletions(-) delete mode 100644 tools/clang/codesearch/README.md create mode 120000 tools/clang/codesearch/build.go create mode 100644 tools/clang/codesearch/codesearch.go (limited to 'tools/clang/codesearch') diff --git a/tools/clang/codesearch/README.md b/tools/clang/codesearch/README.md deleted file mode 100644 index 6d876c78e..000000000 --- a/tools/clang/codesearch/README.md +++ /dev/null @@ -1,9 +0,0 @@ -# syz-codesearch - -Clang-based tool that indexes kernel source code to power -[pkg/aflow/tool/codesearcher](/pkg/aflow/tool/codesearcher/codesearcher.go) -agentic tool. - -The tool can be built following the procedure described for -[syz-declextract tool](/tools/syz-declextract/README.md) or with `make -codesearch`. diff --git a/tools/clang/codesearch/build.go b/tools/clang/codesearch/build.go new file mode 120000 index 000000000..0308541aa --- /dev/null +++ b/tools/clang/codesearch/build.go @@ -0,0 +1 @@ +../build.go \ No newline at end of file diff --git a/tools/clang/codesearch/codesearch.cpp b/tools/clang/codesearch/codesearch.cpp index 047edad10..a2d6883a2 100644 --- a/tools/clang/codesearch/codesearch.cpp +++ b/tools/clang/codesearch/codesearch.cpp @@ -1,6 +1,9 @@ // Copyright 2025 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. +// Clang-based tool that indexes kernel source code to power +// pkg/aflow/tool/codesearcher/codesearcher.go agentic tool. + #include "json.h" #include "output.h" @@ -12,6 +15,7 @@ #include "clang/AST/RecordLayout.h" #include "clang/AST/RecursiveASTVisitor.h" #include "clang/Basic/SourceManager.h" +#include "clang/Basic/Version.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Tooling/CommonOptionsParser.h" #include "clang/Tooling/Tooling.h" @@ -311,7 +315,11 @@ bool Indexer::TraverseRecordDecl(RecordDecl* Decl) { uint64_t OffsetInBits = Layout.getFieldOffset(Field->getFieldIndex()); uint64_t SizeInBits; if (Field->isBitField()) { - SizeInBits = Field->getBitWidthValue(Context); + SizeInBits = Field->getBitWidthValue( +#if CLANG_VERSION_MAJOR == 19 + Context +#endif + ); } else { TypeInfo Info = Context.getTypeInfo(Field->getType()); SizeInBits = Info.Width; @@ -338,8 +346,8 @@ bool Indexer::TraverseTypedefDecl(TypedefDecl* Decl) { return Base::TraverseTypedefDecl(Decl); } -int main(int argc, const char** argv) { - llvm::cl::OptionCategory Options("syz-indexer options"); +static int Main(int argc, const char** argv) { + llvm::cl::OptionCategory Options("codesearch options"); auto OptionsParser = tooling::CommonOptionsParser::create(argc, argv, Options); if (!OptionsParser) { llvm::errs() << OptionsParser.takeError(); @@ -351,5 +359,12 @@ int main(int argc, const char** argv) { if (Tool.run(tooling::newFrontendActionFactory(&Instance, &Instance).get())) return 1; Output.print(); + fflush(stdout); return 0; } + +__attribute__((constructor(1000))) static void ctor(int argc, const char** argv) { + const char* run = getenv("SYZ_RUN_CLANGTOOL"); + if (run && !strcmp(run, "codesearch")) + exit(Main(argc, argv)); +} diff --git a/tools/clang/codesearch/codesearch.go b/tools/clang/codesearch/codesearch.go new file mode 100644 index 000000000..6fa5d7de0 --- /dev/null +++ b/tools/clang/codesearch/codesearch.go @@ -0,0 +1,6 @@ +// Copyright 2026 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 clangtoolimpl + +const Tool = "codesearch" -- cgit mrf-deployment