Bug 1448426 - Wrap windows.h to avoid problematic define statements, r=froydnj,glandium

By default, windows.h exposes a large number of problematic define statements
which are UpperCamelCase, such as a define from `CreateWindow` to
`CreateWindow{A,W}`.

As many of these names are generic (e.g. CreateFile, CreateWindow), they can
mess up Gecko code that may legitimately have its own methods with the same
names.

The header also defines some traditional SCREAMING_SNAKE_CASE defines which
can mess up our code by conflicting with local values.

This patch adds a simple code generator which generates wrappers for these
defines, and uses them to wrap the windows.h wrapper using the `stl_wrappers`
mechanism, allowing us to use windows.h in more places.

Differential Revision: https://phabricator.services.mozilla.com/D10932
This commit is contained in:
Nika Layzell 2018-09-25 17:34:53 +02:00
parent 700f0f1e89
commit 568787b95f
14 changed files with 1301 additions and 28 deletions

View File

@ -5,26 +5,9 @@ from __future__ import print_function
import os
import string
from mozbuild.util import FileAvoidWrite
from system_header_util import header_path
def find_in_path(file, searchpath):
for dir in searchpath.split(os.pathsep):
f = os.path.join(dir, file)
if os.path.exists(f):
return f
return ''
def header_path(header, compiler):
if compiler == 'gcc':
# we use include_next on gcc
return header
elif compiler == 'msvc':
return find_in_path(header, os.environ.get('INCLUDE', ''))
else:
# hope someone notices this ...
raise NotImplementedError(compiler)
# The 'unused' arg is the output file from the file_generate action. We actually
# generate all the files in header_list

View File

@ -0,0 +1,86 @@
# 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 http://mozilla.org/MPL/2.0/.
import re
import textwrap
import string
from system_header_util import header_path
comment_re = re.compile(r'//[^\n]*\n|/\*.*\*/', re.S)
decl_re = re.compile(r'''^(.+)\s+ # type
(\w+)\s* # name
(?:\((.*)\))?$ # optional param tys
''', re.X | re.S)
def read_decls(filename):
"""Parse & yield C-style decls from an input file"""
with open(filename, 'r') as fd:
# Strip comments from the source text.
text = comment_re.sub('', fd.read())
# Parse individual declarations.
raw_decls = [d.strip() for d in text.split(';') if d.strip()]
for raw in raw_decls:
match = decl_re.match(raw)
if match is None:
raise "Invalid decl: %s" % raw
ty, name, params = match.groups()
if params is not None:
params = [a.strip() for a in params.split(',') if a.strip()]
yield ty, name, params
def generate(fd, consts_path, unicodes_path, template_path, compiler):
# Parse the template
with open(template_path, 'r') as template_fd:
template = string.Template(template_fd.read())
decls = ''
# Each constant should be saved to a temporary, and then re-assigned to a
# constant with the correct name, allowing the value to be determined by
# the actual definition.
for ty, name, args in read_decls(consts_path):
assert args is None, "parameters in const decl!"
decls += textwrap.dedent("""
#ifdef {name}
constexpr {ty} _tmp_{name} = {name};
#undef {name}
constexpr {ty} {name} = _tmp_{name};
#endif
""".format(ty=ty, name=name))
# Each unicode declaration defines a static inline function with the
# correct types which calls the 'A' or 'W'-suffixed versions of the
# function. Full types are required here to ensure that '0' to 'nullptr'
# coersions are preserved.
for ty, name, args in read_decls(unicodes_path):
assert args is not None, "argument list required for unicode decl"
# Parameter & argument string list
params = ', '.join('%s a%d' % (ty, i) for i, ty in enumerate(args))
args = ', '.join('a%d' % i for i in range(len(args)))
decls += textwrap.dedent("""
#ifdef {name}
#undef {name}
static inline {ty} WINAPI
{name}({params})
{{
#ifdef UNICODE
return {name}W({args});
#else
return {name}A({args});
#endif
}}
#endif
""".format(ty=ty, name=name, params=params, args=args))
path = header_path('windows.h', compiler)
# Write out the resulting file
fd.write(template.substitute(header_path=path, decls=decls))

View File

@ -63,6 +63,19 @@ if CONFIG['WRAP_STL_INCLUDES']:
stl.flags = [output_dir, stl_compiler, template_file]
stl.flags.extend(stl_headers)
# Wrap <windows.h> to make it easier to use correctly
# NOTE: If we aren't wrapping STL includes, we're building part of the browser
# which won't need this wrapper, such as L10N. Just don't try to generate the
# wrapper in that case.
if CONFIG['OS_ARCH'] == 'WINNT':
GENERATED_FILES += ['../dist/stl_wrappers/windows.h']
windows_h = GENERATED_FILES['../dist/stl_wrappers/windows.h']
windows_h.script = 'make-windows-h-wrapper.py:generate'
windows_h.inputs = ['windows-h-constant.decls.h',
'windows-h-unicode.decls.h',
'windows-h-wrapper.template.h']
windows_h.flags = [stl_compiler]
if CONFIG['WRAP_SYSTEM_INCLUDES']:
include('system-headers.mozbuild')
output_dir = '../dist/system_wrappers'

View File

@ -0,0 +1,20 @@
import os
def find_in_path(file, searchpath):
for dir in searchpath.split(os.pathsep):
f = os.path.join(dir, file)
if os.path.exists(f):
return f
return ''
def header_path(header, compiler):
if compiler == 'gcc':
# we use include_next on gcc
return header
elif compiler == 'msvc':
return find_in_path(header, os.environ.get('INCLUDE', ''))
else:
# hope someone notices this ...
raise NotImplementedError(compiler)

View File

@ -0,0 +1,57 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=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 http://mozilla.org/MPL/2.0/. */
/*
* This file contains a series of C-style declarations for constants defined in
* windows.h using #define. Adding a new constant should be a simple as adding
* its name (and optionally type) to this file.
*
* This file is processed by generate-windows-h-wrapper.py to generate a wrapper
* for the header which removes the defines usually implementing these constants.
*
* Wrappers defined in this file will be declared as `constexpr` values,
* and will have their value derived from the windows.h define.
*
* NOTE: This is *NOT* a real C header, but rather an input to the avove script.
* Only basic declarations in the form found here are allowed.
*/
// XXX(nika): There are a lot of these (>30k)!
// This is just a set of ones I saw in a quick scan which looked problematic.
auto CREATE_NEW;
auto CREATE_ALWAYS;
auto OPEN_EXISTING;
auto OPEN_ALWAYS;
auto TRUNCATE_EXISTING;
auto INVALID_FILE_SIZE;
auto INVALID_SET_FILE_POINTER;
auto INVALID_FILE_ATTRIBUTES;
auto ANSI_NULL;
auto UNICODE_NULL;
auto MINCHAR;
auto MAXCHAR;
auto MINSHORT;
auto MAXSHORT;
auto MINLONG;
auto MAXLONG;
auto MAXBYTE;
auto MAXWORD;
auto MAXDWORD;
auto DELETE;
auto READ_CONTROL;
auto WRITE_DAC;
auto WRITE_OWNER;
auto SYNCHRONIZE;
auto MAXIMUM_ALLOWED;
auto GENERIC_READ;
auto GENERIC_WRITE;
auto GENERIC_EXECUTE;
auto GENERIC_ALL;

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,55 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=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 http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_windows_h
#define mozilla_windows_h
// Include the "real" windows.h header. On clang/gcc, this can be done with the
// `include_next` feature, however MSVC requires a direct include path.
//
// Also turn off deprecation warnings, as we may be wrapping deprecated fns.
#if defined(__GNUC__) || defined(__clang__)
# pragma GCC system_header
# include_next <windows.h>
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#else
# include <${header_path}>
# pragma warning(push)
# pragma warning(disable: 4996 4995)
#endif // defined(__GNUC__) || defined(__clang__)
// Check if the header should be disabled
#if defined(MOZ_DISABLE_WINDOWS_WRAPPER)
#define MOZ_WINDOWS_WRAPPER_DISABLED_REASON "explicitly disabled"
#elif !defined(__cplusplus)
#define MOZ_WINDOWS_WRAPPER_DISABLED_REASON "non-C++ source file"
#elif !defined(__GNUC__) && !defined(__clang__) && !defined(_DLL)
#define MOZ_WINDOWS_WRAPPER_DISABLED_REASON "non-dynamic RTL"
#else
// We're allowed to wrap in the current context. Define `MOZ_WRAPPED_WINDOWS_H`
// to note that fact, and perform the wrapping.
#define MOZ_WRAPPED_WINDOWS_H
extern "C++" {
${decls}
} // extern "C++"
#endif // enabled
#if defined(__GNUC__) || defined(__clang__)
# pragma GCC diagnostic pop
#else
# pragma warning(pop)
#endif // defined(__GNUC__) || defined(__clang__)
#endif // !defined(mozilla_windows_h)

View File

@ -424,7 +424,7 @@ SOURCES += [
'nsDOMWindowUtils.cpp',
# Conflicts with windows.h's definition of SendMessage.
'nsFrameMessageManager.cpp',
# These files have a #error "Never include windows.h in this file!"
# These files have a #error "Never include unwrapped windows.h in this file!"
'nsGlobalWindowInner.cpp',
'nsGlobalWindowOuter.cpp',
# Conflicts with windows.h's definition of LoadImage.

View File

@ -7816,8 +7816,9 @@ nsGlobalWindowInner::FireOnNewGlobalObject()
JS_FireOnNewGlobalObject(aes.cx(), global);
}
#ifdef _WINDOWS_
#error "Never include windows.h in this file!"
#if defined(_WINDOWS_) && !defined(MOZ_WRAPPED_WINDOWS_H)
#pragma message("wrapper failure reason: " MOZ_WINDOWS_WRAPPER_DISABLED_REASON)
#error "Never include unwrapped windows.h in this file!"
#endif
already_AddRefed<Promise>

View File

@ -7733,8 +7733,9 @@ nsGlobalWindowOuter::ReportLargeAllocStatus()
message);
}
#ifdef _WINDOWS_
#error "Never include windows.h in this file!"
#if defined(_WINDOWS_) && !defined(MOZ_WRAPPED_WINDOWS_H)
#pragma message("wrapper failure reason: " MOZ_WINDOWS_WRAPPER_DISABLED_REASON)
#error "Never include unwrapped windows.h in this file!"
#endif
// Helper called by methods that move/resize the window,

View File

@ -34,6 +34,7 @@ if CONFIG['MOZ_WIDGET_TOOLKIT'] not in ('cocoa', 'uikit'):
]
if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'windows':
DEFINES['MOZ_DISABLE_WINDOWS_WRAPPER'] = True
EXPORTS.cairo += [
'cairo-win32.h',
]

View File

@ -261,7 +261,7 @@ gfxDWriteFont::ComputeMetrics(AntialiasOption anAAOption)
UINT32 ucs = L' ';
UINT16 glyph;
HRESULT hr = mFontFace->GetGlyphIndicesW(&ucs, 1, &glyph);
HRESULT hr = mFontFace->GetGlyphIndices(&ucs, 1, &glyph);
if (FAILED(hr)) {
mMetrics->spaceWidth = 0;
} else {
@ -291,7 +291,7 @@ gfxDWriteFont::ComputeMetrics(AntialiasOption anAAOption)
if (mMetrics->aveCharWidth < 1) {
ucs = L'x';
if (SUCCEEDED(mFontFace->GetGlyphIndicesW(&ucs, 1, &glyph))) {
if (SUCCEEDED(mFontFace->GetGlyphIndices(&ucs, 1, &glyph))) {
mMetrics->aveCharWidth = MeasureGlyphWidth(glyph);
}
if (mMetrics->aveCharWidth < 1) {
@ -301,7 +301,7 @@ gfxDWriteFont::ComputeMetrics(AntialiasOption anAAOption)
}
ucs = L'0';
if (SUCCEEDED(mFontFace->GetGlyphIndicesW(&ucs, 1, &glyph))) {
if (SUCCEEDED(mFontFace->GetGlyphIndices(&ucs, 1, &glyph))) {
mMetrics->zeroOrAveCharWidth = MeasureGlyphWidth(glyph);
}
if (mMetrics->zeroOrAveCharWidth < 1) {

View File

@ -8,6 +8,9 @@ FINAL_LIBRARY = 'xul'
DEFINES['VR_API_PUBLIC'] = True
# Windows.h wrappers conflict with internal methods in openvr
DEFINES['MOZ_DISABLE_WINDOWS_WRAPPER'] = True
if CONFIG['OS_ARCH'] == 'WINNT':
if CONFIG['HAVE_64BIT_BUILD']:
DEFINES['WIN64'] = True

View File

@ -367,8 +367,9 @@ class CommonBackend(BuildBackend):
if poison_windows_h:
includeTemplate += (
'\n'
'#ifdef _WINDOWS_\n'
'#error "%(cppfile)s included windows.h"\n'
'#if defined(_WINDOWS_) && !defined(MOZ_WRAPPED_WINDOWS_H)\n'
'#pragma message("wrapper failure reason: " MOZ_WINDOWS_WRAPPER_DISABLED_REASON)\n'
'#error "%(cppfile)s included unwrapped windows.h"\n'
"#endif")
includeTemplate += (
'\n'