debugallocation.cc 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2000, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // ---
  31. // Author: Urs Holzle <opensource@google.com>
  32. #include "config.h"
  33. #include <errno.h>
  34. #ifdef HAVE_FCNTL_H
  35. #include <fcntl.h>
  36. #endif
  37. #ifdef HAVE_INTTYPES_H
  38. #include <inttypes.h>
  39. #endif
  40. // We only need malloc.h for struct mallinfo.
  41. #ifdef HAVE_STRUCT_MALLINFO
  42. // Malloc can be in several places on older versions of OS X.
  43. # if defined(HAVE_MALLOC_H)
  44. # include <malloc.h>
  45. # elif defined(HAVE_MALLOC_MALLOC_H)
  46. # include <malloc/malloc.h>
  47. # elif defined(HAVE_SYS_MALLOC_H)
  48. # include <sys/malloc.h>
  49. # endif
  50. #endif
  51. #ifdef HAVE_PTHREAD
  52. #include <pthread.h>
  53. #endif
  54. #include <stdarg.h>
  55. #include <stdio.h>
  56. #include <string.h>
  57. #ifdef HAVE_MMAP
  58. #include <sys/mman.h>
  59. #endif
  60. #include <sys/stat.h>
  61. #include <sys/types.h>
  62. #ifdef HAVE_UNISTD_H
  63. #include <unistd.h>
  64. #endif
  65. #include <gperftools/malloc_extension.h>
  66. #include <gperftools/malloc_hook.h>
  67. #include <gperftools/stacktrace.h>
  68. #include "addressmap-inl.h"
  69. #include "base/commandlineflags.h"
  70. #include "base/googleinit.h"
  71. #include "base/logging.h"
  72. #include "base/spinlock.h"
  73. #include "malloc_hook-inl.h"
  74. #include "symbolize.h"
  75. // NOTE: due to #define below, tcmalloc.cc will omit tc_XXX
  76. // definitions. So that debug implementations can be defined
  77. // instead. We're going to use do_malloc, do_free and other do_XXX
  78. // functions that are defined in tcmalloc.cc for actual memory
  79. // management
  80. #define TCMALLOC_USING_DEBUGALLOCATION
  81. #include "tcmalloc.cc"
  82. // __THROW is defined in glibc systems. It means, counter-intuitively,
  83. // "This function will never throw an exception." It's an optional
  84. // optimization tool, but we may need to use it to match glibc prototypes.
  85. #ifndef __THROW // I guess we're not on a glibc system
  86. # define __THROW // __THROW is just an optimization, so ok to make it ""
  87. #endif
  88. // On systems (like freebsd) that don't define MAP_ANONYMOUS, use the old
  89. // form of the name instead.
  90. #ifndef MAP_ANONYMOUS
  91. # define MAP_ANONYMOUS MAP_ANON
  92. #endif
  93. // ========================================================================= //
  94. DEFINE_bool(malloctrace,
  95. EnvToBool("TCMALLOC_TRACE", false),
  96. "Enables memory (de)allocation tracing to /tmp/google.alloc.");
  97. #ifdef HAVE_MMAP
  98. DEFINE_bool(malloc_page_fence,
  99. EnvToBool("TCMALLOC_PAGE_FENCE", false),
  100. "Enables putting of memory allocations at page boundaries "
  101. "with a guard page following the allocation (to catch buffer "
  102. "overruns right when they happen).");
  103. DEFINE_bool(malloc_page_fence_never_reclaim,
  104. EnvToBool("TCMALLOC_PAGE_FRANCE_NEVER_RECLAIM", false),
  105. "Enables making the virtual address space inaccessible "
  106. "upon a deallocation instead of returning it and reusing later.");
  107. #else
  108. DEFINE_bool(malloc_page_fence, false, "Not usable (requires mmap)");
  109. DEFINE_bool(malloc_page_fence_never_reclaim, false, "Not usable (required mmap)");
  110. #endif
  111. DEFINE_bool(malloc_reclaim_memory,
  112. EnvToBool("TCMALLOC_RECLAIM_MEMORY", true),
  113. "If set to false, we never return memory to malloc "
  114. "when an object is deallocated. This ensures that all "
  115. "heap object addresses are unique.");
  116. DEFINE_int32(max_free_queue_size,
  117. EnvToInt("TCMALLOC_MAX_FREE_QUEUE_SIZE", 10*1024*1024),
  118. "If greater than 0, keep freed blocks in a queue instead of "
  119. "releasing them to the allocator immediately. Release them when "
  120. "the total size of all blocks in the queue would otherwise exceed "
  121. "this limit.");
  122. DEFINE_bool(symbolize_stacktrace,
  123. EnvToBool("TCMALLOC_SYMBOLIZE_STACKTRACE", true),
  124. "Symbolize the stack trace when provided (on some error exits)");
  125. // If we are LD_PRELOAD-ed against a non-pthreads app, then
  126. // pthread_once won't be defined. We declare it here, for that
  127. // case (with weak linkage) which will cause the non-definition to
  128. // resolve to NULL. We can then check for NULL or not in Instance.
  129. extern "C" int pthread_once(pthread_once_t *, void (*)(void))
  130. ATTRIBUTE_WEAK;
  131. // ========================================================================= //
  132. // A safe version of printf() that does not do any allocation and
  133. // uses very little stack space.
  134. static void TracePrintf(int fd, const char *fmt, ...)
  135. __attribute__ ((__format__ (__printf__, 2, 3)));
  136. // Round "value" up to next "alignment" boundary.
  137. // Requires that "alignment" be a power of two.
  138. static intptr_t RoundUp(intptr_t value, intptr_t alignment) {
  139. return (value + alignment - 1) & ~(alignment - 1);
  140. }
  141. // ========================================================================= //
  142. class MallocBlock;
  143. // A circular buffer to hold freed blocks of memory. MallocBlock::Deallocate
  144. // (below) pushes blocks into this queue instead of returning them to the
  145. // underlying allocator immediately. See MallocBlock::Deallocate for more
  146. // information.
  147. //
  148. // We can't use an STL class for this because we need to be careful not to
  149. // perform any heap de-allocations in any of the code in this class, since the
  150. // code in MallocBlock::Deallocate is not re-entrant.
  151. template <typename QueueEntry>
  152. class FreeQueue {
  153. public:
  154. FreeQueue() : q_front_(0), q_back_(0) {}
  155. bool Full() {
  156. return (q_front_ + 1) % kFreeQueueSize == q_back_;
  157. }
  158. void Push(const QueueEntry& block) {
  159. q_[q_front_] = block;
  160. q_front_ = (q_front_ + 1) % kFreeQueueSize;
  161. }
  162. QueueEntry Pop() {
  163. RAW_CHECK(q_back_ != q_front_, "Queue is empty");
  164. const QueueEntry& ret = q_[q_back_];
  165. q_back_ = (q_back_ + 1) % kFreeQueueSize;
  166. return ret;
  167. }
  168. size_t size() const {
  169. return (q_front_ - q_back_ + kFreeQueueSize) % kFreeQueueSize;
  170. }
  171. private:
  172. // Maximum number of blocks kept in the free queue before being freed.
  173. static const int kFreeQueueSize = 1024;
  174. QueueEntry q_[kFreeQueueSize];
  175. int q_front_;
  176. int q_back_;
  177. };
  178. struct MallocBlockQueueEntry {
  179. MallocBlockQueueEntry() : block(NULL), size(0),
  180. num_deleter_pcs(0), deleter_threadid(0) {}
  181. MallocBlockQueueEntry(MallocBlock* b, size_t s) : block(b), size(s) {
  182. if (FLAGS_max_free_queue_size != 0 && b != NULL) {
  183. // Adjust the number of frames to skip (4) if you change the
  184. // location of this call.
  185. num_deleter_pcs =
  186. GetStackTrace(deleter_pcs,
  187. sizeof(deleter_pcs) / sizeof(deleter_pcs[0]),
  188. 4);
  189. deleter_threadid = pthread_self();
  190. } else {
  191. num_deleter_pcs = 0;
  192. // Zero is an illegal pthread id by my reading of the pthread
  193. // implementation:
  194. deleter_threadid = 0;
  195. }
  196. }
  197. MallocBlock* block;
  198. size_t size;
  199. // When deleted and put in the free queue, we (flag-controlled)
  200. // record the stack so that if corruption is later found, we can
  201. // print the deleter's stack. (These three vars add 144 bytes of
  202. // overhead under the LP64 data model.)
  203. void* deleter_pcs[16];
  204. int num_deleter_pcs;
  205. pthread_t deleter_threadid;
  206. };
  207. class MallocBlock {
  208. public: // allocation type constants
  209. // Different allocation types we distinguish.
  210. // Note: The lower 4 bits are not random: we index kAllocName array
  211. // by these values masked with kAllocTypeMask;
  212. // the rest are "random" magic bits to help catch memory corruption.
  213. static const int kMallocType = 0xEFCDAB90;
  214. static const int kNewType = 0xFEBADC81;
  215. static const int kArrayNewType = 0xBCEADF72;
  216. private: // constants
  217. // A mask used on alloc types above to get to 0, 1, 2
  218. static const int kAllocTypeMask = 0x3;
  219. // An additional bit to set in AllocType constants
  220. // to mark now deallocated regions.
  221. static const int kDeallocatedTypeBit = 0x4;
  222. // For better memory debugging, we initialize all storage to known
  223. // values, and overwrite the storage when it's deallocated:
  224. // Byte that fills uninitialized storage.
  225. static const int kMagicUninitializedByte = 0xAB;
  226. // Byte that fills deallocated storage.
  227. // NOTE: tcmalloc.cc depends on the value of kMagicDeletedByte
  228. // to work around a bug in the pthread library.
  229. static const int kMagicDeletedByte = 0xCD;
  230. // A size_t (type of alloc_type_ below) in a deallocated storage
  231. // filled with kMagicDeletedByte.
  232. static const size_t kMagicDeletedSizeT =
  233. 0xCDCDCDCD | (((size_t)0xCDCDCDCD << 16) << 16);
  234. // Initializer works for 32 and 64 bit size_ts;
  235. // "<< 16 << 16" is to fool gcc from issuing a warning
  236. // when size_ts are 32 bits.
  237. // NOTE: on Linux, you can enable malloc debugging support in libc by
  238. // setting the environment variable MALLOC_CHECK_ to 1 before you
  239. // start the program (see man malloc).
  240. // We use either do_malloc or mmap to make the actual allocation. In
  241. // order to remember which one of the two was used for any block, we store an
  242. // appropriate magic word next to the block.
  243. static const int kMagicMalloc = 0xDEADBEEF;
  244. static const int kMagicMMap = 0xABCDEFAB;
  245. // This array will be filled with 0xCD, for use with memcmp.
  246. static unsigned char kMagicDeletedBuffer[1024];
  247. static pthread_once_t deleted_buffer_initialized_;
  248. static bool deleted_buffer_initialized_no_pthreads_;
  249. private: // data layout
  250. // The four fields size1_,offset_,magic1_,alloc_type_
  251. // should together occupy a multiple of 16 bytes. (At the
  252. // moment, sizeof(size_t) == 4 or 8 depending on piii vs
  253. // k8, and 4 of those sum to 16 or 32 bytes).
  254. // This, combined with do_malloc's alignment guarantees,
  255. // ensures that SSE types can be stored into the returned
  256. // block, at &size2_.
  257. size_t size1_;
  258. size_t offset_; // normally 0 unless memaligned memory
  259. // see comments in memalign() and FromRawPointer().
  260. size_t magic1_;
  261. size_t alloc_type_;
  262. // here comes the actual data (variable length)
  263. // ...
  264. // then come the size2_ and magic2_, or a full page of mprotect-ed memory
  265. // if the malloc_page_fence feature is enabled.
  266. size_t size2_;
  267. int magic2_;
  268. private: // static data and helpers
  269. // Allocation map: stores the allocation type for each allocated object,
  270. // or the type or'ed with kDeallocatedTypeBit
  271. // for each formerly allocated object.
  272. typedef AddressMap<int> AllocMap;
  273. static AllocMap* alloc_map_;
  274. // This protects alloc_map_ and consistent state of metadata
  275. // for each still-allocated object in it.
  276. // We use spin locks instead of pthread_mutex_t locks
  277. // to prevent crashes via calls to pthread_mutex_(un)lock
  278. // for the (de)allocations coming from pthreads initialization itself.
  279. static SpinLock alloc_map_lock_;
  280. // A queue of freed blocks. Instead of releasing blocks to the allocator
  281. // immediately, we put them in a queue, freeing them only when necessary
  282. // to keep the total size of all the freed blocks below the limit set by
  283. // FLAGS_max_free_queue_size.
  284. static FreeQueue<MallocBlockQueueEntry>* free_queue_;
  285. static size_t free_queue_size_; // total size of blocks in free_queue_
  286. // protects free_queue_ and free_queue_size_
  287. static SpinLock free_queue_lock_;
  288. // Names of allocation types (kMallocType, kNewType, kArrayNewType)
  289. static const char* const kAllocName[];
  290. // Names of corresponding deallocation types
  291. static const char* const kDeallocName[];
  292. static const char* AllocName(int type) {
  293. return kAllocName[type & kAllocTypeMask];
  294. }
  295. static const char* DeallocName(int type) {
  296. return kDeallocName[type & kAllocTypeMask];
  297. }
  298. private: // helper accessors
  299. bool IsMMapped() const { return kMagicMMap == magic1_; }
  300. bool IsValidMagicValue(int value) const {
  301. return kMagicMMap == value || kMagicMalloc == value;
  302. }
  303. static size_t real_malloced_size(size_t size) {
  304. return size + sizeof(MallocBlock);
  305. }
  306. /*
  307. * Here we assume size of page is kMinAlign aligned,
  308. * so if size is MALLOC_ALIGNMENT aligned too, then we could
  309. * guarantee return address is also kMinAlign aligned, because
  310. * mmap return address at nearby page boundary on Linux.
  311. */
  312. static size_t real_mmapped_size(size_t size) {
  313. size_t tmp = size + MallocBlock::data_offset();
  314. tmp = RoundUp(tmp, kMinAlign);
  315. return tmp;
  316. }
  317. size_t real_size() {
  318. return IsMMapped() ? real_mmapped_size(size1_) : real_malloced_size(size1_);
  319. }
  320. // NOTE: if the block is mmapped (that is, we're using the
  321. // malloc_page_fence option) then there's no size2 or magic2
  322. // (instead, the guard page begins where size2 would be).
  323. size_t* size2_addr() { return (size_t*)((char*)&size2_ + size1_); }
  324. const size_t* size2_addr() const {
  325. return (const size_t*)((char*)&size2_ + size1_);
  326. }
  327. int* magic2_addr() { return (int*)(size2_addr() + 1); }
  328. const int* magic2_addr() const { return (const int*)(size2_addr() + 1); }
  329. private: // other helpers
  330. void Initialize(size_t size, int type) {
  331. RAW_CHECK(IsValidMagicValue(magic1_), "");
  332. // record us as allocated in the map
  333. alloc_map_lock_.Lock();
  334. if (!alloc_map_) {
  335. void* p = do_malloc(sizeof(AllocMap));
  336. alloc_map_ = new(p) AllocMap(do_malloc, do_free);
  337. }
  338. alloc_map_->Insert(data_addr(), type);
  339. // initialize us
  340. size1_ = size;
  341. offset_ = 0;
  342. alloc_type_ = type;
  343. if (!IsMMapped()) {
  344. *magic2_addr() = magic1_;
  345. *size2_addr() = size;
  346. }
  347. alloc_map_lock_.Unlock();
  348. memset(data_addr(), kMagicUninitializedByte, size);
  349. if (!IsMMapped()) {
  350. RAW_CHECK(size1_ == *size2_addr(), "should hold");
  351. RAW_CHECK(magic1_ == *magic2_addr(), "should hold");
  352. }
  353. }
  354. size_t CheckAndClear(int type, size_t given_size) {
  355. alloc_map_lock_.Lock();
  356. CheckLocked(type);
  357. if (!IsMMapped()) {
  358. RAW_CHECK(size1_ == *size2_addr(), "should hold");
  359. }
  360. // record us as deallocated in the map
  361. alloc_map_->Insert(data_addr(), type | kDeallocatedTypeBit);
  362. alloc_map_lock_.Unlock();
  363. // clear us
  364. const size_t size = real_size();
  365. RAW_CHECK(!given_size || given_size == size1_,
  366. "right size must be passed to sized delete");
  367. memset(this, kMagicDeletedByte, size);
  368. return size;
  369. }
  370. void CheckLocked(int type) const {
  371. int map_type = 0;
  372. const int* found_type =
  373. alloc_map_ != NULL ? alloc_map_->Find(data_addr()) : NULL;
  374. if (found_type == NULL) {
  375. RAW_LOG(FATAL, "memory allocation bug: object at %p "
  376. "has never been allocated", data_addr());
  377. } else {
  378. map_type = *found_type;
  379. }
  380. if ((map_type & kDeallocatedTypeBit) != 0) {
  381. RAW_LOG(FATAL, "memory allocation bug: object at %p "
  382. "has been already deallocated (it was allocated with %s)",
  383. data_addr(), AllocName(map_type & ~kDeallocatedTypeBit));
  384. }
  385. if (alloc_type_ == kMagicDeletedSizeT) {
  386. RAW_LOG(FATAL, "memory stomping bug: a word before object at %p "
  387. "has been corrupted; or else the object has been already "
  388. "deallocated and our memory map has been corrupted",
  389. data_addr());
  390. }
  391. if (!IsValidMagicValue(magic1_)) {
  392. RAW_LOG(FATAL, "memory stomping bug: a word before object at %p "
  393. "has been corrupted; "
  394. "or else our memory map has been corrupted and this is a "
  395. "deallocation for not (currently) heap-allocated object",
  396. data_addr());
  397. }
  398. if (!IsMMapped()) {
  399. if (size1_ != *size2_addr()) {
  400. RAW_LOG(FATAL, "memory stomping bug: a word after object at %p "
  401. "has been corrupted", data_addr());
  402. }
  403. if (!IsValidMagicValue(*magic2_addr())) {
  404. RAW_LOG(FATAL, "memory stomping bug: a word after object at %p "
  405. "has been corrupted", data_addr());
  406. }
  407. }
  408. if (alloc_type_ != type) {
  409. if ((alloc_type_ != MallocBlock::kMallocType) &&
  410. (alloc_type_ != MallocBlock::kNewType) &&
  411. (alloc_type_ != MallocBlock::kArrayNewType)) {
  412. RAW_LOG(FATAL, "memory stomping bug: a word before object at %p "
  413. "has been corrupted", data_addr());
  414. }
  415. RAW_LOG(FATAL, "memory allocation/deallocation mismatch at %p: "
  416. "allocated with %s being deallocated with %s",
  417. data_addr(), AllocName(alloc_type_), DeallocName(type));
  418. }
  419. if (alloc_type_ != map_type) {
  420. RAW_LOG(FATAL, "memory stomping bug: our memory map has been corrupted : "
  421. "allocation at %p made with %s "
  422. "is recorded in the map to be made with %s",
  423. data_addr(), AllocName(alloc_type_), AllocName(map_type));
  424. }
  425. }
  426. public: // public accessors
  427. void* data_addr() { return (void*)&size2_; }
  428. const void* data_addr() const { return (const void*)&size2_; }
  429. static size_t data_offset() { return OFFSETOF_MEMBER(MallocBlock, size2_); }
  430. size_t data_size() const { return size1_; }
  431. void set_offset(int offset) { this->offset_ = offset; }
  432. public: // our main interface
  433. static MallocBlock* Allocate(size_t size, int type) {
  434. // Prevent an integer overflow / crash with large allocation sizes.
  435. // TODO - Note that for a e.g. 64-bit size_t, max_size_t may not actually
  436. // be the maximum value, depending on how the compiler treats ~0. The worst
  437. // practical effect is that allocations are limited to 4Gb or so, even if
  438. // the address space could take more.
  439. static size_t max_size_t = ~0;
  440. if (size > max_size_t - sizeof(MallocBlock)) {
  441. RAW_LOG(ERROR, "Massive size passed to malloc: %" PRIuS "", size);
  442. return NULL;
  443. }
  444. MallocBlock* b = NULL;
  445. const bool use_malloc_page_fence = FLAGS_malloc_page_fence;
  446. #ifdef HAVE_MMAP
  447. if (use_malloc_page_fence) {
  448. // Put the block towards the end of the page and make the next page
  449. // inaccessible. This will catch buffer overrun right when it happens.
  450. size_t sz = real_mmapped_size(size);
  451. int pagesize = getpagesize();
  452. int num_pages = (sz + pagesize - 1) / pagesize + 1;
  453. char* p = (char*) mmap(NULL, num_pages * pagesize, PROT_READ|PROT_WRITE,
  454. MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  455. if (p == MAP_FAILED) {
  456. // If the allocation fails, abort rather than returning NULL to
  457. // malloc. This is because in most cases, the program will run out
  458. // of memory in this mode due to tremendous amount of wastage. There
  459. // is no point in propagating the error elsewhere.
  460. RAW_LOG(FATAL, "Out of memory: possibly due to page fence overhead: %s",
  461. strerror(errno));
  462. }
  463. // Mark the page after the block inaccessible
  464. if (mprotect(p + (num_pages - 1) * pagesize, pagesize, PROT_NONE)) {
  465. RAW_LOG(FATAL, "Guard page setup failed: %s", strerror(errno));
  466. }
  467. b = (MallocBlock*) (p + (num_pages - 1) * pagesize - sz);
  468. } else {
  469. b = (MallocBlock*) do_malloc(real_malloced_size(size));
  470. }
  471. #else
  472. b = (MallocBlock*) do_malloc(real_malloced_size(size));
  473. #endif
  474. // It would be nice to output a diagnostic on allocation failure
  475. // here, but logging (other than FATAL) requires allocating
  476. // memory, which could trigger a nasty recursion. Instead, preserve
  477. // malloc semantics and return NULL on failure.
  478. if (b != NULL) {
  479. b->magic1_ = use_malloc_page_fence ? kMagicMMap : kMagicMalloc;
  480. b->Initialize(size, type);
  481. }
  482. return b;
  483. }
  484. void Deallocate(int type, size_t given_size) {
  485. if (IsMMapped()) { // have to do this before CheckAndClear
  486. #ifdef HAVE_MMAP
  487. int size = CheckAndClear(type, given_size);
  488. int pagesize = getpagesize();
  489. int num_pages = (size + pagesize - 1) / pagesize + 1;
  490. char* p = (char*) this;
  491. if (FLAGS_malloc_page_fence_never_reclaim ||
  492. !FLAGS_malloc_reclaim_memory) {
  493. mprotect(p - (num_pages - 1) * pagesize + size,
  494. num_pages * pagesize, PROT_NONE);
  495. } else {
  496. munmap(p - (num_pages - 1) * pagesize + size, num_pages * pagesize);
  497. }
  498. #endif
  499. } else {
  500. const size_t size = CheckAndClear(type, given_size);
  501. if (FLAGS_malloc_reclaim_memory) {
  502. // Instead of freeing the block immediately, push it onto a queue of
  503. // recently freed blocks. Free only enough blocks to keep from
  504. // exceeding the capacity of the queue or causing the total amount of
  505. // un-released memory in the queue from exceeding
  506. // FLAGS_max_free_queue_size.
  507. ProcessFreeQueue(this, size, FLAGS_max_free_queue_size);
  508. }
  509. }
  510. }
  511. static size_t FreeQueueSize() {
  512. SpinLockHolder l(&free_queue_lock_);
  513. return free_queue_size_;
  514. }
  515. static void ProcessFreeQueue(MallocBlock* b, size_t size,
  516. int max_free_queue_size) {
  517. // MallocBlockQueueEntry are about 144 in size, so we can only
  518. // use a small array of them on the stack.
  519. MallocBlockQueueEntry entries[4];
  520. int num_entries = 0;
  521. MallocBlockQueueEntry new_entry(b, size);
  522. free_queue_lock_.Lock();
  523. if (free_queue_ == NULL)
  524. free_queue_ = new FreeQueue<MallocBlockQueueEntry>;
  525. RAW_CHECK(!free_queue_->Full(), "Free queue mustn't be full!");
  526. if (b != NULL) {
  527. free_queue_size_ += size + sizeof(MallocBlockQueueEntry);
  528. free_queue_->Push(new_entry);
  529. }
  530. // Free blocks until the total size of unfreed blocks no longer exceeds
  531. // max_free_queue_size, and the free queue has at least one free
  532. // space in it.
  533. while (free_queue_size_ > max_free_queue_size || free_queue_->Full()) {
  534. RAW_CHECK(num_entries < arraysize(entries), "entries array overflow");
  535. entries[num_entries] = free_queue_->Pop();
  536. free_queue_size_ -=
  537. entries[num_entries].size + sizeof(MallocBlockQueueEntry);
  538. num_entries++;
  539. if (num_entries == arraysize(entries)) {
  540. // The queue will not be full at this point, so it is ok to
  541. // release the lock. The queue may still contain more than
  542. // max_free_queue_size, but this is not a strict invariant.
  543. free_queue_lock_.Unlock();
  544. for (int i = 0; i < num_entries; i++) {
  545. CheckForDanglingWrites(entries[i]);
  546. do_free(entries[i].block);
  547. }
  548. num_entries = 0;
  549. free_queue_lock_.Lock();
  550. }
  551. }
  552. RAW_CHECK(free_queue_size_ >= 0, "Free queue size went negative!");
  553. free_queue_lock_.Unlock();
  554. for (int i = 0; i < num_entries; i++) {
  555. CheckForDanglingWrites(entries[i]);
  556. do_free(entries[i].block);
  557. }
  558. }
  559. static void InitDeletedBuffer() {
  560. memset(kMagicDeletedBuffer, kMagicDeletedByte, sizeof(kMagicDeletedBuffer));
  561. deleted_buffer_initialized_no_pthreads_ = true;
  562. }
  563. static void CheckForDanglingWrites(const MallocBlockQueueEntry& queue_entry) {
  564. // Initialize the buffer if necessary.
  565. if (pthread_once)
  566. pthread_once(&deleted_buffer_initialized_, &InitDeletedBuffer);
  567. if (!deleted_buffer_initialized_no_pthreads_) {
  568. // This will be the case on systems that don't link in pthreads,
  569. // including on FreeBSD where pthread_once has a non-zero address
  570. // (but doesn't do anything) even when pthreads isn't linked in.
  571. InitDeletedBuffer();
  572. }
  573. const unsigned char* p =
  574. reinterpret_cast<unsigned char*>(queue_entry.block);
  575. static const size_t size_of_buffer = sizeof(kMagicDeletedBuffer);
  576. const size_t size = queue_entry.size;
  577. const size_t buffers = size / size_of_buffer;
  578. const size_t remainder = size % size_of_buffer;
  579. size_t buffer_idx;
  580. for (buffer_idx = 0; buffer_idx < buffers; ++buffer_idx) {
  581. CheckForCorruptedBuffer(queue_entry, buffer_idx, p, size_of_buffer);
  582. p += size_of_buffer;
  583. }
  584. CheckForCorruptedBuffer(queue_entry, buffer_idx, p, remainder);
  585. }
  586. static void CheckForCorruptedBuffer(const MallocBlockQueueEntry& queue_entry,
  587. size_t buffer_idx,
  588. const unsigned char* buffer,
  589. size_t size_of_buffer) {
  590. if (memcmp(buffer, kMagicDeletedBuffer, size_of_buffer) == 0) {
  591. return;
  592. }
  593. RAW_LOG(ERROR,
  594. "Found a corrupted memory buffer in MallocBlock (may be offset "
  595. "from user ptr): buffer index: %zd, buffer ptr: %p, size of "
  596. "buffer: %zd", buffer_idx, buffer, size_of_buffer);
  597. // The magic deleted buffer should only be 1024 bytes, but in case
  598. // this changes, let's put an upper limit on the number of debug
  599. // lines we'll output:
  600. if (size_of_buffer <= 1024) {
  601. for (int i = 0; i < size_of_buffer; ++i) {
  602. if (buffer[i] != kMagicDeletedByte) {
  603. RAW_LOG(ERROR, "Buffer byte %d is 0x%02x (should be 0x%02x).",
  604. i, buffer[i], kMagicDeletedByte);
  605. }
  606. }
  607. } else {
  608. RAW_LOG(ERROR, "Buffer too large to print corruption.");
  609. }
  610. const MallocBlock* b = queue_entry.block;
  611. const size_t size = queue_entry.size;
  612. if (queue_entry.num_deleter_pcs > 0) {
  613. TracePrintf(STDERR_FILENO, "Deleted by thread %p\n",
  614. reinterpret_cast<void*>(
  615. PRINTABLE_PTHREAD(queue_entry.deleter_threadid)));
  616. // We don't want to allocate or deallocate memory here, so we use
  617. // placement-new. It's ok that we don't destroy this, since we're
  618. // just going to error-exit below anyway. Union is for alignment.
  619. union { void* alignment; char buf[sizeof(SymbolTable)]; } tablebuf;
  620. SymbolTable* symbolization_table = new (tablebuf.buf) SymbolTable;
  621. for (int i = 0; i < queue_entry.num_deleter_pcs; i++) {
  622. // Symbolizes the previous address of pc because pc may be in the
  623. // next function. This may happen when the function ends with
  624. // a call to a function annotated noreturn (e.g. CHECK).
  625. char *pc = reinterpret_cast<char*>(queue_entry.deleter_pcs[i]);
  626. symbolization_table->Add(pc - 1);
  627. }
  628. if (FLAGS_symbolize_stacktrace)
  629. symbolization_table->Symbolize();
  630. for (int i = 0; i < queue_entry.num_deleter_pcs; i++) {
  631. char *pc = reinterpret_cast<char*>(queue_entry.deleter_pcs[i]);
  632. TracePrintf(STDERR_FILENO, " @ %p %s\n",
  633. pc, symbolization_table->GetSymbol(pc - 1));
  634. }
  635. } else {
  636. RAW_LOG(ERROR,
  637. "Skipping the printing of the deleter's stack! Its stack was "
  638. "not found; either the corruption occurred too early in "
  639. "execution to obtain a stack trace or --max_free_queue_size was "
  640. "set to 0.");
  641. }
  642. RAW_LOG(FATAL,
  643. "Memory was written to after being freed. MallocBlock: %p, user "
  644. "ptr: %p, size: %zd. If you can't find the source of the error, "
  645. "try using ASan (http://code.google.com/p/address-sanitizer/), "
  646. "Valgrind, or Purify, or study the "
  647. "output of the deleter's stack printed above.",
  648. b, b->data_addr(), size);
  649. }
  650. static MallocBlock* FromRawPointer(void* p) {
  651. const size_t data_offset = MallocBlock::data_offset();
  652. // Find the header just before client's memory.
  653. MallocBlock *mb = reinterpret_cast<MallocBlock *>(
  654. reinterpret_cast<char *>(p) - data_offset);
  655. // If mb->alloc_type_ is kMagicDeletedSizeT, we're not an ok pointer.
  656. if (mb->alloc_type_ == kMagicDeletedSizeT) {
  657. RAW_LOG(FATAL, "memory allocation bug: object at %p has been already"
  658. " deallocated; or else a word before the object has been"
  659. " corrupted (memory stomping bug)", p);
  660. }
  661. // If mb->offset_ is zero (common case), mb is the real header.
  662. // If mb->offset_ is non-zero, this block was allocated by debug
  663. // memallign implementation, and mb->offset_ is the distance
  664. // backwards to the real header from mb, which is a fake header.
  665. if (mb->offset_ == 0) {
  666. return mb;
  667. }
  668. MallocBlock *main_block = reinterpret_cast<MallocBlock *>(
  669. reinterpret_cast<char *>(mb) - mb->offset_);
  670. if (main_block->offset_ != 0) {
  671. RAW_LOG(FATAL, "memory corruption bug: offset_ field is corrupted."
  672. " Need 0 but got %x",
  673. (unsigned)(main_block->offset_));
  674. }
  675. if (main_block >= p) {
  676. RAW_LOG(FATAL, "memory corruption bug: offset_ field is corrupted."
  677. " Detected main_block address overflow: %x",
  678. (unsigned)(mb->offset_));
  679. }
  680. if (main_block->size2_addr() < p) {
  681. RAW_LOG(FATAL, "memory corruption bug: offset_ field is corrupted."
  682. " It points below it's own main_block: %x",
  683. (unsigned)(mb->offset_));
  684. }
  685. return main_block;
  686. }
  687. static const MallocBlock* FromRawPointer(const void* p) {
  688. // const-safe version: we just cast about
  689. return FromRawPointer(const_cast<void*>(p));
  690. }
  691. void Check(int type) const {
  692. alloc_map_lock_.Lock();
  693. CheckLocked(type);
  694. alloc_map_lock_.Unlock();
  695. }
  696. static bool CheckEverything() {
  697. alloc_map_lock_.Lock();
  698. if (alloc_map_ != NULL) alloc_map_->Iterate(CheckCallback, 0);
  699. alloc_map_lock_.Unlock();
  700. return true; // if we get here, we're okay
  701. }
  702. static bool MemoryStats(int* blocks, size_t* total,
  703. int histogram[kMallocHistogramSize]) {
  704. memset(histogram, 0, kMallocHistogramSize * sizeof(int));
  705. alloc_map_lock_.Lock();
  706. stats_blocks_ = 0;
  707. stats_total_ = 0;
  708. stats_histogram_ = histogram;
  709. if (alloc_map_ != NULL) alloc_map_->Iterate(StatsCallback, 0);
  710. *blocks = stats_blocks_;
  711. *total = stats_total_;
  712. alloc_map_lock_.Unlock();
  713. return true;
  714. }
  715. private: // helpers for CheckEverything and MemoryStats
  716. static void CheckCallback(const void* ptr, int* type, int dummy) {
  717. if ((*type & kDeallocatedTypeBit) == 0) {
  718. FromRawPointer(ptr)->CheckLocked(*type);
  719. }
  720. }
  721. // Accumulation variables for StatsCallback protected by alloc_map_lock_
  722. static int stats_blocks_;
  723. static size_t stats_total_;
  724. static int* stats_histogram_;
  725. static void StatsCallback(const void* ptr, int* type, int dummy) {
  726. if ((*type & kDeallocatedTypeBit) == 0) {
  727. const MallocBlock* b = FromRawPointer(ptr);
  728. b->CheckLocked(*type);
  729. ++stats_blocks_;
  730. size_t mysize = b->size1_;
  731. int entry = 0;
  732. stats_total_ += mysize;
  733. while (mysize) {
  734. ++entry;
  735. mysize >>= 1;
  736. }
  737. RAW_CHECK(entry < kMallocHistogramSize,
  738. "kMallocHistogramSize should be at least as large as log2 "
  739. "of the maximum process memory size");
  740. stats_histogram_[entry] += 1;
  741. }
  742. }
  743. };
  744. void DanglingWriteChecker() {
  745. // Clear out the remaining free queue to check for dangling writes.
  746. MallocBlock::ProcessFreeQueue(NULL, 0, 0);
  747. }
  748. // ========================================================================= //
  749. const int MallocBlock::kMagicMalloc;
  750. const int MallocBlock::kMagicMMap;
  751. MallocBlock::AllocMap* MallocBlock::alloc_map_ = NULL;
  752. SpinLock MallocBlock::alloc_map_lock_(SpinLock::LINKER_INITIALIZED);
  753. FreeQueue<MallocBlockQueueEntry>* MallocBlock::free_queue_ = NULL;
  754. size_t MallocBlock::free_queue_size_ = 0;
  755. SpinLock MallocBlock::free_queue_lock_(SpinLock::LINKER_INITIALIZED);
  756. unsigned char MallocBlock::kMagicDeletedBuffer[1024];
  757. pthread_once_t MallocBlock::deleted_buffer_initialized_ = PTHREAD_ONCE_INIT;
  758. bool MallocBlock::deleted_buffer_initialized_no_pthreads_ = false;
  759. const char* const MallocBlock::kAllocName[] = {
  760. "malloc",
  761. "new",
  762. "new []",
  763. NULL,
  764. };
  765. const char* const MallocBlock::kDeallocName[] = {
  766. "free",
  767. "delete",
  768. "delete []",
  769. NULL,
  770. };
  771. int MallocBlock::stats_blocks_;
  772. size_t MallocBlock::stats_total_;
  773. int* MallocBlock::stats_histogram_;
  774. // ========================================================================= //
  775. // The following cut-down version of printf() avoids
  776. // using stdio or ostreams.
  777. // This is to guarantee no recursive calls into
  778. // the allocator and to bound the stack space consumed. (The pthread
  779. // manager thread in linuxthreads has a very small stack,
  780. // so fprintf can't be called.)
  781. static void TracePrintf(int fd, const char *fmt, ...) {
  782. char buf[64];
  783. int i = 0;
  784. va_list ap;
  785. va_start(ap, fmt);
  786. const char *p = fmt;
  787. char numbuf[25];
  788. if (fd < 0) {
  789. return;
  790. }
  791. numbuf[sizeof(numbuf)-1] = 0;
  792. while (*p != '\0') { // until end of format string
  793. char *s = &numbuf[sizeof(numbuf)-1];
  794. if (p[0] == '%' && p[1] != 0) { // handle % formats
  795. int64 l = 0;
  796. unsigned long base = 0;
  797. if (*++p == 's') { // %s
  798. s = va_arg(ap, char *);
  799. } else if (*p == 'l' && p[1] == 'd') { // %ld
  800. l = va_arg(ap, long);
  801. base = 10;
  802. p++;
  803. } else if (*p == 'l' && p[1] == 'u') { // %lu
  804. l = va_arg(ap, unsigned long);
  805. base = 10;
  806. p++;
  807. } else if (*p == 'z' && p[1] == 'u') { // %zu
  808. l = va_arg(ap, size_t);
  809. base = 10;
  810. p++;
  811. } else if (*p == 'u') { // %u
  812. l = va_arg(ap, unsigned int);
  813. base = 10;
  814. } else if (*p == 'd') { // %d
  815. l = va_arg(ap, int);
  816. base = 10;
  817. } else if (*p == 'p') { // %p
  818. l = va_arg(ap, intptr_t);
  819. base = 16;
  820. } else {
  821. write(STDERR_FILENO, "Unimplemented TracePrintf format\n", 33);
  822. write(STDERR_FILENO, p, 2);
  823. write(STDERR_FILENO, "\n", 1);
  824. abort();
  825. }
  826. p++;
  827. if (base != 0) {
  828. bool minus = (l < 0 && base == 10);
  829. uint64 ul = minus? -l : l;
  830. do {
  831. *--s = "0123456789abcdef"[ul % base];
  832. ul /= base;
  833. } while (ul != 0);
  834. if (base == 16) {
  835. *--s = 'x';
  836. *--s = '0';
  837. } else if (minus) {
  838. *--s = '-';
  839. }
  840. }
  841. } else { // handle normal characters
  842. *--s = *p++;
  843. }
  844. while (*s != 0) {
  845. if (i == sizeof(buf)) {
  846. write(fd, buf, i);
  847. i = 0;
  848. }
  849. buf[i++] = *s++;
  850. }
  851. }
  852. if (i != 0) {
  853. write(fd, buf, i);
  854. }
  855. va_end(ap);
  856. }
  857. // Return the file descriptor we're writing a log to
  858. static int TraceFd() {
  859. static int trace_fd = -1;
  860. if (trace_fd == -1) { // Open the trace file on the first call
  861. const char *val = getenv("TCMALLOC_TRACE_FILE");
  862. bool fallback_to_stderr = false;
  863. if (!val) {
  864. val = "/tmp/google.alloc";
  865. fallback_to_stderr = true;
  866. }
  867. trace_fd = open(val, O_CREAT|O_TRUNC|O_WRONLY, 0666);
  868. if (trace_fd == -1) {
  869. if (fallback_to_stderr) {
  870. trace_fd = 2;
  871. TracePrintf(trace_fd, "Can't open %s. Logging to stderr.\n", val);
  872. } else {
  873. TracePrintf(2, "Can't open %s. Logging disabled.\n", val);
  874. }
  875. }
  876. // Add a header to the log.
  877. TracePrintf(trace_fd, "Trace started: %lu\n",
  878. static_cast<unsigned long>(time(NULL)));
  879. TracePrintf(trace_fd,
  880. "func\tsize\tptr\tthread_id\tstack pcs for tools/symbolize\n");
  881. }
  882. return trace_fd;
  883. }
  884. // Print the hex stack dump on a single line. PCs are separated by tabs.
  885. static void TraceStack(void) {
  886. void *pcs[16];
  887. int n = GetStackTrace(pcs, sizeof(pcs)/sizeof(pcs[0]), 0);
  888. for (int i = 0; i != n; i++) {
  889. TracePrintf(TraceFd(), "\t%p", pcs[i]);
  890. }
  891. }
  892. // This protects MALLOC_TRACE, to make sure its info is atomically written.
  893. static SpinLock malloc_trace_lock(SpinLock::LINKER_INITIALIZED);
  894. #define MALLOC_TRACE(name, size, addr) \
  895. do { \
  896. if (FLAGS_malloctrace) { \
  897. SpinLockHolder l(&malloc_trace_lock); \
  898. TracePrintf(TraceFd(), "%s\t%" PRIuS "\t%p\t%" GPRIuPTHREAD, \
  899. name, size, addr, PRINTABLE_PTHREAD(pthread_self())); \
  900. TraceStack(); \
  901. TracePrintf(TraceFd(), "\n"); \
  902. } \
  903. } while (0)
  904. // ========================================================================= //
  905. // Write the characters buf[0, ..., size-1] to
  906. // the malloc trace buffer.
  907. // This function is intended for debugging,
  908. // and is not declared in any header file.
  909. // You must insert a declaration of it by hand when you need
  910. // to use it.
  911. void __malloctrace_write(const char *buf, size_t size) {
  912. if (FLAGS_malloctrace) {
  913. write(TraceFd(), buf, size);
  914. }
  915. }
  916. // ========================================================================= //
  917. // General debug allocation/deallocation
  918. static inline void* DebugAllocate(size_t size, int type) {
  919. MallocBlock* ptr = MallocBlock::Allocate(size, type);
  920. if (ptr == NULL) return NULL;
  921. MALLOC_TRACE("malloc", size, ptr->data_addr());
  922. return ptr->data_addr();
  923. }
  924. static inline void DebugDeallocate(void* ptr, int type, size_t given_size) {
  925. MALLOC_TRACE("free",
  926. (ptr != 0 ? MallocBlock::FromRawPointer(ptr)->data_size() : 0),
  927. ptr);
  928. if (ptr) MallocBlock::FromRawPointer(ptr)->Deallocate(type, given_size);
  929. }
  930. // ========================================================================= //
  931. // The following functions may be called via MallocExtension::instance()
  932. // for memory verification and statistics.
  933. class DebugMallocImplementation : public TCMallocImplementation {
  934. public:
  935. virtual bool GetNumericProperty(const char* name, size_t* value) {
  936. bool result = TCMallocImplementation::GetNumericProperty(name, value);
  937. if (result && (strcmp(name, "generic.current_allocated_bytes") == 0)) {
  938. // Subtract bytes kept in the free queue
  939. size_t qsize = MallocBlock::FreeQueueSize();
  940. if (*value >= qsize) {
  941. *value -= qsize;
  942. }
  943. }
  944. return result;
  945. }
  946. virtual bool VerifyNewMemory(const void* p) {
  947. if (p) MallocBlock::FromRawPointer(p)->Check(MallocBlock::kNewType);
  948. return true;
  949. }
  950. virtual bool VerifyArrayNewMemory(const void* p) {
  951. if (p) MallocBlock::FromRawPointer(p)->Check(MallocBlock::kArrayNewType);
  952. return true;
  953. }
  954. virtual bool VerifyMallocMemory(const void* p) {
  955. if (p) MallocBlock::FromRawPointer(p)->Check(MallocBlock::kMallocType);
  956. return true;
  957. }
  958. virtual bool VerifyAllMemory() {
  959. return MallocBlock::CheckEverything();
  960. }
  961. virtual bool MallocMemoryStats(int* blocks, size_t* total,
  962. int histogram[kMallocHistogramSize]) {
  963. return MallocBlock::MemoryStats(blocks, total, histogram);
  964. }
  965. virtual size_t GetEstimatedAllocatedSize(size_t size) {
  966. return size;
  967. }
  968. virtual size_t GetAllocatedSize(const void* p) {
  969. if (p) {
  970. RAW_CHECK(GetOwnership(p) != MallocExtension::kNotOwned,
  971. "ptr not allocated by tcmalloc");
  972. return MallocBlock::FromRawPointer(p)->data_size();
  973. }
  974. return 0;
  975. }
  976. virtual MallocExtension::Ownership GetOwnership(const void* p) {
  977. if (!p) {
  978. // nobody owns NULL
  979. return MallocExtension::kNotOwned;
  980. }
  981. // FIXME: note that correct GetOwnership should not touch memory
  982. // that is not owned by tcmalloc. Main implementation is using
  983. // pagemap to discover if page in question is owned by us or
  984. // not. But pagemap only has marks for first and last page of
  985. // spans. Note that if p was returned out of our memalign with
  986. // big alignment, then it will point outside of marked pages. Also
  987. // note that FromRawPointer call below requires touching memory
  988. // before pointer in order to handle memalign-ed chunks
  989. // (offset_). This leaves us with two options:
  990. //
  991. // * do FromRawPointer first and have possibility of crashing if
  992. // we're given not owned pointer
  993. //
  994. // * return incorrect ownership for those large memalign chunks
  995. //
  996. // I've decided to choose later, which appears to happen rarer and
  997. // therefore is arguably a lesser evil
  998. MallocExtension::Ownership rv = TCMallocImplementation::GetOwnership(p);
  999. if (rv != MallocExtension::kOwned) {
  1000. return rv;
  1001. }
  1002. const MallocBlock* mb = MallocBlock::FromRawPointer(p);
  1003. return TCMallocImplementation::GetOwnership(mb);
  1004. }
  1005. virtual void GetFreeListSizes(vector<MallocExtension::FreeListInfo>* v) {
  1006. static const char* kDebugFreeQueue = "debug.free_queue";
  1007. TCMallocImplementation::GetFreeListSizes(v);
  1008. MallocExtension::FreeListInfo i;
  1009. i.type = kDebugFreeQueue;
  1010. i.min_object_size = 0;
  1011. i.max_object_size = numeric_limits<size_t>::max();
  1012. i.total_bytes_free = MallocBlock::FreeQueueSize();
  1013. v->push_back(i);
  1014. }
  1015. };
  1016. static union {
  1017. char chars[sizeof(DebugMallocImplementation)];
  1018. void *ptr;
  1019. } debug_malloc_implementation_space;
  1020. REGISTER_MODULE_INITIALIZER(debugallocation, {
  1021. #if (__cplusplus >= 201103L)
  1022. COMPILE_ASSERT(alignof(debug_malloc_implementation_space) >= alignof(DebugMallocImplementation),
  1023. debug_malloc_implementation_space_is_not_properly_aligned);
  1024. #endif
  1025. // Either we or valgrind will control memory management. We
  1026. // register our extension if we're the winner. Otherwise let
  1027. // Valgrind use its own malloc (so don't register our extension).
  1028. if (!RunningOnValgrind()) {
  1029. DebugMallocImplementation *impl = new (debug_malloc_implementation_space.chars) DebugMallocImplementation();
  1030. MallocExtension::Register(impl);
  1031. }
  1032. });
  1033. REGISTER_MODULE_DESTRUCTOR(debugallocation, {
  1034. if (!RunningOnValgrind()) {
  1035. // When the program exits, check all blocks still in the free
  1036. // queue for corruption.
  1037. DanglingWriteChecker();
  1038. }
  1039. });
  1040. // ========================================================================= //
  1041. struct debug_alloc_retry_data {
  1042. size_t size;
  1043. int new_type;
  1044. };
  1045. static void *retry_debug_allocate(void *arg) {
  1046. debug_alloc_retry_data *data = static_cast<debug_alloc_retry_data *>(arg);
  1047. return DebugAllocate(data->size, data->new_type);
  1048. }
  1049. // This is mostly the same a cpp_alloc in tcmalloc.cc.
  1050. // TODO(csilvers): change Allocate() above to call cpp_alloc, so we
  1051. // don't have to reproduce the logic here. To make tc_new_mode work
  1052. // properly, I think we'll need to separate out the logic of throwing
  1053. // from the logic of calling the new-handler.
  1054. inline void* debug_cpp_alloc(size_t size, int new_type, bool nothrow) {
  1055. void* p = DebugAllocate(size, new_type);
  1056. if (p != NULL) {
  1057. return p;
  1058. }
  1059. struct debug_alloc_retry_data data;
  1060. data.size = size;
  1061. data.new_type = new_type;
  1062. return handle_oom(retry_debug_allocate, &data,
  1063. true, nothrow);
  1064. }
  1065. inline void* do_debug_malloc_or_debug_cpp_alloc(size_t size) {
  1066. void* p = DebugAllocate(size, MallocBlock::kMallocType);
  1067. if (p != NULL) {
  1068. return p;
  1069. }
  1070. struct debug_alloc_retry_data data;
  1071. data.size = size;
  1072. data.new_type = MallocBlock::kMallocType;
  1073. return handle_oom(retry_debug_allocate, &data,
  1074. false, true);
  1075. }
  1076. // Exported routines
  1077. extern "C" PERFTOOLS_DLL_DECL void* tc_malloc(size_t size) PERFTOOLS_THROW {
  1078. if (ThreadCache::IsUseEmergencyMalloc()) {
  1079. return tcmalloc::EmergencyMalloc(size);
  1080. }
  1081. void* ptr = do_debug_malloc_or_debug_cpp_alloc(size);
  1082. MallocHook::InvokeNewHook(ptr, size);
  1083. return ptr;
  1084. }
  1085. extern "C" PERFTOOLS_DLL_DECL void tc_free(void* ptr) PERFTOOLS_THROW {
  1086. if (tcmalloc::IsEmergencyPtr(ptr)) {
  1087. return tcmalloc::EmergencyFree(ptr);
  1088. }
  1089. MallocHook::InvokeDeleteHook(ptr);
  1090. DebugDeallocate(ptr, MallocBlock::kMallocType, 0);
  1091. }
  1092. extern "C" PERFTOOLS_DLL_DECL void tc_free_sized(void *ptr, size_t size) PERFTOOLS_THROW {
  1093. MallocHook::InvokeDeleteHook(ptr);
  1094. DebugDeallocate(ptr, MallocBlock::kMallocType, size);
  1095. }
  1096. extern "C" PERFTOOLS_DLL_DECL void* tc_calloc(size_t count, size_t size) PERFTOOLS_THROW {
  1097. if (ThreadCache::IsUseEmergencyMalloc()) {
  1098. return tcmalloc::EmergencyCalloc(count, size);
  1099. }
  1100. // Overflow check
  1101. const size_t total_size = count * size;
  1102. if (size != 0 && total_size / size != count) return NULL;
  1103. void* block = do_debug_malloc_or_debug_cpp_alloc(total_size);
  1104. MallocHook::InvokeNewHook(block, total_size);
  1105. if (block) memset(block, 0, total_size);
  1106. return block;
  1107. }
  1108. extern "C" PERFTOOLS_DLL_DECL void tc_cfree(void* ptr) PERFTOOLS_THROW {
  1109. if (tcmalloc::IsEmergencyPtr(ptr)) {
  1110. return tcmalloc::EmergencyFree(ptr);
  1111. }
  1112. MallocHook::InvokeDeleteHook(ptr);
  1113. DebugDeallocate(ptr, MallocBlock::kMallocType, 0);
  1114. }
  1115. extern "C" PERFTOOLS_DLL_DECL void* tc_realloc(void* ptr, size_t size) PERFTOOLS_THROW {
  1116. if (tcmalloc::IsEmergencyPtr(ptr)) {
  1117. return tcmalloc::EmergencyRealloc(ptr, size);
  1118. }
  1119. if (ptr == NULL) {
  1120. ptr = do_debug_malloc_or_debug_cpp_alloc(size);
  1121. MallocHook::InvokeNewHook(ptr, size);
  1122. return ptr;
  1123. }
  1124. if (size == 0) {
  1125. MallocHook::InvokeDeleteHook(ptr);
  1126. DebugDeallocate(ptr, MallocBlock::kMallocType, 0);
  1127. return NULL;
  1128. }
  1129. MallocBlock* old = MallocBlock::FromRawPointer(ptr);
  1130. old->Check(MallocBlock::kMallocType);
  1131. MallocBlock* p = MallocBlock::Allocate(size, MallocBlock::kMallocType);
  1132. // If realloc fails we are to leave the old block untouched and
  1133. // return null
  1134. if (p == NULL) return NULL;
  1135. // if ptr was allocated via memalign, then old->data_size() is not
  1136. // start of user data. So we must be careful to copy only user-data
  1137. char *old_begin = (char *)old->data_addr();
  1138. char *old_end = old_begin + old->data_size();
  1139. ssize_t old_ssize = old_end - (char *)ptr;
  1140. CHECK_CONDITION(old_ssize >= 0);
  1141. size_t old_size = (size_t)old_ssize;
  1142. CHECK_CONDITION(old_size <= old->data_size());
  1143. memcpy(p->data_addr(), ptr, (old_size < size) ? old_size : size);
  1144. MallocHook::InvokeDeleteHook(ptr);
  1145. MallocHook::InvokeNewHook(p->data_addr(), size);
  1146. DebugDeallocate(ptr, MallocBlock::kMallocType, 0);
  1147. MALLOC_TRACE("realloc", p->data_size(), p->data_addr());
  1148. return p->data_addr();
  1149. }
  1150. extern "C" PERFTOOLS_DLL_DECL void* tc_new(size_t size) {
  1151. void* ptr = debug_cpp_alloc(size, MallocBlock::kNewType, false);
  1152. MallocHook::InvokeNewHook(ptr, size);
  1153. if (ptr == NULL) {
  1154. RAW_LOG(FATAL, "Unable to allocate %" PRIuS " bytes: new failed.", size);
  1155. }
  1156. return ptr;
  1157. }
  1158. extern "C" PERFTOOLS_DLL_DECL void* tc_new_nothrow(size_t size, const std::nothrow_t&) PERFTOOLS_THROW {
  1159. void* ptr = debug_cpp_alloc(size, MallocBlock::kNewType, true);
  1160. MallocHook::InvokeNewHook(ptr, size);
  1161. return ptr;
  1162. }
  1163. extern "C" PERFTOOLS_DLL_DECL void tc_delete(void* p) PERFTOOLS_THROW {
  1164. MallocHook::InvokeDeleteHook(p);
  1165. DebugDeallocate(p, MallocBlock::kNewType, 0);
  1166. }
  1167. extern "C" PERFTOOLS_DLL_DECL void tc_delete_sized(void* p, size_t size) throw() {
  1168. MallocHook::InvokeDeleteHook(p);
  1169. DebugDeallocate(p, MallocBlock::kNewType, size);
  1170. }
  1171. // Some STL implementations explicitly invoke this.
  1172. // It is completely equivalent to a normal delete (delete never throws).
  1173. extern "C" PERFTOOLS_DLL_DECL void tc_delete_nothrow(void* p, const std::nothrow_t&) PERFTOOLS_THROW {
  1174. MallocHook::InvokeDeleteHook(p);
  1175. DebugDeallocate(p, MallocBlock::kNewType, 0);
  1176. }
  1177. extern "C" PERFTOOLS_DLL_DECL void* tc_newarray(size_t size) {
  1178. void* ptr = debug_cpp_alloc(size, MallocBlock::kArrayNewType, false);
  1179. MallocHook::InvokeNewHook(ptr, size);
  1180. if (ptr == NULL) {
  1181. RAW_LOG(FATAL, "Unable to allocate %" PRIuS " bytes: new[] failed.", size);
  1182. }
  1183. return ptr;
  1184. }
  1185. extern "C" PERFTOOLS_DLL_DECL void* tc_newarray_nothrow(size_t size, const std::nothrow_t&)
  1186. PERFTOOLS_THROW {
  1187. void* ptr = debug_cpp_alloc(size, MallocBlock::kArrayNewType, true);
  1188. MallocHook::InvokeNewHook(ptr, size);
  1189. return ptr;
  1190. }
  1191. extern "C" PERFTOOLS_DLL_DECL void tc_deletearray(void* p) PERFTOOLS_THROW {
  1192. MallocHook::InvokeDeleteHook(p);
  1193. DebugDeallocate(p, MallocBlock::kArrayNewType, 0);
  1194. }
  1195. extern "C" PERFTOOLS_DLL_DECL void tc_deletearray_sized(void* p, size_t size) throw() {
  1196. MallocHook::InvokeDeleteHook(p);
  1197. DebugDeallocate(p, MallocBlock::kArrayNewType, size);
  1198. }
  1199. // Some STL implementations explicitly invoke this.
  1200. // It is completely equivalent to a normal delete (delete never throws).
  1201. extern "C" PERFTOOLS_DLL_DECL void tc_deletearray_nothrow(void* p, const std::nothrow_t&) PERFTOOLS_THROW {
  1202. MallocHook::InvokeDeleteHook(p);
  1203. DebugDeallocate(p, MallocBlock::kArrayNewType, 0);
  1204. }
  1205. // This is mostly the same as do_memalign in tcmalloc.cc.
  1206. static void *do_debug_memalign(size_t alignment, size_t size) {
  1207. // Allocate >= size bytes aligned on "alignment" boundary
  1208. // "alignment" is a power of two.
  1209. void *p = 0;
  1210. RAW_CHECK((alignment & (alignment-1)) == 0, "must be power of two");
  1211. const size_t data_offset = MallocBlock::data_offset();
  1212. // Allocate "alignment-1" extra bytes to ensure alignment is possible, and
  1213. // a further data_offset bytes for an additional fake header.
  1214. size_t extra_bytes = data_offset + alignment - 1;
  1215. if (size + extra_bytes < size) return NULL; // Overflow
  1216. p = DebugAllocate(size + extra_bytes, MallocBlock::kMallocType);
  1217. if (p != 0) {
  1218. intptr_t orig_p = reinterpret_cast<intptr_t>(p);
  1219. // Leave data_offset bytes for fake header, and round up to meet
  1220. // alignment.
  1221. p = reinterpret_cast<void *>(RoundUp(orig_p + data_offset, alignment));
  1222. // Create a fake header block with an offset_ that points back to the
  1223. // real header. FromRawPointer uses this value.
  1224. MallocBlock *fake_hdr = reinterpret_cast<MallocBlock *>(
  1225. reinterpret_cast<char *>(p) - data_offset);
  1226. // offset_ is distance between real and fake headers.
  1227. // p is now end of fake header (beginning of client area),
  1228. // and orig_p is the end of the real header, so offset_
  1229. // is their difference.
  1230. //
  1231. // Note that other fields of fake_hdr are initialized with
  1232. // kMagicUninitializedByte
  1233. fake_hdr->set_offset(reinterpret_cast<intptr_t>(p) - orig_p);
  1234. }
  1235. return p;
  1236. }
  1237. struct memalign_retry_data {
  1238. size_t align;
  1239. size_t size;
  1240. };
  1241. static void *retry_debug_memalign(void *arg) {
  1242. memalign_retry_data *data = static_cast<memalign_retry_data *>(arg);
  1243. return do_debug_memalign(data->align, data->size);
  1244. }
  1245. inline void* do_debug_memalign_or_debug_cpp_memalign(size_t align,
  1246. size_t size) {
  1247. void* p = do_debug_memalign(align, size);
  1248. if (p != NULL) {
  1249. return p;
  1250. }
  1251. struct memalign_retry_data data;
  1252. data.align = align;
  1253. data.size = size;
  1254. return handle_oom(retry_debug_memalign, &data,
  1255. false, true);
  1256. }
  1257. extern "C" PERFTOOLS_DLL_DECL void* tc_memalign(size_t align, size_t size) PERFTOOLS_THROW {
  1258. void *p = do_debug_memalign_or_debug_cpp_memalign(align, size);
  1259. MallocHook::InvokeNewHook(p, size);
  1260. return p;
  1261. }
  1262. // Implementation taken from tcmalloc/tcmalloc.cc
  1263. extern "C" PERFTOOLS_DLL_DECL int tc_posix_memalign(void** result_ptr, size_t align, size_t size)
  1264. PERFTOOLS_THROW {
  1265. if (((align % sizeof(void*)) != 0) ||
  1266. ((align & (align - 1)) != 0) ||
  1267. (align == 0)) {
  1268. return EINVAL;
  1269. }
  1270. void* result = do_debug_memalign_or_debug_cpp_memalign(align, size);
  1271. MallocHook::InvokeNewHook(result, size);
  1272. if (result == NULL) {
  1273. return ENOMEM;
  1274. } else {
  1275. *result_ptr = result;
  1276. return 0;
  1277. }
  1278. }
  1279. extern "C" PERFTOOLS_DLL_DECL void* tc_valloc(size_t size) PERFTOOLS_THROW {
  1280. // Allocate >= size bytes starting on a page boundary
  1281. void *p = do_debug_memalign_or_debug_cpp_memalign(getpagesize(), size);
  1282. MallocHook::InvokeNewHook(p, size);
  1283. return p;
  1284. }
  1285. extern "C" PERFTOOLS_DLL_DECL void* tc_pvalloc(size_t size) PERFTOOLS_THROW {
  1286. // Round size up to a multiple of pages
  1287. // then allocate memory on a page boundary
  1288. int pagesize = getpagesize();
  1289. size = RoundUp(size, pagesize);
  1290. if (size == 0) { // pvalloc(0) should allocate one page, according to
  1291. size = pagesize; // http://man.free4web.biz/man3/libmpatrol.3.html
  1292. }
  1293. void *p = do_debug_memalign_or_debug_cpp_memalign(pagesize, size);
  1294. MallocHook::InvokeNewHook(p, size);
  1295. return p;
  1296. }
  1297. // malloc_stats just falls through to the base implementation.
  1298. extern "C" PERFTOOLS_DLL_DECL void tc_malloc_stats(void) PERFTOOLS_THROW {
  1299. do_malloc_stats();
  1300. }
  1301. extern "C" PERFTOOLS_DLL_DECL int tc_mallopt(int cmd, int value) PERFTOOLS_THROW {
  1302. return do_mallopt(cmd, value);
  1303. }
  1304. #ifdef HAVE_STRUCT_MALLINFO
  1305. extern "C" PERFTOOLS_DLL_DECL struct mallinfo tc_mallinfo(void) PERFTOOLS_THROW {
  1306. return do_mallinfo();
  1307. }
  1308. #endif
  1309. extern "C" PERFTOOLS_DLL_DECL size_t tc_malloc_size(void* ptr) PERFTOOLS_THROW {
  1310. return MallocExtension::instance()->GetAllocatedSize(ptr);
  1311. }
  1312. extern "C" PERFTOOLS_DLL_DECL void* tc_malloc_skip_new_handler(size_t size) PERFTOOLS_THROW {
  1313. void* result = DebugAllocate(size, MallocBlock::kMallocType);
  1314. MallocHook::InvokeNewHook(result, size);
  1315. return result;
  1316. }