Bug 801571 - Override vasprintf and asprint for Android platform to avoid the mismatch of malloc()/free(). r=glandium
authorShelly Lin <slin@mozilla.com>
Fri, 17 Jan 2014 16:54:06 +0800
changeset 164093 63fd0e5db62207614927a1c4924c9745064c0a00
parent 164092 b068b01943489566413ed5e9a8823b504119e48f
child 164094 dbe8147a798142b5b467df1d9b631331c972634d
push id26026
push userphilringnalda@gmail.com
push dateSat, 18 Jan 2014 23:17:27 +0000
treeherdermozilla-central@61fd0f987cf2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersglandium
bugs801571
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 801571 - 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
@@ -7030,16 +7030,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,65 @@ 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) {
+    *str = NULL;
+    return -1;
+  }
+
+  int ret = vsnprintf(ptr, 128, fmt, ap);
+  if (ret < 0) {
+    free_impl(ptr);
+    *str = NULL;
+    return -1;
+  }
+
+  char* _ptr = realloc_impl(ptr, ret + 1);
+  if (_ptr == NULL) {
+    free_impl(ptr);
+    *str = 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 */