Skip to content

Commit a579464

Browse files
committed
Bug 1829049 - Adjust check_vanilla_allocations to handle the case where operator new is inlined. r=sfink
Differential Revision: https://phabricator.services.mozilla.com/D175995
1 parent a51db66 commit a579464

File tree

1 file changed

+43
-14
lines changed

1 file changed

+43
-14
lines changed

config/check_vanilla_allocations.py

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,44 @@ def main():
8989
# alloc_fns contains all the vanilla allocation/free functions that we look
9090
# for. Regexp chars are escaped appropriately.
9191

92-
alloc_fns = [
92+
operator_news = [
9393
# Matches |operator new(unsigned T)|, where |T| is |int| or |long|.
94-
r"operator new\(unsigned",
94+
r"operator new(unsigned",
9595
# Matches |operator new[](unsigned T)|, where |T| is |int| or |long|.
96-
r"operator new\[\]\(unsigned",
97-
r"memalign",
98-
# These three aren't available on all Linux configurations.
99-
# r'posix_memalign',
100-
# r'aligned_alloc',
101-
# r'valloc',
96+
r"operator new[](unsigned",
10297
]
10398

99+
# operator new may end up inlined and replaced with moz_xmalloc.
100+
inlined_operator_news = [
101+
r"moz_xmalloc",
102+
]
103+
104+
alloc_fns = (
105+
operator_news
106+
+ inlined_operator_news
107+
+ [
108+
r"memalign",
109+
# These three aren't available on all Linux configurations.
110+
# r'posix_memalign',
111+
# r'aligned_alloc',
112+
# r'valloc',
113+
]
114+
)
115+
104116
if args.aggressive:
105117
alloc_fns += [r"malloc", r"calloc", r"realloc", r"free", r"strdup"]
106118

107119
# This is like alloc_fns, but regexp chars are not escaped.
108-
alloc_fns_unescaped = [fn.replace("\\", "") for fn in alloc_fns]
120+
alloc_fns_escaped = [re.escape(fn) for fn in alloc_fns]
109121

110122
# This regexp matches the relevant lines in the output of |nm|, which look
111123
# like the following.
112124
#
113125
# js/src/libjs_static.a:Utility.o: U malloc
114126
# js/src/libjs_static.a:Utility.o: 00000000000007e0 T js::SetSourceOptions(...)
115127
#
116-
nm_line_re = re.compile(r"([^:/ ]+):\s*[0-9a-fA-F]*\s+([TU]) (.*)")
117-
alloc_fns_re = re.compile(r"|".join(alloc_fns))
128+
nm_line_re = re.compile(r"([^:/ ]+):\s*[0-9a-fA-F]*\s+([TUw]) (.*)")
129+
alloc_fns_re = re.compile(r"|".join(alloc_fns_escaped))
118130

119131
# This tracks which allocation/free functions have been seen.
120132
functions = defaultdict(set)
@@ -202,9 +214,24 @@ def main():
202214

203215
# Check that all functions we expect are used in util/Utility.cpp. (This
204216
# will fail if the function-detection code breaks at any point.)
205-
for fn in alloc_fns_unescaped:
217+
# operator new and its inlined equivalent are mutually exclusive.
218+
has_operator_news = any(fn in operator_news for fn in util_Utility_cpp)
219+
has_inlined_operator_news = any(
220+
fn in inlined_operator_news for fn in util_Utility_cpp
221+
)
222+
if has_operator_news and has_inlined_operator_news:
223+
fail(
224+
"Both operator new and moz_xmalloc aren't expected in util/Utility.cpp at the same time"
225+
)
226+
227+
for fn in alloc_fns:
206228
if fn not in util_Utility_cpp:
207-
fail("'" + fn + "' isn't used as expected in util/Utility.cpp")
229+
if (
230+
(fn in operator_news and not has_inlined_operator_news)
231+
or (fn in inlined_operator_news and not has_operator_news)
232+
or (fn not in operator_news and fn not in inlined_operator_news)
233+
):
234+
fail("'" + fn + "' isn't used as expected in util/Utility.cpp")
208235
else:
209236
util_Utility_cpp.remove(fn)
210237

@@ -240,7 +267,9 @@ def main():
240267
#
241268
# U malloc util/Utility.cpp:117
242269
#
243-
alloc_lines_re = r"U ((" + r"|".join(alloc_fns) + r").*)\s+(\S+:\d+)$"
270+
alloc_lines_re = (
271+
r"[Uw] ((" + r"|".join(alloc_fns_escaped) + r").*)\s+(\S+:\d+)$"
272+
)
244273

245274
for line in lines:
246275
m = re.search(alloc_lines_re, line)

0 commit comments

Comments
 (0)