Tue, 06 Jan 2015 21:39:09 +0100
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 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef AutoObjectMapper_h |
michael@0 | 8 | #define AutoObjectMapper_h |
michael@0 | 9 | |
michael@0 | 10 | #include <string> |
michael@0 | 11 | |
michael@0 | 12 | #include "mozilla/Attributes.h" |
michael@0 | 13 | #include "PlatformMacros.h" |
michael@0 | 14 | |
michael@0 | 15 | // A (nearly-) RAII class that maps an object in and then unmaps it on |
michael@0 | 16 | // destruction. This base class version uses the "normal" POSIX |
michael@0 | 17 | // functions: open, fstat, close, mmap, munmap. |
michael@0 | 18 | |
michael@0 | 19 | class MOZ_STACK_CLASS AutoObjectMapperPOSIX { |
michael@0 | 20 | public: |
michael@0 | 21 | // The constructor does not attempt to map the file, because that |
michael@0 | 22 | // might fail. Instead, once the object has been constructed, |
michael@0 | 23 | // call Map() to attempt the mapping. There is no corresponding |
michael@0 | 24 | // Unmap() since the unmapping is done in the destructor. Failure |
michael@0 | 25 | // messages are sent to |aLog|. |
michael@0 | 26 | AutoObjectMapperPOSIX(void(*aLog)(const char*)); |
michael@0 | 27 | |
michael@0 | 28 | // Unmap the file on destruction of this object. |
michael@0 | 29 | ~AutoObjectMapperPOSIX(); |
michael@0 | 30 | |
michael@0 | 31 | // Map |fileName| into the address space and return the mapping |
michael@0 | 32 | // extents. If the file is zero sized this will fail. The file is |
michael@0 | 33 | // mapped read-only and private. Returns true iff the mapping |
michael@0 | 34 | // succeeded, in which case *start and *length hold its extent. |
michael@0 | 35 | // Once a call to Map succeeds, all subsequent calls to it will |
michael@0 | 36 | // fail. |
michael@0 | 37 | bool Map(/*OUT*/void** start, /*OUT*/size_t* length, std::string fileName); |
michael@0 | 38 | |
michael@0 | 39 | protected: |
michael@0 | 40 | // If we are currently holding a mapped object, these record the |
michael@0 | 41 | // mapped address range. |
michael@0 | 42 | void* mImage; |
michael@0 | 43 | size_t mSize; |
michael@0 | 44 | |
michael@0 | 45 | // A logging sink, for complaining about mapping failures. |
michael@0 | 46 | void (*mLog)(const char*); |
michael@0 | 47 | |
michael@0 | 48 | private: |
michael@0 | 49 | // Are we currently holding a mapped object? This is private to |
michael@0 | 50 | // the base class. Derived classes need to have their own way to |
michael@0 | 51 | // track whether they are holding a mapped object. |
michael@0 | 52 | bool mIsMapped; |
michael@0 | 53 | |
michael@0 | 54 | // Disable copying and assignment. |
michael@0 | 55 | AutoObjectMapperPOSIX(const AutoObjectMapperPOSIX&); |
michael@0 | 56 | AutoObjectMapperPOSIX& operator=(const AutoObjectMapperPOSIX&); |
michael@0 | 57 | // Disable heap allocation of this class. |
michael@0 | 58 | void* operator new(size_t); |
michael@0 | 59 | void* operator new[](size_t); |
michael@0 | 60 | void operator delete(void*); |
michael@0 | 61 | void operator delete[](void*); |
michael@0 | 62 | }; |
michael@0 | 63 | |
michael@0 | 64 | |
michael@0 | 65 | #if defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK) |
michael@0 | 66 | // This is a variant of AutoObjectMapperPOSIX suitable for use in |
michael@0 | 67 | // conjunction with faulty.lib on Android. How it behaves depends on |
michael@0 | 68 | // the name of the file to be mapped. There are three possible cases: |
michael@0 | 69 | // |
michael@0 | 70 | // (1) /foo/bar/xyzzy/blah.apk!/libwurble.so |
michael@0 | 71 | // We hand it as-is to faulty.lib and let it fish the relevant |
michael@0 | 72 | // bits out of the APK. |
michael@0 | 73 | // |
michael@0 | 74 | // (2) libmozglue.so |
michael@0 | 75 | // This is part of the Fennec installation, but is not in the |
michael@0 | 76 | // APK. Instead we have to figure out the installation path |
michael@0 | 77 | // and look for it there. Because of faulty.lib limitations, |
michael@0 | 78 | // we have to use regular open/mmap instead of faulty.lib. |
michael@0 | 79 | // |
michael@0 | 80 | // (3) libanythingelse.so |
michael@0 | 81 | // faulty.lib assumes this is a system library, and prepends |
michael@0 | 82 | // "/system/lib/" to the path. So as in (1), we can give it |
michael@0 | 83 | // as-is to faulty.lib. |
michael@0 | 84 | // |
michael@0 | 85 | // Hence (1) and (3) require special-casing here. Case (2) simply |
michael@0 | 86 | // hands the problem to the parent class. |
michael@0 | 87 | |
michael@0 | 88 | class MOZ_STACK_CLASS AutoObjectMapperFaultyLib : public AutoObjectMapperPOSIX { |
michael@0 | 89 | public: |
michael@0 | 90 | AutoObjectMapperFaultyLib(void(*aLog)(const char*)); |
michael@0 | 91 | |
michael@0 | 92 | ~AutoObjectMapperFaultyLib(); |
michael@0 | 93 | |
michael@0 | 94 | bool Map(/*OUT*/void** start, /*OUT*/size_t* length, std::string fileName); |
michael@0 | 95 | |
michael@0 | 96 | private: |
michael@0 | 97 | // faulty.lib requires us to maintain an abstract handle that can be |
michael@0 | 98 | // used later to unmap the area. If this is non-NULL, it is assumed |
michael@0 | 99 | // that unmapping is to be done by faulty.lib. Otherwise it goes |
michael@0 | 100 | // via the normal mechanism. |
michael@0 | 101 | void* mHdl; |
michael@0 | 102 | |
michael@0 | 103 | // Disable copying and assignment. |
michael@0 | 104 | AutoObjectMapperFaultyLib(const AutoObjectMapperFaultyLib&); |
michael@0 | 105 | AutoObjectMapperFaultyLib& operator=(const AutoObjectMapperFaultyLib&); |
michael@0 | 106 | // Disable heap allocation of this class. |
michael@0 | 107 | void* operator new(size_t); |
michael@0 | 108 | void* operator new[](size_t); |
michael@0 | 109 | void operator delete(void*); |
michael@0 | 110 | void operator delete[](void*); |
michael@0 | 111 | }; |
michael@0 | 112 | |
michael@0 | 113 | #endif // defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK) |
michael@0 | 114 | |
michael@0 | 115 | #endif // AutoObjectMapper_h |