memory/mozalloc/tests/TestVolatileBuffer.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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 "TestHarness.h"
     6 #include "mozilla/VolatileBuffer.h"
     7 #include "mozilla/NullPtr.h"
     8 #include <string.h>
    10 #if defined(ANDROID)
    11 #include <fcntl.h>
    12 #include <linux/ashmem.h>
    13 #include <sys/ioctl.h>
    14 #include <sys/stat.h>
    15 #include <sys/types.h>
    16 #elif defined(XP_DARWIN)
    17 #include <mach/mach.h>
    18 #endif
    20 using namespace mozilla;
    22 int main(int argc, char **argv)
    23 {
    24   ScopedXPCOM xpcom("VolatileBufferTests");
    25   if (xpcom.failed()) {
    26     return 1;
    27   }
    29   RefPtr<VolatileBuffer> heapbuf = new VolatileBuffer();
    30   if (!heapbuf || !heapbuf->Init(512)) {
    31     fail("Failed to initialize VolatileBuffer");
    32     return 1;
    33   }
    35   {
    36     VolatileBufferPtr<char> ptr(heapbuf);
    37     if (ptr.WasBufferPurged()) {
    38       fail("Buffer was immediately purged after initialization");
    39       return 1;
    40     }
    42     if (!ptr) {
    43       fail("Didn't get a pointer");
    44       return 1;
    45     }
    46   }
    48   RefPtr<VolatileBuffer> buf = new VolatileBuffer();
    49   if (!buf || !buf->Init(16384)) {
    50     fail("Failed to initialize VolatileBuffer");
    51     return 1;
    52   }
    54   const char teststr[] = "foobar";
    56   {
    57     VolatileBufferPtr<char> ptr(buf);
    58     if (ptr.WasBufferPurged()) {
    59       fail("Buffer should not be purged immediately after initialization");
    60       return 1;
    61     }
    63     if (!ptr) {
    64       fail("Didn't get a pointer");
    65       return 1;
    66     }
    68     {
    69       VolatileBufferPtr<char> ptr2(buf);
    70       if (ptr2.WasBufferPurged()) {
    71         fail("Failed to Lock buffer again while currently locked");
    72         return 1;
    73       }
    75       if (!ptr2) {
    76         fail("Didn't get a pointer on the second lock");
    77         return 1;
    78       }
    80       strcpy(ptr2, teststr);
    81     }
    82   }
    84   {
    85     VolatileBufferPtr<char> ptr(buf);
    86     if (ptr.WasBufferPurged()) {
    87       fail("Buffer was immediately purged after unlock");
    88       return 1;
    89     }
    91     if (strcmp(ptr, teststr)) {
    92       fail("Buffer failed to retain data after unlock");
    93       return 1;
    94     }
    95   }
    97   // Test purging if we know how to
    98 #if defined(MOZ_WIDGET_GONK)
    99   // This also works on Android, but we need root.
   100   int fd = open("/" ASHMEM_NAME_DEF, O_RDWR);
   101   if (fd < 0) {
   102     fail("Failed to open ashmem device");
   103     return 1;
   104   }
   105   if (ioctl(fd, ASHMEM_PURGE_ALL_CACHES, NULL) < 0) {
   106     fail("Failed to purge ashmem caches");
   107     return 1;
   108   }
   109 #elif defined(XP_DARWIN)
   110   int state;
   111   vm_purgable_control(mach_task_self(), (vm_address_t)NULL,
   112                       VM_PURGABLE_PURGE_ALL, &state);
   113 #else
   114   return 0;
   115 #endif
   117   if (!buf->NonHeapSizeOfExcludingThis()) {
   118     fail("Buffer should not be allocated on heap");
   119     return 1;
   120   }
   122   {
   123     VolatileBufferPtr<char> ptr(buf);
   124     if (!ptr.WasBufferPurged()) {
   125       fail("Buffer should not be unpurged after forced purge");
   126       return 1;
   127     }
   129     if (!strcmp(ptr, teststr)) {
   130       fail("Purge did not actually purge data");
   131       return 1;
   132     }
   133   }
   135   {
   136     VolatileBufferPtr<char> ptr(buf);
   137     if (ptr.WasBufferPurged()) {
   138       fail("Buffer still purged after lock");
   139       return 1;
   140     }
   141   }
   143   return 0;
   144 }

mercurial