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
This commit is contained in:
André Bargull
2025-02-03 11:53:28 +00:00
parent 8da8272423
commit 6306ce5d25
5 changed files with 25 additions and 9 deletions

View File

@@ -146,12 +146,18 @@ oddly_ordered_inclnames = set(
"psapi.h", # Must be included after "util/WindowsWrapper.h" on Windows
"machine/endian.h", # Must be included after <sys/types.h> on BSD
"process.h", # Windows-specific
"winbase.h", # Must precede other system headers(?)
"windef.h", # Must precede other system headers(?)
"windows.h", # Must precede other system headers(?)
"util/WindowsWrapper.h", # Must precede other system headers(?)
]
)
# System headers which shouldn't be included directly, but instead use the
# designated wrapper.
wrapper_system_inclnames = {
"windows.h": "util/WindowsWrapper.h",
"windef.h": "util/WindowsWrapper.h",
"winbase.h": "util/WindowsWrapper.h",
}
# The files in tests/style/ contain code that fails this checking in various
# ways. Here is the output we expect. If the actual output differs from
# this, one of the following must have happened.
@@ -713,6 +719,18 @@ def check_file(
'the #include "..." form',
)
# Check for system header which shouldn't be included directly.
if (
include.inclname in wrapper_system_inclnames
and wrapper_system_inclnames[include.inclname] != inclname
):
wrapper_inclname = wrapper_system_inclnames[include.inclname]
error(
filename,
include.linenum,
f"{include.quote()} should not be included directly, "
f'instead use "{wrapper_inclname}"',
)
else:
msg = deprecated_inclnames.get(include.inclname)
if msg:

View File

@@ -7,7 +7,7 @@
#include "frontend/FrontendContext.h"
#ifdef _WIN32
# include <windows.h>
# include "util/WindowsWrapper.h"
# include <process.h> // GetCurrentThreadId
#else
# include <pthread.h> // pthread_self

View File

@@ -74,12 +74,11 @@ pid_t gettid_pthread() {
#include "vm/MutexIDs.h"
#ifdef XP_WIN
# include <windef.h>
# include "util/WindowsWrapper.h"
# include <codecvt>
# include <evntprov.h>
# include <locale>
# include <string>
# include <windows.h>
const GUID PROVIDER_JSCRIPT9 = {
0x57277741,

View File

@@ -19,7 +19,7 @@
# include <process.h>
# include <string.h>
# include <wchar.h>
# include <windows.h>
# include "util/WindowsWrapper.h"
#elif __wasi__
# include <dirent.h>
# include <sys/types.h>

View File

@@ -17,8 +17,7 @@
#include "jstypes.h"
#ifdef XP_WIN
# include <windef.h>
# include <winbase.h>
# include "util/WindowsWrapper.h"
# include <crtdbg.h> /* for _CrtSetReportMode */
# include <stdlib.h> /* for _set_invalid_parameter_handler */
#endif