virtualx-engine/platform/windows/godot_windows.cpp

215 lines
5.5 KiB
C++
Raw Normal View History

2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* godot_windows.cpp */
2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
2014-02-10 02:10:30 +01:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2014-02-10 02:10:30 +01:00
#include "main/main.h"
#include "os_windows.h"
#include <locale.h>
#include <stdio.h>
// For export templates, add a section; the exporter will patch it to enclose
// the data appended to the executable (bundled PCK)
#ifndef TOOLS_ENABLED
#if defined _MSC_VER
#pragma section("pck", read)
__declspec(allocate("pck")) static char dummy[8] = { 0 };
#elif defined __GNUC__
static const char dummy[8] __attribute__((section("pck"), used)) = { 0 };
#endif
#endif
PCHAR *
CommandLineToArgvA(
PCHAR CmdLine,
int *_argc) {
PCHAR *argv;
PCHAR _argv;
ULONG len;
ULONG argc;
CHAR a;
ULONG i, j;
BOOLEAN in_QM;
BOOLEAN in_TEXT;
BOOLEAN in_SPACE;
len = strlen(CmdLine);
i = ((len + 2) / 2) * sizeof(PVOID) + sizeof(PVOID);
argv = (PCHAR *)GlobalAlloc(GMEM_FIXED,
i + (len + 2) * sizeof(CHAR));
_argv = (PCHAR)(((PUCHAR)argv) + i);
argc = 0;
argv[argc] = _argv;
in_QM = FALSE;
in_TEXT = FALSE;
in_SPACE = TRUE;
i = 0;
j = 0;
while ((a = CmdLine[i])) {
if (in_QM) {
if (a == '\"') {
in_QM = FALSE;
} else {
_argv[j] = a;
j++;
}
} else {
switch (a) {
case '\"':
in_QM = TRUE;
in_TEXT = TRUE;
if (in_SPACE) {
argv[argc] = _argv + j;
argc++;
}
in_SPACE = FALSE;
break;
case ' ':
case '\t':
case '\n':
case '\r':
if (in_TEXT) {
_argv[j] = '\0';
j++;
}
in_TEXT = FALSE;
in_SPACE = TRUE;
break;
default:
in_TEXT = TRUE;
if (in_SPACE) {
argv[argc] = _argv + j;
argc++;
}
_argv[j] = a;
j++;
in_SPACE = FALSE;
break;
}
}
i++;
}
_argv[j] = '\0';
argv[argc] = NULL;
(*_argc) = argc;
return argv;
}
2014-02-10 02:10:30 +01:00
char *wc_to_utf8(const wchar_t *wc) {
int ulen = WideCharToMultiByte(CP_UTF8, 0, wc, -1, NULL, 0, NULL, NULL);
char *ubuf = new char[ulen + 1];
WideCharToMultiByte(CP_UTF8, 0, wc, -1, ubuf, ulen, NULL, NULL);
ubuf[ulen] = 0;
return ubuf;
}
Fix thread_local, tls, ASLR, and DEP with MingW This commit changes the way Thread::caller_id works. By moving caller_id to the .cpp file we make sure that the TLS variable doesn't get relocated twice causing a crash. Since we build with LTO for release builds (and everyone should be doing that anyway) there is no extra overhead from the non-static method. We do do an extra bool check now there but I don't think this will add much in the way of overhead. This check cannot be avoided if we still want to be able to cache the thread ID hash, as we had to move the setter because of limitations of the WinRT platform. The original workaround for this was in #46813 but this has some unintended consequences. Specifically; threads that never create a Thread object will always return 0 in Thread::get_caller_id() which caused a regression. For instance the editor now freezes when importing large textures. This PR also addresses that. Additionally we now enable ASLR support when building with MingW, this includes a workaround for MingW. MingW refuses to create an appropriate relocation table if no symbols are exported. So we just export the various main() functions in godot_windows.cpp. While ASLR support isn't criticial for Godot, previous versions of Godot just happened to work with a dynamic base 'by accident' and some users run Godot this way. After the thread change the .tls section now needs relocations to make this work. By enabling ASLR at build-time we create these relocations and people who forced ALSR on previously will now get a working Godot again. This fixes #47256 and fixes #47219 This is the 3.x version of this PR. For master a different approach is possible which I will make in the coming days.
2021-03-25 20:45:51 +01:00
__declspec(dllexport) int widechar_main(int argc, wchar_t **argv) {
2014-02-10 02:10:30 +01:00
OS_Windows os(NULL);
setlocale(LC_CTYPE, "");
#ifndef TOOLS_ENABLED
// Workaround to prevent LTCG (MSVC LTO) from removing "pck" section
char *dummy_guard = dummy;
#endif
char **argv_utf8 = new char *[argc];
for (int i = 0; i < argc; ++i) {
argv_utf8[i] = wc_to_utf8(argv[i]);
}
2015-05-17 19:17:57 +02:00
Error err = Main::setup(argv_utf8[0], argc - 1, &argv_utf8[1]);
if (err != OK) {
for (int i = 0; i < argc; ++i) {
delete[] argv_utf8[i];
}
delete[] argv_utf8;
2015-05-17 19:17:57 +02:00
return 255;
}
2015-05-17 19:17:57 +02:00
2014-02-10 02:10:30 +01:00
if (Main::start())
os.run();
Main::cleanup();
for (int i = 0; i < argc; ++i) {
delete[] argv_utf8[i];
}
delete[] argv_utf8;
2014-02-10 02:10:30 +01:00
return os.get_exit_code();
};
Fix thread_local, tls, ASLR, and DEP with MingW This commit changes the way Thread::caller_id works. By moving caller_id to the .cpp file we make sure that the TLS variable doesn't get relocated twice causing a crash. Since we build with LTO for release builds (and everyone should be doing that anyway) there is no extra overhead from the non-static method. We do do an extra bool check now there but I don't think this will add much in the way of overhead. This check cannot be avoided if we still want to be able to cache the thread ID hash, as we had to move the setter because of limitations of the WinRT platform. The original workaround for this was in #46813 but this has some unintended consequences. Specifically; threads that never create a Thread object will always return 0 in Thread::get_caller_id() which caused a regression. For instance the editor now freezes when importing large textures. This PR also addresses that. Additionally we now enable ASLR support when building with MingW, this includes a workaround for MingW. MingW refuses to create an appropriate relocation table if no symbols are exported. So we just export the various main() functions in godot_windows.cpp. While ASLR support isn't criticial for Godot, previous versions of Godot just happened to work with a dynamic base 'by accident' and some users run Godot this way. After the thread change the .tls section now needs relocations to make this work. By enabling ASLR at build-time we create these relocations and people who forced ALSR on previously will now get a working Godot again. This fixes #47256 and fixes #47219 This is the 3.x version of this PR. For master a different approach is possible which I will make in the coming days.
2021-03-25 20:45:51 +01:00
__declspec(dllexport) int _main() {
LPWSTR *wc_argv;
int argc;
int result;
2014-02-10 02:10:30 +01:00
wc_argv = CommandLineToArgvW(GetCommandLineW(), &argc);
if (NULL == wc_argv) {
wprintf(L"CommandLineToArgvW failed\n");
return 0;
}
2014-02-10 02:10:30 +01:00
result = widechar_main(argc, wc_argv);
2014-02-10 02:10:30 +01:00
LocalFree(wc_argv);
return result;
}
2014-02-10 02:10:30 +01:00
Fix thread_local, tls, ASLR, and DEP with MingW This commit changes the way Thread::caller_id works. By moving caller_id to the .cpp file we make sure that the TLS variable doesn't get relocated twice causing a crash. Since we build with LTO for release builds (and everyone should be doing that anyway) there is no extra overhead from the non-static method. We do do an extra bool check now there but I don't think this will add much in the way of overhead. This check cannot be avoided if we still want to be able to cache the thread ID hash, as we had to move the setter because of limitations of the WinRT platform. The original workaround for this was in #46813 but this has some unintended consequences. Specifically; threads that never create a Thread object will always return 0 in Thread::get_caller_id() which caused a regression. For instance the editor now freezes when importing large textures. This PR also addresses that. Additionally we now enable ASLR support when building with MingW, this includes a workaround for MingW. MingW refuses to create an appropriate relocation table if no symbols are exported. So we just export the various main() functions in godot_windows.cpp. While ASLR support isn't criticial for Godot, previous versions of Godot just happened to work with a dynamic base 'by accident' and some users run Godot this way. After the thread change the .tls section now needs relocations to make this work. By enabling ASLR at build-time we create these relocations and people who forced ALSR on previously will now get a working Godot again. This fixes #47256 and fixes #47219 This is the 3.x version of this PR. For master a different approach is possible which I will make in the coming days.
2021-03-25 20:45:51 +01:00
__declspec(dllexport) int main(int _argc, char **_argv) {
// _argc and _argv are ignored
// we are going to use the WideChar version of them instead
2014-02-10 02:10:30 +01:00
#ifdef CRASH_HANDLER_EXCEPTION
__try {
return _main();
} __except (CrashHandlerException(GetExceptionInformation())) {
return 1;
}
#else
return _main();
#endif
}
2014-02-10 02:10:30 +01:00
HINSTANCE godot_hinstance = NULL;
2014-02-10 02:10:30 +01:00
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
godot_hinstance = hInstance;
return main(0, NULL);
2014-02-10 02:10:30 +01:00
}