Skip to content

Commit 78b8651

Browse files
authored
Remove -fno-builtin. (#104)
* Remove -fno-builtin. -fno-builtin suppresses optimizations such as turning calls to `sqrt` or `fabs` into `f64.sqrt` or `f64.abs` instructions inline. Libc code itself benefits from these optimizations. I originally added this flag because historically it was needed when building libc to avoid the compiler pattern-matching the body of memcpy into a memcpy call, however clang no longer requires this. * Expand the comment about why we use USE_DL_PREFIX in dlmalloc.
1 parent 472b213 commit 78b8651

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,6 @@ SYSROOT_SHARE = $(SYSROOT)/share/$(MULTIARCH_TRIPLE)
188188

189189
# Set the target.
190190
override WASM_CFLAGS += --target=$(TARGET_TRIPLE)
191-
# We're compiling libc.
192-
override WASM_CFLAGS += -fno-builtin
193191
# WebAssembly floating-point match doesn't trap.
194192
# TODO: Add -fno-signaling-nans when the compiler supports it.
195193
override WASM_CFLAGS += -fno-trapping-math

dlmalloc/src/dlmalloc.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,24 @@ extern const int __ENOMEM;
4141
extern const int __EINVAL;
4242
#define EINVAL __EINVAL
4343

44-
/* Prefix dlmalloc's names with 'dl'. We wrap them with public names below. */
44+
/*
45+
* Define USE_DL_PREFIX so that we leave dlmalloc's names prefixed with 'dl'.
46+
* We define them as "static", and we wrap them with public names below. This
47+
* serves two purposes:
48+
*
49+
* One is to make it easy to control which symbols are exported; dlmalloc
50+
* defines several non-standard functions and we wish to explicitly control
51+
* which functions are part of our public-facing interface.
52+
*
53+
* The other is to protect against compilers optimizing based on the assumption
54+
* that they know what functions with names like "malloc" do. Code in the
55+
* implementation will call functions like "dlmalloc" and assume it can use
56+
* the resulting pointers to access the metadata outside of the nominally
57+
* allocated objects. However, if the function were named "malloc", compilers
58+
* might see code like that and assume it has undefined behavior and can be
59+
* optimized away. By using "dlmalloc" in the implementation, we don't need
60+
* -fno-builtin to avoid this problem.
61+
*/
4562
#define USE_DL_PREFIX 1
4663
#define DLMALLOC_EXPORT static inline
4764

0 commit comments

Comments
 (0)