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