xpcom/build/PoisonIOInterposerBase.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.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 ci et: */
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 "mozilla/Mutex.h"
michael@0 8 #include "mozilla/Scoped.h"
michael@0 9
michael@0 10 #include <algorithm>
michael@0 11 #include <vector>
michael@0 12
michael@0 13 #include "PoisonIOInterposer.h"
michael@0 14
michael@0 15 // Auxiliary method to convert file descriptors to ids
michael@0 16 #if defined(XP_WIN32)
michael@0 17 #include <io.h>
michael@0 18 inline intptr_t FileDescriptorToID(int aFd) {
michael@0 19 return _get_osfhandle(aFd);
michael@0 20 }
michael@0 21 #else
michael@0 22 inline intptr_t FileDescriptorToID(int aFd) {
michael@0 23 return aFd;
michael@0 24 }
michael@0 25 #endif /* if not XP_WIN32 */
michael@0 26
michael@0 27 using namespace mozilla;
michael@0 28
michael@0 29 namespace {
michael@0 30 struct DebugFilesAutoLockTraits {
michael@0 31 typedef PRLock *type;
michael@0 32 const static type empty() {
michael@0 33 return nullptr;
michael@0 34 }
michael@0 35 const static void release(type aL) {
michael@0 36 PR_Unlock(aL);
michael@0 37 }
michael@0 38 };
michael@0 39
michael@0 40 class DebugFilesAutoLock : public Scoped<DebugFilesAutoLockTraits> {
michael@0 41 static PRLock *Lock;
michael@0 42 public:
michael@0 43 static void Clear();
michael@0 44 static PRLock *getDebugFileIDsLock() {
michael@0 45 // On windows this static is not thread safe, but we know that the first
michael@0 46 // call is from
michael@0 47 // * An early registration of a debug FD or
michael@0 48 // * The call to InitWritePoisoning.
michael@0 49 // Since the early debug FDs are logs created early in the main thread
michael@0 50 // and no writes are trapped before InitWritePoisoning, we are safe.
michael@0 51 if (!Lock) {
michael@0 52 Lock = PR_NewLock();
michael@0 53 }
michael@0 54
michael@0 55 // We have to use something lower level than a mutex. If we don't, we
michael@0 56 // can get recursive in here when called from logging a call to free.
michael@0 57 return Lock;
michael@0 58 }
michael@0 59
michael@0 60 DebugFilesAutoLock() :
michael@0 61 Scoped<DebugFilesAutoLockTraits>(getDebugFileIDsLock()) {
michael@0 62 PR_Lock(get());
michael@0 63 }
michael@0 64 };
michael@0 65
michael@0 66 PRLock *DebugFilesAutoLock::Lock;
michael@0 67 void DebugFilesAutoLock::Clear() {
michael@0 68 MOZ_ASSERT(Lock != nullptr);
michael@0 69 Lock = nullptr;
michael@0 70 }
michael@0 71
michael@0 72 // Return a vector used to hold the IDs of the current debug files. On unix
michael@0 73 // an ID is a file descriptor. On Windows it is a file HANDLE.
michael@0 74 std::vector<intptr_t>* getDebugFileIDs() {
michael@0 75 PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(DebugFilesAutoLock::getDebugFileIDsLock());
michael@0 76 // We have to use new as some write happen during static destructors
michael@0 77 // so an static std::vector might be destroyed while we still need it.
michael@0 78 static std::vector<intptr_t> *DebugFileIDs = new std::vector<intptr_t>();
michael@0 79 return DebugFileIDs;
michael@0 80 }
michael@0 81
michael@0 82 } // anonymous namespace
michael@0 83
michael@0 84 namespace mozilla{
michael@0 85
michael@0 86 // Auxiliary Method to test if a file descriptor is registered to be ignored
michael@0 87 // by the poisoning IO interposer
michael@0 88 bool IsDebugFile(intptr_t aFileID) {
michael@0 89 DebugFilesAutoLock lockedScope;
michael@0 90
michael@0 91 std::vector<intptr_t> &Vec = *getDebugFileIDs();
michael@0 92 return std::find(Vec.begin(), Vec.end(), aFileID) != Vec.end();
michael@0 93 }
michael@0 94
michael@0 95 // Clean-up for the registered debug files.
michael@0 96 // We should probably make sure all debug files are unregistered instead.
michael@0 97 // But as the poison IO interposer is used for late-write checks we're not
michael@0 98 // disabling it at any point yet. So Really no need for this.
michael@0 99 //
michael@0 100 // void ClearDebugFileRegister() {
michael@0 101 // PRLock *Lock;
michael@0 102 // {
michael@0 103 // DebugFilesAutoLock lockedScope;
michael@0 104 // delete getDebugFileIDs();
michael@0 105 // Lock = DebugFilesAutoLock::getDebugFileIDsLock();
michael@0 106 // DebugFilesAutoLock::Clear();
michael@0 107 // }
michael@0 108 // PR_DestroyLock(Lock);
michael@0 109 // }
michael@0 110
michael@0 111 } // namespace mozilla
michael@0 112
michael@0 113 extern "C" {
michael@0 114
michael@0 115 void MozillaRegisterDebugFD(int fd) {
michael@0 116 intptr_t fileId = FileDescriptorToID(fd);
michael@0 117 DebugFilesAutoLock lockedScope;
michael@0 118 std::vector<intptr_t> &Vec = *getDebugFileIDs();
michael@0 119 MOZ_ASSERT(std::find(Vec.begin(), Vec.end(), fileId) == Vec.end());
michael@0 120 Vec.push_back(fileId);
michael@0 121 }
michael@0 122
michael@0 123 void MozillaRegisterDebugFILE(FILE *f) {
michael@0 124 int fd = fileno(f);
michael@0 125 if (fd == 1 || fd == 2) {
michael@0 126 return;
michael@0 127 }
michael@0 128 MozillaRegisterDebugFD(fd);
michael@0 129 }
michael@0 130
michael@0 131 void MozillaUnRegisterDebugFD(int fd) {
michael@0 132 DebugFilesAutoLock lockedScope;
michael@0 133 intptr_t fileId = FileDescriptorToID(fd);
michael@0 134 std::vector<intptr_t> &Vec = *getDebugFileIDs();
michael@0 135 std::vector<intptr_t>::iterator i =
michael@0 136 std::find(Vec.begin(), Vec.end(), fileId);
michael@0 137 MOZ_ASSERT(i != Vec.end());
michael@0 138 Vec.erase(i);
michael@0 139 }
michael@0 140
michael@0 141 void MozillaUnRegisterDebugFILE(FILE *f) {
michael@0 142 int fd = fileno(f);
michael@0 143 if (fd == 1 || fd == 2) {
michael@0 144 return;
michael@0 145 }
michael@0 146 fflush(f);
michael@0 147 MozillaUnRegisterDebugFD(fd);
michael@0 148 }
michael@0 149
michael@0 150 }

mercurial