| 1 |
# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- |
| 2 |
# vim: set filetype=python: |
| 3 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
|
| 8 |
@depends(target, js_package) |
| 9 |
def jemalloc_default(target, js_package): |
| 10 |
if js_package: |
| 11 |
return False |
| 12 |
return target.kernel in ("Darwin", "Linux", "WINNT") |
| 13 |
|
| 14 |
|
| 15 |
# mozjemalloc doesn't work with these sanitisers, it mostly-works with ubsan |
| 16 |
# (so we leave that enabled) except for loading the configuration from the |
| 17 |
# environment, it will fall-back to default settings. |
| 18 |
imply_option("--enable-jemalloc", False, when=asan | msan | tsan) |
| 19 |
|
| 20 |
option( |
| 21 |
"--enable-jemalloc", |
| 22 |
env="MOZ_MEMORY", |
| 23 |
default=jemalloc_default, |
| 24 |
help="{Replace|Do not replace} memory allocator with jemalloc", |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
set_config("MOZ_MEMORY", True, when="--enable-jemalloc") |
| 29 |
set_define("MOZ_MEMORY", True, when="--enable-jemalloc") |
| 30 |
|
| 31 |
|
| 32 |
@depends("--enable-jemalloc", moz_debug, win32_redist_dir) |
| 33 |
def check_redist(jemalloc, debug, win32_redist_dir): |
| 34 |
if not jemalloc and not win32_redist_dir and not debug: |
| 35 |
log.warning( |
| 36 |
"When not building jemalloc, you need to build with --with-redist or set WIN32_REDIST_DIR." |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
@depends(milestone, build_project) |
| 41 |
def replace_malloc_default(milestone, build_project): |
| 42 |
if build_project == "memory": |
| 43 |
return True |
| 44 |
if milestone.is_early_beta_or_earlier and build_project != "js": |
| 45 |
return True |
| 46 |
|
| 47 |
|
| 48 |
option( |
| 49 |
"--enable-replace-malloc", |
| 50 |
default=replace_malloc_default, |
| 51 |
when="--enable-jemalloc", |
| 52 |
help="{Enable|Disable} ability to dynamically replace the malloc implementation", |
| 53 |
) |
| 54 |
|
| 55 |
|
| 56 |
set_config("MOZ_REPLACE_MALLOC", True, when="--enable-replace-malloc") |
| 57 |
set_define("MOZ_REPLACE_MALLOC", True, when="--enable-replace-malloc") |
| 58 |
|
| 59 |
|
| 60 |
@depends(build_project, when="--enable-replace-malloc") |
| 61 |
def replace_malloc_static(build_project): |
| 62 |
# Default to statically linking replace-malloc libraries that can be |
| 63 |
# statically linked, except when building with --enable-project=memory. |
| 64 |
if build_project != "memory": |
| 65 |
return True |
| 66 |
|
| 67 |
|
| 68 |
set_config("MOZ_REPLACE_MALLOC_STATIC", replace_malloc_static) |
| 69 |
|
| 70 |
# PHC (Probabilistic Heap Checker) |
| 71 |
# ============================================================== |
| 72 |
|
| 73 |
|
| 74 |
# In general, it only makes sense for PHC to run on the platforms that have a |
| 75 |
# crash reporter. |
| 76 |
@depends( |
| 77 |
build_project, |
| 78 |
milestone, |
| 79 |
target, |
| 80 |
when="--enable-jemalloc", |
| 81 |
) |
| 82 |
def phc_default(build_project, milestone, target): |
| 83 |
if build_project == "js": |
| 84 |
return False |
| 85 |
|
| 86 |
# Both Linux32 and Win32 have frequent crashes when stack tracing (for |
| 87 |
# unclear reasons), so PHC is enabled only on 64-bit only in both cases. |
| 88 |
# MacOS doesn't have a 32-bit version. Therefore we've only been |
| 89 |
# testing on 64bit processors. |
| 90 |
return (target.cpu in ("x86_64", "aarch64")) and ( |
| 91 |
(target.os == "GNU" and target.kernel == "Linux") |
| 92 |
or (target.kernel == "WINNT") |
| 93 |
or (target.os == "OSX") |
| 94 |
or (target.os == "Android" and milestone.is_early_beta_or_earlier) |
| 95 |
) |
| 96 |
|
| 97 |
|
| 98 |
option( |
| 99 |
"--enable-phc", |
| 100 |
env="MOZ_PHC", |
| 101 |
default=phc_default, |
| 102 |
when="--enable-jemalloc", |
| 103 |
help="{Enable|Disable} PHC (Probabilistic Memory Checker). " |
| 104 |
"Also enables frame pointers when needed", |
| 105 |
) |
| 106 |
|
| 107 |
set_config("MOZ_PHC", True, when="--enable-phc") |
| 108 |
|
| 109 |
|
| 110 |
# PHC parses stacks using frame pointers on these systems. |
| 111 |
@depends("--enable-phc", target, have_unwind, when="--enable-jemalloc") |
| 112 |
def phc_implies_frame_pointers(phc, target, have_unwind): |
| 113 |
if not phc: |
| 114 |
return False |
| 115 |
|
| 116 |
# This should be kept in sync with the ifdefs in memory/build/PHC.cpp |
| 117 |
# that control stack walking. |
| 118 |
# This is true for the first two options in PHC.cpp |
| 119 |
if (target.os == "WINNT" and target.cpu == "x86") or target.kernel == "Darwin": |
| 120 |
return True |
| 121 |
|
| 122 |
# This should match the #defines in mozglue/misc/StackWalk.cpp |
| 123 |
if (target.cpu in ("x86", "ppc")) and (target.kernel in ("Darwin", "Linux")): |
| 124 |
return not have_unwind |
| 125 |
|
| 126 |
return False |
| 127 |
|
| 128 |
|
| 129 |
imply_option("--enable-frame-pointers", True, when=phc_implies_frame_pointers) |
| 130 |
|
| 131 |
|
| 132 |
with only_when(depends(target.os)(lambda os: os != "WINNT")): |
| 133 |
set_define("HAVE_STRNDUP", check_symbol("strndup")) |
| 134 |
set_define("HAVE_POSIX_MEMALIGN", check_symbol("posix_memalign")) |
| 135 |
set_define("HAVE_MEMALIGN", check_symbol("memalign")) |
| 136 |
|
| 137 |
have_malloc_usable_size = check_symbol("malloc_usable_size") |
| 138 |
set_define("HAVE_MALLOC_USABLE_SIZE", have_malloc_usable_size) |
| 139 |
|
| 140 |
|
| 141 |
@template |
| 142 |
def check_malloc_header(): |
| 143 |
found_header = dependable(None) |
| 144 |
malloc_headers = ("malloc.h", "malloc_np.h", "malloc/malloc.h", "sys/malloc.h") |
| 145 |
for malloc_h in malloc_headers: |
| 146 |
have_malloc_h = check_header(malloc_h, when=~found_header) |
| 147 |
found_header = depends(have_malloc_h, found_header)( |
| 148 |
lambda h, f: malloc_h if h else f |
| 149 |
) |
| 150 |
return found_header |
| 151 |
|
| 152 |
|
| 153 |
malloc_h = check_malloc_header() |
| 154 |
have_malloc_h = depends(malloc_h)(lambda h: h == "malloc.h") |
| 155 |
|
| 156 |
set_define("MALLOC_H", depends_if(malloc_h)(lambda h: f"<{h}>")) |
| 157 |
|
| 158 |
usable_size_constness = c_compiler.try_compile( |
| 159 |
includes=depends(malloc_h)(lambda h: [h, "stddef.h"]), |
| 160 |
body=""" |
| 161 |
extern size_t malloc_usable_size(const void *ptr); |
| 162 |
return malloc_usable_size(0); |
| 163 |
""", |
| 164 |
check_msg="whether malloc_usable_size definition can use const argument", |
| 165 |
when=have_malloc_usable_size, |
| 166 |
) |
| 167 |
|
| 168 |
set_define( |
| 169 |
"MALLOC_USABLE_SIZE_CONST_PTR", |
| 170 |
depends(usable_size_constness, have_malloc_h)( |
| 171 |
lambda u, h: "const" if (u or not h) else "" |
| 172 |
), |
| 173 |
) |