Bug 801517 - Override vasprintf and asprint for Android platform to avoid the mismatch of malloc()/free(). r=glandium
☠☠ backed out by 58c2a13fe0d5 ☠ ☠
authorShelly Lin <slin@mozilla.com>
Fri, 17 Jan 2014 16:54:06 +0800
changeset 163973 0496d73bf88ec60976a9688293289fc3d7dbcf01
parent 163972 1f71357d7de13f2805261d5faba73d5bcd87452b
child 163974 58c2a13fe0d5f91ddae61ae36c9cf0d9844ba60e
push id38601
push userryanvm@gmail.com
push dateFri, 17 Jan 2014 16:10:57 +0000
treeherdermozilla-inbound@0496d73bf88e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersglandium
bugs801517
milestone29.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 801517 - Override vasprintf and asprint for Android platform to avoid the mismatch of malloc()/free(). r=glandium
configure.in
memory/build/mozmemory_wrap.c
memory/build/mozmemory_wrap.h
--- a/configure.in
+++ b/configure.in
@@ -7069,16 +7069,17 @@ MOZ_ARG_ENABLE_BOOL(wrap-malloc,
     _WRAP_MALLOC=1,
     _WRAP_MALLOC= )
 
 if test -n "$_WRAP_MALLOC"; then
     if test -n "$GNU_CC"; then
         WRAP_LDFLAGS="${WRAP_LDFLAGS} -Wl,--wrap=malloc,--wrap=calloc,--wrap=valloc,--wrap=free,--wrap=realloc,--wrap=memalign"
         WRAP_LDFLAGS="${WRAP_LDFLAGS} -Wl,--wrap=__builtin_new,--wrap=__builtin_vec_new,--wrap=__builtin_delete,--wrap=__builtin_vec_delete"
         WRAP_LDFLAGS="${WRAP_LDFLAGS} -Wl,--wrap=strdup,--wrap=strndup"
+        WRAP_LDFLAGS="${WRAP_LDFLAGS} -Wl,--wrap=vasprintf,--wrap=asprintf"
         WRAP_LDFLAGS="${WRAP_LDFLAGS} -Wl,--wrap=posix_memalign,--wrap=malloc_usable_size"
         dnl Wrap operator new and operator delete on Android.
         if test "$OS_TARGET" = "Android"; then
             WRAP_LDFLAGS="${WRAP_LDFLAGS} -Wl,--wrap=_Znwj,--wrap=_Znaj,--wrap=_ZdlPv,--wrap=_ZdaPv"
             dnl Wrap the nothrow variants too.
             WRAP_LDFLAGS="${WRAP_LDFLAGS} -Wl,--wrap=_ZnwjRKSt9nothrow_t,--wrap=_ZnajRKSt9nothrow_t,--wrap=_ZdlPvRKSt9nothrow_t,--wrap=_ZdaPvRKSt9nothrow_t"
         fi
     else
--- a/memory/build/mozmemory_wrap.c
+++ b/memory/build/mozmemory_wrap.c
@@ -83,16 +83,61 @@ strndup_impl(const char *src, size_t len
 MOZ_MEMORY_API char *
 strdup_impl(const char *src)
 {
   size_t len = strlen(src);
   return strndup_impl(src, len);
 }
 #endif /* XP_DARWIN */
 
+#ifdef ANDROID
+#include <stdarg.h>
+
+MOZ_MEMORY_API int
+vasprintf_impl(char **str, const char *fmt, va_list ap)
+{
+  if (str == NULL || fmt == NULL) {
+    return -1;
+  }
+
+  char* ptr = (char*)malloc_impl(128);
+  if (ptr == NULL) {
+    return -1;
+  }
+
+  int ret = vsnprintf(ptr, 128, fmt, ap);
+  if (ret < 0) {
+    free_impl(ptr);
+    return ret;
+  }
+
+  char* _ptr = realloc_impl(ptr, ret + 1);
+  if (_ptr == NULL) {
+    return -1;
+  }
+
+  *str = _ptr;
+
+  return ret;
+}
+
+MOZ_MEMORY_API int
+asprintf_impl(char **str, const char *fmt, ...)
+ {
+   va_list ap;
+   va_start(ap, fmt);
+
+   int ret = vasprintf_impl(str, fmt, ap);
+
+   va_end(ap);
+
+   return ret;
+}
+#endif
+
 #ifdef XP_WIN
 /*
  *  There's a fun allocator mismatch in (at least) the VS 2010 CRT
  *  (see the giant comment in $(topsrcdir)/mozglue/build/Makefile.in)
  *  that gets redirected here to avoid a crash on shutdown.
  */
 void
 dumb_free_thunk(void *ptr)
--- a/memory/build/mozmemory_wrap.h
+++ b/memory/build/mozmemory_wrap.h
@@ -207,14 +207,23 @@
 
 /* Duplication functions */
 #define strndup_impl   mozmem_dup_impl(strndup)
 #define strdup_impl    mozmem_dup_impl(strdup)
 #ifdef XP_WIN
 #  define wcsdup_impl  mozmem_dup_impl(wcsdup)
 #endif
 
+/* String functions */
+#ifdef ANDROID
+/* Bug 801571 and Bug 879668, libstagefright uses vasprintf, causing malloc()/
+ * free() to be mismatched between bionic and mozglue implementation.
+ */
+#define vasprintf_impl  mozmem_dup_impl(vasprintf)
+#define asprintf_impl   mozmem_dup_impl(asprintf)
+#endif
+
 /* Jemalloc specific function */
 #define jemalloc_stats_impl              mozmem_jemalloc_impl(jemalloc_stats)
 #define jemalloc_purge_freed_pages_impl  mozmem_jemalloc_impl(jemalloc_purge_freed_pages)
 #define jemalloc_free_dirty_pages_impl   mozmem_jemalloc_impl(jemalloc_free_dirty_pages)
 
 #endif /* mozmemory_wrap_h */