-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[stdlib/msvc] Runtime with MSVC library #1918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,4 +92,4 @@ struct ReadWriteLockPlatformHelper { | |
}; | ||
} | ||
|
||
#endif | ||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,21 +14,31 @@ | |
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#if defined(__CYGWIN__) || defined(__ANDROID__) || defined(_MSC_VER) | ||
# define SWIFT_SUPPORTS_BACKTRACE_REPORTING 0 | ||
#else | ||
# define SWIFT_SUPPORTS_BACKTRACE_REPORTING 1 | ||
#endif | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <stdint.h> | ||
#if defined(_MSC_VER) | ||
#include <io.h> | ||
#else | ||
#include <unistd.h> | ||
#endif | ||
#include <stdarg.h> | ||
#include "swift/Runtime/Debug.h" | ||
#include "swift/Runtime/Mutex.h" | ||
#include "swift/Basic/Demangle.h" | ||
#include "swift/Basic/LLVM.h" | ||
#include "llvm/ADT/StringRef.h" | ||
|
||
#if !defined(_MSC_VER) | ||
#include <cxxabi.h> | ||
|
||
#if !defined(__CYGWIN__) && !defined(__ANDROID__) | ||
#endif | ||
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING | ||
|
||
// execinfo.h is not available on Android. Checks in this file ensure that | ||
// fatalError behaves as expected, but without stack traces. | ||
|
@@ -50,7 +60,7 @@ enum: uint32_t { | |
|
||
using namespace swift; | ||
|
||
#if !defined(__CYGWIN__) && !defined(__ANDROID__) | ||
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you, this is much cleaner now! |
||
|
||
static bool getSymbolNameAddr(llvm::StringRef libraryName, Dl_info dlinfo, | ||
std::string &symbolName, uintptr_t &addrOut) { | ||
|
@@ -187,11 +197,16 @@ reportOnCrash(uint32_t flags, const char *message) | |
static void | ||
reportNow(uint32_t flags, const char *message) | ||
{ | ||
#if defined(_MSC_VER) | ||
#define STDERR_FILENO 2 | ||
_write(STDERR_FILENO, message, strlen(message)); | ||
#else | ||
write(STDERR_FILENO, message, strlen(message)); | ||
#endif | ||
#ifdef __APPLE__ | ||
asl_log(NULL, NULL, ASL_LEVEL_ERR, "%s", message); | ||
#endif | ||
#if !defined(__CYGWIN__) && !defined(__ANDROID__) | ||
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING | ||
if (flags & FatalErrorFlags::ReportBacktrace) { | ||
fputs("Current stack trace:\n", stderr); | ||
constexpr unsigned maxSupportedStackDepth = 128; | ||
|
@@ -213,6 +228,26 @@ void swift::swift_reportError(uint32_t flags, | |
reportOnCrash(flags, message); | ||
} | ||
|
||
static int swift_vasprintf(char **strp, const char *fmt, va_list ap) { | ||
#if defined(_MSC_VER) | ||
int len = _vscprintf(fmt, ap); | ||
if (len < 0) | ||
return -1; | ||
char *buffer = reinterpret_cast<char *>(malloc(len + 1)); | ||
if (!buffer) | ||
return -1; | ||
int result = vsprintf(*strp, fmt, ap); | ||
if (result < 0) { | ||
free(buffer); | ||
return -1; | ||
} | ||
*strp = buffer; | ||
return result; | ||
#else | ||
return vasprintf(strp, fmt, ap); | ||
#endif | ||
} | ||
|
||
// Report a fatal error to system console, stderr, and crash logs, then abort. | ||
LLVM_ATTRIBUTE_NORETURN | ||
void | ||
|
@@ -222,7 +257,7 @@ swift::fatalError(uint32_t flags, const char *format, ...) | |
va_start(args, format); | ||
|
||
char *log; | ||
vasprintf(&log, format, args); | ||
swift_vasprintf(&log, format, args); | ||
|
||
swift_reportError(flags, log); | ||
abort(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,8 @@ void swift::swift_once(swift_once_t *predicate, void (*fn)(void *)) { | |
// process (the token is a word that is atomically incremented from 0 to | ||
// 1 to 2 during initialization) to work. We should implement our own version | ||
// that we can rely on to continue to work that way. | ||
// The MSVC port also relies on this, because the std::call_once on MSVC | ||
// follows the compatible init process. | ||
// For more information, see rdar://problem/18499385 | ||
std::call_once(*predicate, [fn]() { fn(nullptr); }); | ||
#endif | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changes to this file LGTM. |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,7 +172,7 @@ namespace swift { | |
dest = newValue; | ||
} | ||
|
||
#if defined(__CYGWIN__) | ||
#if defined(__CYGWIN__) || defined(_MSC_VER) | ||
struct dl_phdr_info { | ||
void *dlpi_addr; | ||
const char *dlpi_name; | ||
|
@@ -183,6 +183,8 @@ namespace swift { | |
void *data); | ||
uint8_t *_swift_getSectionDataPE(void *handle, const char *sectionName, | ||
unsigned long *sectionSize); | ||
#endif | ||
#if defined(__CYGWIN__) | ||
void _swift_once_f(uintptr_t *predicate, void *context, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the ABI of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changes to this file LGTM otherwise. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is the code after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The compiler assumes that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The definition of
. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this matches our expectations. |
||
void (*function)(void *)); | ||
#endif | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changes to this file LGTM. |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,11 @@ | |
#include <link.h> | ||
#endif | ||
|
||
#if defined(_MSC_VER) | ||
#include <windows.h> | ||
#else | ||
#include <dlfcn.h> | ||
#endif | ||
|
||
using namespace swift; | ||
|
||
|
@@ -144,7 +148,7 @@ const { | |
#define SWIFT_PROTOCOL_CONFORMANCES_SECTION "__swift2_proto" | ||
#elif defined(__ELF__) | ||
#define SWIFT_PROTOCOL_CONFORMANCES_SECTION ".swift2_protocol_conformances_start" | ||
#elif defined(__CYGWIN__) | ||
#elif defined(__CYGWIN__) || defined(_MSC_VER) | ||
#define SWIFT_PROTOCOL_CONFORMANCES_SECTION ".sw2prtc" | ||
#endif | ||
|
||
|
@@ -396,17 +400,25 @@ void swift::_swift_initializeCallbacksToInspectDylib( | |
// rdar://problem/19045112 | ||
dl_iterate_phdr(_addImageProtocolConformances, &inspectArgs); | ||
} | ||
#elif defined(__CYGWIN__) | ||
#elif defined(__CYGWIN__) || defined(_MSC_VER) | ||
static int _addImageProtocolConformances(struct dl_phdr_info *info, | ||
size_t size, void *data) { | ||
InspectArgs *inspectArgs = (InspectArgs *)data; | ||
// inspectArgs contains addImage*Block function and the section name | ||
#if defined(_MSC_VER) | ||
HMODULE handle; | ||
|
||
if (!info->dlpi_name || info->dlpi_name[0] == '\0') | ||
handle = GetModuleHandle(nullptr); | ||
else | ||
handle = GetModuleHandle(info->dlpi_name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can Cygwin use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, Cygwin can use both functions. I didn't merge because Cygwin prefers |
||
#else | ||
void *handle; | ||
if (!info->dlpi_name || info->dlpi_name[0] == '\0') { | ||
if (!info->dlpi_name || info->dlpi_name[0] == '\0') | ||
handle = dlopen(nullptr, RTLD_LAZY); | ||
} else | ||
else | ||
handle = dlopen(info->dlpi_name, RTLD_LAZY | RTLD_NOLOAD); | ||
#endif | ||
|
||
unsigned long conformancesSize; | ||
const uint8_t *conformances = | ||
|
@@ -416,7 +428,11 @@ static int _addImageProtocolConformances(struct dl_phdr_info *info, | |
if (conformances) | ||
inspectArgs->fnAddImageBlock(conformances, conformancesSize); | ||
|
||
#if defined(_MSC_VER) | ||
FreeLibrary(handle); | ||
#else | ||
dlclose(handle); | ||
#endif | ||
return 0; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changes to this file LGTM. |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,9 @@ | |
#include "../SwiftShims/RuntimeShims.h" | ||
#include "Private.h" | ||
#include "swift/Runtime/Debug.h" | ||
#if SWIFT_OBJC_INTEROP | ||
#include <dlfcn.h> | ||
#endif | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unordered_map> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changes to this file LGTM. |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes to this file LGTM.