Bug 1932613 - temporarily disable browser_ml_end_to_end.js for permanent failures...
[gecko.git] / xpcom / io / FileUtilsWin.cpp
blob5bf7e4c968ac234c137c3ec882ef8fbc27306288
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "FileUtilsWin.h"
9 #include <windows.h>
10 #include <psapi.h>
12 #include "base/process_util.h"
13 #include "mozilla/ProfilerLabels.h"
14 #include "mozilla/ScopeExit.h"
15 #include "mozilla/Unused.h"
16 #include "nsWindowsHelpers.h"
18 namespace mozilla {
20 bool HandleToFilename(HANDLE aHandle, const LARGE_INTEGER& aOffset,
21 nsAString& aFilename) {
22 AUTO_PROFILER_LABEL("HandletoFilename", OTHER);
24 aFilename.Truncate();
25 // This implementation is nice because it uses fully documented APIs that
26 // are available on all Windows versions that we support.
27 nsAutoHandle fileMapping(
28 CreateFileMapping(aHandle, nullptr, PAGE_READONLY, 0, 1, nullptr));
29 if (!fileMapping) {
30 return false;
32 const auto view = MapViewOfFile(fileMapping, FILE_MAP_READ, aOffset.HighPart,
33 aOffset.LowPart, 1);
34 if (!view) {
35 return false;
37 const auto cleanup =
38 MakeScopeExit([&]() { mozilla::Unused << UnmapViewOfFile(view); });
40 nsAutoString mappedFilename;
41 DWORD len = 0;
42 SetLastError(ERROR_SUCCESS);
43 do {
44 mappedFilename.SetLength(mappedFilename.Length() + MAX_PATH);
45 len = GetMappedFileNameW(GetCurrentProcess(), view, mappedFilename.get(),
46 mappedFilename.Length());
47 } while (!len && GetLastError() == ERROR_INSUFFICIENT_BUFFER);
48 if (!len) {
49 return false;
51 mappedFilename.Truncate(len);
52 return NtPathToDosPath(mappedFilename, aFilename);
55 template <class T>
56 struct RVAMap {
57 RVAMap(HANDLE map, DWORD offset) {
58 SYSTEM_INFO info;
59 GetSystemInfo(&info);
61 DWORD alignedOffset =
62 (offset / info.dwAllocationGranularity) * info.dwAllocationGranularity;
64 MOZ_ASSERT(offset - alignedOffset < info.dwAllocationGranularity, "Wtf");
66 mRealView = ::MapViewOfFile(map, FILE_MAP_READ, 0, alignedOffset,
67 sizeof(T) + (offset - alignedOffset));
69 mMappedView =
70 mRealView
71 ? reinterpret_cast<T*>((char*)mRealView + (offset - alignedOffset))
72 : nullptr;
74 ~RVAMap() {
75 if (mRealView) {
76 ::UnmapViewOfFile(mRealView);
79 operator const T*() const { return mMappedView; }
80 const T* operator->() const { return mMappedView; }
82 private:
83 const T* mMappedView;
84 void* mRealView;
87 uint32_t GetExecutableArchitecture(const wchar_t* aPath) {
88 nsAutoHandle file(::CreateFileW(aPath, GENERIC_READ, FILE_SHARE_READ, nullptr,
89 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
90 nullptr));
91 if (!file) {
92 return base::PROCESS_ARCH_INVALID;
95 nsAutoHandle map(
96 ::CreateFileMappingW(file, nullptr, PAGE_READONLY, 0, 0, nullptr));
97 if (!map) {
98 return base::PROCESS_ARCH_INVALID;
101 RVAMap<IMAGE_DOS_HEADER> peHeader(map, 0);
102 if (!peHeader) {
103 return base::PROCESS_ARCH_INVALID;
106 RVAMap<IMAGE_NT_HEADERS> ntHeader(map, peHeader->e_lfanew);
107 if (!ntHeader) {
108 return base::PROCESS_ARCH_INVALID;
111 switch (ntHeader->FileHeader.Machine) {
112 case IMAGE_FILE_MACHINE_I386:
113 return base::PROCESS_ARCH_I386;
114 case IMAGE_FILE_MACHINE_AMD64:
115 return base::PROCESS_ARCH_X86_64;
116 case IMAGE_FILE_MACHINE_ARM64:
117 return base::PROCESS_ARCH_ARM_64;
118 case IMAGE_FILE_MACHINE_ARM:
119 case IMAGE_FILE_MACHINE_ARMNT:
120 case IMAGE_FILE_MACHINE_THUMB:
121 return base::PROCESS_ARCH_ARM;
122 default:
123 return base::PROCESS_ARCH_INVALID;
127 } // namespace mozilla