Skip to content

Commit 89454b1

Browse files
authored
Add -fno-builtin to dlmalloc / math library build (emscripten-core#6289)
Without -fno-builtin, LLVM can optimize away or convert calls to library functions to something else based on assumptions that they behave exactly like the standard library. This can cause unexpected bugs when we use our custom standard library.
1 parent 4c1c351 commit 89454b1

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

tools/system_libs.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ def create_libc(libname):
132132
break
133133
if not cancel:
134134
libc_files.append(os.path.join(musl_srcdir, dirpath, f))
135-
args = ['-Os']
135+
# Without -fno-builtin, LLVM can optimize away or convert calls to library
136+
# functions to something else based on assumptions that they behave exactly
137+
# like the standard library. This can cause unexpected bugs when we use our
138+
# custom standard library. The same for other libc/libm builds.
139+
args = ['-Os', '-fno-builtin']
136140
if shared.Settings.USE_PTHREADS:
137141
args += ['-s', 'USE_PTHREADS=1']
138142
assert '-mt' in libname
@@ -196,7 +200,7 @@ def create_wasm_libc(libname):
196200
'atan2.c', 'atan2f.c', 'atan2l.c', 'exp.c', 'expf.c', 'expl.c',
197201
'log.c', 'logf.c', 'logl.c', 'pow.c', 'powf.c', 'powl.c'])
198202

199-
return build_libc(libname, files, ['-O2'])
203+
return build_libc(libname, files, ['-O2', '-fno-builtin'])
200204

201205
# libcxx
202206
def create_libcxx(libname):
@@ -307,7 +311,7 @@ def dlmalloc_name():
307311

308312
def create_dlmalloc(out_name):
309313
o = in_temp(out_name)
310-
cflags = ['-O2']
314+
cflags = ['-O2', '-fno-builtin']
311315
if shared.Settings.USE_PTHREADS:
312316
cflags += ['-s', 'USE_PTHREADS=1']
313317
if shared.Settings.EMSCRIPTEN_TRACING:
@@ -340,7 +344,9 @@ def create_wasm_rt_lib(libname, files):
340344
'-mthread-model', 'single',
341345
output_flag,
342346
shared.path_from_root('system', 'lib', src),
343-
'-O2', '-o', o] + musl_internal_includes() + shared.EMSDK_OPTS)
347+
'-O2', '-fno-builtin', '-o', o] +
348+
musl_internal_includes() +
349+
shared.EMSDK_OPTS)
344350
o_s.append(o)
345351
run_commands(commands)
346352
lib = in_temp(libname)

0 commit comments

Comments
 (0)