-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
win7-8-8.1-support_thorium.patch
21612 lines (20610 loc) · 854 KB
/
win7-8-8.1-support_thorium.patch
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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/README.md b/README.md
index 590f4c94f2067..7091f956b88b7 100644
--- a/README.md
+++ b/README.md
@@ -1,21 +1,36 @@
-# ![Logo](chrome/app/theme/chromium/product_logo_64.png) Chromium
+# Supermium + Thorium
+An up-to-date, compiler optimized Chromium-based web browser compatible with Windows XP, Vista, 7, 8, and 8.1.
-Chromium is an open-source browser project that aims to build a safer, faster,
-and more stable way for all users to experience the web.
+The Supermium web browser also provides the following features:
-The project's web site is https://www.chromium.org.
+-Aero Glass and Aero Glass-style titlebars instead of Windows 10-style ones (#disable-aero in chrome://flags for the latter)
-To check out the source code locally, don't use `git clone`! Instead,
-follow [the instructions on how to get the code](docs/get_the_code.md).
+-Turnaround for major vulnerability patches generally less than one week from upstream disclosure
-Documentation in the source is rooted in [docs/README.md](docs/README.md).
+-A functional sandbox for enhanced security
-Learn how to [Get Around the Chromium Source Code Directory
-Structure](https://www.chromium.org/developers/how-tos/getting-around-the-chrome-source-code).
+-Google Sync
-For historical reasons, there are some small top level directories. Now the
-guidance is that new top level directories are for product (e.g. Chrome,
-Android WebView, Ash). Even if these products have multiple executables, the
-code should be in subdirectories of the product.
+-On Windows 7 and up, Widevine CDM support for viewing DRM content
-If you found a bug, please file it at https://crbug.com/new.
+-GDI font rendering, using #force-gdi in chrome://flags
+
+-Persistent dark mode on the browser's UI elements, using #force-dark-mode in chrome://flags
+
+-Custom tab options including trapezoidal tabs, transparent tabs, and outlined tabs
+
+-Many flags from ungoogled-chromium
+
+-Support for SSE2-only processors in the 32 bit build
+
+**Future support for:**
+
+-Windows 2000
+
+-Ungoogled Chromium features (in progress)
+
+-WebGPU for D3D9 renderer
+
+-Manifest V2 (once removed by Google)
+
+...and more! If there's a feature you want, which cannot be replicated by a Manifest v2/v3 extension, please ask!
diff --git a/base/BUILD.gn b/base/BUILD.gn
index c00c41bc2c7fd..2f4557aa18fde 100644
--- a/base/BUILD.gn
+++ b/base/BUILD.gn
@@ -135,11 +135,6 @@ if (is_win) {
ldflags = [
# Linking with shcore.lib causes the shcore api forwarder dll to load.
"/DELAYLOAD:api-ms-win-shcore-scaling-l1-1-1.dll",
-
- # Linking with OneCore.lib causes the next three dlls to load.
- "/DELAYLOAD:api-ms-win-core-realtime-l1-1-1.dll",
- "/DELAYLOAD:api-ms-win-power-base-l1-1-0.dll",
- "/DELAYLOAD:api-ms-win-power-setting-l1-1-0.dll",
"/DELAYLOAD:cfgmgr32.dll",
"/DELAYLOAD:powrprof.dll",
"/DELAYLOAD:setupapi.dll",
@@ -1921,7 +1916,6 @@ component("base") {
libs += [
"cfgmgr32.lib",
"ntdll.lib",
- "onecore.lib",
"pdh.lib",
"powrprof.lib",
"propsys.lib",
diff --git a/base/allocator/partition_allocator/src/partition_alloc/page_allocator_internals_win.h b/base/allocator/partition_allocator/src/partition_alloc/page_allocator_internals_win.h
index 44ef45c3e325f..994060bee0a78 100644
--- a/base/allocator/partition_allocator/src/partition_alloc/page_allocator_internals_win.h
+++ b/base/allocator/partition_allocator/src/partition_alloc/page_allocator_internals_win.h
@@ -16,6 +16,17 @@
namespace partition_alloc::internal {
+namespace {
+
+// On Windows, discarded pages are not returned to the system immediately and
+// not guaranteed to be zeroed when returned to the application.
+using DiscardVirtualMemoryFunction = DWORD(WINAPI*)(PVOID virtualAddress,
+ SIZE_T size);
+DiscardVirtualMemoryFunction s_discard_virtual_memory =
+ reinterpret_cast<DiscardVirtualMemoryFunction>(-1);
+
+} // namespace
+
// |VirtualAlloc| will fail if allocation at the hint address is blocked.
constexpr bool kHintIsAdvisory = false;
std::atomic<int32_t> s_allocPageErrorCode{ERROR_SUCCESS};
@@ -226,12 +237,20 @@ bool TryRecommitSystemPagesInternal(
}
void DiscardSystemPagesInternal(uintptr_t address, size_t length) {
+ if (s_discard_virtual_memory ==
+ reinterpret_cast<DiscardVirtualMemoryFunction>(-1)) {
+ s_discard_virtual_memory =
+ reinterpret_cast<DiscardVirtualMemoryFunction>(GetProcAddress(
+ GetModuleHandle(L"Kernel32.dll"), "DiscardVirtualMemory"));
+ }
+
void* ptr = reinterpret_cast<void*>(address);
// Use DiscardVirtualMemory when available because it releases faster than
// MEM_RESET.
- DWORD ret = DiscardVirtualMemory(ptr, length);
- // DiscardVirtualMemory is buggy in Win10 SP0, so fall back to MEM_RESET on
- // failure.
+ DWORD ret = 1;
+ if (s_discard_virtual_memory) {
+ ret = s_discard_virtual_memory(ptr, length);
+ }
if (ret) {
PA_CHECK(VirtualAllocWithRetry(ptr, length, MEM_RESET, PAGE_READWRITE));
}
diff --git a/base/allocator/partition_allocator/src/partition_alloc/partition_address_space.cc b/base/allocator/partition_allocator/src/partition_alloc/partition_address_space.cc
index 39e574a61a3eb..37ba12bfa8c46 100644
--- a/base/allocator/partition_allocator/src/partition_alloc/partition_address_space.cc
+++ b/base/allocator/partition_allocator/src/partition_alloc/partition_address_space.cc
@@ -43,6 +43,34 @@ namespace {
#if BUILDFLAG(IS_WIN)
+#if PA_CONFIG(DYNAMICALLY_SELECT_POOL_SIZE)
+bool IsLegacyWindowsVersion() {
+ // Use ::RtlGetVersion instead of ::GetVersionEx or helpers from
+ // VersionHelpers.h because those alternatives change their behavior depending
+ // on whether or not the calling executable has a compatibility manifest
+ // resource. It's better for the allocator to not depend on that to decide the
+ // pool size.
+ // Assume legacy if ::RtlGetVersion is not available or it fails.
+ using RtlGetVersion = LONG(WINAPI*)(OSVERSIONINFOEX*);
+ const RtlGetVersion rtl_get_version = reinterpret_cast<RtlGetVersion>(
+ ::GetProcAddress(::GetModuleHandle(L"ntdll.dll"), "RtlGetVersion"));
+ if (!rtl_get_version) {
+ return true;
+ }
+
+ OSVERSIONINFOEX version_info = {};
+ version_info.dwOSVersionInfoSize = sizeof(version_info);
+ if (rtl_get_version(&version_info) != ERROR_SUCCESS) {
+ return true;
+ }
+
+ // Anything prior to Windows 8.1 is considered legacy for the allocator.
+ // Windows 8.1 is major 6 with minor 3.
+ return version_info.dwMajorVersion < 6 ||
+ (version_info.dwMajorVersion == 6 && version_info.dwMinorVersion < 3);
+}
+#endif // PA_CONFIG(DYNAMICALLY_SELECT_POOL_SIZE)
+
PA_NOINLINE void HandlePoolAllocFailureOutOfVASpace() {
PA_NO_CODE_FOLDING();
PA_CHECK(false);
@@ -67,9 +95,9 @@ PA_NOINLINE void HandlePoolAllocFailure() {
HandlePoolAllocFailureOutOfVASpace();
} else if (alloc_page_error_code == ERROR_COMMITMENT_LIMIT ||
alloc_page_error_code == ERROR_COMMITMENT_MINIMUM) {
- // Should not happen, since as of Windows 8.1+, reserving address space
- // should not be charged against the commit limit, aside from a very small
- // amount per 64kiB block. Keep this path anyway, to check in crash reports.
+ // On Windows <8.1, MEM_RESERVE increases commit charge to account for
+ // not-yet-committed PTEs needed to cover that VA space, if it was to be
+ // committed (see crbug.com/1101421#c16).
HandlePoolAllocFailureOutOfCommitCharge();
} else
#endif // BUILDFLAG(IS_WIN)
@@ -88,10 +116,7 @@ std::ptrdiff_t PartitionAddressSpace::brp_pool_shadow_offset_ = 0;
#endif
#if PA_CONFIG(DYNAMICALLY_SELECT_POOL_SIZE)
-#if !BUILDFLAG(IS_IOS)
-#error Dynamic pool size is only supported on iOS.
-#endif
-
+#if BUILDFLAG(IS_IOS)
namespace {
bool IsIOSTestProcess() {
// On iOS, only applications with the extended virtual addressing entitlement
@@ -133,6 +158,15 @@ PA_ALWAYS_INLINE size_t PartitionAddressSpace::RegularPoolSize() {
PA_ALWAYS_INLINE size_t PartitionAddressSpace::BRPPoolSize() {
return IsIOSTestProcess() ? kBRPPoolSizeForIOSTestProcess : kBRPPoolSize;
}
+#else
+PA_ALWAYS_INLINE size_t PartitionAddressSpace::RegularPoolSize() {
+ return IsLegacyWindowsVersion() ? kRegularPoolSizeForLegacyWindows
+ : kRegularPoolSize;
+}
+PA_ALWAYS_INLINE size_t PartitionAddressSpace::BRPPoolSize() {
+ return IsLegacyWindowsVersion() ? kBRPPoolSizeForLegacyWindows : kBRPPoolSize;
+}
+#endif // BUILDFLAG(IS_IOS)
#endif // PA_CONFIG(DYNAMICALLY_SELECT_POOL_SIZE)
void PartitionAddressSpace::Init() {
diff --git a/base/allocator/partition_allocator/src/partition_alloc/partition_address_space.h b/base/allocator/partition_allocator/src/partition_alloc/partition_address_space.h
index 0b115ee775d6d..046918e248fb7 100644
--- a/base/allocator/partition_allocator/src/partition_alloc/partition_address_space.h
+++ b/base/allocator/partition_allocator/src/partition_alloc/partition_address_space.h
@@ -276,6 +276,16 @@ class PA_COMPONENT_EXPORT(PARTITION_ALLOC) PartitionAddressSpace {
static constexpr size_t kThreadIsolatedPoolSize = kGiB / 4;
static_assert(std::has_single_bit(kThreadIsolatedPoolSize));
#endif
+#if PA_CONFIG(DYNAMICALLY_SELECT_POOL_SIZE)
+ // We can't afford pool sizes as large as kPoolMaxSize on Windows <8.1 (see
+ // crbug.com/1101421 and crbug.com/1217759).
+ static constexpr size_t kRegularPoolSizeForLegacyWindows = 4 * kGiB;
+ static constexpr size_t kBRPPoolSizeForLegacyWindows = 4 * kGiB;
+ static_assert(kRegularPoolSizeForLegacyWindows < kRegularPoolSize);
+ static_assert(kBRPPoolSizeForLegacyWindows < kBRPPoolSize);
+ static_assert(std::has_single_bit(kRegularPoolSizeForLegacyWindows));
+ static_assert(std::has_single_bit(kBRPPoolSizeForLegacyWindows));
+#endif // PA_CONFIG(DYNAMICALLY_SELECT_POOL_SIZE)
static constexpr size_t kConfigurablePoolMaxSize = kPoolMaxSize;
static constexpr size_t kConfigurablePoolMinSize = 1 * kGiB;
static_assert(kConfigurablePoolMinSize <= kConfigurablePoolMaxSize);
diff --git a/base/allocator/partition_allocator/src/partition_alloc/partition_alloc_base/rand_util_win.cc b/base/allocator/partition_allocator/src/partition_alloc/partition_alloc_base/rand_util_win.cc
index fd1de0bd29861..c6d5a39bfe2e3 100644
--- a/base/allocator/partition_allocator/src/partition_alloc/partition_alloc_base/rand_util_win.cc
+++ b/base/allocator/partition_allocator/src/partition_alloc/partition_alloc_base/rand_util_win.cc
@@ -13,31 +13,27 @@
#include "partition_alloc/partition_alloc_base/check.h"
-// Prototype for ProcessPrng.
-// See: https://learn.microsoft.com/en-us/windows/win32/seccng/processprng
-extern "C" {
-BOOL WINAPI ProcessPrng(PBYTE pbData, SIZE_T cbData);
-}
+// #define needed to link in RtlGenRandom(), a.k.a. SystemFunction036. See the
+// "Community Additions" comment on MSDN here:
+// http://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx
+#define SystemFunction036 NTAPI SystemFunction036
+#include <NTSecAPI.h>
+#undef SystemFunction036
namespace partition_alloc::internal::base {
void RandBytes(void* output, size_t output_length) {
- // Import bcryptprimitives directly rather than cryptbase to avoid opening a
- // handle to \\Device\KsecDD in the renderer.
- // Note: we cannot use a magic static here as PA runs too early in process
- // startup, but this should be safe as the process will be single-threaded
- // when this first runs.
- static decltype(&ProcessPrng) process_prng_fn = nullptr;
- if (!process_prng_fn) {
- HMODULE hmod = LoadLibraryW(L"bcryptprimitives.dll");
- PA_BASE_CHECK(hmod);
- process_prng_fn = reinterpret_cast<decltype(&ProcessPrng)>(
- GetProcAddress(hmod, "ProcessPrng"));
- PA_BASE_CHECK(process_prng_fn);
- }
- BOOL success = process_prng_fn(static_cast<BYTE*>(output), output_length);
- // ProcessPrng is documented to always return TRUE.
- PA_BASE_CHECK(success);
+ char* output_ptr = static_cast<char*>(output);
+ while (output_length > 0) {
+ const ULONG output_bytes_this_pass = static_cast<ULONG>(std::min(
+ output_length, static_cast<size_t>(std::numeric_limits<ULONG>::max())));
+ const bool success =
+ RtlGenRandom(output_ptr, output_bytes_this_pass) != FALSE;
+ PA_BASE_CHECK(success);
+ output_length -= output_bytes_this_pass;
+ output_ptr += output_bytes_this_pass;
+}
+
}
} // namespace partition_alloc::internal::base
diff --git a/base/allocator/partition_allocator/src/partition_alloc/partition_alloc_config.h b/base/allocator/partition_allocator/src/partition_alloc/partition_alloc_config.h
index c6445f267971f..5e19e5f01869b 100644
--- a/base/allocator/partition_allocator/src/partition_alloc/partition_alloc_config.h
+++ b/base/allocator/partition_allocator/src/partition_alloc/partition_alloc_config.h
@@ -34,7 +34,7 @@
static_assert(sizeof(void*) == 8, "");
#else
static_assert(sizeof(void*) != 8, "");
-#endif // PA_CONFIG(HAS_64_BITS_POINTERS)
+#endif // BUILDFLAG(HAS_64_BITS_POINTERS)
#if BUILDFLAG(HAS_64_BIT_POINTERS) && \
(defined(__ARM_NEON) || defined(__ARM_NEON__)) && defined(__ARM_FP)
@@ -43,18 +43,23 @@ static_assert(sizeof(void*) != 8, "");
#define PA_CONFIG_STARSCAN_NEON_SUPPORTED() 0
#endif
-#if BUILDFLAG(HAS_64_BIT_POINTERS) && BUILDFLAG(IS_IOS)
+#if BUILDFLAG(HAS_64_BIT_POINTERS) && (BUILDFLAG(IS_IOS) || BUILDFLAG(IS_WIN))
// Allow PA to select an alternate pool size at run-time before initialization,
// rather than using a single constexpr value.
//
// This is needed on iOS because iOS test processes can't handle large pools
// (see crbug.com/1250788).
//
+// This is needed on Windows, because OS versions <8.1 incur commit charge even
+// on reserved address space, thus don't handle large pools well (see
+// crbug.com/1101421 and crbug.com/1217759).
+//
// This setting is specific to 64-bit, as 32-bit has a different implementation.
#define PA_CONFIG_DYNAMICALLY_SELECT_POOL_SIZE() 1
#else
#define PA_CONFIG_DYNAMICALLY_SELECT_POOL_SIZE() 0
-#endif // BUILDFLAG(HAS_64_BIT_POINTERS) && BUILDFLAG(IS_IOS)
+#endif // BUILDFLAG(HAS_64_BIT_POINTERS) && (BUILDFLAG(IS_IOS) ||
+ // BUILDFLAG(IS_WIN))
#if BUILDFLAG(HAS_64_BIT_POINTERS) && \
(BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_ANDROID))
diff --git a/base/command_line.cc b/base/command_line.cc
index 69e4303165259..da42a22ed2894 100644
--- a/base/command_line.cc
+++ b/base/command_line.cc
@@ -27,8 +27,8 @@
#include <windows.h>
#include <shellapi.h>
-
#include "base/strings/string_util_win.h"
+#include "base/win/windows_version.h"
#endif // BUILDFLAG(IS_WIN)
namespace base {
@@ -310,6 +310,7 @@ bool CommandLine::HasSwitch(StringPiece switch_string) const {
}
bool CommandLine::HasSwitch(const char switch_constant[]) const {
+
return HasSwitch(StringPiece(switch_constant));
}
diff --git a/base/features.cc b/base/features.cc
index 50cb76282689b..82151c93ffd3e 100644
--- a/base/features.cc
+++ b/base/features.cc
@@ -34,6 +34,20 @@ BASE_FEATURE(kUseRustJsonParser,
BASE_FEATURE(kJsonNegativeZero, "JsonNegativeZero", FEATURE_ENABLED_BY_DEFAULT);
+
+
+
+
+BASE_FEATURE(kIncognitoBrandConsistencyForDesktop,
+ "IncognitoBrandConsistencyForDesktop",
+ base::FEATURE_DISABLED_BY_DEFAULT);
+
+#if BUILDFLAG(IS_WIN)
+BASE_FEATURE(kForceGdi,
+ "ForceGdi",
+ base::FEATURE_DISABLED_BY_DEFAULT);
+#endif
+
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS)
// Force to enable LowEndDeviceMode partially on Android 3Gb devices.
// (see PartialLowEndModeOnMidRangeDevices below)
diff --git a/base/features.h b/base/features.h
index a80ac17fb20d1..6b050f7b8bc60 100644
--- a/base/features.h
+++ b/base/features.h
@@ -25,6 +25,14 @@ BASE_EXPORT BASE_DECLARE_FEATURE(kUseRustJsonParser);
BASE_EXPORT BASE_DECLARE_FEATURE(kJsonNegativeZero);
+
+
+BASE_EXPORT BASE_DECLARE_FEATURE(kIncognitoBrandConsistencyForDesktop);
+
+#if BUILDFLAG(IS_WIN)
+BASE_EXPORT BASE_DECLARE_FEATURE(kForceGdi);
+#endif
+
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS)
BASE_EXPORT BASE_DECLARE_FEATURE(kPartialLowEndModeOn3GbDevices);
BASE_EXPORT BASE_DECLARE_FEATURE(kPartialLowEndModeOnMidRangeDevices);
diff --git a/base/files/file_util_win.cc b/base/files/file_util_win.cc
index a12c3324abdba..1ee643a98148f 100644
--- a/base/files/file_util_win.cc
+++ b/base/files/file_util_win.cc
@@ -698,7 +698,7 @@ bool GetSecureSystemTemp(FilePath* temp) {
CHECK(temp);
- for (const auto key : {DIR_WINDOWS, DIR_PROGRAM_FILES}) {
+ for (const auto key : {DIR_WINDOWS, DIR_IE_INTERNET_CACHE, DIR_PROGRAM_FILES}) {
FilePath secure_system_temp;
if (!PathService::Get(key, &secure_system_temp)) {
continue;
@@ -1118,11 +1118,37 @@ bool SetNonBlocking(int fd) {
return false;
}
+namespace {
+
+// ::PrefetchVirtualMemory() is only available on Windows 8 and above. Chrome
+// supports Windows 7, so we need to check for the function's presence
+// dynamically.
+using PrefetchVirtualMemoryPtr = decltype(&::PrefetchVirtualMemory);
+
+// Returns null if ::PrefetchVirtualMemory() is not available.
+PrefetchVirtualMemoryPtr GetPrefetchVirtualMemoryPtr() {
+ HMODULE kernel32_dll = ::GetModuleHandleA("kernel32.dll");
+ return reinterpret_cast<PrefetchVirtualMemoryPtr>(
+ GetProcAddress(kernel32_dll, "PrefetchVirtualMemory"));
+}
+
+} // namespace
+
bool PreReadFile(const FilePath& file_path,
bool is_executable,
int64_t max_bytes) {
DCHECK_GE(max_bytes, 0);
+ // On Win8 and higher use ::PrefetchVirtualMemory(). This is better than a
+ // simple data file read, more from a RAM perspective than CPU. This is
+ // because reading the file as data results in double mapping to
+ // Image/executable pages for all pages of code executed.
+ static PrefetchVirtualMemoryPtr prefetch_virtual_memory =
+ GetPrefetchVirtualMemoryPtr();
+
+ if (prefetch_virtual_memory == nullptr)
+ return internal::PreReadFileSlow(file_path, max_bytes);
+
if (max_bytes == 0) {
// ::PrefetchVirtualMemory() fails when asked to read zero bytes.
// base::MemoryMappedFile::Initialize() fails on an empty file.
@@ -1145,7 +1171,7 @@ bool PreReadFile(const FilePath& file_path,
// simple data file read, more from a RAM perspective than CPU. This is
// because reading the file as data results in double mapping to
// Image/executable pages for all pages of code executed.
- if (!::PrefetchVirtualMemory(::GetCurrentProcess(),
+ if (!prefetch_virtual_memory(::GetCurrentProcess(),
/*NumberOfEntries=*/1, &address_range,
/*Flags=*/0)) {
return internal::PreReadFileSlow(file_path, max_bytes);
@@ -1155,7 +1181,7 @@ bool PreReadFile(const FilePath& file_path,
bool PreventExecuteMapping(const FilePath& path) {
if (!base::FeatureList::IsEnabled(
- features::kEnforceNoExecutableFileHandles)) {
+ features::kEnforceNoExecutableFileHandles) || base::win::GetVersion() == base::win::Version::PRE_XP) {
return true;
}
diff --git a/base/memory/discardable_shared_memory.cc b/base/memory/discardable_shared_memory.cc
index ecba26075f008..2159171af8da0 100644
--- a/base/memory/discardable_shared_memory.cc
+++ b/base/memory/discardable_shared_memory.cc
@@ -422,11 +422,23 @@ bool DiscardableSharedMemory::Purge(Time current_time) {
#elif BUILDFLAG(IS_WIN)
// On Windows, discarded pages are not returned to the system immediately and
// not guaranteed to be zeroed when returned to the application.
+ using DiscardVirtualMemoryFunction =
+ DWORD(WINAPI*)(PVOID virtualAddress, SIZE_T size);
+ static DiscardVirtualMemoryFunction discard_virtual_memory =
+ reinterpret_cast<DiscardVirtualMemoryFunction>(GetProcAddress(
+ GetModuleHandle(L"Kernel32.dll"), "DiscardVirtualMemory"));
+
char* address = static_cast<char*>(shared_memory_mapping_.memory()) +
AlignToPageSize(sizeof(SharedState));
size_t length = AlignToPageSize(mapped_size_);
- DWORD ret = DiscardVirtualMemory(address, length);
+ // Use DiscardVirtualMemory when available because it releases faster than
+ // MEM_RESET.
+ DWORD ret = ERROR_NOT_SUPPORTED;
+ if (discard_virtual_memory) {
+ ret = discard_virtual_memory(address, length);
+ }
+
// DiscardVirtualMemory is buggy in Win10 SP0, so fall back to MEM_RESET on
// failure.
if (ret != ERROR_SUCCESS) {
diff --git a/base/memory/platform_shared_memory_region_win.cc b/base/memory/platform_shared_memory_region_win.cc
index 96986601f6282..ab0bda806704a 100644
--- a/base/memory/platform_shared_memory_region_win.cc
+++ b/base/memory/platform_shared_memory_region_win.cc
@@ -14,7 +14,12 @@
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/process/process_handle.h"
+#include "base/rand_util.h"
#include "base/strings/string_util.h"
+#include "base/strings/stringprintf.h"
+#include "base/strings/utf_string_conversions.h"
+#include "base/win/windows_version.h"
+
namespace base::subtle {
@@ -73,6 +78,7 @@ HANDLE CreateFileMappingWithReducedPermissions(SECURITY_ATTRIBUTES* sa,
HANDLE h = CreateFileMapping(INVALID_HANDLE_VALUE, sa, PAGE_READWRITE, 0,
static_cast<DWORD>(rounded_size), name);
if (!h) {
+ LOG(ERROR) << "CreateFileMappingW failed with error " << GetLastError() << ".";
return nullptr;
}
@@ -211,6 +217,17 @@ PlatformSharedMemoryRegion PlatformSharedMemoryRegion::Create(Mode mode,
}
std::u16string name;
+ if (win::GetVersion() < win::Version::WIN8_1) {
+ // Windows < 8.1 ignores DACLs on certain unnamed objects (like shared
+ // sections). So, we generate a random name when we need to enforce
+ // read-only.
+ uint64_t rand_values[4];
+ RandBytes(&rand_values, sizeof(rand_values));
+ name = ASCIIToUTF16(StringPrintf("CrSharedMem_%016llx%016llx%016llx%016llx",
+ rand_values[0], rand_values[1],
+ rand_values[2], rand_values[3]));
+ DCHECK(!name.empty());
+ }
SECURITY_ATTRIBUTES sa = {sizeof(sa), &sd, FALSE};
// Ask for the file mapping with reduced permisions to avoid passing the
// access control permissions granted by default into unpriviledged process.
diff --git a/base/message_loop/message_pump_win.cc b/base/message_loop/message_pump_win.cc
index c30066f515f63..479ba2345565e 100644
--- a/base/message_loop/message_pump_win.cc
+++ b/base/message_loop/message_pump_win.cc
@@ -792,6 +792,7 @@ bool MessagePumpForIO::GetIOItem(DWORD timeout, IOItem* item) {
&overlapped, timeout)) {
if (!overlapped)
return false; // Nothing in the queue.
+
item->error = GetLastError();
item->bytes_transfered = 0;
}
diff --git a/base/metrics/field_trial.cc b/base/metrics/field_trial.cc
index 103e6460442ef..3129ce529d94b 100644
--- a/base/metrics/field_trial.cc
+++ b/base/metrics/field_trial.cc
@@ -51,6 +51,7 @@
#if BUILDFLAG(IS_WIN)
#include <windows.h>
+#include "base/win/windows_version.h"
#endif
#if BUILDFLAG(IS_FUCHSIA)
@@ -714,6 +715,12 @@ void FieldTrialList::CreateTrialsInChildProcess(const CommandLine& cmd_line,
uint32_t fd_key) {
global_->create_trials_in_child_process_called_ = true;
+#if BUILDFLAG(IS_WIN)
+ if (win::GetVersion() < win::Version::VISTA) {
+ return;
+ }
+#endif
+
#if BUILDFLAG(USE_BLINK)
// TODO(crbug.com/867558): Change to a CHECK.
if (cmd_line.HasSwitch(switches::kFieldTrialHandle)) {
diff --git a/base/power_monitor/power_monitor_device_source_win.cc b/base/power_monitor/power_monitor_device_source_win.cc
index 4e7b6563bc229..3925a372ed509 100644
--- a/base/power_monitor/power_monitor_device_source_win.cc
+++ b/base/power_monitor/power_monitor_device_source_win.cc
@@ -32,8 +32,9 @@ void ProcessWmPowerBroadcastMessage(WPARAM event_id) {
power_event = PowerMonitorSource::POWER_STATE_EVENT;
break;
case PBT_APMRESUMEAUTOMATIC: // Resume from suspend.
- // We don't notify for PBT_APMRESUMESUSPEND
- // because, if it occurs, it is always sent as a
+ //case PBT_APMRESUMESUSPEND: // User-initiated resume from suspend.
+ // We don't notify for this latter event
+ // because if it occurs it is always sent as a
// second event after PBT_APMRESUMEAUTOMATIC.
power_event = PowerMonitorSource::RESUME_EVENT;
break;
@@ -55,6 +56,28 @@ void ProcessWmPowerBroadcastMessage(WPARAM event_id) {
ProcessPowerEventHelper(power_event);
}
+HPOWERNOTIFY RegisterSuspendResumeNotification(HANDLE hRecipient, DWORD Flags) {
+ const auto register_suspend_resume_notification_ptr =
+ reinterpret_cast<decltype(&::RegisterSuspendResumeNotification)>(
+ ::GetProcAddress(::GetModuleHandle(L"user32.dll"),
+ "RegisterSuspendResumeNotification"));
+ if (!register_suspend_resume_notification_ptr)
+ return nullptr;
+
+ return register_suspend_resume_notification_ptr(hRecipient, Flags);
+}
+
+BOOL UnregisterSuspendResumeNotification(HPOWERNOTIFY Handle) {
+ const auto unregister_suspend_resume_notification_ptr =
+ reinterpret_cast<decltype(&::UnregisterSuspendResumeNotification)>(
+ ::GetProcAddress(::GetModuleHandle(L"user32.dll"),
+ "UnregisterSuspendResumeNotification"));
+ if (!unregister_suspend_resume_notification_ptr)
+ return FALSE;
+
+ return unregister_suspend_resume_notification_ptr(Handle);
+}
+
} // namespace
void PowerMonitorDeviceSource::PlatformInit() {
@@ -79,7 +102,7 @@ void PowerMonitorDeviceSource::PlatformDestroy() {
// battery power. Returns true if running on battery.
bool PowerMonitorDeviceSource::IsOnBatteryPower() {
SYSTEM_POWER_STATUS status;
- if (!::GetSystemPowerStatus(&status)) {
+ if (!GetSystemPowerStatus(&status)) {
DPLOG(ERROR) << "GetSystemPowerStatus failed";
return false;
}
@@ -92,7 +115,8 @@ int PowerMonitorDeviceSource::GetInitialSpeedLimit() {
return PowerThermalObserver::kSpeedLimitMax;
}
-PowerMonitorDeviceSource::PowerMessageWindow::PowerMessageWindow() {
+PowerMonitorDeviceSource::PowerMessageWindow::PowerMessageWindow()
+ : instance_(NULL), message_hwnd_(NULL) {
if (!CurrentUIThread::IsSet()) {
// Creating this window in (e.g.) a renderer inhibits shutdown on Windows.
// See http://crbug.com/230122. TODO(vandebo): http://crbug.com/236031
@@ -105,18 +129,21 @@ PowerMonitorDeviceSource::PowerMessageWindow::PowerMessageWindow() {
kWindowClassName,
&base::win::WrappedWindowProc<
PowerMonitorDeviceSource::PowerMessageWindow::WndProcThunk>,
- 0, 0, 0, nullptr, nullptr, nullptr, nullptr, nullptr, &window_class);
+ 0, 0, 0, NULL, NULL, NULL, NULL, NULL,
+ &window_class);
instance_ = window_class.hInstance;
- ATOM clazz = ::RegisterClassEx(&window_class);
+ ATOM clazz = RegisterClassEx(&window_class);
DCHECK(clazz);
message_hwnd_ =
- ::CreateWindowEx(WS_EX_NOACTIVATE, kWindowClassName, nullptr, WS_POPUP, 0,
- 0, 0, 0, nullptr, nullptr, instance_, nullptr);
+ CreateWindowEx(WS_EX_NOACTIVATE, kWindowClassName, NULL, WS_POPUP, 0, 0,
+ 0, 0, NULL, NULL, instance_, NULL);
if (message_hwnd_) {
- // On machines with modern standby calling RegisterSuspendResumeNotification
- // is required in order to get the PBT_APMSUSPEND message.
- power_notify_handle_ = ::RegisterSuspendResumeNotification(
+ // On machines with modern standby and Win8+, calling
+ // RegisterSuspendResumeNotification is required in order to get the
+ // PBT_APMSUSPEND message. The notification is no longer automatically
+ // fired.
+ power_notify_handle_ = base::RegisterSuspendResumeNotification(
message_hwnd_, DEVICE_NOTIFY_WINDOW_HANDLE);
}
}
@@ -124,10 +151,10 @@ PowerMonitorDeviceSource::PowerMessageWindow::PowerMessageWindow() {
PowerMonitorDeviceSource::PowerMessageWindow::~PowerMessageWindow() {
if (message_hwnd_) {
if (power_notify_handle_)
- ::UnregisterSuspendResumeNotification(power_notify_handle_);
+ base::UnregisterSuspendResumeNotification(power_notify_handle_);
- ::DestroyWindow(message_hwnd_);
- ::UnregisterClass(kWindowClassName, instance_);
+ DestroyWindow(message_hwnd_);
+ UnregisterClass(kWindowClassName, instance_);
}
}
diff --git a/base/process/launch_win.cc b/base/process/launch_win.cc
index eca51fa33f7fc..af5924614ed2e 100644
--- a/base/process/launch_win.cc
+++ b/base/process/launch_win.cc
@@ -273,14 +273,17 @@ Process LaunchProcess(const CommandLine::StringType& cmdline,
if (!options.handles_to_inherit.empty())
++attribute_count;
- // Reserve space for attributes.
- if (attribute_count > 0) {
- if (!startup_info_wrapper.InitializeProcThreadAttributeList(
- attribute_count)) {
- DPLOG(ERROR);
- return Process();
- }
- flags |= EXTENDED_STARTUPINFO_PRESENT;
+
+ if (base::win::GetVersion() >= base::win::Version::VISTA) {
+ // Reserve space for attributes.
+ if (attribute_count > 0) {
+ if (!startup_info_wrapper.InitializeProcThreadAttributeList(
+ attribute_count)) {
+ DPLOG(ERROR);
+ return Process();
+ }
+ flags |= EXTENDED_STARTUPINFO_PRESENT;
+ }
}
// Set PROC_THREAD_ATTRIBUTE_MITIGATION_POLICY.
@@ -298,7 +301,11 @@ Process LaunchProcess(const CommandLine::StringType& cmdline,
// Set PROC_THREAD_ATTRIBUTE_HANDLE_LIST.
bool inherit_handles = options.inherit_mode == LaunchOptions::Inherit::kAll;
- if (!options.handles_to_inherit.empty()) {
+ if (!options.handles_to_inherit.empty() &&
+ base::win::GetVersion() < base::win::Version::VISTA) {
+ inherit_handles = true;
+ }
+ if (!options.handles_to_inherit.empty() && base::win::GetVersion() >= base::win::Version::VISTA) {
DCHECK_GT(attribute_count, 0u);
DCHECK_EQ(options.inherit_mode, LaunchOptions::Inherit::kSpecific);
diff --git a/base/process/process_util_unittest.cc b/base/process/process_util_unittest.cc
index 18bed2128814c..d563938dc4e2e 100644
--- a/base/process/process_util_unittest.cc
+++ b/base/process/process_util_unittest.cc
@@ -829,11 +829,19 @@ TEST_F(ProcessUtilTest, LaunchAsUser) {
}
MULTIPROCESS_TEST_MAIN(ChildVerifiesCetDisabled) {
- // Policy not defined for Win < Win10 20H1 but that's ok.
+ auto get_process_mitigation_policy =
+ reinterpret_cast<decltype(&GetProcessMitigationPolicy)>(::GetProcAddress(
+ ::GetModuleHandleW(L"kernel32.dll"), "GetProcessMitigationPolicy"));
+
+ // Not available for Win7 but this process should still work.
+ if (!get_process_mitigation_policy)
+ return kSuccess;
+
+ // Policy not defined for Win < Win10 20H1 but that's also ok.
PROCESS_MITIGATION_USER_SHADOW_STACK_POLICY policy = {};
- if (GetProcessMitigationPolicy(GetCurrentProcess(),
- ProcessUserShadowStackPolicy, &policy,
- sizeof(policy))) {
+ if (get_process_mitigation_policy(GetCurrentProcess(),
+ ProcessUserShadowStackPolicy, &policy,
+ sizeof(policy))) {
if (policy.EnableUserShadowStack)
return 1;
}
diff --git a/base/process/process_win.cc b/base/process/process_win.cc
index 9a414d886f779..917e2a9a18233 100644
--- a/base/process/process_win.cc
+++ b/base/process/process_win.cc
@@ -265,35 +265,12 @@ bool Process::SetPriority(Priority priority) {
// priority inversion, and having a process put itself in background mode is
// broken in Windows 11 22H2. So, it is no longer supported. See
// https://crbug.com/1396155 for details.
+ // NOTE: NtSetInformationProcess call (SetProcessInformation really) using ProcessPowerThrottling class removed because it is useless before Windows 10.
DCHECK(!is_current());
const DWORD priority_class = priority == Priority::kBestEffort
? IDLE_PRIORITY_CLASS
: NORMAL_PRIORITY_CLASS;
- if (base::win::OSInfo::GetInstance()->version() >=
- base::win::Version::WIN11 &&
- FeatureList::IsEnabled(kUseEcoQoSForBackgroundProcess)) {
- PROCESS_POWER_THROTTLING_STATE power_throttling;
- RtlZeroMemory(&power_throttling, sizeof(power_throttling));
- power_throttling.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION;
-
- if (priority == Priority::kBestEffort) {
- // Sets Eco QoS level.
- power_throttling.ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
- power_throttling.StateMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
- } else {
- // Uses system default.
- power_throttling.ControlMask = 0;
- power_throttling.StateMask = 0;
- }
- bool ret =
- ::SetProcessInformation(Handle(), ProcessPowerThrottling,
- &power_throttling, sizeof(power_throttling));
- if (ret == 0) {
- DPLOG(ERROR) << "Setting process QoS policy fails";
- }
- }
-
return (::SetPriorityClass(Handle(), priority_class) != 0);
}
diff --git a/base/rand_util_win.cc b/base/rand_util_win.cc
index 6aae763d98715..f1f21d13fba42 100644
--- a/base/rand_util_win.cc
+++ b/base/rand_util_win.cc
@@ -9,6 +9,13 @@
#include <stddef.h>
#include <stdint.h>
+// #define needed to link in RtlGenRandom(), a.k.a. SystemFunction036. See the
+// "Community Additions" comment on MSDN here:
+// http://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx
+#define SystemFunction036 NTAPI SystemFunction036
+#include <NTSecAPI.h>
+#undef SystemFunction036
+
#include <algorithm>
#include <atomic>
#include <limits>
@@ -18,12 +25,6 @@
#include "third_party/boringssl/src/include/openssl/crypto.h"
#include "third_party/boringssl/src/include/openssl/rand.h"
-// Prototype for ProcessPrng.
-// See: https://learn.microsoft.com/en-us/windows/win32/seccng/processprng
-extern "C" {
-BOOL WINAPI ProcessPrng(PBYTE pbData, SIZE_T cbData);
-}
-
namespace base {
namespace internal {
@@ -53,18 +54,6 @@ bool UseBoringSSLForRandBytes() {
namespace {
-// Import bcryptprimitives!ProcessPrng rather than cryptbase!RtlGenRandom to
-// avoid opening a handle to \\Device\KsecDD in the renderer.
-decltype(&ProcessPrng) GetProcessPrng() {
- HMODULE hmod = LoadLibraryW(L"bcryptprimitives.dll");
- CHECK(hmod);
- decltype(&ProcessPrng) process_prng_fn =
- reinterpret_cast<decltype(&ProcessPrng)>(
- GetProcAddress(hmod, "ProcessPrng"));
- CHECK(process_prng_fn);
- return process_prng_fn;
-}
-
void RandBytes(span<uint8_t> output, bool avoid_allocation) {
if (!avoid_allocation && internal::UseBoringSSLForRandBytes()) {
// Ensure BoringSSL is initialized so it can use things like RDRAND.
@@ -74,11 +63,18 @@ void RandBytes(span<uint8_t> output, bool avoid_allocation) {
return;
}
- static decltype(&ProcessPrng) process_prng_fn = GetProcessPrng();
- BOOL success =
- process_prng_fn(static_cast<BYTE*>(output.data()), output.size());
- // ProcessPrng is documented to always return TRUE.
- CHECK(success);
+ unsigned char* output_ptr = output.data();
+ size_t output_size = output.size();
+ while (output_size > 0) {
+ const ULONG output_bytes_this_pass = static_cast<ULONG>(std::min(
+ output_size, static_cast<size_t>(std::numeric_limits<ULONG>::max())));
+ const bool success =
+ RtlGenRandom(output_ptr, output_bytes_this_pass) != FALSE;
+ CHECK(success);
+ output_size -= output_bytes_this_pass;
+ output_ptr += output_bytes_this_pass;
+}
+
}
} // namespace
diff --git a/base/sampling_heap_profiler/poisson_allocation_sampler.cc b/base/sampling_heap_profiler/poisson_allocation_sampler.cc
index 9c449e95ee85d..ea299d25f35ef 100644
--- a/base/sampling_heap_profiler/poisson_allocation_sampler.cc
+++ b/base/sampling_heap_profiler/poisson_allocation_sampler.cc
@@ -241,6 +241,7 @@ void PoissonAllocationSampler::DoRecordAllocation(
size_t size,
base::allocator::dispatcher::AllocationSubsystem type,
const char* context) {
+
ThreadLocalData* const thread_local_data = GetThreadLocalData();
thread_local_data->accumulated_bytes += size;
diff --git a/base/system/sys_info_win.cc b/base/system/sys_info_win.cc
index 68626967de317..0c68025ebf605 100644
--- a/base/system/sys_info_win.cc
+++ b/base/system/sys_info_win.cc
@@ -153,6 +153,9 @@ int SysInfo::NumberOfProcessors() {
// static
int SysInfo::NumberOfEfficientProcessorsImpl() {
+ // No efficiency support before Windows 10.
+ if (win::GetVersion() < win::Version::WIN10)
+ return 0;
std::vector<BYTE> efficiency_classes = GetCoreEfficiencyClasses();
if (efficiency_classes.empty())
return 0;
diff --git a/base/task/thread_pool/thread_group.cc b/base/task/thread_pool/thread_group.cc
index 38888cf0448a9..a7423863f3716 100644
--- a/base/task/thread_pool/thread_group.cc
+++ b/base/task/thread_pool/thread_group.cc
@@ -17,7 +17,9 @@
#if BUILDFLAG(IS_WIN)
#include "base/win/com_init_check_hook.h"
+#include "base/win/scoped_com_initializer.h"
#include "base/win/scoped_winrt_initializer.h"
+#include "base/win/windows_version.h"
#endif
namespace base {
@@ -445,8 +447,18 @@ bool ThreadGroup::ShouldYield(TaskSourceSortKey sort_key) {
std::unique_ptr<win::ScopedWindowsThreadEnvironment>
ThreadGroup::GetScopedWindowsThreadEnvironment(WorkerEnvironment environment) {
std::unique_ptr<win::ScopedWindowsThreadEnvironment> scoped_environment;
- if (environment == WorkerEnvironment::COM_MTA) {
- scoped_environment = std::make_unique<win::ScopedWinrtInitializer>();
+ switch (environment) {
+ case WorkerEnvironment::COM_MTA: {
+ if (win::GetVersion() >= win::Version::WIN8) {
+ scoped_environment = std::make_unique<win::ScopedWinrtInitializer>();
+ } else {
+ scoped_environment = std::make_unique<win::ScopedCOMInitializer>(
+ win::ScopedCOMInitializer::kMTA);
+ }
+ break;
+ }
+ default:
+ break;
// TODO(crbug.com/1498668): rollback the change or replace it with a CHECK
// before closing the bug.
diff --git a/base/threading/platform_thread_win.cc b/base/threading/platform_thread_win.cc
index b8a0d23a43f6c..52291c7803a6b 100644
--- a/base/threading/platform_thread_win.cc
+++ b/base/threading/platform_thread_win.cc
@@ -54,6 +54,13 @@ namespace {
// Flag used to set thread priority to |THREAD_PRIORITY_LOWEST| for
// |kUseThreadPriorityLowest| Feature.
std::atomic<bool> g_use_thread_priority_lowest{false};
+// The most common value returned by ::GetThreadPriority() after background
+// thread mode is enabled on Windows 7.
+constexpr int kWin7BackgroundThreadModePriority = 4;
+
+// Value sometimes returned by ::GetThreadPriority() after thread priority is
+// set to normal on Windows 7.
+constexpr int kWin7NormalPriority = 3;
// Flag used to map Compositing ThreadType |THREAD_PRIORITY_ABOVE_NORMAL| on the
// UI thread for |kAboveNormalCompositingBrowserWin| Feature.
std::atomic<bool> g_above_normal_compositing_browser{true};
@@ -244,7 +251,10 @@ void AssertMemoryPriority(HANDLE thread, int memory_priority) {
reinterpret_cast<decltype(&::GetThreadInformation)>(::GetProcAddress(
::GetModuleHandle(L"Kernel32.dll"), "GetThreadInformation"));
- DCHECK(get_thread_information_fn);
+ if (!get_thread_information_fn) {
+ DCHECK_EQ(win::GetVersion(), win::Version::WIN7);
+ return;
+ }
MEMORY_PRIORITY_INFORMATION memory_priority_information = {};
DCHECK(get_thread_information_fn(thread, ::ThreadMemoryPriority,
@@ -443,8 +453,12 @@ void SetCurrentThreadPriority(ThreadType thread_type,
// Override the memory priority.
MEMORY_PRIORITY_INFORMATION memory_priority{.MemoryPriority =
MEMORY_PRIORITY_NORMAL};
+ static const auto set_thread_information_fn =
+ reinterpret_cast<decltype(&::SetThreadInformation)>(::GetProcAddress(
+ ::GetModuleHandle(L"kernel32.dll"), "SetThreadInformation"));
+ DCHECK(set_thread_information_fn);
[[maybe_unused]] const BOOL memory_priority_success =
- SetThreadInformation(thread_handle, ::ThreadMemoryPriority,
+ set_thread_information_fn(thread_handle, ::ThreadMemoryPriority,
&memory_priority, sizeof(memory_priority));
DPLOG_IF(ERROR, !memory_priority_success)
<< "Set thread memory priority failed.";
@@ -467,6 +481,15 @@ void SetCurrentThreadPriority(ThreadType thread_type,
void SetCurrentThreadQualityOfService(ThreadType thread_type) {
// QoS and power throttling were introduced in Win10 1709.
+ if (win::GetVersion() < win::Version::WIN10_RS3) {
+ return;
+ }
+
+ static const auto set_thread_information_fn =
+ reinterpret_cast<decltype(&::SetThreadInformation)>(::GetProcAddress(
+ ::GetModuleHandle(L"kernel32.dll"), "SetThreadInformation"));
+ DCHECK(set_thread_information_fn);
+
bool desire_ecoqos = false;
switch (thread_type) {
case ThreadType::kBackground:
@@ -489,11 +512,10 @@ void SetCurrentThreadQualityOfService(ThreadType thread_type) {
.StateMask =
desire_ecoqos ? THREAD_POWER_THROTTLING_EXECUTION_SPEED : 0ul,
};
- [[maybe_unused]] const BOOL success = ::SetThreadInformation(
+ [[maybe_unused]] const BOOL success = set_thread_information_fn(
::GetCurrentThread(), ::ThreadPowerThrottling,
&thread_power_throttling_state, sizeof(thread_power_throttling_state));
- // Failure is expected on versions of Windows prior to RS3.
- DPLOG_IF(ERROR, !success && win::GetVersion() >= win::Version::WIN10_RS3)
+ DPLOG_IF(ERROR, !success)
<< "Failed to set EcoQoS to " << std::boolalpha << desire_ecoqos;
}
@@ -548,8 +570,14 @@ ThreadPriorityForTest PlatformThread::GetCurrentThreadPriorityForTest() {
return ThreadPriorityForTest::kBackground;
switch (priority) {
+ case kWin7BackgroundThreadModePriority:
+ DCHECK_EQ(win::GetVersion(), win::Version::WIN7);
+ return ThreadPriorityForTest::kBackground;
case THREAD_PRIORITY_BELOW_NORMAL:
return ThreadPriorityForTest::kUtility;
+ case kWin7NormalPriority: