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.

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

mercurial