tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

memory.configure (5620B)


      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     # Win32 has frequent crashes when stack tracing (for unclear reasons),
     87     # so PHC is enabled only on 64-bit.  MacOS doesn't have a 32-bit version.
     88     # Therefore we've only been testing on 64bit processors.
     89     return (target.cpu in ("x86_64", "aarch64")) and (
     90         (target.os == "GNU" and target.kernel == "Linux")
     91         or (target.kernel == "WINNT")
     92         or (target.os == "OSX")
     93         or (target.os == "Android" and milestone.is_early_beta_or_earlier)
     94     )
     95 
     96 
     97 option(
     98     "--enable-phc",
     99     env="MOZ_PHC",
    100     default=phc_default,
    101     when="--enable-jemalloc",
    102     help="{Enable|Disable} PHC (Probabilistic Memory Checker). "
    103     "Also enables frame pointers when needed",
    104 )
    105 
    106 set_config("MOZ_PHC", True, when="--enable-phc")
    107 
    108 
    109 # PHC parses stacks using frame pointers on these systems.
    110 @depends("--enable-phc", target, have_unwind, when="--enable-jemalloc")
    111 def phc_implies_frame_pointers(phc, target, have_unwind):
    112     if not phc:
    113         return False
    114 
    115     # This should be kept in sync with the ifdefs in memory/build/PHC.cpp
    116     # that control stack walking.
    117     # This is true for the first two options in PHC.cpp
    118     if (target.os == "WINNT" and target.cpu == "x86") or target.kernel == "Darwin":
    119         return True
    120 
    121     # This should match the #defines in mozglue/misc/StackWalk.cpp
    122     if (target.cpu in ("x86", "ppc")) and (target.kernel in ("Darwin", "Linux")):
    123         return not have_unwind
    124 
    125     return False
    126 
    127 
    128 imply_option("--enable-frame-pointers", True, when=phc_implies_frame_pointers)
    129 
    130 
    131 with only_when(depends(target.os)(lambda os: os != "WINNT")):
    132     set_define("HAVE_STRNDUP", check_symbol("strndup"))
    133     set_define("HAVE_POSIX_MEMALIGN", check_symbol("posix_memalign"))
    134     set_define("HAVE_MEMALIGN", check_symbol("memalign"))
    135 
    136     have_malloc_usable_size = check_symbol("malloc_usable_size")
    137     set_define("HAVE_MALLOC_USABLE_SIZE", have_malloc_usable_size)
    138 
    139 
    140 @template
    141 def check_malloc_header():
    142     found_header = dependable(None)
    143     malloc_headers = ("malloc.h", "malloc_np.h", "malloc/malloc.h", "sys/malloc.h")
    144     for malloc_h in malloc_headers:
    145         have_malloc_h = check_header(malloc_h, when=~found_header)
    146         found_header = depends(have_malloc_h, found_header)(
    147             lambda h, f: malloc_h if h else f
    148         )
    149     return found_header
    150 
    151 
    152 malloc_h = check_malloc_header()
    153 have_malloc_h = depends(malloc_h)(lambda h: h == "malloc.h")
    154 
    155 set_define("MALLOC_H", depends_if(malloc_h)(lambda h: f"<{h}>"))
    156 
    157 usable_size_constness = c_compiler.try_compile(
    158     includes=depends(malloc_h)(lambda h: [h, "stddef.h"]),
    159     body="""
    160         extern size_t malloc_usable_size(const void *ptr);
    161         return malloc_usable_size(0);
    162         """,
    163     check_msg="whether malloc_usable_size definition can use const argument",
    164     when=have_malloc_usable_size,
    165 )
    166 
    167 set_define(
    168     "MALLOC_USABLE_SIZE_CONST_PTR",
    169     depends(usable_size_constness, have_malloc_h)(
    170         lambda u, h: "const" if (u or not h) else ""
    171     ),
    172 )