xpcom/build/NSPRInterposer.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 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "IOInterposer.h"
michael@0 6 #include "NSPRInterposer.h"
michael@0 7
michael@0 8 #include "prio.h"
michael@0 9 #include "private/pprio.h"
michael@0 10
michael@0 11 namespace {
michael@0 12
michael@0 13 using namespace mozilla;
michael@0 14
michael@0 15 /* Original IO methods */
michael@0 16 PRCloseFN sCloseFn = nullptr;
michael@0 17 PRReadFN sReadFn = nullptr;
michael@0 18 PRWriteFN sWriteFn = nullptr;
michael@0 19 PRFsyncFN sFSyncFn = nullptr;
michael@0 20 PRFileInfoFN sFileInfoFn = nullptr;
michael@0 21 PRFileInfo64FN sFileInfo64Fn = nullptr;
michael@0 22
michael@0 23 /**
michael@0 24 * RAII class for timing the duration of an NSPR I/O call and reporting the
michael@0 25 * result to the IOInterposeObserver API.
michael@0 26 */
michael@0 27 class NSPRIOAutoObservation : public IOInterposeObserver::Observation
michael@0 28 {
michael@0 29 public:
michael@0 30 NSPRIOAutoObservation(IOInterposeObserver::Operation aOp)
michael@0 31 : IOInterposeObserver::Observation(aOp, "NSPRIOInterposer")
michael@0 32 {
michael@0 33 }
michael@0 34
michael@0 35 ~NSPRIOAutoObservation()
michael@0 36 {
michael@0 37 Report();
michael@0 38 }
michael@0 39 };
michael@0 40
michael@0 41 PRStatus PR_CALLBACK interposedClose(PRFileDesc* aFd)
michael@0 42 {
michael@0 43 // If we don't have a valid original function pointer something is very wrong.
michael@0 44 NS_ASSERTION(sCloseFn, "NSPR IO Interposing: sCloseFn is NULL");
michael@0 45
michael@0 46 NSPRIOAutoObservation timer(IOInterposeObserver::OpClose);
michael@0 47 return sCloseFn(aFd);
michael@0 48 }
michael@0 49
michael@0 50 int32_t PR_CALLBACK interposedRead(PRFileDesc* aFd, void* aBuf, int32_t aAmt)
michael@0 51 {
michael@0 52 // If we don't have a valid original function pointer something is very wrong.
michael@0 53 NS_ASSERTION(sReadFn, "NSPR IO Interposing: sReadFn is NULL");
michael@0 54
michael@0 55 NSPRIOAutoObservation timer(IOInterposeObserver::OpRead);
michael@0 56 return sReadFn(aFd, aBuf, aAmt);
michael@0 57 }
michael@0 58
michael@0 59 int32_t PR_CALLBACK interposedWrite(PRFileDesc* aFd, const void* aBuf,
michael@0 60 int32_t aAmt)
michael@0 61 {
michael@0 62 // If we don't have a valid original function pointer something is very wrong.
michael@0 63 NS_ASSERTION(sWriteFn, "NSPR IO Interposing: sWriteFn is NULL");
michael@0 64
michael@0 65 NSPRIOAutoObservation timer(IOInterposeObserver::OpWrite);
michael@0 66 return sWriteFn(aFd, aBuf, aAmt);
michael@0 67 }
michael@0 68
michael@0 69 PRStatus PR_CALLBACK interposedFSync(PRFileDesc* aFd)
michael@0 70 {
michael@0 71 // If we don't have a valid original function pointer something is very wrong.
michael@0 72 NS_ASSERTION(sFSyncFn, "NSPR IO Interposing: sFSyncFn is NULL");
michael@0 73
michael@0 74 NSPRIOAutoObservation timer(IOInterposeObserver::OpFSync);
michael@0 75 return sFSyncFn(aFd);
michael@0 76 }
michael@0 77
michael@0 78 PRStatus PR_CALLBACK interposedFileInfo(PRFileDesc *aFd, PRFileInfo *aInfo)
michael@0 79 {
michael@0 80 // If we don't have a valid original function pointer something is very wrong.
michael@0 81 NS_ASSERTION(sFileInfoFn, "NSPR IO Interposing: sFileInfoFn is NULL");
michael@0 82
michael@0 83 NSPRIOAutoObservation timer(IOInterposeObserver::OpStat);
michael@0 84 return sFileInfoFn(aFd, aInfo);
michael@0 85 }
michael@0 86
michael@0 87 PRStatus PR_CALLBACK interposedFileInfo64(PRFileDesc *aFd, PRFileInfo64 *aInfo)
michael@0 88 {
michael@0 89 // If we don't have a valid original function pointer something is very wrong.
michael@0 90 NS_ASSERTION(sFileInfo64Fn, "NSPR IO Interposing: sFileInfo64Fn is NULL");
michael@0 91
michael@0 92 NSPRIOAutoObservation timer(IOInterposeObserver::OpStat);
michael@0 93 return sFileInfo64Fn(aFd, aInfo);
michael@0 94 }
michael@0 95
michael@0 96 } // anonymous namespace
michael@0 97
michael@0 98 namespace mozilla {
michael@0 99
michael@0 100 void InitNSPRIOInterposing()
michael@0 101 {
michael@0 102 // Check that we have not interposed any of the IO methods before
michael@0 103 MOZ_ASSERT(!sCloseFn && !sReadFn && !sWriteFn && !sFSyncFn && !sFileInfoFn &&
michael@0 104 !sFileInfo64Fn);
michael@0 105
michael@0 106 // We can't actually use this assertion because we initialize this code
michael@0 107 // before XPCOM is initialized, so NS_IsMainThread() always returns false.
michael@0 108 // MOZ_ASSERT(NS_IsMainThread());
michael@0 109
michael@0 110 // Get IO methods from NSPR and const cast the structure so we can modify it.
michael@0 111 PRIOMethods* methods = const_cast<PRIOMethods*>(PR_GetFileMethods());
michael@0 112
michael@0 113 // Something is badly wrong if we don't get IO methods... However, we don't
michael@0 114 // want to crash over that in non-debug builds. This is unlikely to happen
michael@0 115 // so an assert is enough, no need to report it to the caller.
michael@0 116 MOZ_ASSERT(methods);
michael@0 117 if (!methods) {
michael@0 118 return;
michael@0 119 }
michael@0 120
michael@0 121 // Store original functions
michael@0 122 sCloseFn = methods->close;
michael@0 123 sReadFn = methods->read;
michael@0 124 sWriteFn = methods->write;
michael@0 125 sFSyncFn = methods->fsync;
michael@0 126 sFileInfoFn = methods->fileInfo;
michael@0 127 sFileInfo64Fn = methods->fileInfo64;
michael@0 128
michael@0 129 // Overwrite with our interposed functions
michael@0 130 methods->close = &interposedClose;
michael@0 131 methods->read = &interposedRead;
michael@0 132 methods->write = &interposedWrite;
michael@0 133 methods->fsync = &interposedFSync;
michael@0 134 methods->fileInfo = &interposedFileInfo;
michael@0 135 methods->fileInfo64 = &interposedFileInfo64;
michael@0 136 }
michael@0 137
michael@0 138 void ClearNSPRIOInterposing()
michael@0 139 {
michael@0 140 // If we have already cleared IO interposing, or not initialized it this is
michael@0 141 // actually bad.
michael@0 142 MOZ_ASSERT(sCloseFn && sReadFn && sWriteFn && sFSyncFn && sFileInfoFn &&
michael@0 143 sFileInfo64Fn);
michael@0 144
michael@0 145 // Get IO methods from NSPR and const cast the structure so we can modify it.
michael@0 146 PRIOMethods* methods = const_cast<PRIOMethods*>(PR_GetFileMethods());
michael@0 147
michael@0 148 // Something is badly wrong if we don't get IO methods... However, we don't
michael@0 149 // want to crash over that in non-debug builds. This is unlikely to happen
michael@0 150 // so an assert is enough, no need to report it to the caller.
michael@0 151 MOZ_ASSERT(methods);
michael@0 152 if (!methods) {
michael@0 153 return;
michael@0 154 }
michael@0 155
michael@0 156 // Restore original functions
michael@0 157 methods->close = sCloseFn;
michael@0 158 methods->read = sReadFn;
michael@0 159 methods->write = sWriteFn;
michael@0 160 methods->fsync = sFSyncFn;
michael@0 161 methods->fileInfo = sFileInfoFn;
michael@0 162 methods->fileInfo64 = sFileInfo64Fn;
michael@0 163
michael@0 164 // Forget about original functions
michael@0 165 sCloseFn = nullptr;
michael@0 166 sReadFn = nullptr;
michael@0 167 sWriteFn = nullptr;
michael@0 168 sFSyncFn = nullptr;
michael@0 169 sFileInfoFn = nullptr;
michael@0 170 sFileInfo64Fn = nullptr;
michael@0 171 }
michael@0 172
michael@0 173 } // namespace mozilla
michael@0 174

mercurial