summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Harrison <rharrison@chromium.org>2018-05-16 19:19:22 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-05-16 19:19:22 +0000
commit98ec53359b8e61e717440f280d3fcc101fe140bb (patch)
tree013925477fa27d2e1077ceaebab5fdf1ee6432e3
parentcda8e00478e97f005fc1d22bc01af7818e6f5101 (diff)
downloadpdfium-98ec53359b8e61e717440f280d3fcc101fe140bb.tar.xz
Add support for PartionRealloc to return nullptr
Currently the PartitionRealloc code path will only exit, with no option to return nullptr on failure, unlike PartitionAlloc code path. This CL refactors the realloc code path to be similar to alloc code path, following the upstream patch: https://chromium-review.googlesource.com/c/chromium/src/+/1044971 This also changes the version of realloc exposed to third party C libs to have the nullptr behaviour, like the exposed version of alloc. This CL is a redo of https://pdfium-review.googlesource.com/c/pdfium/+/31990 BUG=chromium:783022 Change-Id: Ib1b659079585dfd0423d683b8a2c7b6758a22a01 Reviewed-on: https://pdfium-review.googlesource.com/32613 Commit-Queue: Ryan Harrison <rharrison@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org> Reviewed-by: Chris Palmer <palmer@chromium.org>
-rw-r--r--core/fxcrt/fx_memory.cpp5
-rw-r--r--core/fxcrt/fx_memory.h6
-rw-r--r--third_party/base/allocator/partition_allocator/partition_alloc.cc45
-rw-r--r--third_party/base/allocator/partition_allocator/partition_alloc.h12
4 files changed, 48 insertions, 20 deletions
diff --git a/core/fxcrt/fx_memory.cpp b/core/fxcrt/fx_memory.cpp
index 6a135ae0f8..006e03031e 100644
--- a/core/fxcrt/fx_memory.cpp
+++ b/core/fxcrt/fx_memory.cpp
@@ -35,8 +35,9 @@ void* FXMEM_DefaultCalloc(size_t num_elems, size_t byte_size) {
}
void* FXMEM_DefaultRealloc(void* pointer, size_t new_size) {
- return pdfium::base::PartitionReallocGeneric(
- gGeneralPartitionAllocator.root(), pointer, new_size, "GeneralPartition");
+ return pdfium::base::PartitionReallocGenericFlags(
+ gGeneralPartitionAllocator.root(), pdfium::base::PartitionAllocReturnNull,
+ pointer, new_size, "GeneralPartition");
}
void FXMEM_DefaultFree(void* pointer) {
diff --git a/core/fxcrt/fx_memory.h b/core/fxcrt/fx_memory.h
index f7e6d67520..707e084211 100644
--- a/core/fxcrt/fx_memory.h
+++ b/core/fxcrt/fx_memory.h
@@ -57,9 +57,9 @@ inline void* FX_SafeRealloc(void* ptr, size_t num_members, size_t member_size) {
if (!size.IsValid())
return nullptr;
- return pdfium::base::PartitionReallocGeneric(
- gGeneralPartitionAllocator.root(), ptr, size.ValueOrDie(),
- "GeneralPartition");
+ return pdfium::base::PartitionReallocGenericFlags(
+ gGeneralPartitionAllocator.root(), pdfium::base::PartitionAllocReturnNull,
+ ptr, size.ValueOrDie(), "GeneralPartition");
}
inline void* FX_AllocOrDie(size_t num_members, size_t member_size) {
diff --git a/third_party/base/allocator/partition_allocator/partition_alloc.cc b/third_party/base/allocator/partition_allocator/partition_alloc.cc
index ff366b861f..e8aad9420c 100644
--- a/third_party/base/allocator/partition_allocator/partition_alloc.cc
+++ b/third_party/base/allocator/partition_allocator/partition_alloc.cc
@@ -1021,22 +1021,29 @@ bool partitionReallocDirectMappedInPlace(PartitionRootGeneric* root,
return true;
}
-void* PartitionReallocGeneric(PartitionRootGeneric* root,
- void* ptr,
- size_t new_size,
- const char* type_name) {
+void* PartitionReallocGenericFlags(PartitionRootGeneric* root,
+ int flags,
+ void* ptr,
+ size_t new_size,
+ const char* type_name) {
#if defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
- return realloc(ptr, new_size);
+ void* result = realloc(ptr, new_size);
+ CHECK(result || flags & PartitionAllocReturnNull);
+ return result;
#else
if (UNLIKELY(!ptr))
- return PartitionAllocGeneric(root, new_size, type_name);
+ return PartitionAllocGenericFlags(root, flags, new_size, type_name);
if (UNLIKELY(!new_size)) {
PartitionFreeGeneric(root, ptr);
- return 0;
+ return nullptr;
}
- if (new_size > kGenericMaxDirectMapped)
- PartitionExcessiveAllocationSize();
+ if (new_size > kGenericMaxDirectMapped) {
+ if (flags & PartitionAllocReturnNull)
+ return nullptr;
+ else
+ PartitionExcessiveAllocationSize();
+ }
DCHECK(PartitionPointerIsValid(PartitionCookieFreePointerAdjust(ptr)));
@@ -1069,12 +1076,19 @@ void* PartitionReallocGeneric(PartitionRootGeneric* root,
// |new_size| via the raw size pointer.
if (PartitionPageGetRawSizePtr(page))
PartitionCookieWriteValue(static_cast<char*>(ptr) + new_size);
-#endif
+#endif // DCHECK_IS_ON()
return ptr;
}
// This realloc cannot be resized in-place. Sadness.
- void* ret = PartitionAllocGeneric(root, new_size, type_name);
+ void* ret = PartitionAllocGenericFlags(root, flags, new_size, type_name);
+ if (!ret) {
+ if (flags & PartitionAllocReturnNull)
+ return nullptr;
+ else
+ PartitionExcessiveAllocationSize();
+ }
+
size_t copy_size = actual_old_size;
if (new_size < copy_size)
copy_size = new_size;
@@ -1082,7 +1096,14 @@ void* PartitionReallocGeneric(PartitionRootGeneric* root,
memcpy(ret, ptr, copy_size);
PartitionFreeGeneric(root, ptr);
return ret;
-#endif
+#endif // defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
+}
+
+void* PartitionReallocGeneric(PartitionRootGeneric* root,
+ void* ptr,
+ size_t new_size,
+ const char* type_name) {
+ return PartitionReallocGenericFlags(root, 0, ptr, new_size, type_name);
}
static size_t PartitionPurgePage(PartitionPage* page, bool discard) {
diff --git a/third_party/base/allocator/partition_allocator/partition_alloc.h b/third_party/base/allocator/partition_allocator/partition_alloc.h
index 87db329af2..69fba97d62 100644
--- a/third_party/base/allocator/partition_allocator/partition_alloc.h
+++ b/third_party/base/allocator/partition_allocator/partition_alloc.h
@@ -432,9 +432,15 @@ BASE_EXPORT NOINLINE void* PartitionAllocSlowPath(PartitionRootBase*,
size_t,
PartitionBucket*);
BASE_EXPORT NOINLINE void PartitionFreeSlowPath(PartitionPage*);
-BASE_EXPORT NOINLINE void* PartitionReallocGeneric(PartitionRootGeneric*,
- void*,
- size_t,
+BASE_EXPORT NOINLINE void* PartitionReallocGenericFlags(
+ PartitionRootGeneric* root,
+ int flags,
+ void* ptr,
+ size_t new_size,
+ const char* type_name);
+BASE_EXPORT NOINLINE void* PartitionReallocGeneric(PartitionRootGeneric* root,
+ void* ptr,
+ size_t new_size,
const char* type_name);
BASE_EXPORT void PartitionDumpStats(PartitionRoot*,