diff --git a/mozglue/misc/interceptor/MMPolicies.h b/mozglue/misc/interceptor/MMPolicies.h index 244ade376674..fe57c1da0fa3 100644 --- a/mozglue/misc/interceptor/MMPolicies.h +++ b/mozglue/misc/interceptor/MMPolicies.h @@ -150,18 +150,6 @@ class MOZ_TRIVIAL_CTOR_DTOR MMPolicyBase { } public: -#if defined(NIGHTLY_BUILD) - Maybe mLastError; - const Maybe& GetLastError() const { return mLastError; } - template - void SetLastError(Args&&... aArgs) { - mLastError = Some(DetourError(std::forward(aArgs)...)); - } -#else - template - void SetLastError(Args&&... aArgs) {} -#endif // defined(NIGHTLY_BUILD) - DWORD ComputeAllocationSize(const uint32_t aRequestedSize) const { MOZ_ASSERT(aRequestedSize); DWORD result = aRequestedSize; @@ -309,17 +297,15 @@ class MOZ_TRIVIAL_CTOR_DTOR MMPolicyBase { * large. */ PVOID FindRegion(HANDLE aProcess, const size_t aDesiredBytesLen, - const uint8_t* aRangeMin, const uint8_t* aRangeMax) { + const uint8_t* aRangeMin, const uint8_t* aRangeMax) const { const DWORD kGranularity = GetAllocGranularity(); MOZ_ASSERT(aDesiredBytesLen >= kGranularity); if (!aDesiredBytesLen) { - SetLastError(MMPOLICY_RESERVE_FINDREGION_INVALIDLEN); return nullptr; } MOZ_ASSERT(aRangeMin < aRangeMax); if (aRangeMin >= aRangeMax) { - SetLastError(MMPOLICY_RESERVE_FINDREGION_INVALIDRANGE); return nullptr; } @@ -355,8 +341,6 @@ class MOZ_TRIVIAL_CTOR_DTOR MMPolicyBase { reinterpret_cast(mbi.BaseAddress) + mbi.RegionSize; } - SetLastError(MMPOLICY_RESERVE_FINDREGION_VIRTUALQUERY_ERROR, - ::GetLastError()); return nullptr; } @@ -379,14 +363,10 @@ class MOZ_TRIVIAL_CTOR_DTOR MMPolicyBase { PVOID Reserve(HANDLE aProcess, const uint32_t aSize, const ReserveFnT& aReserveFn, const ReserveRangeFnT& aReserveRangeFn, - const Maybe>& aBounds) { + const Maybe>& aBounds) const { if (!aBounds) { // No restrictions, let the OS choose the base address - PVOID ret = aReserveFn(aProcess, nullptr, aSize); - if (!ret) { - SetLastError(MMPOLICY_RESERVE_NOBOUND_RESERVE_ERROR, ::GetLastError()); - } - return ret; + return aReserveFn(aProcess, nullptr, aSize); } const uint8_t* lowerBound = GetLowerBound(aBounds.ref()); @@ -424,11 +404,8 @@ class MOZ_TRIVIAL_CTOR_DTOR MMPolicyBase { // If we run out of attempts, we fall through to the default case where // the system chooses any base address it wants. In that case, the hook // will be set on a best-effort basis. - PVOID ret = aReserveFn(aProcess, nullptr, aSize); - if (!ret) { - SetLastError(MMPOLICY_RESERVE_FINAL_RESERVE_ERROR, ::GetLastError()); - } - return ret; + + return aReserveFn(aProcess, nullptr, aSize); } }; @@ -825,13 +802,11 @@ class MMPolicyOutOfProcess : public MMPolicyBase { uint32_t Reserve(const uint32_t aSize, const Maybe>& aBounds) { if (!aSize || !mProcess) { - SetLastError(MMPOLICY_RESERVE_INVALIDARG); return 0; } if (mRemoteView) { MOZ_ASSERT(mReservationSize >= aSize); - SetLastError(MMPOLICY_RESERVE_ZERO_RESERVATIONSIZE); return mReservationSize; } @@ -841,14 +816,12 @@ class MMPolicyOutOfProcess : public MMPolicyBase { PAGE_EXECUTE_READWRITE | SEC_RESERVE, 0, mReservationSize, nullptr); if (!mMapping) { - SetLastError(MMPOLICY_RESERVE_CREATEFILEMAPPING, ::GetLastError()); return 0; } mLocalView = static_cast( ::MapViewOfFile(mMapping, FILE_MAP_WRITE, 0, 0, 0)); if (!mLocalView) { - SetLastError(MMPOLICY_RESERVE_MAPVIEWOFFILE, ::GetLastError()); return 0; } diff --git a/mozglue/misc/interceptor/PatcherBase.h b/mozglue/misc/interceptor/PatcherBase.h index aa4bb5cb330d..4abcd3484898 100644 --- a/mozglue/misc/interceptor/PatcherBase.h +++ b/mozglue/misc/interceptor/PatcherBase.h @@ -121,16 +121,6 @@ class WindowsDllPatcherBase { return mVMPolicy.IsPageAccessible(aAddress); } -#if defined(NIGHTLY_BUILD) - const Maybe& GetLastError() const { - return mVMPolicy.GetLastError(); - } -#endif // defined(NIGHTLY_BUILD) - template - void SetLastError(Args&&... aArgs) { - mVMPolicy.SetLastError(std::forward(aArgs)...); - } - protected: VMPolicy mVMPolicy; }; diff --git a/mozglue/misc/interceptor/PatcherDetour.h b/mozglue/misc/interceptor/PatcherDetour.h index 0451a8478ace..7f75ef202251 100644 --- a/mozglue/misc/interceptor/PatcherDetour.h +++ b/mozglue/misc/interceptor/PatcherDetour.h @@ -128,6 +128,10 @@ class WindowsDllDetourPatcher final using PrimitiveT = WindowsDllDetourPatcherPrimitive; Maybe mFlags; +#if defined(NIGHTLY_BUILD) + Maybe mLastError; +#endif // defined(NIGHTLY_BUILD) + public: template explicit WindowsDllDetourPatcher(Args&&... aArgs) @@ -140,6 +144,15 @@ class WindowsDllDetourPatcher final WindowsDllDetourPatcher& operator=(const WindowsDllDetourPatcher&) = delete; WindowsDllDetourPatcher& operator=(WindowsDllDetourPatcher&&) = delete; +#if defined(NIGHTLY_BUILD) + const Maybe& GetLastError() const { return mLastError; } + void SetLastError(DetourResultCode aError) { + mLastError = Some(DetourError(aError)); + } +#else + void SetLastError(DetourResultCode) {} +#endif // defined(NIGHTLY_BUILD) + void Clear() { if (!this->mVMPolicy.ShouldUnhookUponDestruction()) { return; @@ -540,11 +553,9 @@ class WindowsDllDetourPatcher final #endif // defined(_M_X64) Maybe maybeTrampPool = this->mVMPolicy.Reserve(pivot, distance); -#if defined(NIGHTLY_BUILD) - if (!maybeTrampPool && this->GetLastError().isNothing()) { + if (!maybeTrampPool) { SetLastError(DetourResultCode::DETOUR_PATCHER_DO_RESERVE_ERROR); } -#endif // defined(NIGHTLY_BUILD) return maybeTrampPool; } @@ -932,19 +943,18 @@ class WindowsDllDetourPatcher final #if defined(NIGHTLY_BUILD) origBytes.Rewind(); SetLastError(DetourResultCode::DETOUR_PATCHER_CREATE_TRAMPOLINE_ERROR); - DetourError& lastError = *this->mVMPolicy.mLastError; size_t bytesToCapture = std::min( - ArrayLength(lastError.mOrigBytes), + ArrayLength(mLastError->mOrigBytes), static_cast(PrimitiveT::GetWorstCaseRequiredBytesToPatch())); # if defined(_M_ARM64) size_t numInstructionsToCapture = bytesToCapture / sizeof(uint32_t); - auto origBytesDst = reinterpret_cast(lastError.mOrigBytes); + auto origBytesDst = reinterpret_cast(mLastError->mOrigBytes); for (size_t i = 0; i < numInstructionsToCapture; ++i) { origBytesDst[i] = origBytes.ReadNextInstruction(); } # else for (size_t i = 0; i < bytesToCapture; ++i) { - lastError.mOrigBytes[i] = origBytes[i]; + mLastError->mOrigBytes[i] = origBytes[i]; } # endif // defined(_M_ARM64) #else diff --git a/mozglue/misc/nsWindowsDllInterceptor.h b/mozglue/misc/nsWindowsDllInterceptor.h index 5a7560d2f312..e0fcd3a3b03e 100644 --- a/mozglue/misc/nsWindowsDllInterceptor.h +++ b/mozglue/misc/nsWindowsDllInterceptor.h @@ -229,12 +229,7 @@ class MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS FuncHookCrossProcess final { return false; } - bool ret = CopyStubToChildProcess(origFunc, aProcess); - if (!ret) { - aInterceptor.SetLastError(FUNCHOOKCROSSPROCESS_COPYSTUB_ERROR, - ::GetLastError()); - } - return ret; + return CopyStubToChildProcess(origFunc, aProcess); } bool SetDetour(HANDLE aProcess, InterceptorT& aInterceptor, const char* aName, @@ -245,12 +240,7 @@ class MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS FuncHookCrossProcess final { return false; } - bool ret = CopyStubToChildProcess(origFunc, aProcess); - if (!ret) { - aInterceptor.SetLastError(FUNCHOOKCROSSPROCESS_COPYSTUB_ERROR, - ::GetLastError()); - } - return ret; + return CopyStubToChildProcess(origFunc, aProcess); } explicit operator bool() const { return !!mOrigFunc; } @@ -376,10 +366,6 @@ class WindowsDllInterceptor final return mDetourPatcher.GetLastError(); } #endif // defined(NIGHTLY_BUILD) - template - void SetLastError(Args&&... aArgs) { - return mDetourPatcher.SetLastError(std::forward(aArgs)...); - } constexpr static uint32_t GetWorstCaseRequiredBytesToPatch() { return WindowsDllDetourPatcherPrimitive< diff --git a/widget/windows/WinHeaderOnlyUtils.h b/widget/windows/WinHeaderOnlyUtils.h index bdfcc8e7f3c4..e018fcf4caff 100644 --- a/widget/windows/WinHeaderOnlyUtils.h +++ b/widget/windows/WinHeaderOnlyUtils.h @@ -235,16 +235,6 @@ enum DetourResultCode : uint32_t { DETOUR_PATCHER_INVALID_TRAMPOLINE, DETOUR_PATCHER_WRITE_POINTER_ERROR, DETOUR_PATCHER_CREATE_TRAMPOLINE_ERROR, - FUNCHOOKCROSSPROCESS_COPYSTUB_ERROR, - MMPOLICY_RESERVE_INVALIDARG, - MMPOLICY_RESERVE_ZERO_RESERVATIONSIZE, - MMPOLICY_RESERVE_CREATEFILEMAPPING, - MMPOLICY_RESERVE_MAPVIEWOFFILE, - MMPOLICY_RESERVE_NOBOUND_RESERVE_ERROR, - MMPOLICY_RESERVE_FINDREGION_INVALIDLEN, - MMPOLICY_RESERVE_FINDREGION_INVALIDRANGE, - MMPOLICY_RESERVE_FINDREGION_VIRTUALQUERY_ERROR, - MMPOLICY_RESERVE_FINAL_RESERVE_ERROR, }; #if defined(NIGHTLY_BUILD) @@ -255,12 +245,6 @@ struct DetourError { uint8_t mOrigBytes[16]; explicit DetourError(DetourResultCode aError) : mErrorCode(aError), mOrigBytes{} {} - DetourError(DetourResultCode aError, DWORD aWin32Error) - : mErrorCode(aError), mOrigBytes{} { - static_assert(sizeof(mOrigBytes) >= sizeof(aWin32Error), - "Can't fit a DWORD in mOrigBytes"); - *reinterpret_cast(mOrigBytes) = aWin32Error; - } operator WindowsError() const { return WindowsError::FromHResult(mErrorCode); }