Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 57e9ccc

Browse files
committed
Bug 1945305: Add check to disallow directly including Windows headers. r=jandem
Don't allow to directly include any of: - `<windows.h>` - `<winbase.h>` - `<windef.h>` Instead require to use "util/WindowsWrapper.h" which takes care to `#undef` common function names. `<windows.h>` includes both `<winbase.h>` and `<windef.h>` and is also the preferred way to include Windows headers, so it's okay to always use "util/WindowsWrapper.h". Bug 1945305 happened because Beta builds bundle "vm/ToSource.cpp" and "vm/Time.cpp" in the same unified cpp-file. "vm/ToSource.cpp" has a transitive dependency to "jit/AtomicOp.h", which defines the class `jit::MemoryBarrier`. And "vm/Time.cpp" includes `<winbase.h>` and `<windef.h>`, which define a macro named `MemoryBarrier`. Differential Revision: https://phabricator.services.mozilla.com/D236460
1 parent 978fa86 commit 57e9ccc

File tree

5 files changed

+25
-9
lines changed

5 files changed

+25
-9
lines changed

config/check_spidermonkey_style.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,18 @@
146146
"psapi.h", # Must be included after "util/WindowsWrapper.h" on Windows
147147
"machine/endian.h", # Must be included after <sys/types.h> on BSD
148148
"process.h", # Windows-specific
149-
"winbase.h", # Must precede other system headers(?)
150-
"windef.h", # Must precede other system headers(?)
151-
"windows.h", # Must precede other system headers(?)
149+
"util/WindowsWrapper.h", # Must precede other system headers(?)
152150
]
153151
)
154152

153+
# System headers which shouldn't be included directly, but instead use the
154+
# designated wrapper.
155+
wrapper_system_inclnames = {
156+
"windows.h": "util/WindowsWrapper.h",
157+
"windef.h": "util/WindowsWrapper.h",
158+
"winbase.h": "util/WindowsWrapper.h",
159+
}
160+
155161
# The files in tests/style/ contain code that fails this checking in various
156162
# ways. Here is the output we expect. If the actual output differs from
157163
# this, one of the following must have happened.
@@ -713,6 +719,18 @@ def check_include_statement(include):
713719
'the #include "..." form',
714720
)
715721

722+
# Check for system header which shouldn't be included directly.
723+
if (
724+
include.inclname in wrapper_system_inclnames
725+
and wrapper_system_inclnames[include.inclname] != inclname
726+
):
727+
wrapper_inclname = wrapper_system_inclnames[include.inclname]
728+
error(
729+
filename,
730+
include.linenum,
731+
f"{include.quote()} should not be included directly, "
732+
f'instead use "{wrapper_inclname}"',
733+
)
716734
else:
717735
msg = deprecated_inclnames.get(include.inclname)
718736
if msg:

js/src/frontend/FrontendContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "frontend/FrontendContext.h"
88

99
#ifdef _WIN32
10-
# include <windows.h>
10+
# include "util/WindowsWrapper.h"
1111
# include <process.h> // GetCurrentThreadId
1212
#else
1313
# include <pthread.h> // pthread_self

js/src/jit/PerfSpewer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,11 @@ pid_t gettid_pthread() {
7474
#include "vm/MutexIDs.h"
7575

7676
#ifdef XP_WIN
77-
# include <windef.h>
77+
# include "util/WindowsWrapper.h"
7878
# include <codecvt>
7979
# include <evntprov.h>
8080
# include <locale>
8181
# include <string>
82-
# include <windows.h>
8382

8483
const GUID PROVIDER_JSCRIPT9 = {
8584
0x57277741,

js/src/shell/OSObject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# include <process.h>
2020
# include <string.h>
2121
# include <wchar.h>
22-
# include <windows.h>
22+
# include "util/WindowsWrapper.h"
2323
#elif __wasi__
2424
# include <dirent.h>
2525
# include <sys/types.h>

js/src/vm/Time.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
#include "jstypes.h"
1818

1919
#ifdef XP_WIN
20-
# include <windef.h>
21-
# include <winbase.h>
20+
# include "util/WindowsWrapper.h"
2221
# include <crtdbg.h> /* for _CrtSetReportMode */
2322
# include <stdlib.h> /* for _set_invalid_parameter_handler */
2423
#endif

0 commit comments

Comments
 (0)