From 622b111f9e4238f6d8fb7b587b366e98e76f6474 Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Tue, 16 Feb 2021 21:20:04 +0000 Subject: [PATCH] Bug 1690167 - Change VsprintfLiteral/SprintfLiteral to rely on PrintfTarget. r=nika,Gankra,firefox-build-system-reviewers,mhentges Instead of snprintf. Because some standalone code uses those functions directly or indirectly, and PrintfTarget lives in mozglue, they now need to depend on mozglue instead of mfbt. Except logalloc/replay, which cherry-picks what it uses. Differential Revision: https://phabricator.services.mozilla.com/D103730 --- media/gmp-clearkey/0.1/moz.build | 2 +- memory/replace/logalloc/replay/moz.build | 14 ++++++- mfbt/tests/moz.build | 5 +-- mozglue/misc/Printf.h | 2 + mozglue/misc/Sprintf.h | 39 +++++++++++++++++-- .../mozapps/update/updater/TsanOptions.cpp | 23 ----------- .../update/updater/updater-common.build | 8 +--- xpcom/tests/moz.build | 2 +- 8 files changed, 55 insertions(+), 40 deletions(-) delete mode 100644 toolkit/mozapps/update/updater/TsanOptions.cpp diff --git a/media/gmp-clearkey/0.1/moz.build b/media/gmp-clearkey/0.1/moz.build index 4dc7e87678896..a4200a76ac23f 100644 --- a/media/gmp-clearkey/0.1/moz.build +++ b/media/gmp-clearkey/0.1/moz.build @@ -7,7 +7,7 @@ with Files("**"): BUG_COMPONENT = ("Core", "Audio/Video: GMP") -SharedLibrary("clearkey") +GeckoSharedLibrary("clearkey", linkage=None) FINAL_TARGET = "dist/bin/gmp-clearkey/0.1" diff --git a/memory/replace/logalloc/replay/moz.build b/memory/replace/logalloc/replay/moz.build index 3e0c8c395aeed..ca85f175570fc 100644 --- a/memory/replace/logalloc/replay/moz.build +++ b/memory/replace/logalloc/replay/moz.build @@ -44,7 +44,19 @@ if CONFIG["MOZ_DMD"] or CONFIG["MOZ_PHC"]: "/mozglue/misc/StackWalk.h", ] -if not CONFIG["MOZ_REPLACE_MALLOC_STATIC"]: +if CONFIG["MOZ_REPLACE_MALLOC_STATIC"]: + UNIFIED_SOURCES += [ + "/mfbt/double-conversion/double-conversion/bignum-dtoa.cc", + "/mfbt/double-conversion/double-conversion/bignum.cc", + "/mfbt/double-conversion/double-conversion/cached-powers.cc", + "/mfbt/double-conversion/double-conversion/double-to-string.cc", + "/mfbt/double-conversion/double-conversion/fast-dtoa.cc", + "/mfbt/double-conversion/double-conversion/fixed-dtoa.cc", + "/mfbt/double-conversion/double-conversion/string-to-double.cc", + "/mfbt/double-conversion/double-conversion/strtod.cc", + "/mozglue/misc/Printf.cpp", + ] +else: SOURCES += [ "../FdPrintf.cpp", ] diff --git a/mfbt/tests/moz.build b/mfbt/tests/moz.build index 5d08728f84928..9c8ebc838ced9 100644 --- a/mfbt/tests/moz.build +++ b/mfbt/tests/moz.build @@ -91,9 +91,6 @@ if not CONFIG["MOZ_ASAN"]: ] ) -# Since we link directly with MFBT object files, define IMPL_MFBT -DEFINES["IMPL_MFBT"] = True - DisableStlWrapping() if CONFIG["CC_TYPE"] == "clang-cl": @@ -103,7 +100,7 @@ if CONFIG["CC_TYPE"] == "clang-cl": ] USE_LIBS += [ - "mfbt", + "mozglue", ] if CONFIG["CC_TYPE"] in ("clang", "gcc"): diff --git a/mozglue/misc/Printf.h b/mozglue/misc/Printf.h index e073a0d61cac4..3f4fa2c1bb89b 100644 --- a/mozglue/misc/Printf.h +++ b/mozglue/misc/Printf.h @@ -88,6 +88,8 @@ class PrintfTarget { bool MFBT_API appendIntOct(uint64_t); bool MFBT_API appendIntHex(uint64_t); + inline size_t emitted() { return mEmitted; } + protected: MFBT_API PrintfTarget(); virtual ~PrintfTarget() = default; diff --git a/mozglue/misc/Sprintf.h b/mozglue/misc/Sprintf.h index 14dcbe0c1deab..381958f612b1c 100644 --- a/mozglue/misc/Sprintf.h +++ b/mozglue/misc/Sprintf.h @@ -11,19 +11,52 @@ #include #include +#include #include "mozilla/Assertions.h" #include "mozilla/Attributes.h" +#include "mozilla/Printf.h" #ifdef __cplusplus +namespace mozilla { +namespace detail { + +struct MOZ_STACK_CLASS SprintfAppend final : public mozilla::PrintfTarget { + template + explicit SprintfAppend(char (&aBuf)[N]) : mBuf(aBuf), mBufLen(N) {} + + bool append(const char* aStr, size_t aLen) override { + if (aLen == 0) { + return true; + } + // Don't copy more than what's left to use. + size_t copy = std::min(mBufLen, aLen); + if (copy > 0) { + memcpy(mBuf, aStr, copy); + mBuf += copy; + mBufLen -= copy; + } + return true; + } + + private: + char* mBuf; + size_t mBufLen; +}; + +} // namespace detail +} // namespace mozilla + template MOZ_FORMAT_PRINTF(2, 0) int VsprintfLiteral(char (&buffer)[N], const char* format, va_list args) { MOZ_ASSERT(format != buffer); - int result = vsnprintf(buffer, N, format, args); - buffer[N - 1] = '\0'; - return result; + mozilla::detail::SprintfAppend ss(buffer); + ss.vprint(format, args); + size_t len = ss.emitted(); + buffer[std::min(len, N - 1)] = '\0'; + return len; } template diff --git a/toolkit/mozapps/update/updater/TsanOptions.cpp b/toolkit/mozapps/update/updater/TsanOptions.cpp deleted file mode 100644 index 44a0b53afc2d3..0000000000000 --- a/toolkit/mozapps/update/updater/TsanOptions.cpp +++ /dev/null @@ -1,23 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include "mozilla/Attributes.h" -#include "mozilla/TsanOptions.h" - -#ifndef _MSC_VER // Not supported by clang-cl yet - -// See also mozglue/build/TsanOptions.cpp before modifying this -extern "C" const char* __tsan_default_suppressions() { - // clang-format off - return "# Add your suppressions below\n" - - // External uninstrumented libraries - MOZ_TSAN_DEFAULT_EXTLIB_SUPPRESSIONS - - // End of suppressions. - ; // Please keep this semicolon. - // clang-format on -} -#endif // _MSC_VER diff --git a/toolkit/mozapps/update/updater/updater-common.build b/toolkit/mozapps/update/updater/updater-common.build index 2f2a210f255b0..70bb71b735c1a 100644 --- a/toolkit/mozapps/update/updater/updater-common.build +++ b/toolkit/mozapps/update/updater/updater-common.build @@ -43,6 +43,7 @@ if CONFIG["OS_ARCH"] == "WINNT": USE_LIBS += [ "bspatch", "mar", + "mozglue", "updatecommon", "xz-embedded", ] @@ -78,13 +79,6 @@ if have_progressui == 0: SOURCES += sorted(srcs) -if CONFIG["MOZ_TSAN"]: - # Since mozglue is not linked to the updater, - # we need to include our own TSan suppression list. - SOURCES += [ - "TsanOptions.cpp", - ] - DEFINES["NS_NO_XPCOM"] = True DisableStlWrapping() for var in ("MAR_CHANNEL_ID", "MOZ_APP_VERSION"): diff --git a/xpcom/tests/moz.build b/xpcom/tests/moz.build index e05fe9093d910..4758c655ace10 100644 --- a/xpcom/tests/moz.build +++ b/xpcom/tests/moz.build @@ -24,7 +24,7 @@ test_progs = [ ] SimplePrograms(test_progs) -USE_LIBS += ["mfbt"] +USE_LIBS += ["mozglue"] XPCSHELL_TESTS_MANIFESTS += ["unit/xpcshell.ini"]