2013-11-05 16:45:20 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim:set ts=4 sw=4 sts=4 ci et: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "PoisonIOInterposer.h"
|
|
|
|
#include "mach_override.h"
|
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2013-11-05 16:45:20 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
#include "mozilla/IOInterposer.h"
|
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
#include "mozilla/ProcessedStack.h"
|
|
|
|
#include "mozilla/Scoped.h"
|
|
|
|
#include "mozilla/Telemetry.h"
|
|
|
|
#include "nsPrintfCString.h"
|
|
|
|
#include "nsStackWalk.h"
|
2014-02-27 01:36:36 +04:00
|
|
|
#include "nsTraceRefcnt.h"
|
2013-11-05 16:45:20 +04:00
|
|
|
#include "plstr.h"
|
|
|
|
#include "prio.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2014-02-01 07:14:03 +04:00
|
|
|
#include <vector>
|
2013-11-05 16:45:20 +04:00
|
|
|
|
2014-02-01 07:14:03 +04:00
|
|
|
#include <sys/param.h>
|
2013-11-05 16:45:20 +04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <aio.h>
|
|
|
|
#include <dlfcn.h>
|
2014-02-01 07:14:03 +04:00
|
|
|
#include <fcntl.h>
|
2013-11-05 16:45:20 +04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
// Bit tracking if poisoned writes are enabled
|
|
|
|
static bool sIsEnabled = false;
|
|
|
|
|
|
|
|
// Check if writes are dirty before reporting IO
|
|
|
|
static bool sOnlyReportDirtyWrites = false;
|
|
|
|
|
|
|
|
// Routines for write validation
|
2014-08-13 22:45:37 +04:00
|
|
|
bool IsValidWrite(int aFd, const void* aWbuf, size_t aCount);
|
|
|
|
bool IsIPCWrite(int aFd, const struct stat& aBuf);
|
2013-11-05 16:45:20 +04:00
|
|
|
|
|
|
|
/******************************** IO AutoTimer ********************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RAII class for timing the duration of an I/O call and reporting the result
|
|
|
|
* to the IOInterposeObserver API.
|
|
|
|
*/
|
|
|
|
class MacIOAutoObservation : public IOInterposeObserver::Observation
|
|
|
|
{
|
|
|
|
public:
|
2014-02-01 07:14:03 +04:00
|
|
|
MacIOAutoObservation(IOInterposeObserver::Operation aOp, int aFd)
|
|
|
|
: IOInterposeObserver::Observation(aOp, sReference, sIsEnabled &&
|
|
|
|
!IsDebugFile(aFd))
|
|
|
|
, mFd(aFd)
|
|
|
|
, mHasQueriedFilename(false)
|
|
|
|
, mFilename(nullptr)
|
2013-11-05 16:45:20 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-01 07:14:03 +04:00
|
|
|
MacIOAutoObservation(IOInterposeObserver::Operation aOp, int aFd,
|
2014-08-13 22:45:37 +04:00
|
|
|
const void* aBuf, size_t aCount)
|
2014-02-01 07:14:03 +04:00
|
|
|
: IOInterposeObserver::Observation(aOp, sReference, sIsEnabled &&
|
|
|
|
!IsDebugFile(aFd) &&
|
|
|
|
IsValidWrite(aFd, aBuf, aCount))
|
|
|
|
, mFd(aFd)
|
|
|
|
, mHasQueriedFilename(false)
|
|
|
|
, mFilename(nullptr)
|
2013-11-05 16:45:20 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-01 07:14:03 +04:00
|
|
|
// Custom implementation of IOInterposeObserver::Observation::Filename
|
|
|
|
const char16_t* Filename() MOZ_OVERRIDE;
|
|
|
|
|
2013-11-05 16:45:20 +04:00
|
|
|
~MacIOAutoObservation()
|
|
|
|
{
|
2014-02-01 07:14:03 +04:00
|
|
|
Report();
|
|
|
|
if (mFilename) {
|
|
|
|
NS_Free(mFilename);
|
|
|
|
mFilename = nullptr;
|
2013-11-05 16:45:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-02-01 07:14:03 +04:00
|
|
|
int mFd;
|
|
|
|
bool mHasQueriedFilename;
|
|
|
|
char16_t* mFilename;
|
|
|
|
static const char* sReference;
|
2013-11-05 16:45:20 +04:00
|
|
|
};
|
|
|
|
|
2014-02-01 07:14:03 +04:00
|
|
|
const char* MacIOAutoObservation::sReference = "PoisonIOInterposer";
|
|
|
|
|
|
|
|
// Get filename for this observation
|
2014-08-13 22:45:37 +04:00
|
|
|
const char16_t*
|
|
|
|
MacIOAutoObservation::Filename()
|
2014-02-01 07:14:03 +04:00
|
|
|
{
|
|
|
|
// If mHasQueriedFilename is true, then we already have it
|
|
|
|
if (mHasQueriedFilename) {
|
|
|
|
return mFilename;
|
|
|
|
}
|
|
|
|
char filename[MAXPATHLEN];
|
|
|
|
if (fcntl(mFd, F_GETPATH, filename) != -1) {
|
|
|
|
mFilename = UTF8ToNewUnicode(nsDependentCString(filename));
|
|
|
|
} else {
|
|
|
|
mFilename = nullptr;
|
|
|
|
}
|
|
|
|
mHasQueriedFilename = true;
|
|
|
|
|
|
|
|
// Return filename
|
|
|
|
return mFilename;
|
|
|
|
}
|
|
|
|
|
2013-11-05 16:45:20 +04:00
|
|
|
/****************************** Write Validation ******************************/
|
|
|
|
|
|
|
|
// We want to detect "actual" writes, not IPC. Some IPC mechanisms are
|
|
|
|
// implemented with file descriptors, so filter them out.
|
2014-08-13 22:45:37 +04:00
|
|
|
bool
|
|
|
|
IsIPCWrite(int aFd, const struct stat& aBuf)
|
|
|
|
{
|
|
|
|
if ((aBuf.st_mode & S_IFMT) == S_IFIFO) {
|
2013-11-05 16:45:20 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
if ((aBuf.st_mode & S_IFMT) != S_IFSOCK) {
|
2013-11-05 16:45:20 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sockaddr_storage address;
|
|
|
|
socklen_t len = sizeof(address);
|
2014-08-13 22:45:37 +04:00
|
|
|
if (getsockname(aFd, (sockaddr*)&address, &len) != 0) {
|
|
|
|
return true; // Ignore the aFd if we can't find what it is.
|
2013-11-05 16:45:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return address.ss_family == AF_UNIX;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We want to report actual disk IO not things that don't move bits on the disk
|
2014-08-13 22:45:37 +04:00
|
|
|
bool
|
|
|
|
IsValidWrite(int aFd, const void* aWbuf, size_t aCount)
|
2013-11-05 16:45:20 +04:00
|
|
|
{
|
|
|
|
// Ignore writes of zero bytes, Firefox does some during shutdown.
|
2014-08-13 22:45:37 +04:00
|
|
|
if (aCount == 0) {
|
2013-11-05 16:45:20 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
struct stat buf;
|
2014-08-13 22:45:37 +04:00
|
|
|
int rv = fstat(aFd, &buf);
|
2013-11-05 16:45:20 +04:00
|
|
|
if (rv != 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
if (IsIPCWrite(aFd, buf)) {
|
2013-11-05 16:45:20 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
// For writev we pass a nullptr aWbuf. We should only get here from
|
|
|
|
// dbm, and it uses write, so assert that we have aWbuf.
|
|
|
|
if (!aWbuf) {
|
2013-11-05 16:45:20 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Break, here if we're allowed to report non-dirty writes
|
2014-08-13 22:45:37 +04:00
|
|
|
if (!sOnlyReportDirtyWrites) {
|
2013-11-05 16:45:20 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// As a really bad hack, accept writes that don't change the on disk
|
|
|
|
// content. This is needed because dbm doesn't keep track of dirty bits
|
|
|
|
// and can end up writing the same data to disk twice. Once when the
|
|
|
|
// user (nss) asks it to sync and once when closing the database.
|
2014-08-13 22:45:37 +04:00
|
|
|
ScopedFreePtr<void> wbuf2(malloc(aCount));
|
2013-11-05 16:45:20 +04:00
|
|
|
if (!wbuf2) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-13 22:45:37 +04:00
|
|
|
off_t pos = lseek(aFd, 0, SEEK_CUR);
|
2013-11-05 16:45:20 +04:00
|
|
|
if (pos == -1) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-13 22:45:37 +04:00
|
|
|
ssize_t r = read(aFd, wbuf2, aCount);
|
|
|
|
if (r < 0 || (size_t)r != aCount) {
|
2013-11-05 16:45:20 +04:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-13 22:45:37 +04:00
|
|
|
int cmp = memcmp(aWbuf, wbuf2, aCount);
|
2013-11-05 16:45:20 +04:00
|
|
|
if (cmp != 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-13 22:45:37 +04:00
|
|
|
off_t pos2 = lseek(aFd, pos, SEEK_SET);
|
2013-11-05 16:45:20 +04:00
|
|
|
if (pos2 != pos) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise this is not a valid write
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************** Function Interception ***************************/
|
|
|
|
|
|
|
|
/** Structure for declaration of function override */
|
2014-08-13 22:45:37 +04:00
|
|
|
struct FuncData
|
|
|
|
{
|
|
|
|
const char* Name; // Name of the function for the ones we use dlsym
|
|
|
|
const void* Wrapper; // The function that we will replace 'Function' with
|
|
|
|
void* Function; // The function that will be replaced with 'Wrapper'
|
|
|
|
void* Buffer; // Will point to the jump buffer that lets us call
|
2013-11-05 16:45:20 +04:00
|
|
|
// 'Function' after it has been replaced.
|
|
|
|
};
|
|
|
|
|
|
|
|
// Wrap aio_write. We have not seen it before, so just assert/report it.
|
2014-08-13 22:45:37 +04:00
|
|
|
typedef ssize_t (*aio_write_t)(struct aiocb* aAioCbp);
|
|
|
|
ssize_t wrap_aio_write(struct aiocb* aAioCbp);
|
|
|
|
FuncData aio_write_data = { 0, (void*)wrap_aio_write, (void*)aio_write };
|
|
|
|
ssize_t
|
|
|
|
wrap_aio_write(struct aiocb* aAioCbp)
|
|
|
|
{
|
|
|
|
MacIOAutoObservation timer(IOInterposeObserver::OpWrite,
|
|
|
|
aAioCbp->aio_fildes);
|
|
|
|
|
|
|
|
aio_write_t old_write = (aio_write_t)aio_write_data.Buffer;
|
|
|
|
return old_write(aAioCbp);
|
2013-11-05 16:45:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap pwrite-like functions.
|
|
|
|
// We have not seen them before, so just assert/report it.
|
2014-08-13 22:45:37 +04:00
|
|
|
typedef ssize_t (*pwrite_t)(int aFd, const void* buf, size_t aNumBytes,
|
|
|
|
off_t aOffset);
|
|
|
|
template<FuncData& foo>
|
|
|
|
ssize_t
|
|
|
|
wrap_pwrite_temp(int aFd, const void* aBuf, size_t aNumBytes, off_t aOffset)
|
|
|
|
{
|
|
|
|
MacIOAutoObservation timer(IOInterposeObserver::OpWrite, aFd);
|
|
|
|
pwrite_t old_write = (pwrite_t)foo.Buffer;
|
|
|
|
return old_write(aFd, aBuf, aNumBytes, aOffset);
|
2013-11-05 16:45:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Define a FuncData for a pwrite-like functions.
|
|
|
|
#define DEFINE_PWRITE_DATA(X, NAME) \
|
|
|
|
FuncData X ## _data = { NAME, (void*) wrap_pwrite_temp<X ## _data> }; \
|
|
|
|
|
|
|
|
// This exists everywhere.
|
|
|
|
DEFINE_PWRITE_DATA(pwrite, "pwrite")
|
|
|
|
// These exist on 32 bit OS X
|
|
|
|
DEFINE_PWRITE_DATA(pwrite_NOCANCEL_UNIX2003, "pwrite$NOCANCEL$UNIX2003");
|
|
|
|
DEFINE_PWRITE_DATA(pwrite_UNIX2003, "pwrite$UNIX2003");
|
|
|
|
// This exists on 64 bit OS X
|
|
|
|
DEFINE_PWRITE_DATA(pwrite_NOCANCEL, "pwrite$NOCANCEL");
|
|
|
|
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
typedef ssize_t (*writev_t)(int aFd, const struct iovec* aIov, int aIovCount);
|
|
|
|
template<FuncData& foo>
|
|
|
|
ssize_t
|
|
|
|
wrap_writev_temp(int aFd, const struct iovec* aIov, int aIovCount)
|
|
|
|
{
|
|
|
|
MacIOAutoObservation timer(IOInterposeObserver::OpWrite, aFd, nullptr,
|
|
|
|
aIovCount);
|
|
|
|
writev_t old_write = (writev_t)foo.Buffer;
|
|
|
|
return old_write(aFd, aIov, aIovCount);
|
2013-11-05 16:45:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Define a FuncData for a writev-like functions.
|
|
|
|
#define DEFINE_WRITEV_DATA(X, NAME) \
|
|
|
|
FuncData X ## _data = { NAME, (void*) wrap_writev_temp<X ## _data> }; \
|
|
|
|
|
|
|
|
// This exists everywhere.
|
|
|
|
DEFINE_WRITEV_DATA(writev, "writev");
|
|
|
|
// These exist on 32 bit OS X
|
|
|
|
DEFINE_WRITEV_DATA(writev_NOCANCEL_UNIX2003, "writev$NOCANCEL$UNIX2003");
|
|
|
|
DEFINE_WRITEV_DATA(writev_UNIX2003, "writev$UNIX2003");
|
|
|
|
// This exists on 64 bit OS X
|
|
|
|
DEFINE_WRITEV_DATA(writev_NOCANCEL, "writev$NOCANCEL");
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
typedef ssize_t (*write_t)(int aFd, const void* aBuf, size_t aCount);
|
|
|
|
template<FuncData& foo>
|
|
|
|
ssize_t
|
|
|
|
wrap_write_temp(int aFd, const void* aBuf, size_t aCount)
|
|
|
|
{
|
|
|
|
MacIOAutoObservation timer(IOInterposeObserver::OpWrite, aFd, aBuf, aCount);
|
|
|
|
write_t old_write = (write_t)foo.Buffer;
|
|
|
|
return old_write(aFd, aBuf, aCount);
|
2013-11-05 16:45:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Define a FuncData for a write-like functions.
|
|
|
|
#define DEFINE_WRITE_DATA(X, NAME) \
|
|
|
|
FuncData X ## _data = { NAME, (void*) wrap_write_temp<X ## _data> }; \
|
|
|
|
|
|
|
|
// This exists everywhere.
|
|
|
|
DEFINE_WRITE_DATA(write, "write");
|
|
|
|
// These exist on 32 bit OS X
|
|
|
|
DEFINE_WRITE_DATA(write_NOCANCEL_UNIX2003, "write$NOCANCEL$UNIX2003");
|
|
|
|
DEFINE_WRITE_DATA(write_UNIX2003, "write$UNIX2003");
|
|
|
|
// This exists on 64 bit OS X
|
|
|
|
DEFINE_WRITE_DATA(write_NOCANCEL, "write$NOCANCEL");
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
FuncData* Functions[] = {
|
|
|
|
&aio_write_data,
|
2013-11-05 16:45:20 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
&pwrite_data,
|
|
|
|
&pwrite_NOCANCEL_UNIX2003_data,
|
|
|
|
&pwrite_UNIX2003_data,
|
|
|
|
&pwrite_NOCANCEL_data,
|
2013-11-05 16:45:20 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
&write_data,
|
|
|
|
&write_NOCANCEL_UNIX2003_data,
|
|
|
|
&write_UNIX2003_data,
|
|
|
|
&write_NOCANCEL_data,
|
2013-11-05 16:45:20 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
&writev_data,
|
|
|
|
&writev_NOCANCEL_UNIX2003_data,
|
|
|
|
&writev_UNIX2003_data,
|
|
|
|
&writev_NOCANCEL_data
|
|
|
|
};
|
2013-11-05 16:45:20 +04:00
|
|
|
|
|
|
|
const int NumFunctions = ArrayLength(Functions);
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
/******************************** IO Poisoning ********************************/
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
void
|
|
|
|
InitPoisonIOInterposer()
|
|
|
|
{
|
2013-11-05 16:45:20 +04:00
|
|
|
// Enable reporting from poisoned write methods
|
|
|
|
sIsEnabled = true;
|
|
|
|
|
|
|
|
// Make sure we only poison writes once!
|
|
|
|
static bool WritesArePoisoned = false;
|
|
|
|
if (WritesArePoisoned) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
WritesArePoisoned = true;
|
|
|
|
|
|
|
|
// stdout and stderr are OK.
|
|
|
|
MozillaRegisterDebugFD(1);
|
|
|
|
MozillaRegisterDebugFD(2);
|
|
|
|
|
|
|
|
for (int i = 0; i < NumFunctions; ++i) {
|
2014-08-13 22:45:37 +04:00
|
|
|
FuncData* d = Functions[i];
|
2013-11-05 16:45:20 +04:00
|
|
|
if (!d->Function) {
|
|
|
|
d->Function = dlsym(RTLD_DEFAULT, d->Name);
|
|
|
|
}
|
|
|
|
if (!d->Function) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
DebugOnly<mach_error_t> t = mach_override_ptr(d->Function, d->Wrapper,
|
2014-08-13 22:45:37 +04:00
|
|
|
&d->Buffer);
|
2013-11-05 16:45:20 +04:00
|
|
|
MOZ_ASSERT(t == err_none);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
void
|
|
|
|
OnlyReportDirtyWrites()
|
|
|
|
{
|
2013-11-05 16:45:20 +04:00
|
|
|
sOnlyReportDirtyWrites = true;
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
void
|
|
|
|
ClearPoisonIOInterposer()
|
|
|
|
{
|
2013-11-05 16:45:20 +04:00
|
|
|
// Not sure how or if we can unpoison the functions. Would be nice, but no
|
|
|
|
// worries we won't need to do this anyway.
|
|
|
|
sIsEnabled = false;
|
|
|
|
}
|
|
|
|
|
2013-11-08 08:13:20 +04:00
|
|
|
} // namespace mozilla
|