Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | # |
michael@0 | 2 | # This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | # License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 5 | |
michael@0 | 6 | DIST_INSTALL = 1 |
michael@0 | 7 | |
michael@0 | 8 | # For FORCE_SHARED_LIB |
michael@0 | 9 | include $(topsrcdir)/config/config.mk |
michael@0 | 10 | |
michael@0 | 11 | ifneq (1_1,$(MOZ_MEMORY)_$(or $(MOZ_NATIVE_JEMALLOC),$(FORCE_SHARED_LIB))) |
michael@0 | 12 | |
michael@0 | 13 | ifneq (,$(filter WINNT,$(OS_ARCH))) |
michael@0 | 14 | SDK_LIBRARY = $(IMPORT_LIBRARY) |
michael@0 | 15 | else |
michael@0 | 16 | ifdef FORCE_SHARED_LIB |
michael@0 | 17 | SDK_LIBRARY = $(SHARED_LIBRARY) |
michael@0 | 18 | else |
michael@0 | 19 | SDK_LIBRARY = $(REAL_LIBRARY) |
michael@0 | 20 | endif |
michael@0 | 21 | endif |
michael@0 | 22 | |
michael@0 | 23 | endif |
michael@0 | 24 | |
michael@0 | 25 | MOZ_GLUE_LDFLAGS = # Don't link against ourselves |
michael@0 | 26 | |
michael@0 | 27 | SHARED_LIBRARY_LIBS += $(call EXPAND_LIBNAME_PATH,mfbt,$(DEPTH)/mfbt) |
michael@0 | 28 | |
michael@0 | 29 | ifneq (,$(ZLIB_IN_MOZGLUE)$(MOZ_LINKER)) |
michael@0 | 30 | ifdef MOZ_NATIVE_ZLIB |
michael@0 | 31 | EXTRA_DSO_LDOPTS += $(MOZ_ZLIB_LIBS) |
michael@0 | 32 | else |
michael@0 | 33 | SHARED_LIBRARY_LIBS += $(MOZ_ZLIB_LIBS) |
michael@0 | 34 | endif |
michael@0 | 35 | endif |
michael@0 | 36 | |
michael@0 | 37 | ifeq (WINNT,$(OS_TARGET)) |
michael@0 | 38 | mozglue.def: mozglue.def.in |
michael@0 | 39 | $(call py_action,preprocessor,$(if $(MOZ_REPLACE_MALLOC),-DMOZ_REPLACE_MALLOC) $(ACDEFINES) $< -o $@) |
michael@0 | 40 | |
michael@0 | 41 | GARBAGE += mozglue.def |
michael@0 | 42 | |
michael@0 | 43 | ifneq (,$(filter -DEFAULTLIB:mozcrt,$(MOZ_GLUE_LDFLAGS))) |
michael@0 | 44 | # Don't install the import library if we use mozcrt |
michael@0 | 45 | NO_INSTALL_IMPORT_LIBRARY = 1 |
michael@0 | 46 | endif |
michael@0 | 47 | |
michael@0 | 48 | |
michael@0 | 49 | EXTRA_DSO_LDOPTS += \ |
michael@0 | 50 | $(call EXPAND_LIBNAME,version) \ |
michael@0 | 51 | $(NULL) |
michael@0 | 52 | |
michael@0 | 53 | endif |
michael@0 | 54 | |
michael@0 | 55 | ifeq (Darwin_1,$(OS_TARGET)_$(MOZ_REPLACE_MALLOC)) |
michael@0 | 56 | EXTRA_DSO_LDOPTS += \ |
michael@0 | 57 | -Wl,-U,_replace_init \ |
michael@0 | 58 | -Wl,-U,_replace_malloc \ |
michael@0 | 59 | -Wl,-U,_replace_posix_memalign \ |
michael@0 | 60 | -Wl,-U,_replace_aligned_alloc \ |
michael@0 | 61 | -Wl,-U,_replace_calloc \ |
michael@0 | 62 | -Wl,-U,_replace_realloc \ |
michael@0 | 63 | -Wl,-U,_replace_free \ |
michael@0 | 64 | -Wl,-U,_replace_memalign \ |
michael@0 | 65 | -Wl,-U,_replace_valloc \ |
michael@0 | 66 | -Wl,-U,_replace_malloc_usable_size \ |
michael@0 | 67 | -Wl,-U,_replace_malloc_good_size \ |
michael@0 | 68 | -Wl,-U,_replace_jemalloc_stats \ |
michael@0 | 69 | -Wl,-U,_replace_jemalloc_purge_freed_pages \ |
michael@0 | 70 | -Wl,-U,_replace_jemalloc_free_dirty_pages \ |
michael@0 | 71 | $(NULL) |
michael@0 | 72 | |
michael@0 | 73 | ifneq ($(MOZ_REPLACE_MALLOC_LINKAGE),compiler support) |
michael@0 | 74 | EXTRA_DSO_LDOPTS += -flat_namespace |
michael@0 | 75 | endif |
michael@0 | 76 | ifeq ($(MOZ_REPLACE_MALLOC_LINKAGE),dummy library) |
michael@0 | 77 | EXTRA_DSO_LDOPTS += -Wl,-weak_library,$(DEPTH)/memory/replace/dummy/$(DLL_PREFIX)replace_malloc$(DLL_SUFFIX) |
michael@0 | 78 | endif |
michael@0 | 79 | endif |
michael@0 | 80 | |
michael@0 | 81 | ifeq (android, $(MOZ_WIDGET_TOOLKIT)) |
michael@0 | 82 | # To properly wrap jemalloc's pthread_atfork call. |
michael@0 | 83 | EXTRA_DSO_LDOPTS += -Wl,--wrap=pthread_atfork |
michael@0 | 84 | endif |
michael@0 | 85 | |
michael@0 | 86 | ifdef MOZ_LINKER |
michael@0 | 87 | ifeq (arm, $(TARGET_CPU)) |
michael@0 | 88 | EXTRA_DSO_LDOPTS += -Wl,-version-script,$(srcdir)/arm-eabi-filter |
michael@0 | 89 | endif |
michael@0 | 90 | |
michael@0 | 91 | endif |
michael@0 | 92 | |
michael@0 | 93 | ifeq (Android, $(OS_TARGET)) |
michael@0 | 94 | WRAP_LDFLAGS := $(filter -Wl%,$(WRAP_LDFLAGS)) |
michael@0 | 95 | endif |
michael@0 | 96 | |
michael@0 | 97 | include $(topsrcdir)/config/rules.mk |
michael@0 | 98 | |
michael@0 | 99 | ifdef MOZ_MEMORY |
michael@0 | 100 | ifeq (WINNT,$(OS_TARGET)) |
michael@0 | 101 | # Roll our own custom logic here for the import library |
michael@0 | 102 | |
michael@0 | 103 | ############################################################################### |
michael@0 | 104 | # |
michael@0 | 105 | # Linking Mozilla itself to jemalloc is not particularly difficult. To do this |
michael@0 | 106 | # we avoid linking directly to the Microsoft-provided CRT import libraries. |
michael@0 | 107 | # Instead, we link to our own import library which we generate here. To |
michael@0 | 108 | # replace the CRT's malloc/free/other memory management symbols we export |
michael@0 | 109 | # our own versions out of jemalloc.dll. We then take the import library that |
michael@0 | 110 | # the compiler generates for jemalloc.dll and combine it with the MS CRT import |
michael@0 | 111 | # libraries. We put our library on the command line first, and the CRT symbols |
michael@0 | 112 | # are discarded in favor of our versions! |
michael@0 | 113 | # |
michael@0 | 114 | # Unfortunately that was too easy. The CRT import library is not a standard |
michael@0 | 115 | # import library that contains a list of symbols and whatnot. It also includes |
michael@0 | 116 | # object files that are linked into generated programs. One of these, |
michael@0 | 117 | # crtdll.obj is (as one might expect) linked into all DLLs that link against |
michael@0 | 118 | # the CRT. This file does things like run static C++ constructors when the |
michael@0 | 119 | # DLL is attached, call DllMain, etc. |
michael@0 | 120 | # |
michael@0 | 121 | # In the CRT source all malloc/free calls are made to malloc_crt and free_crt. |
michael@0 | 122 | # In debug builds these are both defined to malloc_dbg and free_dbg. In opt |
michael@0 | 123 | # builds malloc_crt is an actual function, implemented and exposed from the |
michael@0 | 124 | # CRT. free_crt is, however, defined to be just plain old free. This works |
michael@0 | 125 | # fine inside the CRT where malloc_crt and free operate on the same heap. |
michael@0 | 126 | # Outside the CRT malloc_crt is in the CRT's heap, but free is in jemalloc's |
michael@0 | 127 | # heap. This causes much pain at shutdown :-( |
michael@0 | 128 | # |
michael@0 | 129 | # The obvious solution here is to override malloc_crt too. Unfortunately, |
michael@0 | 130 | # that doesn't work because the CRT expects to be able to call msize on this |
michael@0 | 131 | # piece of memory deep inside the CRT, which will fail because it'll call the |
michael@0 | 132 | # CRT's msize on a pointer in jemalloc's heap. |
michael@0 | 133 | # |
michael@0 | 134 | # Our solution to this is quite devious. We take apart the CRT's import lib |
michael@0 | 135 | # and remove the problematic object file. We then poke at the object file's |
michael@0 | 136 | # symbol table and replace '__imp__free' (which means grab free from some |
michael@0 | 137 | # other DLL) with '__imp__frex'. Then we define our own dummy no-op function |
michael@0 | 138 | # in jemalloc.dll and export it as frex. Then we put the CRT import lib |
michael@0 | 139 | # back together with the patched crtdll.obj, glue it to the end of jemalloc's |
michael@0 | 140 | # import library and link the rest of Mozilla to that. |
michael@0 | 141 | # |
michael@0 | 142 | # The result? A binary that uses jemalloc, doesn't crash, and leaks a tiny |
michael@0 | 143 | # amount of memory (32 words per DLL in the 2010 CRT) at shutdown. |
michael@0 | 144 | # |
michael@0 | 145 | ############################################################################### |
michael@0 | 146 | |
michael@0 | 147 | libs:: mozcrt.lib |
michael@0 | 148 | $(INSTALL) $(IFLAGS2) mozcrt.lib $(DIST)/lib |
michael@0 | 149 | |
michael@0 | 150 | # And finally combine that with the jemalloc import library to get an import |
michael@0 | 151 | # library that has our malloc/free/etc and the CRT's everything else |
michael@0 | 152 | mozcrt.lib: $(IMPORT_LIBRARY) msvc_modified.lib |
michael@0 | 153 | lib -OUT:$@ $^ |
michael@0 | 154 | |
michael@0 | 155 | # Put the fixed object file back in |
michael@0 | 156 | msvc_modified.lib: msvc_removed.lib crtdll_fixed.obj |
michael@0 | 157 | lib -OUT:$@ $^ |
michael@0 | 158 | |
michael@0 | 159 | # Fix the object file |
michael@0 | 160 | crtdll_fixed.obj: crtdll.obj |
michael@0 | 161 | $(PYTHON) $(srcdir)/fixcrt.py |
michael@0 | 162 | |
michael@0 | 163 | # Find the path of crtdll.obj |
michael@0 | 164 | CRTDLL_FULLPATH=$(subst \,\\,$(shell lib -list msvc_combined.lib | grep crtdll\\.obj)) |
michael@0 | 165 | |
michael@0 | 166 | # Remove the broken object file, only after we have extracted it |
michael@0 | 167 | msvc_removed.lib: msvc_combined.lib crtdll.obj |
michael@0 | 168 | lib -OUT:$@ msvc_combined.lib -REMOVE:$(CRTDLL_FULLPATH) |
michael@0 | 169 | |
michael@0 | 170 | # Extract the broken object file out of the combined library |
michael@0 | 171 | crtdll.obj: msvc_combined.lib |
michael@0 | 172 | lib -OUT:$@ $^ -EXTRACT:$(CRTDLL_FULLPATH) |
michael@0 | 173 | |
michael@0 | 174 | # Grab both CRT libraries and combine them into one library to simplify things |
michael@0 | 175 | msvc_combined.lib: |
michael@0 | 176 | lib -OUT:$@ $(WIN32_CRT_LIBS) |
michael@0 | 177 | endif |
michael@0 | 178 | endif # MOZ_MEMORY |