2015-04-09 17:25:05 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-06-14 18:01:02 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-09-12 14:47:37 +00:00
|
|
|
#include "IOInterposer.h"
|
2013-06-14 18:01:02 +00:00
|
|
|
#include "NSPRInterposer.h"
|
|
|
|
|
2013-09-12 14:47:37 +00:00
|
|
|
#include "prio.h"
|
2013-06-14 18:01:02 +00:00
|
|
|
#include "private/pprio.h"
|
2015-06-05 20:03:11 +00:00
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nscore.h"
|
2013-06-14 18:01:02 +00:00
|
|
|
|
2013-09-12 14:47:37 +00:00
|
|
|
namespace {
|
|
|
|
|
2013-06-14 18:01:02 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2013-09-12 14:47:37 +00:00
|
|
|
/* Original IO methods */
|
2013-10-01 19:53:19 +00:00
|
|
|
PRCloseFN sCloseFn = nullptr;
|
2013-09-12 14:47:37 +00:00
|
|
|
PRReadFN sReadFn = nullptr;
|
|
|
|
PRWriteFN sWriteFn = nullptr;
|
|
|
|
PRFsyncFN sFSyncFn = nullptr;
|
2013-10-01 19:53:19 +00:00
|
|
|
PRFileInfoFN sFileInfoFn = nullptr;
|
|
|
|
PRFileInfo64FN sFileInfo64Fn = nullptr;
|
2013-06-14 18:01:02 +00:00
|
|
|
|
2013-09-12 14:47:37 +00:00
|
|
|
/**
|
|
|
|
* RAII class for timing the duration of an NSPR I/O call and reporting the
|
|
|
|
* result to the IOInterposeObserver API.
|
|
|
|
*/
|
|
|
|
class NSPRIOAutoObservation : public IOInterposeObserver::Observation
|
2013-06-14 18:01:02 +00:00
|
|
|
{
|
2013-09-12 14:47:37 +00:00
|
|
|
public:
|
2014-08-05 13:36:59 +00:00
|
|
|
explicit NSPRIOAutoObservation(IOInterposeObserver::Operation aOp)
|
2014-02-01 03:14:03 +00:00
|
|
|
: IOInterposeObserver::Observation(aOp, "NSPRIOInterposer")
|
2013-09-12 14:47:37 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~NSPRIOAutoObservation()
|
|
|
|
{
|
2014-02-01 03:14:03 +00:00
|
|
|
Report();
|
2013-06-14 18:01:02 +00:00
|
|
|
}
|
2013-09-12 14:47:37 +00:00
|
|
|
};
|
|
|
|
|
2014-08-13 18:45:37 +00:00
|
|
|
PRStatus PR_CALLBACK
|
|
|
|
interposedClose(PRFileDesc* aFd)
|
2013-10-01 19:53:19 +00:00
|
|
|
{
|
|
|
|
// If we don't have a valid original function pointer something is very wrong.
|
|
|
|
NS_ASSERTION(sCloseFn, "NSPR IO Interposing: sCloseFn is NULL");
|
|
|
|
|
|
|
|
NSPRIOAutoObservation timer(IOInterposeObserver::OpClose);
|
|
|
|
return sCloseFn(aFd);
|
|
|
|
}
|
|
|
|
|
2014-08-13 18:45:37 +00:00
|
|
|
int32_t PR_CALLBACK
|
|
|
|
interposedRead(PRFileDesc* aFd, void* aBuf, int32_t aAmt)
|
2013-06-14 18:01:02 +00:00
|
|
|
{
|
2013-09-12 14:47:37 +00:00
|
|
|
// If we don't have a valid original function pointer something is very wrong.
|
|
|
|
NS_ASSERTION(sReadFn, "NSPR IO Interposing: sReadFn is NULL");
|
|
|
|
|
|
|
|
NSPRIOAutoObservation timer(IOInterposeObserver::OpRead);
|
|
|
|
return sReadFn(aFd, aBuf, aAmt);
|
2013-06-14 18:01:02 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 18:45:37 +00:00
|
|
|
int32_t PR_CALLBACK
|
|
|
|
interposedWrite(PRFileDesc* aFd, const void* aBuf, int32_t aAmt)
|
2013-06-14 18:01:02 +00:00
|
|
|
{
|
2013-09-12 14:47:37 +00:00
|
|
|
// If we don't have a valid original function pointer something is very wrong.
|
|
|
|
NS_ASSERTION(sWriteFn, "NSPR IO Interposing: sWriteFn is NULL");
|
|
|
|
|
|
|
|
NSPRIOAutoObservation timer(IOInterposeObserver::OpWrite);
|
|
|
|
return sWriteFn(aFd, aBuf, aAmt);
|
2013-06-14 18:01:02 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 18:45:37 +00:00
|
|
|
PRStatus PR_CALLBACK
|
|
|
|
interposedFSync(PRFileDesc* aFd)
|
2013-06-14 18:01:02 +00:00
|
|
|
{
|
2013-09-12 14:47:37 +00:00
|
|
|
// If we don't have a valid original function pointer something is very wrong.
|
|
|
|
NS_ASSERTION(sFSyncFn, "NSPR IO Interposing: sFSyncFn is NULL");
|
|
|
|
|
|
|
|
NSPRIOAutoObservation timer(IOInterposeObserver::OpFSync);
|
|
|
|
return sFSyncFn(aFd);
|
2013-06-14 18:01:02 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 18:45:37 +00:00
|
|
|
PRStatus PR_CALLBACK
|
|
|
|
interposedFileInfo(PRFileDesc* aFd, PRFileInfo* aInfo)
|
2013-10-01 19:53:19 +00:00
|
|
|
{
|
|
|
|
// If we don't have a valid original function pointer something is very wrong.
|
|
|
|
NS_ASSERTION(sFileInfoFn, "NSPR IO Interposing: sFileInfoFn is NULL");
|
|
|
|
|
|
|
|
NSPRIOAutoObservation timer(IOInterposeObserver::OpStat);
|
|
|
|
return sFileInfoFn(aFd, aInfo);
|
|
|
|
}
|
|
|
|
|
2014-08-13 18:45:37 +00:00
|
|
|
PRStatus PR_CALLBACK
|
|
|
|
interposedFileInfo64(PRFileDesc* aFd, PRFileInfo64* aInfo)
|
2013-10-01 19:53:19 +00:00
|
|
|
{
|
|
|
|
// If we don't have a valid original function pointer something is very wrong.
|
|
|
|
NS_ASSERTION(sFileInfo64Fn, "NSPR IO Interposing: sFileInfo64Fn is NULL");
|
|
|
|
|
|
|
|
NSPRIOAutoObservation timer(IOInterposeObserver::OpStat);
|
|
|
|
return sFileInfo64Fn(aFd, aInfo);
|
|
|
|
}
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace
|
2013-09-12 14:47:37 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-08-13 18:45:37 +00:00
|
|
|
void
|
|
|
|
InitNSPRIOInterposing()
|
2013-06-14 18:01:02 +00:00
|
|
|
{
|
2013-09-12 14:47:37 +00:00
|
|
|
// Check that we have not interposed any of the IO methods before
|
2013-10-01 19:53:19 +00:00
|
|
|
MOZ_ASSERT(!sCloseFn && !sReadFn && !sWriteFn && !sFSyncFn && !sFileInfoFn &&
|
|
|
|
!sFileInfo64Fn);
|
2013-09-12 14:47:37 +00:00
|
|
|
|
2013-06-14 18:01:02 +00:00
|
|
|
// We can't actually use this assertion because we initialize this code
|
|
|
|
// before XPCOM is initialized, so NS_IsMainThread() always returns false.
|
|
|
|
// MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-09-12 14:47:37 +00:00
|
|
|
// Get IO methods from NSPR and const cast the structure so we can modify it.
|
|
|
|
PRIOMethods* methods = const_cast<PRIOMethods*>(PR_GetFileMethods());
|
2013-06-14 18:01:02 +00:00
|
|
|
|
2013-09-12 14:47:37 +00:00
|
|
|
// Something is badly wrong if we don't get IO methods... However, we don't
|
|
|
|
// want to crash over that in non-debug builds. This is unlikely to happen
|
|
|
|
// so an assert is enough, no need to report it to the caller.
|
|
|
|
MOZ_ASSERT(methods);
|
|
|
|
if (!methods) {
|
|
|
|
return;
|
|
|
|
}
|
2013-06-14 18:01:02 +00:00
|
|
|
|
2013-10-01 19:53:19 +00:00
|
|
|
// Store original functions
|
|
|
|
sCloseFn = methods->close;
|
|
|
|
sReadFn = methods->read;
|
|
|
|
sWriteFn = methods->write;
|
|
|
|
sFSyncFn = methods->fsync;
|
|
|
|
sFileInfoFn = methods->fileInfo;
|
|
|
|
sFileInfo64Fn = methods->fileInfo64;
|
|
|
|
|
|
|
|
// Overwrite with our interposed functions
|
|
|
|
methods->close = &interposedClose;
|
|
|
|
methods->read = &interposedRead;
|
|
|
|
methods->write = &interposedWrite;
|
|
|
|
methods->fsync = &interposedFSync;
|
|
|
|
methods->fileInfo = &interposedFileInfo;
|
|
|
|
methods->fileInfo64 = &interposedFileInfo64;
|
2013-06-14 18:01:02 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 18:45:37 +00:00
|
|
|
void
|
|
|
|
ClearNSPRIOInterposing()
|
2013-06-14 18:01:02 +00:00
|
|
|
{
|
2013-09-12 14:47:37 +00:00
|
|
|
// If we have already cleared IO interposing, or not initialized it this is
|
|
|
|
// actually bad.
|
2013-10-01 19:53:19 +00:00
|
|
|
MOZ_ASSERT(sCloseFn && sReadFn && sWriteFn && sFSyncFn && sFileInfoFn &&
|
|
|
|
sFileInfo64Fn);
|
2013-09-12 14:47:37 +00:00
|
|
|
|
|
|
|
// Get IO methods from NSPR and const cast the structure so we can modify it.
|
|
|
|
PRIOMethods* methods = const_cast<PRIOMethods*>(PR_GetFileMethods());
|
|
|
|
|
|
|
|
// Something is badly wrong if we don't get IO methods... However, we don't
|
|
|
|
// want to crash over that in non-debug builds. This is unlikely to happen
|
|
|
|
// so an assert is enough, no need to report it to the caller.
|
|
|
|
MOZ_ASSERT(methods);
|
|
|
|
if (!methods) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-01 19:53:19 +00:00
|
|
|
// Restore original functions
|
|
|
|
methods->close = sCloseFn;
|
|
|
|
methods->read = sReadFn;
|
|
|
|
methods->write = sWriteFn;
|
|
|
|
methods->fsync = sFSyncFn;
|
|
|
|
methods->fileInfo = sFileInfoFn;
|
|
|
|
methods->fileInfo64 = sFileInfo64Fn;
|
2013-06-14 18:01:02 +00:00
|
|
|
|
2013-09-12 14:47:37 +00:00
|
|
|
// Forget about original functions
|
2013-10-01 19:53:19 +00:00
|
|
|
sCloseFn = nullptr;
|
|
|
|
sReadFn = nullptr;
|
|
|
|
sWriteFn = nullptr;
|
|
|
|
sFSyncFn = nullptr;
|
|
|
|
sFileInfoFn = nullptr;
|
|
|
|
sFileInfo64Fn = nullptr;
|
2013-06-14 18:01:02 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 14:47:37 +00:00
|
|
|
} // namespace mozilla
|
2013-10-01 19:53:19 +00:00
|
|
|
|