tools/trace-malloc/adreader.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "adreader.h"
michael@0 6
michael@0 7 #include <stdio.h>
michael@0 8 #include <stdint.h>
michael@0 9 #include <string.h>
michael@0 10
michael@0 11 ADLog::ADLog()
michael@0 12 : mEntryCount(0)
michael@0 13 {
michael@0 14 mEntries.mNext = static_cast<EntryBlock*>(&mEntries);
michael@0 15 mEntries.mPrev = static_cast<EntryBlock*>(&mEntries);
michael@0 16 }
michael@0 17
michael@0 18 ADLog::~ADLog()
michael@0 19 {
michael@0 20 for (const_iterator entry = begin(), entry_end = end();
michael@0 21 entry != entry_end; ++entry) {
michael@0 22 free((void*) (*entry)->type);
michael@0 23 free((char*) (*entry)->data);
michael@0 24 free((char*) (*entry)->allocation_stack);
michael@0 25 }
michael@0 26
michael@0 27 for (EntryBlock *b = mEntries.mNext, *next; b != &mEntries; b = next) {
michael@0 28 next = b->mNext;
michael@0 29 delete b;
michael@0 30 }
michael@0 31 }
michael@0 32
michael@0 33 bool
michael@0 34 ADLog::Read(const char* aFileName)
michael@0 35 {
michael@0 36 FILE *in = fopen(aFileName, "r");
michael@0 37 if (!in) {
michael@0 38 return false;
michael@0 39 }
michael@0 40
michael@0 41 while (!feof(in)) {
michael@0 42 unsigned int ptr;
michael@0 43 char typebuf[256];
michael@0 44 int datasize;
michael@0 45 int res = fscanf(in, "%x %s (%d)\n", &ptr, typebuf, &datasize);
michael@0 46 if (res == EOF)
michael@0 47 break;
michael@0 48 if (res != 3) {
michael@0 49 return false;
michael@0 50 }
michael@0 51
michael@0 52 size_t data_mem_size = ((datasize + sizeof(unsigned long) - 1) /
michael@0 53 sizeof(unsigned long)) * sizeof(unsigned long);
michael@0 54 char *data = (char*)malloc(data_mem_size);
michael@0 55
michael@0 56 for (size_t *cur_data = (size_t*) data,
michael@0 57 *cur_data_end = (size_t*) ((char*)data + data_mem_size);
michael@0 58 cur_data != cur_data_end; ++cur_data) {
michael@0 59 res = fscanf(in, " %zX\n", cur_data);
michael@0 60 if (res != 1) {
michael@0 61 return false;
michael@0 62 }
michael@0 63 }
michael@0 64
michael@0 65 char stackbuf[100000];
michael@0 66 stackbuf[0] = '\0';
michael@0 67
michael@0 68 char *stack = stackbuf;
michael@0 69 int len;
michael@0 70 do {
michael@0 71 fgets(stack, sizeof(stackbuf) - (stack - stackbuf), in);
michael@0 72 len = strlen(stack);
michael@0 73 stack += len;
michael@0 74 } while (len > 1);
michael@0 75
michael@0 76 if (mEntryCount % ADLOG_ENTRY_BLOCK_SIZE == 0) {
michael@0 77 EntryBlock *new_block = new EntryBlock();
michael@0 78 new_block->mNext = static_cast<EntryBlock*>(&mEntries);
michael@0 79 new_block->mPrev = mEntries.mPrev;
michael@0 80 mEntries.mPrev->mNext = new_block;
michael@0 81 mEntries.mPrev = new_block;
michael@0 82 }
michael@0 83
michael@0 84 size_t typelen = strlen(typebuf);
michael@0 85 char *type = (char*)malloc(typelen-1);
michael@0 86 strncpy(type, typebuf+1, typelen-2);
michael@0 87 type[typelen-2] = '\0';
michael@0 88
michael@0 89 Entry *entry =
michael@0 90 &mEntries.mPrev->entries[mEntryCount % ADLOG_ENTRY_BLOCK_SIZE];
michael@0 91 entry->address = (Pointer) (uintptr_t) ptr;
michael@0 92 entry->type = type;
michael@0 93 entry->datasize = datasize;
michael@0 94 entry->data = data;
michael@0 95 entry->allocation_stack = strdup(stackbuf);
michael@0 96
michael@0 97 ++mEntryCount;
michael@0 98 }
michael@0 99
michael@0 100 return true;
michael@0 101 }
michael@0 102
michael@0 103 ADLog::const_iterator::const_iterator(ADLog::EntryBlock *aBlock,
michael@0 104 size_t aOffset)
michael@0 105 {
michael@0 106 SetBlock(aBlock);
michael@0 107 mCur = mBlockStart + aOffset;
michael@0 108 }
michael@0 109
michael@0 110 ADLog::const_iterator&
michael@0 111 ADLog::const_iterator::operator++()
michael@0 112 {
michael@0 113 ++mCur;
michael@0 114 if (mCur == mBlockEnd) {
michael@0 115 SetBlock(mBlock->mNext);
michael@0 116 mCur = mBlockStart;
michael@0 117 }
michael@0 118
michael@0 119 return *this;
michael@0 120 }
michael@0 121
michael@0 122 ADLog::const_iterator&
michael@0 123 ADLog::const_iterator::operator--()
michael@0 124 {
michael@0 125 if (mCur == mBlockStart) {
michael@0 126 SetBlock(mBlock->mPrev);
michael@0 127 mCur = mBlockEnd;
michael@0 128 }
michael@0 129 --mCur;
michael@0 130
michael@0 131 return *this;
michael@0 132 }

mercurial