Move getenv for AS_SECURE_LOG_FILE to clang

Avoid calling getenv in the MC layer and let the clang driver do it so
that it is reflected in the command-line as an -mllvm option.

rdar://101558354

Differential Revision: https://reviews.llvm.org/D136888
This commit is contained in:
Ben Langmuir 2022-10-27 16:52:15 -07:00
parent 5661647e85
commit e1f9983022
15 changed files with 51 additions and 16 deletions

View File

@ -432,6 +432,9 @@ public:
/// values in order to be included in misexpect diagnostics.
Optional<uint32_t> DiagnosticsMisExpectTolerance = 0;
/// The name of a file to use with \c .secure_log_unique directives.
std::string AsSecureLogFile;
public:
// Define accessors/mutators for code generation options of enumeration type.
#define CODEGENOPT(Name, Bits, Default)

View File

@ -5349,6 +5349,9 @@ def fno_use_ctor_homing: Flag<["-"], "fno-use-ctor-homing">,
HelpText<"Don't use constructor homing for debug info">;
def fuse_ctor_homing: Flag<["-"], "fuse-ctor-homing">,
HelpText<"Use constructor homing if we are using limited debug info already">;
def as_secure_log_file : Separate<["-"], "as-secure-log-file">,
HelpText<"Emit .secure_log_unique directives to this filename.">,
MarshallingInfoString<CodeGenOpts<"AsSecureLogFile">>;
} // let Flags = [CC1Option, CC1AsOption, NoDriverOption]

View File

@ -497,6 +497,7 @@ static bool initTargetOptions(DiagnosticsEngine &Diags,
Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path);
Options.MCOptions.Argv0 = CodeGenOpts.Argv0;
Options.MCOptions.CommandLineArgs = CodeGenOpts.CommandLineArgs;
Options.MCOptions.AsSecureLogFile = CodeGenOpts.AsSecureLogFile;
Options.MisExpect = CodeGenOpts.MisExpect;
return true;

View File

@ -2737,6 +2737,11 @@ static void CollectArgsForIntegratedAssembler(Compilation &C,
if (C.getDriver().embedBitcodeEnabled() ||
C.getDriver().embedBitcodeMarkerOnly())
Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ);
if (const char *AsSecureLogFile = getenv("AS_SECURE_LOG_FILE")) {
CmdArgs.push_back("-as-secure-log-file");
CmdArgs.push_back(Args.MakeArgString(AsSecureLogFile));
}
}
static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,

View File

@ -0,0 +1,7 @@
// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-obj %s -o %t.o -as-secure-log-file %t.log
// RUN: FileCheck %s -input-file %t.log
// CHECK: "foobar"
void test(void) {
__asm__(".secure_log_unique \"foobar\"");
}

View File

@ -0,0 +1,3 @@
// RUN: env AS_SECURE_LOG_FILE=log_file %clang --target=x86_64-apple-darwin -c %s -### 2>&1 | FileCheck %s
// CHECK: "-cc1as"
// CHECK-SAME: "-as-secure-log-file" "log_file"

View File

@ -0,0 +1,5 @@
// RUN: %clang -cc1as -triple x86_64-apple-darwin %s -o %t.o -as-secure-log-file %t.log
// RUN: FileCheck %s -input-file %t.log
// CHECK: "foobar"
.secure_log_unique "foobar"

View File

@ -155,6 +155,9 @@ struct AssemblerInvocation {
/// The version of the darwin target variant SDK which was used during the
/// compilation
llvm::VersionTuple DarwinTargetVariantSDKVersion;
/// The name of a file to use with \c .secure_log_unique directives.
std::string AsSecureLogFile;
/// @}
public:
@ -345,6 +348,8 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
.Case("default", EmitDwarfUnwindType::Default);
}
Opts.AsSecureLogFile = Args.getLastArgValue(OPT_as_secure_log_file);
return Success;
}
@ -396,6 +401,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
MCTargetOptions MCOptions;
MCOptions.EmitDwarfUnwind = Opts.EmitDwarfUnwind;
MCOptions.AsSecureLogFile = Opts.AsSecureLogFile;
std::unique_ptr<MCAsmInfo> MAI(
TheTarget->createMCAsmInfo(*MRI, Opts.Triple, MCOptions));

View File

@ -178,7 +178,7 @@ private:
/// The file name of the log file from the environment variable
/// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique
/// directive is used or it is an error.
char *SecureLogFile;
std::string SecureLogFile;
/// The stream that gets written to for the .secure_log_unique directive.
std::unique_ptr<raw_fd_ostream> SecureLog;
/// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
@ -828,7 +828,7 @@ public:
/// @}
char *getSecureLogFile() { return SecureLogFile; }
StringRef getSecureLogFile() { return SecureLogFile; }
raw_fd_ostream *getSecureLog() { return SecureLog.get(); }
void setSecureLog(std::unique_ptr<raw_fd_ostream> Value) {

View File

@ -76,6 +76,7 @@ public:
std::string ABIName;
std::string AssemblyLanguage;
std::string SplitDwarfFile;
std::string AsSecureLogFile;
const char *Argv0 = nullptr;
ArrayRef<std::string> CommandLineArgs;

View File

@ -47,6 +47,8 @@ bool getNoTypeCheck();
std::string getABIName();
std::string getAsSecureLogFile();
/// Create this object with static storage to register mc-related command
/// line options.
struct RegisterMCTargetOptionsFlags {

View File

@ -59,12 +59,6 @@
using namespace llvm;
static cl::opt<char*>
AsSecureLogFileName("as-secure-log-file-name",
cl::desc("As secure log file name (initialized from "
"AS_SECURE_LOG_FILE env variable)"),
cl::init(getenv("AS_SECURE_LOG_FILE")), cl::Hidden);
static void defaultDiagHandler(const SMDiagnostic &SMD, bool, const SourceMgr &,
std::vector<const MDNode *> &) {
SMD.print(nullptr, errs());
@ -80,7 +74,7 @@ MCContext::MCContext(const Triple &TheTriple, const MCAsmInfo *mai,
InlineAsmUsedLabelNames(Allocator),
CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0),
AutoReset(DoAutoReset), TargetOptions(TargetOpts) {
SecureLogFile = AsSecureLogFileName;
SecureLogFile = TargetOptions ? TargetOptions->AsSecureLogFile : "";
if (SrcMgr && SrcMgr->getNumBuffers())
MainFileName = std::string(SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())

View File

@ -767,8 +767,8 @@ bool DarwinAsmParser::parseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
return Error(IDLoc, ".secure_log_unique specified multiple times");
// Get the secure log path.
const char *SecureLogFile = getContext().getSecureLogFile();
if (!SecureLogFile)
StringRef SecureLogFile = getContext().getSecureLogFile();
if (SecureLogFile.empty())
return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
"environment variable unset.");
@ -776,9 +776,8 @@ bool DarwinAsmParser::parseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
raw_fd_ostream *OS = getContext().getSecureLog();
if (!OS) {
std::error_code EC;
auto NewOS = std::make_unique<raw_fd_ostream>(StringRef(SecureLogFile), EC,
sys::fs::OF_Append |
sys::fs::OF_TextWithCRLF);
auto NewOS = std::make_unique<raw_fd_ostream>(
SecureLogFile, EC, sys::fs::OF_Append | sys::fs::OF_TextWithCRLF);
if (EC)
return Error(IDLoc, Twine("can't open secure log file: ") +
SecureLogFile + " (" + EC.message() + ")");

View File

@ -45,6 +45,7 @@ MCOPT(bool, NoWarn)
MCOPT(bool, NoDeprecatedWarn)
MCOPT(bool, NoTypeCheck)
MCOPT(std::string, ABIName)
MCOPT(std::string, AsSecureLogFile)
llvm::mc::RegisterMCTargetOptionsFlags::RegisterMCTargetOptionsFlags() {
#define MCBINDOPT(NAME) \
@ -114,6 +115,10 @@ llvm::mc::RegisterMCTargetOptionsFlags::RegisterMCTargetOptionsFlags() {
cl::init(""));
MCBINDOPT(ABIName);
static cl::opt<std::string> AsSecureLogFile(
"as-secure-log-file", cl::desc("As secure log file name"), cl::Hidden);
MCBINDOPT(AsSecureLogFile);
#undef MCBINDOPT
}
@ -130,6 +135,7 @@ MCTargetOptions llvm::mc::InitMCTargetOptionsFromFlags() {
Options.MCNoDeprecatedWarn = getNoDeprecatedWarn();
Options.MCNoTypeCheck = getNoTypeCheck();
Options.EmitDwarfUnwind = getEmitDwarfUnwind();
Options.AsSecureLogFile = getAsSecureLogFile();
return Options;
}

View File

@ -1,6 +1,6 @@
// RUN: rm -f %t
// RUN: env AS_SECURE_LOG_FILE=%t llvm-mc -triple x86_64-apple-darwin %s
// RUN: env AS_SECURE_LOG_FILE=%t llvm-mc -triple x86_64-apple-darwin %s
// RUN: llvm-mc -as-secure-log-file %t -triple x86_64-apple-darwin %s
// RUN: llvm-mc -as-secure-log-file %t -triple x86_64-apple-darwin %s
// RUN: FileCheck --input-file=%t %s
.secure_log_unique "foobar"