From e367fc303d6998af2cd2542ca052fa9b122588de Mon Sep 17 00:00:00 2001 From: George Burgess IV Date: Wed, 9 Apr 2025 09:31:42 -0600 Subject: [PATCH] cros: add user-enumeration patch Patch to complain if someone's using the portable toolchains without indicating through flags/the environment that they've talked to the CrOS toolchain about their usage. This should be removed by EOY2025. BUG=b:396436337 TEST=CQ patch.cherry: false patch.platforms: chromiumos patch.version_range.from: null patch.version_range.until: null --- clang/lib/Driver/Driver.cpp | 9 ++++ llvm/include/llvm/Support/ChromeOSUserEnum.h | 57 ++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 llvm/include/llvm/Support/ChromeOSUserEnum.h diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index e12416e51f8d..1d7d0e8dae86 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -78,6 +78,7 @@ #include "llvm/Option/OptTable.h" #include "llvm/Option/Option.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/ChromeOSUserEnum.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ExitCodes.h" #include "llvm/Support/FileSystem.h" @@ -1204,6 +1205,14 @@ bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) { Compilation *Driver::BuildCompilation(ArrayRef ArgList) { llvm::PrettyStackTraceString CrashInfo("Compilation construction"); + if (chromeos_user_enum::shouldCheckForCmdlineFlag()) { + StringRef AckFlag = "-D_CROSTC_IS_AWARE_OF_THIS_USECASE"; + bool HasAckFlag = + llvm::any_of(ArgList, [&](const char *A) { return AckFlag == A; }); + if (!HasAckFlag) + chromeos_user_enum::complainAboutNoAckAndDie(AckFlag); + } + // FIXME: Handle environment options which affect driver behavior, somewhere // (client?). GCC_EXEC_PREFIX, LPATH, CC_PRINT_OPTIONS. diff --git a/llvm/include/llvm/Support/ChromeOSUserEnum.h b/llvm/include/llvm/Support/ChromeOSUserEnum.h new file mode 100644 index 000000000000..c30a40544d81 --- /dev/null +++ b/llvm/include/llvm/Support/ChromeOSUserEnum.h @@ -0,0 +1,57 @@ +#ifndef LLVM_SUPPORT_CHROMEOSUSERENUM_H +#define LLVM_SUPPORT_CHROMEOSUSERENUM_H + +#include "llvm/ADT/StringRef.h" + +#include +#include +#include + +// N.B., To minimize the chance of merge conflicts in cmake files, this is +// header-only. The 'inline's below are written for linkage, not performance. +namespace chromeos_user_enum { +const char ACK_ENV_VAR[] = "CROSTC_IS_AWARE_OF_THIS_USECASE"; + +inline bool isExecutingInPortableEnv() { + // TODO: This may be too tricky? + // This env var is set by shell scripts that invoke portable toolchain + // binaries. + return getenv("LD_ARGV0_REL") != nullptr; +} + +inline bool shouldCheckForCmdlineFlag() { + return isExecutingInPortableEnv() && getenv(ACK_ENV_VAR) == nullptr; +} + +[[noreturn]] inline void complainAboutNoAckAndDie(llvm::StringRef flagToPass) { + std::string s; + s += "Hi!\n"; + s += "\n"; + s += "We (the CrOS toolchain team) are trying to identify\n"; + s += "users of this toolchain outside of ChromeOS.\n"; + s += "\n"; + s += "If your team/use-case isn't listed on b/396436337,\n"; + s += "please comment on that bug to notify us of your usage,\n"; + s += "and give us info on how to best contact you. If you\n"; + s += "can't access that bug, please instead email\n"; + s += "chromeos-toolchain@google.com with this information.\n"; + s += "\n"; + s += "After you've done this, you can fully bypass this\n"; + s += "message by setting the env var '"; + s += ACK_ENV_VAR; + s += "=1'.\n"; + s += "\n"; + if (!flagToPass.empty()) { + s += "Alternatively, you can pass the flag '"; + s += flagToPass.str(); + s += "' to\n"; + s += "this binary\n"; + s += "\n"; + } + s += "Thanks for helping us determine who's using our tooling!\n"; + fprintf(stderr, s.c_str()); + exit(1); +} +} // namespace chromeos_user_enum + +#endif // LLVM_SUPPORT_CHROMEOSUSERENUM_H -- 2.49.0.1112.g889b7c5bd8-goog