Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 | #include <sys/mman.h> |
michael@0 | 8 | #include <unistd.h> |
michael@0 | 9 | #include <sys/types.h> |
michael@0 | 10 | #include <sys/stat.h> |
michael@0 | 11 | #include <fcntl.h> |
michael@0 | 12 | |
michael@0 | 13 | #include "mozilla/Assertions.h" |
michael@0 | 14 | #include "mozilla/NullPtr.h" |
michael@0 | 15 | |
michael@0 | 16 | #include "PlatformMacros.h" |
michael@0 | 17 | #include "AutoObjectMapper.h" |
michael@0 | 18 | |
michael@0 | 19 | #if defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK) |
michael@0 | 20 | # include <dlfcn.h> |
michael@0 | 21 | # include "mozilla/Types.h" |
michael@0 | 22 | // FIXME move these out of mozglue/linker/ElfLoader.h into their |
michael@0 | 23 | // own header, so as to avoid conflicts arising from two definitions |
michael@0 | 24 | // of Array |
michael@0 | 25 | extern "C" { |
michael@0 | 26 | MFBT_API size_t |
michael@0 | 27 | __dl_get_mappable_length(void *handle); |
michael@0 | 28 | MFBT_API void * |
michael@0 | 29 | __dl_mmap(void *handle, void *addr, size_t length, off_t offset); |
michael@0 | 30 | MFBT_API void |
michael@0 | 31 | __dl_munmap(void *handle, void *addr, size_t length); |
michael@0 | 32 | } |
michael@0 | 33 | // The following are for get_installation_lib_dir() |
michael@0 | 34 | # include "nsString.h" |
michael@0 | 35 | # include "nsDirectoryServiceUtils.h" |
michael@0 | 36 | # include "nsDirectoryServiceDefs.h" |
michael@0 | 37 | #endif |
michael@0 | 38 | |
michael@0 | 39 | |
michael@0 | 40 | // A helper function for creating failure error messages in |
michael@0 | 41 | // AutoObjectMapper*::Map. |
michael@0 | 42 | static void |
michael@0 | 43 | failedToMessage(void(*aLog)(const char*), |
michael@0 | 44 | const char* aHowFailed, std::string aFileName) |
michael@0 | 45 | { |
michael@0 | 46 | char buf[300]; |
michael@0 | 47 | snprintf(buf, sizeof(buf), "AutoObjectMapper::Map: Failed to %s \'%s\'", |
michael@0 | 48 | aHowFailed, aFileName.c_str()); |
michael@0 | 49 | buf[sizeof(buf)-1] = 0; |
michael@0 | 50 | aLog(buf); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | |
michael@0 | 54 | AutoObjectMapperPOSIX::AutoObjectMapperPOSIX(void(*aLog)(const char*)) |
michael@0 | 55 | : mImage(nullptr) |
michael@0 | 56 | , mSize(0) |
michael@0 | 57 | , mLog(aLog) |
michael@0 | 58 | , mIsMapped(false) |
michael@0 | 59 | {} |
michael@0 | 60 | |
michael@0 | 61 | AutoObjectMapperPOSIX::~AutoObjectMapperPOSIX() { |
michael@0 | 62 | if (!mIsMapped) { |
michael@0 | 63 | // There's nothing to do. |
michael@0 | 64 | MOZ_ASSERT(!mImage); |
michael@0 | 65 | MOZ_ASSERT(mSize == 0); |
michael@0 | 66 | return; |
michael@0 | 67 | } |
michael@0 | 68 | MOZ_ASSERT(mSize > 0); |
michael@0 | 69 | // The following assertion doesn't necessarily have to be true, |
michael@0 | 70 | // but we assume (reasonably enough) that no mmap facility would |
michael@0 | 71 | // be crazy enough to map anything at page zero. |
michael@0 | 72 | MOZ_ASSERT(mImage); |
michael@0 | 73 | munmap(mImage, mSize); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | bool AutoObjectMapperPOSIX::Map(/*OUT*/void** start, /*OUT*/size_t* length, |
michael@0 | 77 | std::string fileName) |
michael@0 | 78 | { |
michael@0 | 79 | MOZ_ASSERT(!mIsMapped); |
michael@0 | 80 | |
michael@0 | 81 | int fd = open(fileName.c_str(), O_RDONLY); |
michael@0 | 82 | if (fd == -1) { |
michael@0 | 83 | failedToMessage(mLog, "open", fileName); |
michael@0 | 84 | return false; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | struct stat st; |
michael@0 | 88 | int err = fstat(fd, &st); |
michael@0 | 89 | size_t sz = (err == 0) ? st.st_size : 0; |
michael@0 | 90 | if (err != 0 || sz == 0) { |
michael@0 | 91 | failedToMessage(mLog, "fstat", fileName); |
michael@0 | 92 | close(fd); |
michael@0 | 93 | return false; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | void* image = mmap(nullptr, sz, PROT_READ, MAP_SHARED, fd, 0); |
michael@0 | 97 | if (image == MAP_FAILED) { |
michael@0 | 98 | failedToMessage(mLog, "mmap", fileName); |
michael@0 | 99 | close(fd); |
michael@0 | 100 | return false; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | close(fd); |
michael@0 | 104 | mIsMapped = true; |
michael@0 | 105 | mImage = *start = image; |
michael@0 | 106 | mSize = *length = sz; |
michael@0 | 107 | return true; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | |
michael@0 | 111 | #if defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK) |
michael@0 | 112 | // A helper function for AutoObjectMapperFaultyLib::Map. Finds out |
michael@0 | 113 | // where the installation's lib directory is, since we'll have to look |
michael@0 | 114 | // in there to get hold of libmozglue.so. Returned C string is heap |
michael@0 | 115 | // allocated and the caller must deallocate it. |
michael@0 | 116 | static char* |
michael@0 | 117 | get_installation_lib_dir() |
michael@0 | 118 | { |
michael@0 | 119 | nsCOMPtr<nsIProperties> |
michael@0 | 120 | directoryService(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID)); |
michael@0 | 121 | if (!directoryService) { |
michael@0 | 122 | return nullptr; |
michael@0 | 123 | } |
michael@0 | 124 | nsCOMPtr<nsIFile> greDir; |
michael@0 | 125 | nsresult rv = directoryService->Get(NS_GRE_DIR, NS_GET_IID(nsIFile), |
michael@0 | 126 | getter_AddRefs(greDir)); |
michael@0 | 127 | if (NS_FAILED(rv)) return nullptr; |
michael@0 | 128 | nsCString path; |
michael@0 | 129 | rv = greDir->GetNativePath(path); |
michael@0 | 130 | if (NS_FAILED(rv)) { |
michael@0 | 131 | return nullptr; |
michael@0 | 132 | } |
michael@0 | 133 | return strdup(path.get()); |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | AutoObjectMapperFaultyLib::AutoObjectMapperFaultyLib(void(*aLog)(const char*)) |
michael@0 | 137 | : AutoObjectMapperPOSIX(aLog) |
michael@0 | 138 | , mHdl(nullptr) |
michael@0 | 139 | {} |
michael@0 | 140 | |
michael@0 | 141 | AutoObjectMapperFaultyLib::~AutoObjectMapperFaultyLib() { |
michael@0 | 142 | if (mHdl) { |
michael@0 | 143 | // We've got an object mapped by faulty.lib. Unmap it via faulty.lib. |
michael@0 | 144 | MOZ_ASSERT(mSize > 0); |
michael@0 | 145 | // Assert on the basis that no valid mapping would start at page zero. |
michael@0 | 146 | MOZ_ASSERT(mImage); |
michael@0 | 147 | __dl_munmap(mHdl, mImage, mSize); |
michael@0 | 148 | dlclose(mHdl); |
michael@0 | 149 | // Stop assertions in ~AutoObjectMapperPOSIX from failing. |
michael@0 | 150 | mImage = nullptr; |
michael@0 | 151 | mSize = 0; |
michael@0 | 152 | } |
michael@0 | 153 | // At this point the parent class destructor, ~AutoObjectMapperPOSIX, |
michael@0 | 154 | // gets called. If that has something mapped in the normal way, it |
michael@0 | 155 | // will unmap it in the normal way. Unfortunately there's no |
michael@0 | 156 | // obvious way to enforce the requirement that the object is mapped |
michael@0 | 157 | // either by faulty.lib or by the parent class, but not by both. |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | bool AutoObjectMapperFaultyLib::Map(/*OUT*/void** start, /*OUT*/size_t* length, |
michael@0 | 161 | std::string fileName) |
michael@0 | 162 | { |
michael@0 | 163 | MOZ_ASSERT(!mHdl); |
michael@0 | 164 | |
michael@0 | 165 | if (fileName == "libmozglue.so") { |
michael@0 | 166 | |
michael@0 | 167 | // Do (2) in the comment above. |
michael@0 | 168 | char* libdir = get_installation_lib_dir(); |
michael@0 | 169 | if (libdir) { |
michael@0 | 170 | fileName = std::string(libdir) + "/lib/" + fileName; |
michael@0 | 171 | free(libdir); |
michael@0 | 172 | } |
michael@0 | 173 | // Hand the problem off to the standard mapper. |
michael@0 | 174 | return AutoObjectMapperPOSIX::Map(start, length, fileName); |
michael@0 | 175 | |
michael@0 | 176 | } else { |
michael@0 | 177 | |
michael@0 | 178 | // Do cases (1) and (3) in the comment above. We have to |
michael@0 | 179 | // grapple with faulty.lib directly. |
michael@0 | 180 | void* hdl = dlopen(fileName.c_str(), RTLD_GLOBAL | RTLD_LAZY); |
michael@0 | 181 | if (!hdl) { |
michael@0 | 182 | failedToMessage(mLog, "get handle for ELF file", fileName); |
michael@0 | 183 | return false; |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | size_t sz = __dl_get_mappable_length(hdl); |
michael@0 | 187 | if (sz == 0) { |
michael@0 | 188 | dlclose(hdl); |
michael@0 | 189 | failedToMessage(mLog, "get size for ELF file", fileName); |
michael@0 | 190 | return false; |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | void* image = __dl_mmap(hdl, nullptr, sz, 0); |
michael@0 | 194 | if (image == MAP_FAILED) { |
michael@0 | 195 | dlclose(hdl); |
michael@0 | 196 | failedToMessage(mLog, "mmap ELF file", fileName); |
michael@0 | 197 | return false; |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | mHdl = hdl; |
michael@0 | 201 | mImage = *start = image; |
michael@0 | 202 | mSize = *length = sz; |
michael@0 | 203 | return true; |
michael@0 | 204 | } |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | #endif // defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK) |