Files
palemoon27/xpcom/build/PoisonIOInterposerWin.cpp
roytam1 f617a91abb import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1167633 - Remove SpiderMonkey specific JS syntax from App Manager / WebIDE. r=pbrosset (882d61d26)
- missing parts of Bug 932179 - Part 1: Expose security information in the WebConsoleActor. r=past (202d33c93)
- Bug 1106470 - Drop SSLv3 support from devtools. r=vporof (4d6f99643)
- Bug 955933 - Allow copying the network response string. r=vporof (0f1a79fe1)
- Follow-up nit to bug 1147648 (alphabetize prefs). r=kats (e251ec2e5)
- Bug 1155252 * -WError fix (2d4c016d2)
- Disable layers acceleration on Windows if the last startup crashed during driver initialization. (bug 1168935, r=mattwoodrow) (cb4769ace)
- Add telemetry for reporting graphics driver startup states. (bug 1168935 part 2, r=vdjeric,mattwoodrow) (a7a605d93)
- Bug 1150715 - Implement "Copy Request/Response Headers" context menu items. r=jsantell (0eabd68f7)
- part of Bug 1102219 - Part 4: Replace  with  in chrome code. r=till (dab16f660)
- Bug 1150717 - Implement "Copy URL Parameters" context menu item. r=bgrins (f20e86e8f)
- Bug 1158046 - Add access keys to all context menu items in the Network panel. r=bgrins r=jsantell (71fd1e050)
- Remove static vars from DriverInitCrashDetection. (bug 1183910 part 1, r=mattwoodrow) (29ea344ce)
- Bug 1151541, part 3 - Fix leading tabs in xpcom/. r=froydnj (eb1fa5c14)
- Bug 1151541, part 1 - Add MPL2 to StaticMutex.h and stub_test.cpp. r=froydnj (bf00b19ee)
- Bug 1140760 - Move TestXPIDLString.cpp to gtest and enable it; r=froydnj (7bea14ce1)
- Bug 1140762 - Move TestThreads.cpp to gtest and enable it; r=froydnj (93a007768)
- Bug 1151541, part 2 - Fix mode lines in xpcom/. r=froydnj (0ac8fd176)
- Bug 1159751: Ensure WARP can never be used for Windows 7. r=milan (ad4a84b10)
- Bug 1178426. Add GfxInfo to ServicesList.h. r=nfroyd (8e09c6349)
- Bug 1158144 - Fix "Copy URL Parameters" line separation on Windows. r=vporof (d7359d740)
- Bug 1158144 - Implement "Copy POST Data" context menu item. r=vporof (adddce05a)
- Bug 1127423 - Don't scroll horizontally when selecting an element in markup view;r=jryans (3256aa5eb)
- Bug 1159938 - Remove dead nodeChanged() function;r=pbrosset (213880754)
- Bug 1153022 - Add trimOutput option to inplace editor;r=pbrosset (52228252a)
- Bug 1120111 - Adds key listeners to highlighter pick mode to traverse DOM and submit/cancel selection; r=pbrosset (40f6e8e33)
- Bug 1120111 - Tests for the highlighter pick mode key listeners; r=pbrosset (6c9ff1c95)
- Bug 988278 - Fixes ESCape keypress mess in the inspector to make sure the split console opens; r=miker (d915ca7ae)
- Bug 1153635 - Focus first focusable element in MarkupContainer when line is clicked; r=bgrins (f7b231d4c)
- Bug 1158781 - Make sure attributes gets focused when clicked in the inspector. r=bgrins (d6e1521f3)
- Bug 966787 - 1 - Code cleanup in breadcrumbs.js; r=bgrins For a better consistency with the rest of the DevTools codebase. (867a768c6)
- fix imports again (180eb1ac7)
- Bug 966787 - 2 - Skip inspector breadcrumb updates when the output doesn't change; r=bgrins (5bddce972)
- Bug 892935 - Auto-expand elements with only text in the markup view;r=pbrosset,r=bgrins (6dddecd01)
2021-06-15 11:33:14 +08:00

502 lines
16 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 <algorithm>
#include <stdio.h>
#include <vector>
#include <io.h>
#include <windows.h>
#include <winternl.h>
#include "mozilla/Assertions.h"
#include "mozilla/FileUtilsWin.h"
#include "mozilla/IOInterposer.h"
#include "mozilla/Mutex.h"
#include "mozilla/TimeStamp.h"
#include "nsTArray.h"
#include "nsWindowsDllInterceptor.h"
#include "plstr.h"
#ifdef MOZ_REPLACE_MALLOC
#include "replace_malloc_bridge.h"
#endif
using namespace mozilla;
namespace {
// Keep track of poisoned state. Notice that there is no reason to lock access
// to this variable as it's only changed in InitPoisonIOInterposer and
// ClearPoisonIOInterposer which may only be called on the main-thread when no
// other threads are running.
static bool sIOPoisoned = false;
/************************ Internal NT API Declarations ************************/
/*
* Function pointer declaration for internal NT routine to create/open files.
* For documentation on the NtCreateFile routine, see MSDN.
*/
typedef NTSTATUS (NTAPI* NtCreateFileFn)(
PHANDLE aFileHandle,
ACCESS_MASK aDesiredAccess,
POBJECT_ATTRIBUTES aObjectAttributes,
PIO_STATUS_BLOCK aIoStatusBlock,
PLARGE_INTEGER aAllocationSize,
ULONG aFileAttributes,
ULONG aShareAccess,
ULONG aCreateDisposition,
ULONG aCreateOptions,
PVOID aEaBuffer,
ULONG aEaLength);
/**
* Function pointer declaration for internal NT routine to read data from file.
* For documentation on the NtReadFile routine, see ZwReadFile on MSDN.
*/
typedef NTSTATUS (NTAPI* NtReadFileFn)(
HANDLE aFileHandle,
HANDLE aEvent,
PIO_APC_ROUTINE aApc,
PVOID aApcCtx,
PIO_STATUS_BLOCK aIoStatus,
PVOID aBuffer,
ULONG aLength,
PLARGE_INTEGER aOffset,
PULONG aKey);
/**
* Function pointer declaration for internal NT routine to read data from file.
* No documentation exists, see wine sources for details.
*/
typedef NTSTATUS (NTAPI* NtReadFileScatterFn)(
HANDLE aFileHandle,
HANDLE aEvent,
PIO_APC_ROUTINE aApc,
PVOID aApcCtx,
PIO_STATUS_BLOCK aIoStatus,
FILE_SEGMENT_ELEMENT* aSegments,
ULONG aLength,
PLARGE_INTEGER aOffset,
PULONG aKey);
/**
* Function pointer declaration for internal NT routine to write data to file.
* For documentation on the NtWriteFile routine, see ZwWriteFile on MSDN.
*/
typedef NTSTATUS (NTAPI* NtWriteFileFn)(
HANDLE aFileHandle,
HANDLE aEvent,
PIO_APC_ROUTINE aApc,
PVOID aApcCtx,
PIO_STATUS_BLOCK aIoStatus,
PVOID aBuffer,
ULONG aLength,
PLARGE_INTEGER aOffset,
PULONG aKey);
/**
* Function pointer declaration for internal NT routine to write data to file.
* No documentation exists, see wine sources for details.
*/
typedef NTSTATUS (NTAPI* NtWriteFileGatherFn)(
HANDLE aFileHandle,
HANDLE aEvent,
PIO_APC_ROUTINE aApc,
PVOID aApcCtx,
PIO_STATUS_BLOCK aIoStatus,
FILE_SEGMENT_ELEMENT* aSegments,
ULONG aLength,
PLARGE_INTEGER aOffset,
PULONG aKey);
/**
* Function pointer declaration for internal NT routine to flush to disk.
* For documentation on the NtFlushBuffersFile routine, see ZwFlushBuffersFile
* on MSDN.
*/
typedef NTSTATUS (NTAPI* NtFlushBuffersFileFn)(
HANDLE aFileHandle,
PIO_STATUS_BLOCK aIoStatusBlock);
typedef struct _FILE_NETWORK_OPEN_INFORMATION* PFILE_NETWORK_OPEN_INFORMATION;
/**
* Function pointer delaration for internal NT routine to query file attributes.
* (equivalent to stat)
*/
typedef NTSTATUS (NTAPI* NtQueryFullAttributesFileFn)(
POBJECT_ATTRIBUTES aObjectAttributes,
PFILE_NETWORK_OPEN_INFORMATION aFileInformation);
/*************************** Auxiliary Declarations ***************************/
/**
* RAII class for timing the duration of an I/O call and reporting the result
* to the IOInterposeObserver API.
*/
class WinIOAutoObservation : public IOInterposeObserver::Observation
{
public:
WinIOAutoObservation(IOInterposeObserver::Operation aOp,
HANDLE aFileHandle, const LARGE_INTEGER* aOffset)
: IOInterposeObserver::Observation(
aOp, sReference, !IsDebugFile(reinterpret_cast<intptr_t>(aFileHandle)))
, mFileHandle(aFileHandle)
, mHasQueriedFilename(false)
, mFilename(nullptr)
{
if (mShouldReport) {
mOffset.QuadPart = aOffset ? aOffset->QuadPart : 0;
}
}
WinIOAutoObservation(IOInterposeObserver::Operation aOp, nsAString& aFilename)
: IOInterposeObserver::Observation(aOp, sReference)
, mFileHandle(nullptr)
, mHasQueriedFilename(false)
, mFilename(nullptr)
{
if (mShouldReport) {
nsAutoString dosPath;
if (NtPathToDosPath(aFilename, dosPath)) {
mFilename = ToNewUnicode(dosPath);
mHasQueriedFilename = true;
}
mOffset.QuadPart = 0;
}
}
// Custom implementation of IOInterposeObserver::Observation::Filename
const char16_t* Filename() override;
~WinIOAutoObservation()
{
Report();
if (mFilename) {
MOZ_ASSERT(mHasQueriedFilename);
NS_Free(mFilename);
mFilename = nullptr;
}
}
private:
HANDLE mFileHandle;
LARGE_INTEGER mOffset;
bool mHasQueriedFilename;
char16_t* mFilename;
static const char* sReference;
};
const char* WinIOAutoObservation::sReference = "PoisonIOInterposer";
// Get filename for this observation
const char16_t*
WinIOAutoObservation::Filename()
{
// If mHasQueriedFilename is true, then filename is already stored in mFilename
if (mHasQueriedFilename) {
return mFilename;
}
nsAutoString utf16Filename;
if (HandleToFilename(mFileHandle, mOffset, utf16Filename)) {
// Heap allocate with leakable memory
mFilename = ToNewUnicode(utf16Filename);
}
mHasQueriedFilename = true;
// Return filename
return mFilename;
}
/*************************** IO Interposing Methods ***************************/
// Function pointers to original functions
static NtCreateFileFn gOriginalNtCreateFile;
static NtReadFileFn gOriginalNtReadFile;
static NtReadFileScatterFn gOriginalNtReadFileScatter;
static NtWriteFileFn gOriginalNtWriteFile;
static NtWriteFileGatherFn gOriginalNtWriteFileGather;
static NtFlushBuffersFileFn gOriginalNtFlushBuffersFile;
static NtQueryFullAttributesFileFn gOriginalNtQueryFullAttributesFile;
static NTSTATUS NTAPI
InterposedNtCreateFile(PHANDLE aFileHandle,
ACCESS_MASK aDesiredAccess,
POBJECT_ATTRIBUTES aObjectAttributes,
PIO_STATUS_BLOCK aIoStatusBlock,
PLARGE_INTEGER aAllocationSize,
ULONG aFileAttributes,
ULONG aShareAccess,
ULONG aCreateDisposition,
ULONG aCreateOptions,
PVOID aEaBuffer,
ULONG aEaLength)
{
// Report IO
const wchar_t* buf =
aObjectAttributes ? aObjectAttributes->ObjectName->Buffer : L"";
uint32_t len =
aObjectAttributes ? aObjectAttributes->ObjectName->Length / sizeof(WCHAR) :
0;
nsDependentSubstring filename(buf, len);
WinIOAutoObservation timer(IOInterposeObserver::OpCreateOrOpen, filename);
// Something is badly wrong if this function is undefined
MOZ_ASSERT(gOriginalNtCreateFile);
// Execute original function
return gOriginalNtCreateFile(aFileHandle,
aDesiredAccess,
aObjectAttributes,
aIoStatusBlock,
aAllocationSize,
aFileAttributes,
aShareAccess,
aCreateDisposition,
aCreateOptions,
aEaBuffer,
aEaLength);
}
static NTSTATUS NTAPI
InterposedNtReadFile(HANDLE aFileHandle,
HANDLE aEvent,
PIO_APC_ROUTINE aApc,
PVOID aApcCtx,
PIO_STATUS_BLOCK aIoStatus,
PVOID aBuffer,
ULONG aLength,
PLARGE_INTEGER aOffset,
PULONG aKey)
{
// Report IO
WinIOAutoObservation timer(IOInterposeObserver::OpRead, aFileHandle, aOffset);
// Something is badly wrong if this function is undefined
MOZ_ASSERT(gOriginalNtReadFile);
// Execute original function
return gOriginalNtReadFile(aFileHandle,
aEvent,
aApc,
aApcCtx,
aIoStatus,
aBuffer,
aLength,
aOffset,
aKey);
}
static NTSTATUS NTAPI
InterposedNtReadFileScatter(HANDLE aFileHandle,
HANDLE aEvent,
PIO_APC_ROUTINE aApc,
PVOID aApcCtx,
PIO_STATUS_BLOCK aIoStatus,
FILE_SEGMENT_ELEMENT* aSegments,
ULONG aLength,
PLARGE_INTEGER aOffset,
PULONG aKey)
{
// Report IO
WinIOAutoObservation timer(IOInterposeObserver::OpRead, aFileHandle, aOffset);
// Something is badly wrong if this function is undefined
MOZ_ASSERT(gOriginalNtReadFileScatter);
// Execute original function
return gOriginalNtReadFileScatter(aFileHandle,
aEvent,
aApc,
aApcCtx,
aIoStatus,
aSegments,
aLength,
aOffset,
aKey);
}
// Interposed NtWriteFile function
static NTSTATUS NTAPI
InterposedNtWriteFile(HANDLE aFileHandle,
HANDLE aEvent,
PIO_APC_ROUTINE aApc,
PVOID aApcCtx,
PIO_STATUS_BLOCK aIoStatus,
PVOID aBuffer,
ULONG aLength,
PLARGE_INTEGER aOffset,
PULONG aKey)
{
// Report IO
WinIOAutoObservation timer(IOInterposeObserver::OpWrite, aFileHandle,
aOffset);
// Something is badly wrong if this function is undefined
MOZ_ASSERT(gOriginalNtWriteFile);
// Execute original function
return gOriginalNtWriteFile(aFileHandle,
aEvent,
aApc,
aApcCtx,
aIoStatus,
aBuffer,
aLength,
aOffset,
aKey);
}
// Interposed NtWriteFileGather function
static NTSTATUS NTAPI
InterposedNtWriteFileGather(HANDLE aFileHandle,
HANDLE aEvent,
PIO_APC_ROUTINE aApc,
PVOID aApcCtx,
PIO_STATUS_BLOCK aIoStatus,
FILE_SEGMENT_ELEMENT* aSegments,
ULONG aLength,
PLARGE_INTEGER aOffset,
PULONG aKey)
{
// Report IO
WinIOAutoObservation timer(IOInterposeObserver::OpWrite, aFileHandle,
aOffset);
// Something is badly wrong if this function is undefined
MOZ_ASSERT(gOriginalNtWriteFileGather);
// Execute original function
return gOriginalNtWriteFileGather(aFileHandle,
aEvent,
aApc,
aApcCtx,
aIoStatus,
aSegments,
aLength,
aOffset,
aKey);
}
static NTSTATUS NTAPI
InterposedNtFlushBuffersFile(HANDLE aFileHandle,
PIO_STATUS_BLOCK aIoStatusBlock)
{
// Report IO
WinIOAutoObservation timer(IOInterposeObserver::OpFSync, aFileHandle,
nullptr);
// Something is badly wrong if this function is undefined
MOZ_ASSERT(gOriginalNtFlushBuffersFile);
// Execute original function
return gOriginalNtFlushBuffersFile(aFileHandle,
aIoStatusBlock);
}
static NTSTATUS NTAPI
InterposedNtQueryFullAttributesFile(
POBJECT_ATTRIBUTES aObjectAttributes,
PFILE_NETWORK_OPEN_INFORMATION aFileInformation)
{
// Report IO
const wchar_t* buf =
aObjectAttributes ? aObjectAttributes->ObjectName->Buffer : L"";
uint32_t len =
aObjectAttributes ? aObjectAttributes->ObjectName->Length / sizeof(WCHAR) :
0;
nsDependentSubstring filename(buf, len);
WinIOAutoObservation timer(IOInterposeObserver::OpStat, filename);
// Something is badly wrong if this function is undefined
MOZ_ASSERT(gOriginalNtQueryFullAttributesFile);
// Execute original function
return gOriginalNtQueryFullAttributesFile(aObjectAttributes,
aFileInformation);
}
} // namespace
/******************************** IO Poisoning ********************************/
// Windows DLL interceptor
static WindowsDllInterceptor sNtDllInterceptor;
namespace mozilla {
void
InitPoisonIOInterposer()
{
// Don't poison twice... as this function may only be invoked on the main
// thread when no other threads are running, it safe to allow multiple calls
// to InitPoisonIOInterposer() without complaining (ie. failing assertions).
if (sIOPoisoned) {
return;
}
sIOPoisoned = true;
// Stdout and Stderr are OK.
MozillaRegisterDebugFD(1);
MozillaRegisterDebugFD(2);
#ifdef MOZ_REPLACE_MALLOC
// The contract with InitDebugFd is that the given registry can be used
// at any moment, so the instance needs to persist longer than the scope
// of this functions.
static DebugFdRegistry registry;
ReplaceMalloc::InitDebugFd(registry);
#endif
// Initialize dll interceptor and add hooks
sNtDllInterceptor.Init("ntdll.dll");
sNtDllInterceptor.AddHook(
"NtCreateFile",
reinterpret_cast<intptr_t>(InterposedNtCreateFile),
reinterpret_cast<void**>(&gOriginalNtCreateFile));
sNtDllInterceptor.AddHook(
"NtReadFile",
reinterpret_cast<intptr_t>(InterposedNtReadFile),
reinterpret_cast<void**>(&gOriginalNtReadFile));
sNtDllInterceptor.AddHook(
"NtReadFileScatter",
reinterpret_cast<intptr_t>(InterposedNtReadFileScatter),
reinterpret_cast<void**>(&gOriginalNtReadFileScatter));
sNtDllInterceptor.AddHook(
"NtWriteFile",
reinterpret_cast<intptr_t>(InterposedNtWriteFile),
reinterpret_cast<void**>(&gOriginalNtWriteFile));
sNtDllInterceptor.AddHook(
"NtWriteFileGather",
reinterpret_cast<intptr_t>(InterposedNtWriteFileGather),
reinterpret_cast<void**>(&gOriginalNtWriteFileGather));
sNtDllInterceptor.AddHook(
"NtFlushBuffersFile",
reinterpret_cast<intptr_t>(InterposedNtFlushBuffersFile),
reinterpret_cast<void**>(&gOriginalNtFlushBuffersFile));
sNtDllInterceptor.AddHook(
"NtQueryFullAttributesFile",
reinterpret_cast<intptr_t>(InterposedNtQueryFullAttributesFile),
reinterpret_cast<void**>(&gOriginalNtQueryFullAttributesFile));
}
void
ClearPoisonIOInterposer()
{
MOZ_ASSERT(false);
if (sIOPoisoned) {
// Destroy the DLL interceptor
sIOPoisoned = false;
sNtDllInterceptor = WindowsDllInterceptor();
}
}
} // namespace mozilla