Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: third_party/tcmalloc/chromium/src/tcmalloc.cc

Issue 10391178: 1. Enable large object pointer offset check in release build. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2005, Google Inc. 1 // Copyright (c) 2005, Google Inc.
2 // All rights reserved. 2 // All rights reserved.
3 // 3 //
4 // Redistribution and use in source and binary forms, with or without 4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are 5 // modification, are permitted provided that the following conditions are
6 // met: 6 // met:
7 // 7 //
8 // * Redistributions of source code must retain the above copyright 8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer. 9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above 10 // * Redistributions in binary form must reproduce the above
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 using tcmalloc::kCrashWithStats; 171 using tcmalloc::kCrashWithStats;
172 using tcmalloc::Log; 172 using tcmalloc::Log;
173 using tcmalloc::PageHeap; 173 using tcmalloc::PageHeap;
174 using tcmalloc::PageHeapAllocator; 174 using tcmalloc::PageHeapAllocator;
175 using tcmalloc::SizeMap; 175 using tcmalloc::SizeMap;
176 using tcmalloc::Span; 176 using tcmalloc::Span;
177 using tcmalloc::StackTrace; 177 using tcmalloc::StackTrace;
178 using tcmalloc::Static; 178 using tcmalloc::Static;
179 using tcmalloc::ThreadCache; 179 using tcmalloc::ThreadCache;
180 180
181 // ---- Double free debug declarations 181 // ---- Functions doing validation with an extra mark.
182 static size_t ExcludeSpaceForMark(size_t size); 182 static size_t ExcludeSpaceForMark(size_t size);
183 static void AddRoomForMark(size_t* size); 183 static void AddRoomForMark(size_t* size);
184 static void ExcludeMarkFromSize(size_t* new_size); 184 static void ExcludeMarkFromSize(size_t* new_size);
185 static void MarkAllocatedRegion(void* ptr); 185 static void MarkAllocatedRegion(void* ptr);
186 static void ValidateAllocatedRegion(void* ptr, size_t cl); 186 static void ValidateAllocatedRegion(void* ptr, size_t cl);
187 // ---- End Double free debug declarations 187 // ---- End validation functions.
188 188
189 DECLARE_int64(tcmalloc_sample_parameter); 189 DECLARE_int64(tcmalloc_sample_parameter);
190 DECLARE_double(tcmalloc_release_rate); 190 DECLARE_double(tcmalloc_release_rate);
191 191
192 // For windows, the printf we use to report large allocs is 192 // For windows, the printf we use to report large allocs is
193 // potentially dangerous: it could cause a malloc that would cause an 193 // potentially dangerous: it could cause a malloc that would cause an
194 // infinite loop. So by default we set the threshold to a huge number 194 // infinite loop. So by default we set the threshold to a huge number
195 // on windows, so this bad situation will never trigger. You can 195 // on windows, so this bad situation will never trigger. You can
196 // always set TCMALLOC_LARGE_ALLOC_REPORT_THRESHOLD manually if you 196 // always set TCMALLOC_LARGE_ALLOC_REPORT_THRESHOLD manually if you
197 // want this functionality. 197 // want this functionality.
(...skipping 950 matching lines...) Expand 10 before | Expand all | Expand 10 after
1148 // pointer was allocated with some other allocator besides 1148 // pointer was allocated with some other allocator besides
1149 // tcmalloc. The latter can happen if tcmalloc is linked in via 1149 // tcmalloc. The latter can happen if tcmalloc is linked in via
1150 // a dynamic library, but is not listed last on the link line. 1150 // a dynamic library, but is not listed last on the link line.
1151 // In that case, libraries after it on the link line will 1151 // In that case, libraries after it on the link line will
1152 // allocate with libc malloc, but free with tcmalloc's free. 1152 // allocate with libc malloc, but free with tcmalloc's free.
1153 (*invalid_free_fn)(ptr); // Decide how to handle the bad free request 1153 (*invalid_free_fn)(ptr); // Decide how to handle the bad free request
1154 return; 1154 return;
1155 } 1155 }
1156 cl = span->sizeclass; 1156 cl = span->sizeclass;
1157 Static::pageheap()->CacheSizeClass(p, cl); 1157 Static::pageheap()->CacheSizeClass(p, cl);
1158
1159 // The span is not in use! A double free?
gpike 2012/05/21 19:04:58 At this point cl could be anything. Don't these ch
kaiwang 2012/05/21 22:21:42 oops, sorry I accidentally removed a {} pair. The
1160 CHECK_CONDITION_PRINT(span->location == Span::IN_USE,
1161 "Freeing a span not in use");
1162
1163 // Mimic debug assertion below to validate pointer of large objects.
1164 CHECK_CONDITION_PRINT(span->start == p,
gpike 2012/05/21 19:04:58 It seems safe to replace the last two checks with
kaiwang 2012/05/21 22:21:42 Done. Good idea
1165 "Pointer is not inside the first page of a span");
1166 CHECK_CONDITION_PRINT(reinterpret_cast<uintptr_t>(ptr) % kPageSize == 0,
1167 "Pointer is not pointing to the start of a span");
jar (doing other things) 2012/05/21 18:44:10 I appreciate that the text of these messages is on
kaiwang 2012/05/21 22:21:42 Actually for this specific case, the size will not
1158 } 1168 }
1159
1160 ValidateAllocatedRegion(ptr, cl); 1169 ValidateAllocatedRegion(ptr, cl);
1161 1170
1162 if (cl != 0) { 1171 if (cl != 0) {
1163 ASSERT(!Static::pageheap()->GetDescriptor(p)->sample); 1172 ASSERT(!Static::pageheap()->GetDescriptor(p)->sample);
1164 ThreadCache* heap = GetCacheIfPresent(); 1173 ThreadCache* heap = GetCacheIfPresent();
1165 if (heap != NULL) { 1174 if (heap != NULL) {
1166 heap->Deallocate(ptr, cl); 1175 heap->Deallocate(ptr, cl);
1167 } else { 1176 } else {
1168 // Delete directly into central cache 1177 // Delete directly into central cache
1169 tcmalloc::FL_Init(ptr); 1178 tcmalloc::FL_Init(ptr);
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
1269 // For use by exported routines below that want specific alignments 1278 // For use by exported routines below that want specific alignments
1270 // 1279 //
1271 // Note: this code can be slow for alignments > 16, and can 1280 // Note: this code can be slow for alignments > 16, and can
1272 // significantly fragment memory. The expectation is that 1281 // significantly fragment memory. The expectation is that
1273 // memalign/posix_memalign/valloc/pvalloc will not be invoked very 1282 // memalign/posix_memalign/valloc/pvalloc will not be invoked very
1274 // often. This requirement simplifies our implementation and allows 1283 // often. This requirement simplifies our implementation and allows
1275 // us to tune for expected allocation patterns. 1284 // us to tune for expected allocation patterns.
1276 void* do_memalign(size_t align, size_t size) { 1285 void* do_memalign(size_t align, size_t size) {
1277 ASSERT((align & (align - 1)) == 0); 1286 ASSERT((align & (align - 1)) == 0);
1278 ASSERT(align > 0); 1287 ASSERT(align > 0);
1279 // Marked in CheckMallocResult(), which is also inside SpanToMallocResult(). 1288 // Marked in CheckedMallocResult(), which is also inside SpanToMallocResult().
1280 AddRoomForMark(&size); 1289 AddRoomForMark(&size);
1281 if (size + align < size) return NULL; // Overflow 1290 if (size + align < size) return NULL; // Overflow
1282 1291
1283 // Fall back to malloc if we would already align this memory access properly. 1292 // Fall back to malloc if we would already align this memory access properly.
1284 if (align <= AlignmentForSize(size)) { 1293 if (align <= AlignmentForSize(size)) {
1285 void* p = do_malloc(size); 1294 void* p = do_malloc(size);
1286 ASSERT((reinterpret_cast<uintptr_t>(p) % align) == 0); 1295 ASSERT((reinterpret_cast<uintptr_t>(p) % align) == 0);
1287 return p; 1296 return p;
1288 } 1297 }
1289 1298
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after
1691 return do_mallinfo(); 1700 return do_mallinfo();
1692 } 1701 }
1693 #endif 1702 #endif
1694 1703
1695 extern "C" PERFTOOLS_DLL_DECL size_t tc_malloc_size(void* ptr) __THROW { 1704 extern "C" PERFTOOLS_DLL_DECL size_t tc_malloc_size(void* ptr) __THROW {
1696 return MallocExtension::instance()->GetAllocatedSize(ptr); 1705 return MallocExtension::instance()->GetAllocatedSize(ptr);
1697 } 1706 }
1698 1707
1699 #endif // TCMALLOC_USING_DEBUGALLOCATION 1708 #endif // TCMALLOC_USING_DEBUGALLOCATION
1700 1709
1701 // ---Double free() debugging implementation ----------------------------------- 1710 // --- Validation implementation with an extra mark ----------------------------
1702 // We will put a mark at the extreme end of each allocation block. We make 1711 // We will put a mark at the extreme end of each allocation block. We make
1703 // sure that we always allocate enough "extra memory" that we can fit in the 1712 // sure that we always allocate enough "extra memory" that we can fit in the
1704 // mark, and still provide the requested usable region. If ever that mark is 1713 // mark, and still provide the requested usable region. If ever that mark is
1705 // not as expected, then we know that the user is corrupting memory beyond their 1714 // not as expected, then we know that the user is corrupting memory beyond their
1706 // request size, or that they have called free a second time without having 1715 // request size, or that they have called free a second time without having
1707 // the memory allocated (again). This allows us to spot most double free()s, 1716 // the memory allocated (again). This allows us to spot most double free()s,
1708 // but some can "slip by" or confuse our logic if the caller reallocates memory 1717 // but some can "slip by" or confuse our logic if the caller reallocates memory
1709 // (for a second use) before performing an evil double-free of a first 1718 // (for a second use) before performing an evil double-free of a first
1710 // allocation 1719 // allocation
1711 1720
(...skipping 22 matching lines...) Expand all
1734 1743
1735 static size_t ExcludeSpaceForMark(size_t size) { return size; } 1744 static size_t ExcludeSpaceForMark(size_t size) { return size; }
1736 static void AddRoomForMark(size_t* size) {} 1745 static void AddRoomForMark(size_t* size) {}
1737 static void ExcludeMarkFromSize(size_t* new_size) {} 1746 static void ExcludeMarkFromSize(size_t* new_size) {}
1738 static void MarkAllocatedRegion(void* ptr) {} 1747 static void MarkAllocatedRegion(void* ptr) {}
1739 static void ValidateAllocatedRegion(void* ptr, size_t cl) {} 1748 static void ValidateAllocatedRegion(void* ptr, size_t cl) {}
1740 1749
1741 #else // TCMALLOC_VALIDATION 1750 #else // TCMALLOC_VALIDATION
1742 1751
1743 static void DieFromDoubleFree() { 1752 static void DieFromDoubleFree() {
1744 char* p = NULL; 1753 Log(kCrash, __FILE__, __LINE__, "Attempt to double free");
1745 p++;
1746 *p += 1; // Segv.
1747 }
1748
1749 static size_t DieFromBadFreePointer(const void* unused) {
1750 char* p = NULL;
1751 p += 2;
1752 *p += 2; // Segv.
1753 return 0;
1754 } 1754 }
1755 1755
1756 static void DieFromMemoryCorruption() { 1756 static void DieFromMemoryCorruption() {
1757 char* p = NULL; 1757 Log(kCrash, __FILE__, __LINE__, "Memory corrupted");
1758 p += 3;
1759 *p += 3; // Segv.
1760 } 1758 }
1761 1759
1762 // We can either do byte marking, or whole word marking based on the following 1760 // We can either do byte marking, or whole word marking based on the following
1763 // define. char is as small as we can get, and word marking probably provides 1761 // define. char is as small as we can get, and word marking probably provides
1764 // more than enough bits that we won't miss a corruption. Any sized integral 1762 // more than enough bits that we won't miss a corruption. Any sized integral
1765 // type can be used, but we just define two examples. 1763 // type can be used, but we just define two examples.
1766 1764
1767 // #define TCMALLOC_SMALL_VALIDATION 1765 // #define TCMALLOC_SMALL_VALIDATION
1768 #if defined (TCMALLOC_SMALL_VALIDATION) 1766 #if defined (TCMALLOC_SMALL_VALIDATION)
1769 1767
1770 typedef char MarkType; // char saves memory... int is more complete. 1768 typedef char MarkType; // char saves memory... int is more complete.
1771 static const MarkType kAllocationMarkMask = static_cast<MarkType>(0x36); 1769 static const MarkType kAllocationMarkMask = static_cast<MarkType>(0x36);
1772 1770
1773 #else 1771 #else
1774 1772
1775 typedef int MarkType; // char saves memory... int is more complete. 1773 typedef int MarkType; // char saves memory... int is more complete.
1776 static const MarkType kAllocationMarkMask = static_cast<MarkType>(0xE1AB9536); 1774 static const MarkType kAllocationMarkMask = static_cast<MarkType>(0xE1AB9536);
1777 1775
1778 #endif 1776 #endif
1779 1777
1780 // TODO(jar): See if use of reference rather than pointer gets better inlining, 1778 // TODO(jar): See if use of reference rather than pointer gets better inlining,
1781 // or if macro is needed. My fear is that taking address map preclude register 1779 // or if macro is needed. My fear is that taking address map preclude register
1782 // allocation :-(. 1780 // allocation :-(.
1783 inline static void AddRoomForMark(size_t* size) { 1781 inline static void AddRoomForMark(size_t* size) {
1784 *size += sizeof(kAllocationMarkMask); 1782 *size += sizeof(kAllocationMarkMask);
1785 } 1783 }
1786 1784
1787 inline static void ExcludeMarkFromSize(size_t* new_size) { 1785 inline static void ExcludeMarkFromSize(size_t* new_size) {
1788 *new_size -= sizeof(kAllocationMarkMask); 1786 *new_size -= sizeof(kAllocationMarkMask);
1789 } 1787 }
1790 1788
1791 inline static size_t ExcludeSpaceForMark(size_t size) { 1789 inline static size_t ExcludeSpaceForMark(size_t size) {
1792 return size - sizeof(kAllocationMarkMask); // Lie about size when asked. 1790 return size - sizeof(kAllocationMarkMask); // Lie about size when asked.
1793 } 1791 }
1794 1792
1795 inline static MarkType* GetMarkLocation(void* ptr) { 1793 inline static MarkType* GetMarkLocation(void* ptr) {
1796 size_t class_size = GetSizeWithCallback(ptr, DieFromBadFreePointer); 1794 size_t size = GetSizeWithCallback(ptr, &InvalidGetAllocatedSize);
1797 ASSERT(class_size % sizeof(kAllocationMarkMask) == 0); 1795 ASSERT(size % sizeof(kAllocationMarkMask) == 0);
1798 size_t last_index = (class_size / sizeof(kAllocationMarkMask)) - 1; 1796 size_t last_index = (size / sizeof(kAllocationMarkMask)) - 1;
1799 return static_cast<MarkType*>(ptr) + last_index; 1797 return static_cast<MarkType*>(ptr) + last_index;
1800 } 1798 }
1801 1799
1802 // We hash in the mark location plus the pointer so that we effectively mix in 1800 // We hash in the mark location plus the pointer so that we effectively mix in
1803 // the size of the block. This means that if a span is used for different sizes 1801 // the size of the block. This means that if a span is used for different sizes
1804 // that the mark will be different. It would be good to hash in the size (which 1802 // that the mark will be different. It would be good to hash in the size (which
1805 // we effectively get by using both mark location and pointer), but even better 1803 // we effectively get by using both mark location and pointer), but even better
1806 // would be to also include the class, as it concisely contains the entropy 1804 // would be to also include the class, as it concisely contains the entropy
1807 // found in the size (when we don't have large allocation), and there is less 1805 // found in the size (when we don't have large allocation), and there is less
1808 // risk of losing those bits to truncation. It would probably be good to combine 1806 // risk of losing those bits to truncation. It would probably be good to combine
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
1859 *mark = ~allocated_mark; // Distinctively not allocated. 1857 *mark = ~allocated_mark; // Distinctively not allocated.
1860 } 1858 }
1861 1859
1862 static void MarkAllocatedRegion(void* ptr) { 1860 static void MarkAllocatedRegion(void* ptr) {
1863 if (ptr == NULL) return; 1861 if (ptr == NULL) return;
1864 MarkType* mark = GetMarkLocation(ptr); 1862 MarkType* mark = GetMarkLocation(ptr);
1865 *mark = GetMarkValue(ptr, mark); 1863 *mark = GetMarkValue(ptr, mark);
1866 } 1864 }
1867 1865
1868 #endif // TCMALLOC_VALIDATION 1866 #endif // TCMALLOC_VALIDATION
OLDNEW
« base/allocator/allocator.gyp ('K') | « third_party/tcmalloc/chromium/src/internal_logging.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698