summaryrefslogtreecommitdiffstats
path: root/widget/windows/WinHeaderOnlyUtils.h
blob: 93137defe9761eee28fb6104b0806978797d91f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
/* -*- 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 https://mozilla.org/MPL/2.0/. */

#ifndef mozilla_WinHeaderOnlyUtils_h
#define mozilla_WinHeaderOnlyUtils_h

#include <windows.h>
#include <winerror.h>
#include <winnt.h>
#include <winternl.h>
#include <objbase.h>
#include <shlwapi.h>
#undef ParseURL
#include <stdlib.h>
#include <tuple>

#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"
#include "mozilla/DynamicallyLinkedFunctionPtr.h"
#include "mozilla/Maybe.h"
#include "mozilla/ResultVariant.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/WindowsVersion.h"
#include "nsWindowsHelpers.h"

#if defined(MOZILLA_INTERNAL_API)
#  include "nsIFile.h"
#  include "nsString.h"
#endif  // defined(MOZILLA_INTERNAL_API)

/**
 * This header is intended for self-contained, header-only, utility code for
 * Win32. It may be used outside of xul.dll, in places such as firefox.exe or
 * mozglue.dll. If your code creates dependencies on Mozilla libraries, you
 * should put it elsewhere.
 */

#if _WIN32_WINNT < _WIN32_WINNT_WIN8
typedef struct _FILE_ID_INFO {
  ULONGLONG VolumeSerialNumber;
  FILE_ID_128 FileId;
} FILE_ID_INFO;

#  define FileIdInfo ((FILE_INFO_BY_HANDLE_CLASS)18)

#endif  // _WIN32_WINNT < _WIN32_WINNT_WIN8

#if !defined(STATUS_SUCCESS)
#  define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
#endif  // !defined(STATUS_SUCCESS)

// Our data indicates a few users of Win7 x86 hit failure to load urlmon.dll
// for unknown reasons.  Since we don't always require urlmon.dll on Win7,
// we delay-load it, which causes a crash if loading urlmon.dll fails.  This
// macro is to safely load and call urlmon's API graciously without crash.
#if defined(_X86_)
#  define SAFECALL_URLMON_FUNC(FuncName, ...)                                  \
    do {                                                                       \
      static const mozilla::StaticDynamicallyLinkedFunctionPtr<                \
          decltype(&::FuncName)>                                               \
          func(L"urlmon.dll", #FuncName);                                      \
      hr =                                                                     \
          func ? func(__VA_ARGS__) : HRESULT_FROM_WIN32(ERROR_PROC_NOT_FOUND); \
    } while (0)
#else
#  define SAFECALL_URLMON_FUNC(FuncName, ...) hr = ::FuncName(__VA_ARGS__)
#endif

namespace mozilla {

class WindowsError final {
 private:
  // HRESULT and NTSTATUS are both typedefs of LONG, so we cannot use
  // overloading to properly differentiate between the two. Instead we'll use
  // static functions to convert the various error types to HRESULTs before
  // instantiating.
  explicit constexpr WindowsError(HRESULT aHResult) : mHResult(aHResult) {}

 public:
  using UniqueString = UniquePtr<WCHAR[], LocalFreeDeleter>;

  static constexpr WindowsError FromNtStatus(NTSTATUS aNtStatus) {
    if (aNtStatus == STATUS_SUCCESS) {
      // Special case: we don't want to set FACILITY_NT_BIT
      // (HRESULT_FROM_NT does not handle this case, unlike HRESULT_FROM_WIN32)
      return WindowsError(S_OK);
    }

    return WindowsError(HRESULT_FROM_NT(aNtStatus));
  }

  static constexpr WindowsError FromHResult(HRESULT aHResult) {
    return WindowsError(aHResult);
  }

  static constexpr WindowsError FromWin32Error(DWORD aWin32Err) {
    return WindowsError(HRESULT_FROM_WIN32(aWin32Err));
  }

  static WindowsError FromLastError() {
    return FromWin32Error(::GetLastError());
  }

  static WindowsError CreateSuccess() { return WindowsError(S_OK); }

  static WindowsError CreateGeneric() {
    return FromWin32Error(ERROR_UNIDENTIFIED_ERROR);
  }

  bool IsSuccess() const { return SUCCEEDED(mHResult); }

  bool IsFailure() const { return FAILED(mHResult); }

  bool IsAvailableAsWin32Error() const {
    return IsAvailableAsNtStatus() ||
           HRESULT_FACILITY(mHResult) == FACILITY_WIN32;
  }

  bool IsAvailableAsNtStatus() const {
    return mHResult == S_OK || (mHResult & FACILITY_NT_BIT);
  }

  bool IsAvailableAsHResult() const { return true; }

  UniqueString AsString() const {
    LPWSTR rawMsgBuf = nullptr;
    constexpr DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
                            FORMAT_MESSAGE_FROM_SYSTEM |
                            FORMAT_MESSAGE_IGNORE_INSERTS;
    DWORD result =
        ::FormatMessageW(flags, nullptr, mHResult, 0,
                         reinterpret_cast<LPWSTR>(&rawMsgBuf), 0, nullptr);
    if (!result) {
      return nullptr;
    }

    return UniqueString(rawMsgBuf);
  }

  HRESULT AsHResult() const { return mHResult; }

  // Not all HRESULTs are convertible to Win32 Errors, so we use Maybe
  Maybe<DWORD> AsWin32Error() const {
    if (mHResult == S_OK) {
      return Some(static_cast<DWORD>(ERROR_SUCCESS));
    }

    if (HRESULT_FACILITY(mHResult) == FACILITY_WIN32) {
      // This is the inverse of HRESULT_FROM_WIN32
      return Some(static_cast<DWORD>(HRESULT_CODE(mHResult)));
    }

    // The NTSTATUS facility is a special case and thus does not utilize the
    // HRESULT_FACILITY and HRESULT_CODE macros.
    if (mHResult & FACILITY_NT_BIT) {
      return Some(NtStatusToWin32Error(
          static_cast<NTSTATUS>(mHResult & ~FACILITY_NT_BIT)));
    }

    return Nothing();
  }

  // Not all HRESULTs are convertible to NTSTATUS, so we use Maybe
  Maybe<NTSTATUS> AsNtStatus() const {
    if (mHResult == S_OK) {
      return Some(STATUS_SUCCESS);
    }

    // The NTSTATUS facility is a special case and thus does not utilize the
    // HRESULT_FACILITY and HRESULT_CODE macros.
    if (mHResult & FACILITY_NT_BIT) {
      return Some(static_cast<NTSTATUS>(mHResult & ~FACILITY_NT_BIT));
    }

    return Nothing();
  }

  constexpr bool operator==(const WindowsError& aOther) const {
    return mHResult == aOther.mHResult;
  }

  constexpr bool operator!=(const WindowsError& aOther) const {
    return mHResult != aOther.mHResult;
  }

  static DWORD NtStatusToWin32Error(NTSTATUS aNtStatus) {
    static const StaticDynamicallyLinkedFunctionPtr<
        decltype(&RtlNtStatusToDosError)>
        pRtlNtStatusToDosError(L"ntdll.dll", "RtlNtStatusToDosError");

    MOZ_ASSERT(!!pRtlNtStatusToDosError);
    if (!pRtlNtStatusToDosError) {
      return ERROR_UNIDENTIFIED_ERROR;
    }

    return pRtlNtStatusToDosError(aNtStatus);
  }

 private:
  // We store the error code as an HRESULT because they can encode both Win32
  // error codes and NTSTATUS codes.
  HRESULT mHResult;
};

namespace detail {
template <>
struct UnusedZero<WindowsError> {
  using StorageType = WindowsError;

  static constexpr bool value = true;
  static constexpr StorageType nullValue = WindowsError::FromHResult(S_OK);

  static constexpr void AssertValid(StorageType aValue) {}
  static constexpr const WindowsError& Inspect(const StorageType& aValue) {
    return aValue;
  }
  static constexpr WindowsError Unwrap(StorageType aValue) { return aValue; }
  static constexpr StorageType Store(WindowsError aValue) { return aValue; }
};
}  // namespace detail

enum DetourResultCode : uint32_t {
  RESULT_OK = 0,
  INTERCEPTOR_MOD_NULL,
  INTERCEPTOR_MOD_INACCESSIBLE,
  INTERCEPTOR_PROC_NULL,
  INTERCEPTOR_PROC_INACCESSIBLE,
  DETOUR_PATCHER_RESERVE_FOR_MODULE_PE_ERROR,
  DETOUR_PATCHER_RESERVE_FOR_MODULE_TEXT_ERROR,
  DETOUR_PATCHER_RESERVE_FOR_MODULE_RESERVE_ERROR,
  DETOUR_PATCHER_DO_RESERVE_ERROR,
  DETOUR_PATCHER_NEXT_TRAMPOLINE_ERROR,
  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_FINDREGION_NO_FREE_REGION,
  MMPOLICY_RESERVE_FINAL_RESERVE_ERROR,
};

#if defined(NIGHTLY_BUILD)
struct DetourError {
  // We have a 16-bytes buffer, but only minimum bytes to detour per
  // architecture are copied.  See CreateTrampoline in PatcherDetour.h.
  DetourResultCode mErrorCode;
  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<DWORD*>(mOrigBytes) = aWin32Error;
  }
  operator WindowsError() const {
    return WindowsError::FromHResult(mErrorCode);
  }
};
#endif  // defined(NIGHTLY_BUILD)

template <typename T>
using WindowsErrorResult = Result<T, WindowsError>;

struct LauncherError {
  LauncherError(const char* aFile, int aLine, WindowsError aWin32Error)
      : mFile(aFile), mLine(aLine), mError(aWin32Error) {}

#if defined(NIGHTLY_BUILD)
  LauncherError(const char* aFile, int aLine,
                const Maybe<DetourError>& aDetourError)
      : mFile(aFile),
        mLine(aLine),
        mError(aDetourError.isSome() ? aDetourError.value()
                                     : WindowsError::CreateGeneric()),
        mDetourError(aDetourError) {}
#endif  // defined(NIGHTLY_BUILD)

  const char* mFile;
  int mLine;
  WindowsError mError;
#if defined(NIGHTLY_BUILD)
  Maybe<DetourError> mDetourError;
#endif  // defined(NIGHTLY_BUILD)

  bool operator==(const LauncherError& aOther) const {
    return mError == aOther.mError;
  }

  bool operator!=(const LauncherError& aOther) const {
    return mError != aOther.mError;
  }

  bool operator==(const WindowsError& aOther) const { return mError == aOther; }

  bool operator!=(const WindowsError& aOther) const { return mError != aOther; }
};

#if defined(MOZ_USE_LAUNCHER_ERROR)

template <typename T>
using LauncherResult = Result<T, LauncherError>;

template <typename T>
using LauncherResultWithLineInfo = LauncherResult<T>;

using WindowsErrorType = LauncherError;

#else

template <typename T>
using LauncherResult = WindowsErrorResult<T>;

template <typename T>
using LauncherResultWithLineInfo = Result<T, LauncherError>;

using WindowsErrorType = WindowsError;

#endif  // defined(MOZ_USE_LAUNCHER_ERROR)

using LauncherVoidResult = LauncherResult<Ok>;

using LauncherVoidResultWithLineInfo = LauncherResultWithLineInfo<Ok>;

#if defined(MOZ_USE_LAUNCHER_ERROR)

#  define LAUNCHER_ERROR_GENERIC()           \
    ::mozilla::Err(::mozilla::LauncherError( \
        __FILE__, __LINE__, ::mozilla::WindowsError::CreateGeneric()))

#  if defined(NIGHTLY_BUILD)
#    define LAUNCHER_ERROR_FROM_DETOUR_ERROR(err) \
      ::mozilla::Err(::mozilla::LauncherError(__FILE__, __LINE__, err))
#  else
#    define LAUNCHER_ERROR_FROM_DETOUR_ERROR(err) LAUNCHER_ERROR_GENERIC()
#  endif  // defined(NIGHTLY_BUILD)

#  define LAUNCHER_ERROR_FROM_WIN32(err)     \
    ::mozilla::Err(::mozilla::LauncherError( \
        __FILE__, __LINE__, ::mozilla::WindowsError::FromWin32Error(err)))

#  define LAUNCHER_ERROR_FROM_LAST()         \
    ::mozilla::Err(::mozilla::LauncherError( \
        __FILE__, __LINE__, ::mozilla::WindowsError::FromLastError()))

#  define LAUNCHER_ERROR_FROM_NTSTATUS(ntstatus) \
    ::mozilla::Err(::mozilla::LauncherError(     \
        __FILE__, __LINE__, ::mozilla::WindowsError::FromNtStatus(ntstatus)))

#  define LAUNCHER_ERROR_FROM_HRESULT(hresult) \
    ::mozilla::Err(::mozilla::LauncherError(   \
        __FILE__, __LINE__, ::mozilla::WindowsError::FromHResult(hresult)))

// This macro wraps the supplied WindowsError with a LauncherError
#  define LAUNCHER_ERROR_FROM_MOZ_WINDOWS_ERROR(err) \
    ::mozilla::Err(::mozilla::LauncherError(__FILE__, __LINE__, err))

#else

#  define LAUNCHER_ERROR_GENERIC() \
    ::mozilla::Err(::mozilla::WindowsError::CreateGeneric())

#  define LAUNCHER_ERROR_FROM_DETOUR_ERROR(err) LAUNCHER_ERROR_GENERIC()

#  define LAUNCHER_ERROR_FROM_WIN32(err) \
    ::mozilla::Err(::mozilla::WindowsError::FromWin32Error(err))

#  define LAUNCHER_ERROR_FROM_LAST() \
    ::mozilla::Err(::mozilla::WindowsError::FromLastError())

#  define LAUNCHER_ERROR_FROM_NTSTATUS(ntstatus) \
    ::mozilla::Err(::mozilla::WindowsError::FromNtStatus(ntstatus))

#  define LAUNCHER_ERROR_FROM_HRESULT(hresult) \
    ::mozilla::Err(::mozilla::WindowsError::FromHResult(hresult))

#  define LAUNCHER_ERROR_FROM_MOZ_WINDOWS_ERROR(err) ::mozilla::Err(err)

#endif  // defined(MOZ_USE_LAUNCHER_ERROR)

// How long to wait for a created process to become available for input,
// to prevent that process's windows being forced to the background.
// This is used across update, restart, and the launcher.
const DWORD kWaitForInputIdleTimeoutMS = 10 * 1000;

/**
 * Wait for a child GUI process to become "idle." Idle means that the process
 * has created its message queue and has begun waiting for user input.
 *
 * Note that this must only be used when the child process is going to display
 * GUI! Otherwise you're going to be waiting for a very long time ;-)
 *
 * @return true if we successfully waited for input idle;
 *         false if we timed out or failed to wait.
 */
inline bool WaitForInputIdle(HANDLE aProcess,
                             DWORD aTimeoutMs = kWaitForInputIdleTimeoutMS) {
  const DWORD kSleepTimeMs = 10;
  const DWORD waitStart = aTimeoutMs == INFINITE ? 0 : ::GetTickCount();
  DWORD elapsed = 0;

  while (true) {
    if (aTimeoutMs != INFINITE) {
      elapsed = ::GetTickCount() - waitStart;
    }

    if (elapsed >= aTimeoutMs) {
      return false;
    }

    // ::WaitForInputIdle() doesn't always set the last-error code on failure
    ::SetLastError(ERROR_SUCCESS);

    DWORD waitResult = ::WaitForInputIdle(aProcess, aTimeoutMs - elapsed);
    if (!waitResult) {
      return true;
    }

    if (waitResult == WAIT_FAILED &&
        ::GetLastError() == ERROR_NOT_GUI_PROCESS) {
      ::Sleep(kSleepTimeMs);
      continue;
    }

    return false;
  }
}

enum class PathType {
  eNtPath,
  eDosPath,
};

class FileUniqueId final {
 public:
  explicit FileUniqueId(const wchar_t* aPath, PathType aPathType)
      : mId(FILE_ID_INFO()) {
    if (!aPath) {
      mId = LAUNCHER_ERROR_FROM_HRESULT(E_INVALIDARG);
      return;
    }

    nsAutoHandle file;

    switch (aPathType) {
      default:
        mId = LAUNCHER_ERROR_FROM_HRESULT(E_INVALIDARG);
        MOZ_ASSERT_UNREACHABLE("Unhandled PathType");
        return;

      case PathType::eNtPath: {
        UNICODE_STRING unicodeString;
        ::RtlInitUnicodeString(&unicodeString, aPath);
        OBJECT_ATTRIBUTES objectAttributes;
        InitializeObjectAttributes(&objectAttributes, &unicodeString,
                                   OBJ_CASE_INSENSITIVE, nullptr, nullptr);
        IO_STATUS_BLOCK ioStatus = {};
        HANDLE ntHandle;
        NTSTATUS status = ::NtOpenFile(
            &ntHandle, SYNCHRONIZE | FILE_READ_ATTRIBUTES, &objectAttributes,
            &ioStatus, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
            FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT);
        // We don't need to check |ntHandle| for INVALID_HANDLE_VALUE here,
        // as that value is set by the Win32 layer.
        if (!NT_SUCCESS(status)) {
          mId = LAUNCHER_ERROR_FROM_NTSTATUS(status);
          return;
        }

        file.own(ntHandle);
        break;
      }

      case PathType::eDosPath: {
        file.own(::CreateFileW(
            aPath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
            nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr));
        if (file == INVALID_HANDLE_VALUE) {
          mId = LAUNCHER_ERROR_FROM_LAST();
          return;
        }

        break;
      }
    }

    GetId(file);
  }

  explicit FileUniqueId(const nsAutoHandle& aFile) : mId(FILE_ID_INFO()) {
    GetId(aFile);
  }

  ~FileUniqueId() = default;

  bool IsError() const { return mId.isErr(); }

  const WindowsErrorType& GetError() const { return mId.inspectErr(); }

  FileUniqueId(FileUniqueId&& aOther) = default;
  FileUniqueId& operator=(FileUniqueId&& aOther) = delete;

  bool operator==(const FileUniqueId& aOther) const {
    return mId.isOk() && aOther.mId.isOk() &&
           !memcmp(&mId.inspect(), &aOther.mId.inspect(), sizeof(FILE_ID_INFO));
  }

  bool operator!=(const FileUniqueId& aOther) const {
    return !((*this) == aOther);
  }

 private:
  void GetId(const nsAutoHandle& aFile) {
    FILE_ID_INFO fileIdInfo = {};
    if (IsWin8OrLater()) {
      if (::GetFileInformationByHandleEx(aFile.get(), FileIdInfo, &fileIdInfo,
                                         sizeof(fileIdInfo))) {
        mId = fileIdInfo;
        return;
      }
      // Only NTFS and ReFS support FileIdInfo. So we have to fallback if
      // GetFileInformationByHandleEx failed.
    }

    BY_HANDLE_FILE_INFORMATION info = {};
    if (!::GetFileInformationByHandle(aFile.get(), &info)) {
      mId = LAUNCHER_ERROR_FROM_LAST();
      return;
    }

    fileIdInfo.VolumeSerialNumber = info.dwVolumeSerialNumber;
    memcpy(&fileIdInfo.FileId.Identifier[0], &info.nFileIndexLow,
           sizeof(DWORD));
    memcpy(&fileIdInfo.FileId.Identifier[sizeof(DWORD)], &info.nFileIndexHigh,
           sizeof(DWORD));
    mId = fileIdInfo;
  }

 private:
  LauncherResult<FILE_ID_INFO> mId;
};

class MOZ_RAII AutoVirtualProtect final {
 public:
  AutoVirtualProtect(void* aAddress, size_t aLength, DWORD aProtFlags,
                     HANDLE aTargetProcess = ::GetCurrentProcess())
      : mAddress(aAddress),
        mLength(aLength),
        mTargetProcess(aTargetProcess),
        mPrevProt(0),
        mError(WindowsError::CreateSuccess()) {
    if (!::VirtualProtectEx(aTargetProcess, aAddress, aLength, aProtFlags,
                            &mPrevProt)) {
      mError = WindowsError::FromLastError();
    }
  }

  ~AutoVirtualProtect() {
    if (mError.IsFailure()) {
      return;
    }

    ::VirtualProtectEx(mTargetProcess, mAddress, mLength, mPrevProt,
                       &mPrevProt);
  }

  explicit operator bool() const { return mError.IsSuccess(); }

  WindowsError GetError() const { return mError; }

  DWORD PrevProt() const { return mPrevProt; }

  AutoVirtualProtect(const AutoVirtualProtect&) = delete;
  AutoVirtualProtect(AutoVirtualProtect&&) = delete;
  AutoVirtualProtect& operator=(const AutoVirtualProtect&) = delete;
  AutoVirtualProtect& operator=(AutoVirtualProtect&&) = delete;

 private:
  void* mAddress;
  size_t mLength;
  HANDLE mTargetProcess;
  DWORD mPrevProt;
  WindowsError mError;
};

inline UniquePtr<wchar_t[]> GetFullModulePath(HMODULE aModule) {
  DWORD bufLen = MAX_PATH;
  mozilla::UniquePtr<wchar_t[]> buf;
  DWORD retLen;

  while (true) {
    buf = mozilla::MakeUnique<wchar_t[]>(bufLen);
    retLen = ::GetModuleFileNameW(aModule, buf.get(), bufLen);
    if (!retLen) {
      return nullptr;
    }

    if (retLen == bufLen && ::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
      bufLen *= 2;
      continue;
    }

    break;
  }

  // Upon success, retLen *excludes* the null character
  ++retLen;

  // Since we're likely to have a bunch of unused space in buf, let's
  // reallocate a string to the actual size of the file name.
  auto result = mozilla::MakeUnique<wchar_t[]>(retLen);
  if (wcscpy_s(result.get(), retLen, buf.get())) {
    return nullptr;
  }

  return result;
}

inline UniquePtr<wchar_t[]> GetFullBinaryPath() {
  return GetFullModulePath(nullptr);
}

// Generates the install directory without a trailing path separator.
inline bool GetInstallDirectory(UniquePtr<wchar_t[]>& installPath) {
  installPath = GetFullBinaryPath();
  // It's not safe to use PathRemoveFileSpecW with strings longer than MAX_PATH
  // (including null terminator).
  if (wcslen(installPath.get()) >= MAX_PATH) {
    return false;
  }
  ::PathRemoveFileSpecW(installPath.get());
  return true;
}

class ModuleVersion final {
 public:
  constexpr ModuleVersion() : mVersion(0ULL) {}

  explicit ModuleVersion(const VS_FIXEDFILEINFO& aFixedInfo)
      : mVersion((static_cast<uint64_t>(aFixedInfo.dwFileVersionMS) << 32) |
                 static_cast<uint64_t>(aFixedInfo.dwFileVersionLS)) {}

  explicit ModuleVersion(const uint64_t aVersion) : mVersion(aVersion) {}

  ModuleVersion(const ModuleVersion& aOther) : mVersion(aOther.mVersion) {}

  uint64_t AsInteger() const { return mVersion; }

  operator uint64_t() const { return AsInteger(); }

  std::tuple<uint16_t, uint16_t, uint16_t, uint16_t> AsTuple() const {
    uint16_t major = static_cast<uint16_t>((mVersion >> 48) & 0xFFFFU);
    uint16_t minor = static_cast<uint16_t>((mVersion >> 32) & 0xFFFFU);
    uint16_t patch = static_cast<uint16_t>((mVersion >> 16) & 0xFFFFU);
    uint16_t build = static_cast<uint16_t>(mVersion & 0xFFFFU);

    return {major, minor, patch, build};
  }

  explicit operator bool() const { return !!mVersion; }

  bool operator<(const ModuleVersion& aOther) const {
    return mVersion < aOther.mVersion;
  }

  bool operator<(const uint64_t& aOther) const { return mVersion < aOther; }

  ModuleVersion& operator=(const uint64_t aIntVersion) {
    mVersion = aIntVersion;
    return *this;
  }

 private:
  uint64_t mVersion;
};

inline LauncherResult<ModuleVersion> GetModuleVersion(
    const wchar_t* aModuleFullPath) {
  DWORD verInfoLen = ::GetFileVersionInfoSizeW(aModuleFullPath, nullptr);
  if (!verInfoLen) {
    return LAUNCHER_ERROR_FROM_LAST();
  }

  auto verInfoBuf = MakeUnique<BYTE[]>(verInfoLen);
  if (!::GetFileVersionInfoW(aModuleFullPath, 0, verInfoLen,
                             verInfoBuf.get())) {
    return LAUNCHER_ERROR_FROM_LAST();
  }

  UINT fixedInfoLen;
  VS_FIXEDFILEINFO* fixedInfo = nullptr;
  if (!::VerQueryValueW(verInfoBuf.get(), L"\\",
                        reinterpret_cast<LPVOID*>(&fixedInfo), &fixedInfoLen)) {
    // VerQueryValue may fail if the resource does not exist. This is not an
    // error; we'll return 0 in this case.
    return ModuleVersion(0ULL);
  }

  return ModuleVersion(*fixedInfo);
}

inline LauncherResult<ModuleVersion> GetModuleVersion(HMODULE aModule) {
  UniquePtr<wchar_t[]> fullPath(GetFullModulePath(aModule));
  if (!fullPath) {
    return LAUNCHER_ERROR_GENERIC();
  }

  return GetModuleVersion(fullPath.get());
}

#if defined(MOZILLA_INTERNAL_API)
inline LauncherResult<ModuleVersion> GetModuleVersion(nsIFile* aFile) {
  if (!aFile) {
    return LAUNCHER_ERROR_FROM_HRESULT(E_INVALIDARG);
  }

  nsAutoString fullPath;
  nsresult rv = aFile->GetPath(fullPath);
  if (NS_FAILED(rv)) {
    return LAUNCHER_ERROR_GENERIC();
  }

  return GetModuleVersion(fullPath.get());
}
#endif  // defined(MOZILLA_INTERNAL_API)

struct CoTaskMemFreeDeleter {
  void operator()(void* aPtr) { ::CoTaskMemFree(aPtr); }
};

inline LauncherResult<TOKEN_ELEVATION_TYPE> GetElevationType(
    const nsAutoHandle& aToken) {
  DWORD retLen;
  TOKEN_ELEVATION_TYPE elevationType;
  if (!::GetTokenInformation(aToken.get(), TokenElevationType, &elevationType,
                             sizeof(elevationType), &retLen)) {
    return LAUNCHER_ERROR_FROM_LAST();
  }

  return elevationType;
}

inline bool HasPackageIdentity() {
  if (!IsWin8OrLater()) {
    return false;
  }

  HMODULE kernel32Dll = ::GetModuleHandleW(L"kernel32");
  if (!kernel32Dll) {
    return false;
  }

  typedef LONG(WINAPI * GetCurrentPackageIdProc)(UINT32*, BYTE*);
  GetCurrentPackageIdProc pGetCurrentPackageId =
      (GetCurrentPackageIdProc)::GetProcAddress(kernel32Dll,
                                                "GetCurrentPackageId");

  // If there was any package identity to retrieve, we get
  // ERROR_INSUFFICIENT_BUFFER. If there had been no package identity it
  // would instead return APPMODEL_ERROR_NO_PACKAGE.
  UINT32 packageNameSize = 0;
  return pGetCurrentPackageId &&
         (pGetCurrentPackageId(&packageNameSize, nullptr) ==
          ERROR_INSUFFICIENT_BUFFER);
}

inline UniquePtr<wchar_t[]> GetPackageFamilyName() {
  HMODULE kernel32Dll = ::GetModuleHandleW(L"kernel32");
  if (!kernel32Dll) {
    return nullptr;
  }

  typedef LONG(WINAPI * GetCurrentPackageFamilyNameProc)(UINT32*, PWSTR);
  GetCurrentPackageFamilyNameProc pGetCurrentPackageFamilyName =
      (GetCurrentPackageFamilyNameProc)::GetProcAddress(
          kernel32Dll, "GetCurrentPackageFamilyName");
  if (!pGetCurrentPackageFamilyName) {
    return nullptr;
  }

  UINT32 packageNameSize = 0;
  if (pGetCurrentPackageFamilyName(&packageNameSize, nullptr) !=
      ERROR_INSUFFICIENT_BUFFER) {
    return nullptr;
  }

  UniquePtr<wchar_t[]> packageIdentity = MakeUnique<wchar_t[]>(packageNameSize);
  if (pGetCurrentPackageFamilyName(&packageNameSize, packageIdentity.get()) !=
      ERROR_SUCCESS) {
    return nullptr;
  }

  return packageIdentity;
}

// This implementation is equivalent to PathGetDriveNumber[AW].
// We define our own version because using PathGetDriveNumber
// delay-loads shlwapi.dll, which may fail when the process is
// sandboxed.
template <typename T>
int MozPathGetDriveNumber(const T* aPath) {
  const auto ToDriveNumber = [](const T* aPath) -> int {
    if (*aPath == '\0' || *(aPath + 1) != ':') {
      return -1;
    }

    T c = *aPath;
    return (c >= 'A' && c <= 'Z')   ? c - 'A'
           : (c >= 'a' && c <= 'z') ? c - 'a'
                                    : -1;
  };

  if (!aPath) {
    return -1;
  }

  if (*aPath == '\\' && *(aPath + 1) == '\\' && *(aPath + 2) == '?' &&
      *(aPath + 3) == '\\') {
    return ToDriveNumber(aPath + 4);
  }

  return ToDriveNumber(aPath);
}

}  // namespace mozilla

#endif  // mozilla_WinHeaderOnlyUtils_h