From f9594071aea70ec344bf086bde7126b6335a54d8 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Wed, 11 May 2022 13:24:49 +0200 Subject: [PATCH] Make the library signature more difficult to remove Use compiler-specific mechanisms to prevent the removal of the identifying hash value and version number. --- include/nlohmann/json.hpp | 17 ++++++++++++++++- single_include/nlohmann/json.hpp | 17 ++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index aed4a1a01..ab5367584 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -546,10 +546,14 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec default: { object = nullptr; // silence warning, see #821 +#if !(defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER)) if (JSON_HEDLEY_UNLIKELY(t == value_t::null)) { - JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.10.5", nullptr)); // LCOV_EXCL_LINE + // inject a hash value into binaries using this library; this method serves as a fallback + // and is easily removed by compiler optimizations + JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.10.5", nullptr)); } +#endif break; } } @@ -5026,6 +5030,17 @@ std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j) return j.dump(); } +// inject a hash value into binaries using this library; this method is inspired by +// __AFL_INIT() from AFL++ and won't be removed from optimized builds +#if defined(__clang__) || defined(__GNUC__) + static const volatile char* _NLOHMANN_JSON_SIGNATURE __attribute__((used, unused)) = + "961c151d2e87f2686a955a9be24d316f1362bf21 3.10.5"; +#elif defined(_MSC_VER) + static const volatile char* _NLOHMANN_JSON_SIGNATURE = + "961c151d2e87f2686a955a9be24d316f1362bf21 3.10.5"; + #pragma comment(linker, "/export:_NLOHMANN_JSON_SIGNATURE") +#endif + } // namespace nlohmann /////////////////////// diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index bbd84d2be..2d56cc562 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -18630,10 +18630,14 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec default: { object = nullptr; // silence warning, see #821 +#if !(defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER)) if (JSON_HEDLEY_UNLIKELY(t == value_t::null)) { - JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.10.5", nullptr)); // LCOV_EXCL_LINE + // inject a hash value into binaries using this library; this method serves as a fallback + // and is easily removed by compiler optimizations + JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.10.5", nullptr)); } +#endif break; } } @@ -23110,6 +23114,17 @@ std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j) return j.dump(); } +// inject a hash value into binaries using this library; this method is inspired by +// __AFL_INIT() from AFL++ and won't be removed from optimized builds +#if defined(__clang__) || defined(__GNUC__) + static const volatile char* _NLOHMANN_JSON_SIGNATURE __attribute__((used, unused)) = + "961c151d2e87f2686a955a9be24d316f1362bf21 3.10.5"; +#elif defined(_MSC_VER) + static const volatile char* _NLOHMANN_JSON_SIGNATURE = + "961c151d2e87f2686a955a9be24d316f1362bf21 3.10.5"; + #pragma comment(linker, "/export:_NLOHMANN_JSON_SIGNATURE") +#endif + } // namespace nlohmann ///////////////////////