Commit 230523ac authored by Yuki Shiino's avatar Yuki Shiino Committed by Commit Bot

base/allocator: Intercept strdup on Android when use_allocator_shim

On Android, malloc, free, etc. are intercepted by using the
linker option -Wl,-wrap,FUNC , which means we're intercepting
only the symbols undefined at link time.  Non-undefined symbols
like a call to malloc inside libc are not intercepted.

strdup and strndup are such functions that internally call to
malloc.  However, the return values of strdup/strndup are
deallocated with free (= our own free), so we should intercept
strdup/strndup to have them use our own malloc.

This patch implements wrapper functions for strdup/strndup just
like one for malloc.

Bug: 1111332
Change-Id: I5747a2c32f19bb63c0380f10f589b9a8f6dd3b19
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2418513Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarBenoit L <lizeb@chromium.org>
Reviewed-by: default avatarBartek Nowierski <bartekn@chromium.org>
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Cr-Commit-Position: refs/heads/master@{#808861}
parent bc4d68b8
...@@ -296,5 +296,9 @@ config("wrap_malloc_symbols") { ...@@ -296,5 +296,9 @@ config("wrap_malloc_symbols") {
"-Wl,-wrap,pvalloc", "-Wl,-wrap,pvalloc",
"-Wl,-wrap,realloc", "-Wl,-wrap,realloc",
"-Wl,-wrap,valloc", "-Wl,-wrap,valloc",
# <string.h> functions
"-Wl,-wrap,strdup",
"-Wl,-wrap,strndup",
] ]
} }
...@@ -13,6 +13,9 @@ ...@@ -13,6 +13,9 @@
// -wrap linker flags (e.g., libchrome.so) will be rewritten to the // -wrap linker flags (e.g., libchrome.so) will be rewritten to the
// linker as references to __wrap_malloc, __wrap_free, which are defined here. // linker as references to __wrap_malloc, __wrap_free, which are defined here.
#include <algorithm>
#include <cstring>
#include "base/allocator/allocator_shim_internals.h" #include "base/allocator/allocator_shim_internals.h"
extern "C" { extern "C" {
...@@ -51,4 +54,24 @@ SHIM_ALWAYS_EXPORT void* __wrap_valloc(size_t size) { ...@@ -51,4 +54,24 @@ SHIM_ALWAYS_EXPORT void* __wrap_valloc(size_t size) {
return ShimValloc(size, nullptr); return ShimValloc(size, nullptr);
} }
// Override <string.h> functions
SHIM_ALWAYS_EXPORT char* __wrap_strdup(const char* str) {
std::size_t length = std::strlen(str) + 1;
void* buffer = ShimMalloc(length, nullptr);
if (!buffer)
return nullptr;
return reinterpret_cast<char*>(std::memcpy(buffer, str, length));
}
SHIM_ALWAYS_EXPORT char* __wrap_strndup(const char* str, size_t n) {
std::size_t length = std::min(std::strlen(str), n);
char* buffer = reinterpret_cast<char*>(ShimMalloc(length + 1, nullptr));
if (!buffer)
return nullptr;
std::memcpy(buffer, str, length);
buffer[length] = '\0';
return buffer;
}
} // extern "C" } // extern "C"
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment