From a8e50f882e284b1dc47eb60951ef0672490ac31d Mon Sep 17 00:00:00 2001 From: Guilherme Amadio Date: Thu, 3 Nov 2022 16:55:37 +0100 Subject: [PATCH] Accept only true values for CLING_PROFILE and CLING_DEBUG This implementation of ConvertEnvValueToBool uses const char* as input intentionally in order to be able to accept nullptr for when environment variables are not set, but also because using something like std::string would require bringing in several extra headers. --- interpreter/cling/include/cling/Utils/Utils.h | 38 +++++++++++++++++++ .../cling/lib/Interpreter/CIFactory.cpp | 12 ++++-- .../cling/lib/Interpreter/IncrementalJIT.cpp | 5 ++- 3 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 interpreter/cling/include/cling/Utils/Utils.h diff --git a/interpreter/cling/include/cling/Utils/Utils.h b/interpreter/cling/include/cling/Utils/Utils.h new file mode 100644 index 0000000000000..48f5ef47cafc5 --- /dev/null +++ b/interpreter/cling/include/cling/Utils/Utils.h @@ -0,0 +1,38 @@ +//--------------------------------------------------------------------*- C++ -*- +// CLING - the C++ LLVM-based InterpreterG :) +// author: Guilherme Amadio +// +// This file is dual-licensed: you can choose to license it under the University +// of Illinois Open Source License or the GNU Lesser General Public License. See +// LICENSE.TXT for details. +//------------------------------------------------------------------------------ + +#include +#include + +#ifndef CLING_UTILS_UTILS_H +#define CLING_UTILS_UTILS_H + +#if defined(_MSC_VER) && !defined(strcasecmp) +#define strcasecmp _stricmp +#endif + +namespace cling { + namespace utils { + /** Convert @p value to boolean */ + static inline bool ConvertEnvValueToBool(const char* value) { + const char* true_strs[] = {"1", "true", "on", "yes"}; + + if (!value) + return false; + + for (auto str : true_strs) + if (strcasecmp(value, str) == 0) + return true; + + return false; + } + } // namespace utils +} // namespace cling + +#endif // CLING_UTILS_UTILS_H diff --git a/interpreter/cling/lib/Interpreter/CIFactory.cpp b/interpreter/cling/lib/Interpreter/CIFactory.cpp index 3c028bee7999c..2caa9d51237ab 100644 --- a/interpreter/cling/lib/Interpreter/CIFactory.cpp +++ b/interpreter/cling/lib/Interpreter/CIFactory.cpp @@ -15,6 +15,7 @@ #include "cling/Utils/Output.h" #include "cling/Utils/Paths.h" #include "cling/Utils/Platform.h" +#include "cling/Utils/Utils.h" #include "clang/AST/ASTContext.h" #include "clang/Basic/TargetInfo.h" @@ -1243,6 +1244,11 @@ namespace { std::vector argvCompile(argv, argv+1); argvCompile.reserve(argc+32); + bool debuggingEnabled = + cling::utils::ConvertEnvValueToBool(std::getenv("CLING_DEBUG")); + bool profilingEnabled = + cling::utils::ConvertEnvValueToBool(std::getenv("CLING_PROFILE")); + #if __APPLE__ && __arm64__ argvCompile.push_back("--target=arm64-apple-darwin20.3.0"); #endif @@ -1325,12 +1331,12 @@ namespace { #ifdef __linux__ // Keep frame pointer to make JIT stack unwinding reliable for profiling - if (std::getenv("CLING_PROFILE")) + if (profilingEnabled) argvCompile.push_back("-fno-omit-frame-pointer"); #endif // Disable optimizations and keep frame pointer when debugging - if (std::getenv("CLING_DEBUG")) + if (debuggingEnabled) argvCompile.push_back("-O0 -fno-omit-frame-pointer"); // argv[0] already inserted, get the rest @@ -1672,7 +1678,7 @@ namespace { CGOpts.setInlining(CodeGenOptions::NormalInlining); // Add debugging info when debugging or profiling - if (std::getenv("CLING_DEBUG") || std::getenv("CLING_PROFILE")) + if (debuggingEnabled || profilingEnabled) CGOpts.setDebugInfo(clang::codegenoptions::FullDebugInfo); // CGOpts.EmitDeclMetadata = 1; // For unloading, for later diff --git a/interpreter/cling/lib/Interpreter/IncrementalJIT.cpp b/interpreter/cling/lib/Interpreter/IncrementalJIT.cpp index f0130e5e4fc07..d8b7ac05271f8 100644 --- a/interpreter/cling/lib/Interpreter/IncrementalJIT.cpp +++ b/interpreter/cling/lib/Interpreter/IncrementalJIT.cpp @@ -11,6 +11,7 @@ #include "IncrementalExecutor.h" #include "cling/Utils/Platform.h" +#include "cling/Utils/Utils.h" #include "llvm/ExecutionEngine/Orc/Legacy.h" #include "llvm/ExecutionEngine/SectionMemoryManager.h" @@ -359,12 +360,12 @@ IncrementalJIT::IncrementalJIT(IncrementalExecutor& exe, llvm::sys::DynamicLibrary::LoadLibraryPermanently(0, 0); // Enable GDB JIT listener for debugging if CLING_DEBUG is set - if (std::getenv("CLING_DEBUG")) + if (cling::utils::ConvertEnvValueToBool(std::getenv("CLING_DEBUG"))) m_Listeners.push_back(JITEventListener::createGDBRegistrationListener()); #ifdef __linux__ // Enable perf profiling of JITted code on Linux if CLING_PROFILE is set - if (std::getenv("CLING_PROFILE")) + if (cling::utils::ConvertEnvValueToBool(std::getenv("CLING_PROFILE"))) m_Listeners.push_back(cling::createPerfJITEventListener()); #endif