llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang-driver Author: Fangrui Song (MaskRay) <details> <summary>Changes</summary> #<!-- -->227414 added `-plugin-arg=<plugin>,<arg>` to opt and llc; clang reaches plugin options only through `-mllvm`. Add `-fpass-plugin-arg=`, which, as in opt, reaches both `-fpass-plugin=` plugins and statically linked extensions such as Polly. Plugins parse arguments with cl::ParseCommandLineOptions, which obtains the real file system for response file expansion, an I/O sandbox violation in -cc1. Skip the expansion when no argument starts with '@'. LLM-aided --- Full diff: https://github.com/llvm/llvm-project/pull/244917.diff 14 Files Affected: - (modified) clang/docs/ReleaseNotes.md (+4) - (modified) clang/include/clang/Basic/CodeGenOptions.h (+3) - (modified) clang/include/clang/Basic/DiagnosticFrontendKinds.td (+1) - (modified) clang/include/clang/CodeGen/BackendUtil.h (+4) - (modified) clang/include/clang/Options/Options.td (+5) - (modified) clang/lib/CodeGen/BackendUtil.cpp (+17) - (modified) clang/lib/Driver/ToolChains/Clang.cpp (+1) - (modified) clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp (+3) - (modified) clang/test/CodeGen/pass-plugins.c (+3) - (modified) clang/test/Driver/plugin-driver-args.cpp (+4-1) - (modified) llvm/lib/Plugins/PassPlugin.cpp (+2-3) - (modified) llvm/lib/Support/CommandLine.cpp (+12-11) - (modified) llvm/test/Feature/load_extension.ll (+2-2) - (modified) llvm/unittests/Support/CommandLineTest.cpp (+10) ``````````diff diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index 63e17e4880a8db..e1d838dad8cf6f 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -450,6 +450,10 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and {doc}`Return Address Authentication Hardening <ReturnAddressAuthenticationHardening>` for more information. +- Added `-fpass-plugin-arg=<plugin>,<arg>`, which passes `<arg>` to the pass + plugin named `<plugin>`, as `opt -plugin-arg=<plugin>,<arg>` does. It reaches + plugins loaded with `-fpass-plugin=` and statically linked ones such as Polly. + ### Deprecated Compiler Flags ### Modified Compiler Flags diff --git a/clang/include/clang/Basic/CodeGenOptions.h b/clang/include/clang/Basic/CodeGenOptions.h index 4f1875d8c0bd47..c7c69dac4483af 100644 --- a/clang/include/clang/Basic/CodeGenOptions.h +++ b/clang/include/clang/Basic/CodeGenOptions.h @@ -567,6 +567,9 @@ class CodeGenOptions : public CodeGenOptionsBase { /// List of dynamic shared object files to be loaded as pass plugins. std::vector<std::string> PassPlugins; + /// "<plugin>,<arg>" values of -fpass-plugin-arg=. + std::vector<std::string> PassPluginArgs; + /// List of pass builder callbacks. std::vector<std::function<void(llvm::PassBuilder &)>> PassBuilderCallbacks; diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td index 82a84f7fc54331..b6d02797b61c54 100644 --- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td +++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td @@ -123,6 +123,7 @@ def err_fe_remap_missing_to_file : Error< def err_fe_unable_to_load_ast_file : Error<"unable to load precompiled file">; def err_fe_unable_to_load_plugin : Error< "unable to load plugin '%0': '%1'">; +def err_fe_pass_plugin_arg : Error<"%0">; def err_fe_unable_to_create_target : Error< "unable to create target: '%0'">; def err_fe_unable_to_create_subtarget : Error< diff --git a/clang/include/clang/CodeGen/BackendUtil.h b/clang/include/clang/CodeGen/BackendUtil.h index 43fae638f93d10..dfac1116adea31 100644 --- a/clang/include/clang/CodeGen/BackendUtil.h +++ b/clang/include/clang/CodeGen/BackendUtil.h @@ -43,6 +43,10 @@ void emitBackendOutput(CompilerInstance &CI, CodeGenOptions &CGOpts, std::unique_ptr<raw_pwrite_stream> OS, BackendConsumer *BC = nullptr); +/// Passes the -fpass-plugin-arg= values to the loaded pass plugins and the +/// statically linked extensions. Returns false after reporting an error. +bool passPluginArguments(CompilerInstance &CI); + void EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts, llvm::MemoryBufferRef Buf); diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td index 304a0f1bcb44ab..6abe104c8acb05 100644 --- a/clang/include/clang/Options/Options.td +++ b/clang/include/clang/Options/Options.td @@ -4562,6 +4562,11 @@ def fpass_plugin_EQ : Joined<["-"], "fpass-plugin=">, MetaVarName<"<dsopath>">, Flags<[NoArgumentUnused]>, HelpText<"Load pass plugin from a dynamic shared object file (only with new pass manager).">, MarshallingInfoStringVector<CodeGenOpts<"PassPlugins">>; +def fpass_plugin_arg_EQ : Joined<["-"], "fpass-plugin-arg=">, + Group<f_Group>, Visibility<[ClangOption, CC1Option]>, + MetaVarName<"<plugin>,<arg>">, Flags<[NoArgumentUnused]>, + HelpText<"Pass <arg> to the pass plugin named <plugin>">, + MarshallingInfoStringVector<CodeGenOpts<"PassPluginArgs">>; defm tocdata : BoolOption<"m","tocdata", CodeGenOpts<"AllTocData">, DefaultFalse, PosFlag<SetTrue, [], [ClangOption, CC1Option], diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index a6c4e296fa4551..0ef49e16ca6072 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -1548,6 +1548,23 @@ static void createAndEmbedModuleForDynamicDebugging( SaveModule("dyndbg.2.outer", *M); } +bool clang::passPluginArguments(CompilerInstance &CI) { + const std::vector<std::string> &Args = CI.getCodeGenOpts().PassPluginArgs; + if (Args.empty()) + return true; + SmallVector<PassPluginLibraryInfo, 0> Infos; + for (const std::unique_ptr<PassPlugin> &Plugin : CI.getPassPlugins()) + Infos.push_back(Plugin->getInfo()); +#define HANDLE_EXTENSION(Ext) Infos.push_back(get##Ext##PluginInfo()); +#include "llvm/Support/Extension.def" + if (Error E = llvm::passPluginArguments(Infos, Args)) { + CI.getDiagnostics().Report(diag::err_fe_pass_plugin_arg) + << toString(std::move(E)); + return false; + } + return true; +} + void clang::emitBackendOutput(CompilerInstance &CI, CodeGenOptions &CGOpts, llvm::Module *M, BackendAction Action, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 8da0251c302e24..014c20a2bdf2c8 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -8298,6 +8298,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, Args.MakeArgString(Twine("-fpass-plugin=") + A->getValue())); A->claim(); } + Args.AddAllArgs(CmdArgs, options::OPT_fpass_plugin_arg_EQ); // Forward --vfsoverlay to -cc1. for (const Arg *A : Args.filtered(options::OPT_vfsoverlay)) { diff --git a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp index 48d9e0acd3a781..8fbfd2a5b5d6b7 100644 --- a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp +++ b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "clang/Basic/DiagnosticFrontend.h" +#include "clang/CodeGen/BackendUtil.h" #include "clang/CodeGen/CodeGenAction.h" #include "clang/Config/config.h" #include "clang/ExtractAPI/FrontendActions.h" @@ -262,6 +263,8 @@ bool ExecuteCompilerInvocation(CompilerInstance *Clang) { Clang->LoadRequestedPlugins(); Clang->parseLLVMArgs(); + if (!passPluginArguments(*Clang)) + return false; #if CLANG_ENABLE_STATIC_ANALYZER // These should happen AFTER plugins have been loaded! diff --git a/clang/test/CodeGen/pass-plugins.c b/clang/test/CodeGen/pass-plugins.c index a87e36c7c725c8..311f7cb83adb84 100644 --- a/clang/test/CodeGen/pass-plugins.c +++ b/clang/test/CodeGen/pass-plugins.c @@ -1,11 +1,14 @@ // RUN: %clang_cc1 -S < %s -fpass-plugin=%llvmshlibdir/Bye%pluginext -O2 2>&1 | FileCheck %s --check-prefix=CHECK-INACTIVE // RUN: %clang_cc1 -S < %s -fpass-plugin=%llvmshlibdir/Bye%pluginext -O2 -mllvm -wave-goodbye 2>&1 | FileCheck %s --check-prefix=CHECK-ACTIVE +// RUN: %clang_cc1 -S < %s -fpass-plugin=%llvmshlibdir/Bye%pluginext -O2 -fpass-plugin-arg=Bye,-wave-goodbye 2>&1 | FileCheck %s --check-prefix=CHECK-ACTIVE +// RUN: not %clang_cc1 -S < %s -fpass-plugin=%llvmshlibdir/Bye%pluginext -fpass-plugin-arg=Bye,-nope 2>&1 | FileCheck %s --check-prefix=BADOPT // REQUIRES: plugins, llvm-examples // UNSUPPORTED: target={{.*windows.*}} // Plugins are currently broken on AIX, at least in the CI. // XFAIL: target={{.*}}-aix{{.*}} // CHECK-INACTIVE-NOT: Bye // CHECK-ACTIVE: Bye: f +// BADOPT: error: Bye: Unknown command line argument '-nope'. int f(int x) { return x; diff --git a/clang/test/Driver/plugin-driver-args.cpp b/clang/test/Driver/plugin-driver-args.cpp index 6efd859f9d085c..99c8504eb4623f 100644 --- a/clang/test/Driver/plugin-driver-args.cpp +++ b/clang/test/Driver/plugin-driver-args.cpp @@ -21,7 +21,10 @@ // RUN: %clang -fplugin=%llvmshlibdir/CallSuperAttr%pluginext -fplugin-arg-testname- -fsyntax-only %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-PLUGIN-ARG2 // CHECK-NO-PLUGIN-ARG2: missing plugin argument for plugin testname in -fplugin-arg-testname- +// RUN: %clang -c -fpass-plugin=bar.so -fpass-plugin-arg=bar,-x -fpass-plugin-arg=bar,y,z %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-PASS-PLUGIN-ARG +// CHECK-PASS-PLUGIN-ARG: "-fpass-plugin=bar.so" "-fpass-plugin-arg=bar,-x" "-fpass-plugin-arg=bar,y,z" + // Plugins are only relevant for the -cc1 phase. No warning should be raised // when only using the assembler. See GH #88173. -// RUN: %clang -c -fpass-plugin=bar.so -fplugin=bar.so -fplugin-arg-bar-option -Wunused-command-line-argument -x assembler %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-PLUGIN-ASM +// RUN: %clang -c -fpass-plugin=bar.so -fpass-plugin-arg=bar,-x -fplugin=bar.so -fplugin-arg-bar-option -Wunused-command-line-argument -x assembler %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-PLUGIN-ASM // CHECK-PLUGIN-ASM-NOT: argument unused during compilation: '-f{{[a-z-]*plugin[^']*}}' diff --git a/llvm/lib/Plugins/PassPlugin.cpp b/llvm/lib/Plugins/PassPlugin.cpp index f7b7b67a6befbd..5b164c6957978e 100644 --- a/llvm/lib/Plugins/PassPlugin.cpp +++ b/llvm/lib/Plugins/PassPlugin.cpp @@ -77,11 +77,10 @@ Error llvm::passPluginArguments(ArrayRef<PassPluginLibraryInfo> Infos, auto [Name, Rest] = StringRef(Arg).split(','); // Rest is null without a comma and empty for an empty argument. if (!Rest.data()) - return createStringError("expected <plugin>,<arg> in -plugin-arg=" + Arg); + return createStringError("expected <plugin>,<arg>, got '" + Arg + "'"); auto It = Index.find(Name); if (It == Index.end()) - return createStringError("no pass plugin named '" + Name + - "' is loaded, in -plugin-arg=" + Arg); + return createStringError("no pass plugin named '" + Name + "' is loaded"); PluginArgs[It->second].push_back(Rest.data()); } for (auto [Info, PArgs] : zip_equal(Infos, PluginArgs)) { diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp index d1547f85727b8a..c9e1f27059a5d5 100644 --- a/llvm/lib/Support/CommandLine.cpp +++ b/llvm/lib/Support/CommandLine.cpp @@ -1535,25 +1535,26 @@ bool CommandLineParser::ParseCommandLineOptions( bool IgnoreErrors = Errs; if (!Errs) Errs = &errs(); - if (!VFS) - VFS = vfs::getRealFileSystem().get(); bool ErrorParsing = false; - // Expand response files. + // Expand response files. Without an @file argument, skip the expansion so + // that the I/O sandbox does not reject the real file system default. SmallVector<const char *, 20> newArgv(argv, argv + argc); BumpPtrAllocator A; + if (any_of(newArgv, [](const char *Arg) { return Arg[0] == '@'; })) { #ifdef _WIN32 - auto Tokenize = cl::TokenizeWindowsCommandLine; + auto Tokenize = cl::TokenizeWindowsCommandLine; #else - auto Tokenize = cl::TokenizeGNUCommandLine; + auto Tokenize = cl::TokenizeGNUCommandLine; #endif - ExpansionContext ECtx(A, Tokenize, VFS); - if (Error Err = ECtx.expandResponseFiles(newArgv)) { - *Errs << toString(std::move(Err)) << '\n'; - return false; + ExpansionContext ECtx(A, Tokenize, VFS); + if (Error Err = ECtx.expandResponseFiles(newArgv)) { + *Errs << toString(std::move(Err)) << '\n'; + return false; + } + argv = &newArgv[0]; + argc = static_cast<int>(newArgv.size()); } - argv = &newArgv[0]; - argc = static_cast<int>(newArgv.size()); // Copy the program name into ProgName, making sure not to overflow it. ProgramName = std::string(sys::path::filename(StringRef(argv[0]))); diff --git a/llvm/test/Feature/load_extension.ll b/llvm/test/Feature/load_extension.ll index d5309836496167..93ec05ce758df8 100644 --- a/llvm/test/Feature/load_extension.ll +++ b/llvm/test/Feature/load_extension.ll @@ -17,8 +17,8 @@ ; CHECK: Bye ; BOTH: Bye: somefunk ; BOTH-NEXT: See,you: somefunk -; NOCOMMA: expected <plugin>,<arg> in -plugin-arg=Bye{{$}} -; UNKNOWN: no pass plugin named 'Nope' is loaded, in -plugin-arg=Nope,-x +; NOCOMMA: expected <plugin>,<arg>, got 'Bye'{{$}} +; UNKNOWN: no pass plugin named 'Nope' is loaded{{$}} ; DUP: multiple pass plugins are named 'Bye'{{$}} target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" diff --git a/llvm/unittests/Support/CommandLineTest.cpp b/llvm/unittests/Support/CommandLineTest.cpp index b08caed783ede4..2cf654859d55d5 100644 --- a/llvm/unittests/Support/CommandLineTest.cpp +++ b/llvm/unittests/Support/CommandLineTest.cpp @@ -13,6 +13,7 @@ #include "llvm/Config/config.h" #include "llvm/Support/Allocator.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/IOSandbox.h" #include "llvm/Support/InitLLVM.h" #include "llvm/Support/LibraryOptionsContext.h" #include "llvm/Support/MemoryBuffer.h" @@ -619,6 +620,15 @@ TEST(CommandLineTest, SetMultiValues) { EXPECT_EQ(Option, 2); } +TEST(CommandLineTest, NoResponseFileInSandbox) { + cl::ResetCommandLineParser(); + StackOption<bool> Opt("opt"); + const char *Args[] = {"prog", "-opt"}; + auto EnableSandbox = sys::sandbox::scopedEnable(); + EXPECT_TRUE(cl::ParseCommandLineOptions(2, Args, "", &llvm::nulls())); + EXPECT_TRUE(Opt); +} + TEST(CommandLineTest, SetValueInSubcategories) { cl::ResetCommandLineParser(); `````````` </details> https://github.com/llvm/llvm-project/pull/244917 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
