From 754460f861874108887e4bba193c8c46e3ae919f Mon Sep 17 00:00:00 2001 From: oxalica Date: Wed, 1 Dec 2021 10:47:28 +0800 Subject: [PATCH 1/2] llvmPackages_13.clang: revert D100879 The malloc alignment assumption is incorrect for jemalloc and causes mis-compilation in firefox. https://reviews.llvm.org/D100879 https://bugzilla.mozilla.org/show_bug.cgi?id=1741454 --- .../compilers/llvm/13/clang/default.nix | 5 ++ .../revert-malloc-alignment-assumption.patch | 78 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 pkgs/development/compilers/llvm/13/clang/revert-malloc-alignment-assumption.patch diff --git a/pkgs/development/compilers/llvm/13/clang/default.nix b/pkgs/development/compilers/llvm/13/clang/default.nix index a775af3031b7..ab826fa31526 100644 --- a/pkgs/development/compilers/llvm/13/clang/default.nix +++ b/pkgs/development/compilers/llvm/13/clang/default.nix @@ -37,6 +37,11 @@ let ./purity.patch # https://reviews.llvm.org/D51899 ./gnu-install-dirs.patch + # Revert of https://reviews.llvm.org/D100879 + # The malloc alignment assumption is incorrect for jemalloc and causes + # mis-compilation in firefox. + # See: https://bugzilla.mozilla.org/show_bug.cgi?id=1741454 + ./revert-malloc-alignment-assumption.patch (substituteAll { src = ../../clang-11-12-LLVMgold-path.patch; libllvmLibdir = "${libllvm.lib}/lib"; diff --git a/pkgs/development/compilers/llvm/13/clang/revert-malloc-alignment-assumption.patch b/pkgs/development/compilers/llvm/13/clang/revert-malloc-alignment-assumption.patch new file mode 100644 index 000000000000..f8e55ff7f897 --- /dev/null +++ b/pkgs/development/compilers/llvm/13/clang/revert-malloc-alignment-assumption.patch @@ -0,0 +1,78 @@ +--- b/include/clang/Basic/TargetInfo.h ++++ a/include/clang/Basic/TargetInfo.h +@@ -612,8 +612,8 @@ + } + + /// Return the largest alignment for which a suitably-sized allocation with ++ /// '::operator new(size_t)' is guaranteed to produce a correctly-aligned ++ /// pointer. +- /// '::operator new(size_t)' or 'malloc' is guaranteed to produce a +- /// correctly-aligned pointer. + unsigned getNewAlign() const { + return NewAlign ? NewAlign : std::max(LongDoubleAlign, LongLongAlign); + } +--- b/lib/CodeGen/CGCall.cpp ++++ a/lib/CodeGen/CGCall.cpp +@@ -2048,24 +2048,6 @@ + // allows it to work on indirect virtual function calls. + if (AttrOnCallSite && TargetDecl->hasAttr()) + FuncAttrs.addAttribute(llvm::Attribute::NoMerge); +- +- // Add known guaranteed alignment for allocation functions. +- if (unsigned BuiltinID = Fn->getBuiltinID()) { +- switch (BuiltinID) { +- case Builtin::BIaligned_alloc: +- case Builtin::BIcalloc: +- case Builtin::BImalloc: +- case Builtin::BImemalign: +- case Builtin::BIrealloc: +- case Builtin::BIstrdup: +- case Builtin::BIstrndup: +- RetAttrs.addAlignmentAttr(Context.getTargetInfo().getNewAlign() / +- Context.getTargetInfo().getCharWidth()); +- break; +- default: +- break; +- } +- } + } + + // 'const', 'pure' and 'noalias' attributed functions are also nounwind. +--- b/test/CodeGen/alloc-fns-alignment.c ++++ /dev/null +@@ -1,35 +0,0 @@ +-// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm < %s | FileCheck %s --check-prefix=ALIGN16 +-// RUN: %clang_cc1 -triple x86_64-windows-msvc -emit-llvm < %s | FileCheck %s --check-prefix=ALIGN16 +-// RUN: %clang_cc1 -triple i386-apple-darwin -emit-llvm < %s | FileCheck %s --check-prefix=ALIGN16 +-// RUN: %clang_cc1 -triple i386-unknown-linux-gnu -emit-llvm < %s | FileCheck %s --check-prefix=ALIGN8 +-// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-builtin-malloc -emit-llvm < %s | FileCheck %s --check-prefix=NOBUILTIN-MALLOC +-// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-builtin-calloc -emit-llvm < %s | FileCheck %s --check-prefix=NOBUILTIN-CALLOC +-// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-builtin-realloc -emit-llvm < %s | FileCheck %s --check-prefix=NOBUILTIN-REALLOC +- +-typedef __SIZE_TYPE__ size_t; +- +-void *malloc(size_t); +-void *calloc(size_t, size_t); +-void *realloc(void *, size_t); +- +-void *malloc_test(size_t n) { +- return malloc(n); +-} +- +-void *calloc_test(size_t n) { +- return calloc(1, n); +-} +- +-void *raalloc_test(void *p, size_t n) { +- return realloc(p, n); +-} +- +-// ALIGN16: align 16 i8* @malloc +-// ALIGN16: align 16 i8* @calloc +-// ALIGN16: align 16 i8* @realloc +-// ALIGN8: align 8 i8* @malloc +-// ALIGN8: align 8 i8* @calloc +-// ALIGN8: align 8 i8* @realloc +-// NOBUILTIN-MALLOC: declare i8* @malloc +-// NOBUILTIN-CALLOC: declare i8* @calloc +-// NOBUILTIN-REALLOC: declare i8* @realloc From cd44576bff17680e248e32e1cc36f21fde5268c3 Mon Sep 17 00:00:00 2001 From: oxalica Date: Wed, 1 Dec 2021 10:51:18 +0800 Subject: [PATCH 2/2] firefox: re-enable jemalloc --- pkgs/applications/networking/browsers/firefox/common.nix | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkgs/applications/networking/browsers/firefox/common.nix b/pkgs/applications/networking/browsers/firefox/common.nix index b0d11bd835b6..f3801a9dbe0c 100644 --- a/pkgs/applications/networking/browsers/firefox/common.nix +++ b/pkgs/applications/networking/browsers/firefox/common.nix @@ -27,9 +27,8 @@ , ltoSupport ? (stdenv.isLinux && stdenv.is64bit), overrideCC, buildPackages , gssSupport ? true, libkrb5 , pipewireSupport ? waylandSupport && webrtcSupport, pipewire -# Workaround: disabled since currently jemalloc causes crashes with LLVM 13. -# https://bugzilla.mozilla.org/show_bug.cgi?id=1741454 -, jemallocSupport ? false, jemalloc +# Jemalloc could reduce memory consumption. +, jemallocSupport ? true, jemalloc ## privacy-related options