diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_flag_parser.h b/compiler-rt/lib/sanitizer_common/sanitizer_flag_parser.h --- a/compiler-rt/lib/sanitizer_common/sanitizer_flag_parser.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_flag_parser.h @@ -22,6 +22,10 @@ class FlagHandlerBase { public: virtual bool Parse(const char *value) { return false; } + virtual bool CurrentValueAsString(char *buffer, uptr size) { + buffer[0] = '\0'; + return false; + } protected: ~FlagHandlerBase() {} @@ -34,6 +38,10 @@ public: explicit FlagHandler(T *t) : t_(t) {} bool Parse(const char *value) final; + // Write the current value into the buffer of size `size` as a C-string. + // Returns true if value was written without truncation, returns false + // otherwise. + bool CurrentValueAsString(char *buffer, uptr size) final; }; inline bool ParseBool(const char *value, bool *b) { @@ -59,6 +67,16 @@ return false; } +template <> +inline bool FlagHandler::CurrentValueAsString(char *buffer, uptr size) { + const char *bool_str = *t_ ? "true" : "false"; + uptr bool_str_len = internal_strlen(bool_str); + uptr copy_count = Min(bool_str_len + 1, size); + internal_strncpy(buffer, bool_str, copy_count); + buffer[copy_count - 1] = '\0'; + return bool_str_len < size; +} + template <> inline bool FlagHandler::Parse(const char *value) { bool b; @@ -75,12 +93,35 @@ return false; } +template <> +inline bool FlagHandler::CurrentValueAsString(char *buffer, + uptr size) { + uptr num_symbols_should_write = internal_snprintf(buffer, size, "%d", *t_); + // Not `<=` because `num_symbols_should_write` does not include null + // terminator. + return num_symbols_should_write < size; +} + template <> inline bool FlagHandler::Parse(const char *value) { *t_ = value; return true; } +template <> +inline bool FlagHandler::CurrentValueAsString(char *buffer, + uptr size) { + const char *str_to_use = *t_; + if (!str_to_use) { + str_to_use = "(nullptr)"; + } + uptr str_len = internal_strlen(str_to_use); + uptr copy_count = Min(str_len + 1, size); + internal_strncpy(buffer, str_to_use, copy_count); + buffer[copy_count - 1] = '\0'; + return str_len < size; +} + template <> inline bool FlagHandler::Parse(const char *value) { const char *value_end; @@ -90,6 +131,14 @@ return ok; } +template <> +inline bool FlagHandler::CurrentValueAsString(char *buffer, uptr size) { + uptr num_symbols_should_write = internal_snprintf(buffer, size, "%d", *t_); + // Not `<=` because `num_symbols_should_write` does not include null + // terminator. + return num_symbols_should_write < size; +} + template <> inline bool FlagHandler::Parse(const char *value) { const char *value_end; @@ -99,6 +148,14 @@ return ok; } +template <> +inline bool FlagHandler::CurrentValueAsString(char *buffer, uptr size) { + uptr num_symbols_should_write = internal_snprintf(buffer, size, "%p", *t_); + // Not `<=` because `num_symbols_should_write` does not include null + // terminator. + return num_symbols_should_write < size; +} + template <> inline bool FlagHandler::Parse(const char *value) { const char *value_end; @@ -108,6 +165,14 @@ return ok; } +template <> +inline bool FlagHandler::CurrentValueAsString(char *buffer, uptr size) { + uptr num_symbols_should_write = internal_snprintf(buffer, size, "%lld", *t_); + // Not `<=` because `num_symbols_should_write` does not include null + // terminator. + return num_symbols_should_write < size; +} + class FlagParser { static const int kMaxFlags = 200; struct Flag { diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_flag_parser.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_flag_parser.cpp --- a/compiler-rt/lib/sanitizer_common/sanitizer_flag_parser.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_flag_parser.cpp @@ -56,9 +56,16 @@ } void FlagParser::PrintFlagDescriptions() { + char buffer[50]; Printf("Available flags for %s:\n", SanitizerToolName); - for (int i = 0; i < n_flags_; ++i) - Printf("\t%s\n\t\t- %s\n", flags_[i].name, flags_[i].desc); + for (int i = 0; i < n_flags_; ++i) { + bool truncated = + !(flags_[i].handler->CurrentValueAsString(buffer, sizeof(buffer))); + buffer[sizeof(buffer) - 1] = '\0'; // Ensure buffer is terminated. + const char *truncation_str = truncated ? " Truncated" : ""; + Printf("\t%s\n\t\t- %s (Current Value%s: %s)\n", flags_[i].name, + flags_[i].desc, truncation_str, buffer); + } } void FlagParser::fatal_error(const char *err) { diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_flags.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_flags.cpp --- a/compiler-rt/lib/sanitizer_common/sanitizer_flags.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_flags.cpp @@ -75,11 +75,13 @@ class FlagHandlerInclude : public FlagHandlerBase { FlagParser *parser_; bool ignore_missing_; + const char *original_path_; public: explicit FlagHandlerInclude(FlagParser *parser, bool ignore_missing) - : parser_(parser), ignore_missing_(ignore_missing) {} + : parser_(parser), ignore_missing_(ignore_missing), original_path_("") {} bool Parse(const char *value) final { + original_path_ = value; if (internal_strchr(value, '%')) { char *buf = (char *)MmapOrDie(kMaxPathLength, "FlagHandlerInclude"); SubstituteForFlagValue(value, buf, kMaxPathLength); @@ -89,6 +91,16 @@ } return parser_->ParseFile(value, ignore_missing_); } + bool CurrentValueAsString(char *buffer, uptr size) { + // FIXME(dliew): `original_path_` isn't actually what's parsed due to `%` + // substitutions. Printing the substituted path would require holding onto + // mmap'ed memory. + uptr str_len = internal_strlen(original_path_); + uptr copy_count = Min(str_len + 1, size); + internal_strncpy(buffer, original_path_, copy_count); + buffer[copy_count - 1] = '\0'; + return str_len < size; + } }; void RegisterIncludeFlags(FlagParser *parser, CommonFlags *cf) { diff --git a/compiler-rt/test/sanitizer_common/TestCases/options-help-include-current-value.cpp b/compiler-rt/test/sanitizer_common/TestCases/options-help-include-current-value.cpp new file mode 100644 --- /dev/null +++ b/compiler-rt/test/sanitizer_common/TestCases/options-help-include-current-value.cpp @@ -0,0 +1,9 @@ +// RUN: %clangxx -O0 %s -o %t +// RUN: %env_tool_opts=help=1,include_if_exists=some_ridculously_long_path_that_will_get_truncated_and_does_not_exist_on_file_system__________ %run %t 2>&1 | FileCheck %s + +int main() { +} + +// CHECK: Available flags for {{.*}}Sanitizer: +// CHECK: include_if_exists +// CHECK-NEXT: (Current Value Truncated: some_ridculously_long_path_{{.*}}) diff --git a/compiler-rt/test/sanitizer_common/TestCases/options-help.cpp b/compiler-rt/test/sanitizer_common/TestCases/options-help.cpp --- a/compiler-rt/test/sanitizer_common/TestCases/options-help.cpp +++ b/compiler-rt/test/sanitizer_common/TestCases/options-help.cpp @@ -1,8 +1,28 @@ // RUN: %clangxx -O0 %s -o %t // RUN: %env_tool_opts=help=1 %run %t 2>&1 | FileCheck %s +// RUN: %env_tool_opts=help=1,symbolize=0 %run %t 2>&1 | FileCheck --check-prefix=CHECK-CV %s int main() { } // CHECK: Available flags for {{.*}}Sanitizer: -// CHECK: handle_segv + +// Test bool option assuming the default. +// CHECK: {{^[ \t]+symbolize$}} +// CHECK-NEXT: (Current Value: true) +// +// Test int option +// CHECK: {{^[ \t]+verbosity$}} +// CHECK-NEXT: (Current Value: {{-?[0-9]+}}) + +// Test HandleSignalMode option +// CHECK: {{^[ \t]+handle_segv$}} +// CHECK-NEXT: (Current Value: {{0|1|2}}) + +// Test uptr option +// CHECK: {{^[ \t]+mmap_limit_mb$}} +// CHECK-NEXT: (Current Value: 0x{{[0-9a-fA-F]+}}) + +// Test we show the current value and not the default. +// CHECK-CV: {{^[ \t]+symbolize$}} +// CHECK-CV-NEXT: (Current Value: false)