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