smeenai created this revision.
smeenai added reviewers: compnerd, EricWF, howard.hinnant, ikudrin, mclow.lists.
smeenai added a subscriber: cfe-commits.

Mach-O defaults to two-level namespaces, so `calloc` cannot be
interpositoned. Override it via the default malloc zone instead.

Note: `DYLD_FORCE_FLAT_NAMESPACE` can be used to enable interpositioning
on Mach-O, but `calloc` is used during library initialization, so
replacing it with a version which always returns NULL causes segfaults.
This could be worked around, but malloc zones are a cleaner solution.


https://reviews.llvm.org/D26150

Files:
  test/test_exception_storage_nodynmem.pass.cpp


Index: test/test_exception_storage_nodynmem.pass.cpp
===================================================================
--- test/test_exception_storage_nodynmem.pass.cpp
+++ test/test_exception_storage_nodynmem.pass.cpp
@@ -17,16 +17,28 @@
 
 #include <assert.h>
 #include <cstdlib>
+#if defined(__APPLE__)
+#include <malloc/malloc.h>
+#endif
 
 static bool OverwrittenCallocCalled = false;
 
 // Override calloc to simulate exhaustion of dynamic memory
+#if !defined(__APPLE__)
 void *calloc(size_t, size_t) {
+#else
+void *calloc(malloc_zone_t *, size_t, size_t) {
+#endif
     OverwrittenCallocCalled = true;
     return 0;
 }
 
 int main(int argc, char *argv[]) {
+#if defined(__APPLE__)
+    malloc_zone_t *default_zone = malloc_default_zone();
+    default_zone->calloc = calloc;
+#endif
+
     // Run the test a couple of times
     // to ensure that fallback memory doesn't leak.
     for (int I = 0; I < 1000; ++I)


Index: test/test_exception_storage_nodynmem.pass.cpp
===================================================================
--- test/test_exception_storage_nodynmem.pass.cpp
+++ test/test_exception_storage_nodynmem.pass.cpp
@@ -17,16 +17,28 @@
 
 #include <assert.h>
 #include <cstdlib>
+#if defined(__APPLE__)
+#include <malloc/malloc.h>
+#endif
 
 static bool OverwrittenCallocCalled = false;
 
 // Override calloc to simulate exhaustion of dynamic memory
+#if !defined(__APPLE__)
 void *calloc(size_t, size_t) {
+#else
+void *calloc(malloc_zone_t *, size_t, size_t) {
+#endif
     OverwrittenCallocCalled = true;
     return 0;
 }
 
 int main(int argc, char *argv[]) {
+#if defined(__APPLE__)
+    malloc_zone_t *default_zone = malloc_default_zone();
+    default_zone->calloc = calloc;
+#endif
+
     // Run the test a couple of times
     // to ensure that fallback memory doesn't leak.
     for (int I = 0; I < 1000; ++I)
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to