2015-04-09 20:25:05 +03: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 22:01:02 +04: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 18:47:37 +04:00
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
|
2013-06-14 22:01:02 +04:00
|
|
|
#include "IOInterposer.h"
|
2013-09-12 18:47:37 +04:00
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
#include "IOInterposerPrivate.h"
|
|
|
|
#include "MainThreadIOLogger.h"
|
2014-03-15 10:10:39 +04:00
|
|
|
#include "mozilla/Atomics.h"
|
2013-09-12 18:47:37 +04:00
|
|
|
#include "mozilla/Mutex.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2013-09-12 18:47:37 +04:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2014-03-15 02:35:01 +04:00
|
|
|
#include "mozilla/ThreadLocal.h"
|
2015-07-31 22:11:48 +03:00
|
|
|
#include "nscore.h" // for NS_FREE_PERMANENT_DATA
|
2014-03-15 02:35:01 +04:00
|
|
|
#if !defined(XP_WIN)
|
|
|
|
# include "NSPRInterposer.h"
|
|
|
|
#endif // !defined(XP_WIN)
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
#include "PoisonIOInterposer.h"
|
2019-03-01 22:34:54 +03:00
|
|
|
#include "prenv.h"
|
2013-06-14 22:01:02 +04:00
|
|
|
|
2013-09-12 18:47:37 +04:00
|
|
|
namespace {
|
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
/** Find if a vector contains a specific element */
|
|
|
|
template <class T>
|
2014-08-13 22:45:37 +04:00
|
|
|
bool VectorContains(const std::vector<T>& aVector, const T& aElement) {
|
|
|
|
return std::find(aVector.begin(), aVector.end(), aElement) != aVector.end();
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Remove element from a vector */
|
|
|
|
template <class T>
|
2014-08-13 22:45:37 +04:00
|
|
|
void VectorRemove(std::vector<T>& aVector, const T& aElement) {
|
|
|
|
typename std::vector<T>::iterator newEnd =
|
|
|
|
std::remove(aVector.begin(), aVector.end(), aElement);
|
|
|
|
aVector.erase(newEnd, aVector.end());
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
|
2013-09-12 18:47:37 +04:00
|
|
|
/** Lists of Observers */
|
2014-04-29 21:32:03 +04:00
|
|
|
struct ObserverLists {
|
|
|
|
private:
|
2020-02-12 14:13:33 +03:00
|
|
|
~ObserverLists() = default;
|
2014-04-29 21:32:03 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ObserverLists)
|
|
|
|
|
2020-02-12 14:13:33 +03:00
|
|
|
ObserverLists() = default;
|
2013-09-12 18:47:37 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
ObserverLists(ObserverLists const& aOther)
|
2014-04-09 08:57:52 +04:00
|
|
|
: mCreateObservers(aOther.mCreateObservers),
|
|
|
|
mReadObservers(aOther.mReadObservers),
|
|
|
|
mWriteObservers(aOther.mWriteObservers),
|
|
|
|
mFSyncObservers(aOther.mFSyncObservers),
|
|
|
|
mStatObservers(aOther.mStatObservers),
|
|
|
|
mCloseObservers(aOther.mCloseObservers),
|
2014-04-20 00:28:02 +04:00
|
|
|
mStageObservers(aOther.mStageObservers) {}
|
|
|
|
// Lists of observers for I/O events.
|
2013-09-12 18:47:37 +04:00
|
|
|
// These are implemented as vectors since they are allowed to survive gecko,
|
|
|
|
// without reporting leaks. This is necessary for the IOInterposer to be used
|
|
|
|
// for late-write checks.
|
2019-11-20 22:58:05 +03:00
|
|
|
std::vector<mozilla::IOInterposeObserver*> mCreateObservers;
|
|
|
|
std::vector<mozilla::IOInterposeObserver*> mReadObservers;
|
|
|
|
std::vector<mozilla::IOInterposeObserver*> mWriteObservers;
|
|
|
|
std::vector<mozilla::IOInterposeObserver*> mFSyncObservers;
|
|
|
|
std::vector<mozilla::IOInterposeObserver*> mStatObservers;
|
|
|
|
std::vector<mozilla::IOInterposeObserver*> mCloseObservers;
|
|
|
|
std::vector<mozilla::IOInterposeObserver*> mStageObservers;
|
2013-09-12 18:47:37 +04:00
|
|
|
};
|
2013-06-14 22:01:02 +04:00
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
class PerThreadData {
|
|
|
|
public:
|
2014-08-05 17:36:59 +04:00
|
|
|
explicit PerThreadData(bool aIsMainThread = false)
|
2014-04-09 08:57:52 +04:00
|
|
|
: mIsMainThread(aIsMainThread),
|
|
|
|
mIsHandlingObservation(false),
|
|
|
|
mCurrentGeneration(0) {
|
2014-04-29 21:29:43 +04:00
|
|
|
MOZ_COUNT_CTOR(PerThreadData);
|
|
|
|
}
|
|
|
|
|
2020-02-20 14:40:14 +03:00
|
|
|
MOZ_COUNTED_DTOR(PerThreadData)
|
2013-06-14 22:01:02 +04:00
|
|
|
|
2019-11-20 22:58:05 +03:00
|
|
|
void CallObservers(mozilla::IOInterposeObserver::Observation& aObservation) {
|
2014-04-09 08:57:52 +04:00
|
|
|
// Prevent recursive reporting.
|
|
|
|
if (mIsHandlingObservation) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIsHandlingObservation = true;
|
|
|
|
// Decide which list of observers to inform
|
2022-06-30 10:30:07 +03:00
|
|
|
const std::vector<mozilla::IOInterposeObserver*>* observers = nullptr;
|
2014-04-09 08:57:52 +04:00
|
|
|
switch (aObservation.ObservedOperation()) {
|
2019-11-20 22:58:05 +03:00
|
|
|
case mozilla::IOInterposeObserver::OpCreateOrOpen:
|
2014-08-13 22:45:37 +04:00
|
|
|
observers = &mObserverLists->mCreateObservers;
|
2014-04-09 08:57:52 +04:00
|
|
|
break;
|
2019-11-20 22:58:05 +03:00
|
|
|
case mozilla::IOInterposeObserver::OpRead:
|
2014-08-13 22:45:37 +04:00
|
|
|
observers = &mObserverLists->mReadObservers;
|
2014-04-09 08:57:52 +04:00
|
|
|
break;
|
2019-11-20 22:58:05 +03:00
|
|
|
case mozilla::IOInterposeObserver::OpWrite:
|
2014-08-13 22:45:37 +04:00
|
|
|
observers = &mObserverLists->mWriteObservers;
|
2014-04-09 08:57:52 +04:00
|
|
|
break;
|
2019-11-20 22:58:05 +03:00
|
|
|
case mozilla::IOInterposeObserver::OpFSync:
|
2014-08-13 22:45:37 +04:00
|
|
|
observers = &mObserverLists->mFSyncObservers;
|
2014-04-09 08:57:52 +04:00
|
|
|
break;
|
2019-11-20 22:58:05 +03:00
|
|
|
case mozilla::IOInterposeObserver::OpStat:
|
2014-08-13 22:45:37 +04:00
|
|
|
observers = &mObserverLists->mStatObservers;
|
2014-04-09 08:57:52 +04:00
|
|
|
break;
|
2019-11-20 22:58:05 +03:00
|
|
|
case mozilla::IOInterposeObserver::OpClose:
|
2014-08-13 22:45:37 +04:00
|
|
|
observers = &mObserverLists->mCloseObservers;
|
2014-04-09 08:57:52 +04:00
|
|
|
break;
|
2019-11-20 22:58:05 +03:00
|
|
|
case mozilla::IOInterposeObserver::OpNextStage:
|
2014-08-13 22:45:37 +04:00
|
|
|
observers = &mObserverLists->mStageObservers;
|
2014-04-20 00:28:02 +04:00
|
|
|
break;
|
2014-08-13 22:45:37 +04:00
|
|
|
default: {
|
|
|
|
// Invalid IO operation, see documentation comment for
|
|
|
|
// IOInterposer::Report()
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
// Just ignore it in non-debug builds.
|
|
|
|
return;
|
|
|
|
}
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(observers);
|
|
|
|
|
|
|
|
// Inform observers
|
2014-08-13 22:45:37 +04:00
|
|
|
for (auto i = observers->begin(), e = observers->end(); i != e; ++i) {
|
2014-04-09 08:57:52 +04:00
|
|
|
(*i)->Observe(aObservation);
|
|
|
|
}
|
|
|
|
mIsHandlingObservation = false;
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
inline uint32_t GetCurrentGeneration() const { return mCurrentGeneration; }
|
2014-04-09 08:57:52 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
inline bool IsMainThread() const { return mIsMainThread; }
|
2014-04-09 08:57:52 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
inline void SetObserverLists(uint32_t aNewGeneration,
|
2022-06-30 10:30:07 +03:00
|
|
|
RefPtr<const ObserverLists>& aNewLists) {
|
2014-04-09 08:57:52 +04:00
|
|
|
mCurrentGeneration = aNewGeneration;
|
|
|
|
mObserverLists = aNewLists;
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
inline void ClearObserverLists() {
|
2014-04-29 21:29:43 +04:00
|
|
|
if (mObserverLists) {
|
|
|
|
mCurrentGeneration = 0;
|
|
|
|
mObserverLists = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
private:
|
|
|
|
bool mIsMainThread;
|
|
|
|
bool mIsHandlingObservation;
|
|
|
|
uint32_t mCurrentGeneration;
|
2022-06-30 10:30:07 +03:00
|
|
|
RefPtr<const ObserverLists> mObserverLists;
|
2014-04-09 08:57:52 +04:00
|
|
|
};
|
|
|
|
|
2022-04-19 23:39:38 +03:00
|
|
|
// Thread-safe list of observers, from which `PerThreadData` sources its own
|
|
|
|
// local list when needed.
|
|
|
|
class SourceList {
|
2014-04-09 08:57:52 +04:00
|
|
|
public:
|
2022-04-19 23:39:38 +03:00
|
|
|
SourceList()
|
2019-11-20 22:58:05 +03:00
|
|
|
: mObservedOperations(mozilla::IOInterposeObserver::OpNone),
|
|
|
|
mIsEnabled(true) {
|
2022-04-19 23:39:38 +03:00
|
|
|
MOZ_COUNT_CTOR(SourceList);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
|
2022-04-19 23:39:38 +03:00
|
|
|
MOZ_COUNTED_DTOR(SourceList)
|
2014-04-09 08:57:52 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
inline void Disable() { mIsEnabled = false; }
|
2017-01-11 00:31:25 +03:00
|
|
|
inline void Enable() { mIsEnabled = true; }
|
2014-04-09 08:57:52 +04:00
|
|
|
|
2019-11-20 22:58:05 +03:00
|
|
|
void Register(mozilla::IOInterposeObserver::Operation aOp,
|
2022-06-30 10:30:08 +03:00
|
|
|
mozilla::IOInterposeObserver* aStaticObserver) {
|
2019-11-20 22:58:05 +03:00
|
|
|
mozilla::IOInterposer::AutoLock lock(mLock);
|
2014-04-09 08:57:52 +04:00
|
|
|
|
|
|
|
ObserverLists* newLists = nullptr;
|
|
|
|
if (mObserverLists) {
|
|
|
|
newLists = new ObserverLists(*mObserverLists);
|
|
|
|
} else {
|
|
|
|
newLists = new ObserverLists();
|
|
|
|
}
|
|
|
|
// You can register to observe multiple types of observations
|
|
|
|
// but you'll never be registered twice for the same observations.
|
2019-11-20 22:58:05 +03:00
|
|
|
if (aOp & mozilla::IOInterposeObserver::OpCreateOrOpen &&
|
2022-06-30 10:30:08 +03:00
|
|
|
!VectorContains(newLists->mCreateObservers, aStaticObserver)) {
|
|
|
|
newLists->mCreateObservers.push_back(aStaticObserver);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
2019-11-20 22:58:05 +03:00
|
|
|
if (aOp & mozilla::IOInterposeObserver::OpRead &&
|
2022-06-30 10:30:08 +03:00
|
|
|
!VectorContains(newLists->mReadObservers, aStaticObserver)) {
|
|
|
|
newLists->mReadObservers.push_back(aStaticObserver);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
2019-11-20 22:58:05 +03:00
|
|
|
if (aOp & mozilla::IOInterposeObserver::OpWrite &&
|
2022-06-30 10:30:08 +03:00
|
|
|
!VectorContains(newLists->mWriteObservers, aStaticObserver)) {
|
|
|
|
newLists->mWriteObservers.push_back(aStaticObserver);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
2019-11-20 22:58:05 +03:00
|
|
|
if (aOp & mozilla::IOInterposeObserver::OpFSync &&
|
2022-06-30 10:30:08 +03:00
|
|
|
!VectorContains(newLists->mFSyncObservers, aStaticObserver)) {
|
|
|
|
newLists->mFSyncObservers.push_back(aStaticObserver);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
2019-11-20 22:58:05 +03:00
|
|
|
if (aOp & mozilla::IOInterposeObserver::OpStat &&
|
2022-06-30 10:30:08 +03:00
|
|
|
!VectorContains(newLists->mStatObservers, aStaticObserver)) {
|
|
|
|
newLists->mStatObservers.push_back(aStaticObserver);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
2019-11-20 22:58:05 +03:00
|
|
|
if (aOp & mozilla::IOInterposeObserver::OpClose &&
|
2022-06-30 10:30:08 +03:00
|
|
|
!VectorContains(newLists->mCloseObservers, aStaticObserver)) {
|
|
|
|
newLists->mCloseObservers.push_back(aStaticObserver);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
2019-11-20 22:58:05 +03:00
|
|
|
if (aOp & mozilla::IOInterposeObserver::OpNextStage &&
|
2022-06-30 10:30:08 +03:00
|
|
|
!VectorContains(newLists->mStageObservers, aStaticObserver)) {
|
|
|
|
newLists->mStageObservers.push_back(aStaticObserver);
|
2014-04-20 00:28:02 +04:00
|
|
|
}
|
2014-04-09 08:57:52 +04:00
|
|
|
mObserverLists = newLists;
|
2014-08-13 22:45:37 +04:00
|
|
|
mObservedOperations =
|
2019-11-20 22:58:05 +03:00
|
|
|
(mozilla::IOInterposeObserver::Operation)(mObservedOperations | aOp);
|
2014-04-09 08:57:52 +04:00
|
|
|
|
|
|
|
mCurrentGeneration++;
|
|
|
|
}
|
2013-06-14 22:01:02 +04:00
|
|
|
|
2019-11-20 22:58:05 +03:00
|
|
|
void Unregister(mozilla::IOInterposeObserver::Operation aOp,
|
2022-06-30 10:30:08 +03:00
|
|
|
mozilla::IOInterposeObserver* aStaticObserver) {
|
2019-11-20 22:58:05 +03:00
|
|
|
mozilla::IOInterposer::AutoLock lock(mLock);
|
2014-04-09 08:57:52 +04:00
|
|
|
|
|
|
|
ObserverLists* newLists = nullptr;
|
|
|
|
if (mObserverLists) {
|
|
|
|
newLists = new ObserverLists(*mObserverLists);
|
|
|
|
} else {
|
|
|
|
newLists = new ObserverLists();
|
|
|
|
}
|
|
|
|
|
2019-11-20 22:58:05 +03:00
|
|
|
if (aOp & mozilla::IOInterposeObserver::OpCreateOrOpen) {
|
2022-06-30 10:30:08 +03:00
|
|
|
VectorRemove(newLists->mCreateObservers, aStaticObserver);
|
2014-04-09 08:57:52 +04:00
|
|
|
if (newLists->mCreateObservers.empty()) {
|
2021-08-23 12:30:23 +03:00
|
|
|
mObservedOperations = (mozilla::IOInterposeObserver::Operation)(
|
|
|
|
mObservedOperations &
|
|
|
|
~mozilla::IOInterposeObserver::OpCreateOrOpen);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
}
|
2019-11-20 22:58:05 +03:00
|
|
|
if (aOp & mozilla::IOInterposeObserver::OpRead) {
|
2022-06-30 10:30:08 +03:00
|
|
|
VectorRemove(newLists->mReadObservers, aStaticObserver);
|
2014-04-09 08:57:52 +04:00
|
|
|
if (newLists->mReadObservers.empty()) {
|
2021-08-23 12:30:23 +03:00
|
|
|
mObservedOperations = (mozilla::IOInterposeObserver::Operation)(
|
|
|
|
mObservedOperations & ~mozilla::IOInterposeObserver::OpRead);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
}
|
2019-11-20 22:58:05 +03:00
|
|
|
if (aOp & mozilla::IOInterposeObserver::OpWrite) {
|
2022-06-30 10:30:08 +03:00
|
|
|
VectorRemove(newLists->mWriteObservers, aStaticObserver);
|
2014-04-09 08:57:52 +04:00
|
|
|
if (newLists->mWriteObservers.empty()) {
|
2021-08-23 12:30:23 +03:00
|
|
|
mObservedOperations = (mozilla::IOInterposeObserver::Operation)(
|
|
|
|
mObservedOperations & ~mozilla::IOInterposeObserver::OpWrite);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
}
|
2019-11-20 22:58:05 +03:00
|
|
|
if (aOp & mozilla::IOInterposeObserver::OpFSync) {
|
2022-06-30 10:30:08 +03:00
|
|
|
VectorRemove(newLists->mFSyncObservers, aStaticObserver);
|
2014-04-09 08:57:52 +04:00
|
|
|
if (newLists->mFSyncObservers.empty()) {
|
2021-08-23 12:30:23 +03:00
|
|
|
mObservedOperations = (mozilla::IOInterposeObserver::Operation)(
|
|
|
|
mObservedOperations & ~mozilla::IOInterposeObserver::OpFSync);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
}
|
2019-11-20 22:58:05 +03:00
|
|
|
if (aOp & mozilla::IOInterposeObserver::OpStat) {
|
2022-06-30 10:30:08 +03:00
|
|
|
VectorRemove(newLists->mStatObservers, aStaticObserver);
|
2014-04-09 08:57:52 +04:00
|
|
|
if (newLists->mStatObservers.empty()) {
|
2021-08-23 12:30:23 +03:00
|
|
|
mObservedOperations = (mozilla::IOInterposeObserver::Operation)(
|
|
|
|
mObservedOperations & ~mozilla::IOInterposeObserver::OpStat);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
}
|
2019-11-20 22:58:05 +03:00
|
|
|
if (aOp & mozilla::IOInterposeObserver::OpClose) {
|
2022-06-30 10:30:08 +03:00
|
|
|
VectorRemove(newLists->mCloseObservers, aStaticObserver);
|
2014-04-09 08:57:52 +04:00
|
|
|
if (newLists->mCloseObservers.empty()) {
|
2021-08-23 12:30:23 +03:00
|
|
|
mObservedOperations = (mozilla::IOInterposeObserver::Operation)(
|
|
|
|
mObservedOperations & ~mozilla::IOInterposeObserver::OpClose);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
}
|
2019-11-20 22:58:05 +03:00
|
|
|
if (aOp & mozilla::IOInterposeObserver::OpNextStage) {
|
2022-06-30 10:30:08 +03:00
|
|
|
VectorRemove(newLists->mStageObservers, aStaticObserver);
|
2014-04-20 00:28:02 +04:00
|
|
|
if (newLists->mStageObservers.empty()) {
|
2021-08-23 12:30:23 +03:00
|
|
|
mObservedOperations = (mozilla::IOInterposeObserver::Operation)(
|
|
|
|
mObservedOperations & ~mozilla::IOInterposeObserver::OpNextStage);
|
2014-04-20 00:28:02 +04:00
|
|
|
}
|
|
|
|
}
|
2014-04-09 08:57:52 +04:00
|
|
|
mObserverLists = newLists;
|
|
|
|
mCurrentGeneration++;
|
|
|
|
}
|
2014-04-29 21:29:43 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
void Update(PerThreadData& aPtd) {
|
2014-04-09 08:57:52 +04:00
|
|
|
if (mCurrentGeneration == aPtd.GetCurrentGeneration()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// If the generation counts don't match then we need to update the current
|
2022-04-19 23:39:38 +03:00
|
|
|
// thread's observer list with the new source list.
|
2019-11-20 22:58:05 +03:00
|
|
|
mozilla::IOInterposer::AutoLock lock(mLock);
|
2014-04-09 08:57:52 +04:00
|
|
|
aPtd.SetObserverLists(mCurrentGeneration, mObserverLists);
|
|
|
|
}
|
|
|
|
|
2019-11-20 22:58:05 +03:00
|
|
|
inline bool IsObservedOperation(mozilla::IOInterposeObserver::Operation aOp) {
|
2020-04-30 20:17:21 +03:00
|
|
|
// This does not occur inside of a lock, so this makes no guarantees that
|
|
|
|
// the observers are in sync with this. That is acceptable; it is not a
|
|
|
|
// problem if we occasionally report more or less IO than is actually
|
|
|
|
// occurring.
|
2014-04-09 08:57:52 +04:00
|
|
|
return mIsEnabled && !!(mObservedOperations & aOp);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-08-03 19:39:41 +03:00
|
|
|
RefPtr<const ObserverLists> mObserverLists MOZ_GUARDED_BY(mLock);
|
2014-04-09 08:57:52 +04:00
|
|
|
// Note, we cannot use mozilla::Mutex here as the ObserverLists may be leaked
|
|
|
|
// (We want to monitor IO during shutdown). Furthermore, as we may have to
|
|
|
|
// unregister observers during shutdown an OffTheBooksMutex is not an option
|
|
|
|
// either, as its base calls into sDeadlockDetector which may be nullptr
|
|
|
|
// during shutdown.
|
2022-04-05 01:55:21 +03:00
|
|
|
mozilla::IOInterposer::Mutex mLock;
|
2014-04-09 08:57:52 +04:00
|
|
|
// Flags tracking which operations are being observed
|
2020-04-30 20:17:21 +03:00
|
|
|
mozilla::Atomic<mozilla::IOInterposeObserver::Operation,
|
|
|
|
mozilla::MemoryOrdering::Relaxed>
|
|
|
|
mObservedOperations;
|
2014-04-09 08:57:52 +04:00
|
|
|
// Used for quickly disabling everything by IOInterposer::Disable()
|
2019-11-20 22:58:05 +03:00
|
|
|
mozilla::Atomic<bool> mIsEnabled;
|
2022-04-19 23:39:38 +03:00
|
|
|
// Used to inform threads that the source observer list has changed
|
2019-11-20 22:58:05 +03:00
|
|
|
mozilla::Atomic<uint32_t> mCurrentGeneration;
|
2014-04-09 08:57:52 +04:00
|
|
|
};
|
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
// Special observation used by IOInterposer::EnteringNextStage()
|
2019-11-20 22:58:05 +03:00
|
|
|
class NextStageObservation : public mozilla::IOInterposeObserver::Observation {
|
2014-04-20 00:28:02 +04:00
|
|
|
public:
|
|
|
|
NextStageObservation()
|
2019-11-20 22:58:05 +03:00
|
|
|
: mozilla::IOInterposeObserver::Observation(
|
|
|
|
mozilla::IOInterposeObserver::OpNextStage, "IOInterposer", false) {
|
|
|
|
mStart = mozilla::TimeStamp::Now();
|
2014-08-19 02:42:14 +04:00
|
|
|
mEnd = mStart;
|
2014-04-20 00:28:02 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
// List of observers registered
|
2022-04-19 23:39:38 +03:00
|
|
|
static mozilla::StaticAutoPtr<SourceList> sSourceList;
|
2015-11-23 22:11:22 +03:00
|
|
|
static MOZ_THREAD_LOCAL(PerThreadData*) sThreadLocalData;
|
|
|
|
static bool sThreadLocalDataInitialized;
|
2019-11-20 22:58:05 +03:00
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace mozilla {
|
2013-09-12 18:47:37 +04:00
|
|
|
|
2014-02-01 07:14:03 +04:00
|
|
|
IOInterposeObserver::Observation::Observation(Operation aOperation,
|
|
|
|
const char* aReference,
|
|
|
|
bool aShouldReport)
|
|
|
|
: mOperation(aOperation),
|
|
|
|
mReference(aReference),
|
|
|
|
mShouldReport(IOInterposer::IsObservedOperation(aOperation) &&
|
|
|
|
aShouldReport) {
|
|
|
|
if (mShouldReport) {
|
|
|
|
mStart = TimeStamp::Now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IOInterposeObserver::Observation::Observation(Operation aOperation,
|
|
|
|
const TimeStamp& aStart,
|
|
|
|
const TimeStamp& aEnd,
|
|
|
|
const char* aReference)
|
|
|
|
: mOperation(aOperation),
|
|
|
|
mStart(aStart),
|
|
|
|
mEnd(aEnd),
|
|
|
|
mReference(aReference),
|
|
|
|
mShouldReport(false) {}
|
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
const char* IOInterposeObserver::Observation::ObservedOperationString() const {
|
2014-08-13 22:45:37 +04:00
|
|
|
switch (mOperation) {
|
2014-04-20 00:28:02 +04:00
|
|
|
case OpCreateOrOpen:
|
|
|
|
return "create/open";
|
|
|
|
case OpRead:
|
|
|
|
return "read";
|
|
|
|
case OpWrite:
|
|
|
|
return "write";
|
|
|
|
case OpFSync:
|
|
|
|
return "fsync";
|
|
|
|
case OpStat:
|
|
|
|
return "stat";
|
|
|
|
case OpClose:
|
|
|
|
return "close";
|
|
|
|
case OpNextStage:
|
|
|
|
return "NextStage";
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-01 07:14:03 +04:00
|
|
|
void IOInterposeObserver::Observation::Report() {
|
|
|
|
if (mShouldReport) {
|
|
|
|
mEnd = TimeStamp::Now();
|
|
|
|
IOInterposer::Report(*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
bool IOInterposer::Init() {
|
2013-09-12 18:47:37 +04:00
|
|
|
// Don't initialize twice...
|
2022-04-19 23:39:38 +03:00
|
|
|
if (sSourceList) {
|
2014-04-09 08:57:52 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!sThreadLocalData.init()) {
|
|
|
|
return false;
|
2013-06-14 22:01:02 +04:00
|
|
|
}
|
2015-11-23 22:11:22 +03:00
|
|
|
sThreadLocalDataInitialized = true;
|
2014-04-09 08:57:52 +04:00
|
|
|
bool isMainThread = true;
|
|
|
|
RegisterCurrentThread(isMainThread);
|
2022-04-19 23:39:38 +03:00
|
|
|
sSourceList = new SourceList();
|
2014-04-09 08:57:52 +04:00
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
MainThreadIOLogger::Init();
|
|
|
|
|
2014-03-15 02:35:01 +04:00
|
|
|
// Now we initialize the various interposers depending on platform
|
2019-03-01 22:34:54 +03:00
|
|
|
|
|
|
|
// Under certain conditions it may be unsafe to initialize PoisonIOInterposer,
|
|
|
|
// such as when a background thread is already running. We set this variable
|
|
|
|
// elsewhere when such a condition applies.
|
|
|
|
if (!PR_GetEnv("MOZ_DISABLE_POISON_IO_INTERPOSER")) {
|
|
|
|
InitPoisonIOInterposer();
|
|
|
|
}
|
|
|
|
|
2014-03-15 02:35:01 +04:00
|
|
|
// We don't hook NSPR on Windows because PoisonIOInterposer captures a
|
|
|
|
// superset of the former's events.
|
|
|
|
#if !defined(XP_WIN)
|
|
|
|
InitNSPRIOInterposing();
|
|
|
|
#endif
|
2014-04-09 08:57:52 +04:00
|
|
|
return true;
|
2014-03-15 02:35:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IOInterposeObserver::IsMainThread() {
|
2015-11-23 22:11:22 +03:00
|
|
|
if (!sThreadLocalDataInitialized) {
|
2014-04-09 08:57:52 +04:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-13 22:45:37 +04:00
|
|
|
PerThreadData* ptd = sThreadLocalData.get();
|
2014-04-09 08:57:52 +04:00
|
|
|
if (!ptd) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return ptd->IsMainThread();
|
2013-06-14 22:01:02 +04:00
|
|
|
}
|
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
void IOInterposer::Clear() {
|
2015-07-31 22:11:48 +03:00
|
|
|
/* Clear() is a no-op on release builds so that we may continue to trap I/O
|
|
|
|
until process termination. In leak-checking builds, we need to shut down
|
|
|
|
IOInterposer so that all references are properly released. */
|
|
|
|
#ifdef NS_FREE_PERMANENT_DATA
|
2014-04-29 21:29:43 +04:00
|
|
|
UnregisterCurrentThread();
|
2022-04-19 23:39:38 +03:00
|
|
|
sSourceList = nullptr;
|
2014-04-29 21:29:43 +04:00
|
|
|
#endif
|
2013-06-14 22:01:02 +04:00
|
|
|
}
|
|
|
|
|
2014-03-15 10:10:39 +04:00
|
|
|
void IOInterposer::Disable() {
|
2022-04-19 23:39:38 +03:00
|
|
|
if (!sSourceList) {
|
2014-03-15 10:10:39 +04:00
|
|
|
return;
|
|
|
|
}
|
2022-04-19 23:39:38 +03:00
|
|
|
sSourceList->Disable();
|
2014-03-15 10:10:39 +04:00
|
|
|
}
|
|
|
|
|
2017-01-11 00:31:25 +03:00
|
|
|
void IOInterposer::Enable() {
|
2022-04-19 23:39:38 +03:00
|
|
|
if (!sSourceList) {
|
2017-01-11 00:31:25 +03:00
|
|
|
return;
|
|
|
|
}
|
2022-04-19 23:39:38 +03:00
|
|
|
sSourceList->Enable();
|
2017-01-11 00:31:25 +03:00
|
|
|
}
|
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
void IOInterposer::Report(IOInterposeObserver::Observation& aObservation) {
|
|
|
|
PerThreadData* ptd = sThreadLocalData.get();
|
|
|
|
if (!ptd) {
|
|
|
|
// In this case the current thread is not registered with IOInterposer.
|
|
|
|
// Alternatively we could take the slow path and just lock everything if
|
|
|
|
// we're not registered. That could potentially perform poorly, though.
|
2013-09-12 18:47:37 +04:00
|
|
|
return;
|
2013-06-14 22:01:02 +04:00
|
|
|
}
|
2014-04-29 21:29:43 +04:00
|
|
|
|
2022-04-19 23:39:38 +03:00
|
|
|
if (!sSourceList) {
|
|
|
|
// If there is no longer a source list then we should clear the local one.
|
2014-04-29 21:29:43 +04:00
|
|
|
ptd->ClearObserverLists();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-19 23:39:38 +03:00
|
|
|
sSourceList->Update(*ptd);
|
2013-06-14 22:01:02 +04:00
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
// Don't try to report if there's nobody listening.
|
|
|
|
if (!IOInterposer::IsObservedOperation(aObservation.ObservedOperation())) {
|
|
|
|
return;
|
2013-09-12 18:47:37 +04:00
|
|
|
}
|
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
ptd->CallObservers(aObservation);
|
2013-09-12 18:47:37 +04:00
|
|
|
}
|
|
|
|
|
2014-03-15 10:10:39 +04:00
|
|
|
bool IOInterposer::IsObservedOperation(IOInterposeObserver::Operation aOp) {
|
2022-04-19 23:39:38 +03:00
|
|
|
return sSourceList && sSourceList->IsObservedOperation(aOp);
|
2014-03-15 10:10:39 +04:00
|
|
|
}
|
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
void IOInterposer::Register(IOInterposeObserver::Operation aOp,
|
2022-06-30 10:30:08 +03:00
|
|
|
IOInterposeObserver* aStaticObserver) {
|
|
|
|
MOZ_ASSERT(aStaticObserver);
|
|
|
|
if (!sSourceList || !aStaticObserver) {
|
2013-09-12 18:47:37 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-30 10:30:08 +03:00
|
|
|
sSourceList->Register(aOp, aStaticObserver);
|
2013-06-14 22:01:02 +04:00
|
|
|
}
|
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
void IOInterposer::Unregister(IOInterposeObserver::Operation aOp,
|
2022-06-30 10:30:08 +03:00
|
|
|
IOInterposeObserver* aStaticObserver) {
|
2022-04-19 23:39:38 +03:00
|
|
|
if (!sSourceList) {
|
2013-09-12 18:47:37 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-30 10:30:08 +03:00
|
|
|
sSourceList->Unregister(aOp, aStaticObserver);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
2013-09-12 18:47:37 +04:00
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
void IOInterposer::RegisterCurrentThread(bool aIsMainThread) {
|
2015-11-23 22:11:22 +03:00
|
|
|
if (!sThreadLocalDataInitialized) {
|
2014-04-09 08:57:52 +04:00
|
|
|
return;
|
2014-01-21 03:02:51 +04:00
|
|
|
}
|
2014-04-09 08:57:52 +04:00
|
|
|
MOZ_ASSERT(!sThreadLocalData.get());
|
|
|
|
PerThreadData* curThreadData = new PerThreadData(aIsMainThread);
|
|
|
|
sThreadLocalData.set(curThreadData);
|
2013-09-12 18:47:37 +04:00
|
|
|
}
|
2014-03-15 02:35:01 +04:00
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
void IOInterposer::UnregisterCurrentThread() {
|
2015-11-23 22:11:22 +03:00
|
|
|
if (!sThreadLocalDataInitialized) {
|
2014-03-15 02:35:01 +04:00
|
|
|
return;
|
|
|
|
}
|
2021-05-05 20:00:09 +03:00
|
|
|
if (PerThreadData* curThreadData = sThreadLocalData.get()) {
|
|
|
|
sThreadLocalData.set(nullptr);
|
|
|
|
delete curThreadData;
|
|
|
|
}
|
2014-03-15 02:35:01 +04:00
|
|
|
}
|
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
void IOInterposer::EnteringNextStage() {
|
2022-04-19 23:39:38 +03:00
|
|
|
if (!sSourceList) {
|
2014-04-20 00:28:02 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
NextStageObservation observation;
|
|
|
|
Report(observation);
|
|
|
|
}
|
2019-11-20 22:58:05 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|