Skip to content

Port swift/remote to Windows and MSVC #6025

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

Merged
merged 1 commit into from
Dec 10, 2016
Merged

Port swift/remote to Windows and MSVC #6025

merged 1 commit into from
Dec 10, 2016

Conversation

hughbe
Copy link
Contributor

@hughbe hughbe commented Dec 2, 2016

  • Don't rely on dlfcn.h, use Win32 APIs instead. This requires adding a CPP file to avoid importing <windows.h> in a header.
  • Fix Error C2248 'anonymous-namespace'::RemoteASTContextImpl::fail': cannot access private member declared in class 'anonymous-namespace'::RemoteASTContextImpl' by making a private method protected.
  • Fix error C2872: 'ReflectionContext': ambiguous symbol by qualifying the namespace of ReflectionContext
  • Unhandled control path warnings - have to use std::cerr and std::abort as we get linker errors when we use llvm_unreachable

@@ -756,6 +757,9 @@ class MetadataReader {
return BuiltOpaque;
}
}

std::cerr << "fatal error: " << "Unhandled MetadataKind in switch." << "\n";
std::abort();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this better handled with a covered switch and llvm_unreachable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We get linker errors compiling swiftCore.dll if we use llvm_unreachable. The errors are unresolved external llvm_unreachable_internal. This is because we can't/don't link any LLVM libraries from the stdlib.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right. I think that having a swift_unreachable is going to be cleaner. We could model it after llvm_unreachable.


RemoteAddress InProcessMemoryReader::getSymbolAddress(const std::string &name) {
#if defined(_WIN32)
HMODULE module = GetModuleHandle(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, 0 is equivalent, but please use NULL as the documentation states.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right - fixed, thanks

@@ -175,6 +175,9 @@ swift_layout_kind_t getTypeInfoKind(const TypeInfo &TI) {
}
}
}

std::cerr << "fatal error: " << "Unhandled TypeInfoKind in switch." << "\n";
std::abort();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Covered switch + llvm_unreachable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@@ -756,6 +757,9 @@ class MetadataReader {
return BuiltOpaque;
}
}

std::cerr << "fatal error: " << "Unhandled MetadataKind in switch." << "\n";
std::abort();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right. I think that having a swift_unreachable is going to be cleaner. We could model it after llvm_unreachable.

#if defined(_WIN32)
HMODULE module = GetModuleHandle(NULL);
auto pointer = GetProcAddress(module, name.c_str());
FreeLibrary(module);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetModuleHandle does not increment reference count for the module. When you are invoking FreeLibrary here, you are decrementing the refcount on the module. This can cause the main module to hit a reference count of 0, and be unloaded! Am I missing something?

Copy link
Contributor Author

@hughbe hughbe Dec 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The GetModuleHandle function returns a handle to a mapped module without incrementing its reference count. However, if this handle is passed to the FreeLibrary function, the reference count of the mapped module will be decremented. Therefore, do not pass a handle returned by GetModuleHandle to the FreeLibrary function. Doing so can cause a DLL module to be unmapped prematurely.

According to the docs - my bad

@hughbe
Copy link
Contributor Author

hughbe commented Dec 8, 2016

Thanks @compnerd. I introduced a swift_unreachable API. I made it a static method as there's no real benefit to it being a macro, I think. Also, I stuck to C-only APIs (assert and abort) to avoid Swift compilation errors - we still get a customized message and a stack trace so that's fine.

Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM


SWIFT_ATTRIBUTE_NORETURN
inline static void swift_unreachable(const char* msg) {
assert(false && msg);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you get an unused variable warning when building release? (_NDEBUG).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, haven't done a release build - I've fixed this

RemoteAddress InProcessMemoryReader::getSymbolAddress(const std::string &name) {
#if defined(_WIN32)
HMODULE module = GetModuleHandle(NULL);
auto pointer = GetProcAddress(module, name.c_str());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could've inlined the GetModuleHandle.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, fixed


#include <iostream>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think that iostream is needed any more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, fixed thanks

@compnerd
Copy link
Member

@swift-ci please test and merge

@hughbe
Copy link
Contributor Author

hughbe commented Dec 10, 2016

@compnerd sorry for the churn, tests passed but this didnt merge

@compnerd
Copy link
Member

@swift-ci Please smoke test OS X platform

@compnerd
Copy link
Member

@hughbe going to be a bit more paranoid and run the smoke tests on macOS before merging.

@compnerd compnerd merged commit ac6edab into swiftlang:master Dec 10, 2016
@hughbe hughbe deleted the remote-msvc branch December 11, 2016 09:52
#endif

SWIFT_ATTRIBUTE_NORETURN
inline static void swift_unreachable(const char* msg) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, please don't name this swift_unreachable—we still want people to prefer llvm_unreachable when possible. How about swift_runtime_unreachable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

…if there isn't already such a thing somewhere in the real runtime, in which case we should use that.

Copy link
Contributor

@jckarter jckarter Dec 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just use llvm_unreachable with a runtime-local implementation of llvm_unreachable_internal for debug builds? Unreachable branches should be optimized as really unreachable in release builds of the runtime.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That could be even better. I'm honestly fairly unhappy about us having runtime things in include/swift/Basic/, but maybe I should be unhappy about having runtime libraries in lib/.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking we'd just reimplement llvm_unreachable_internal locally inside the runtime. We already have to provide a reimplementation of some LLVM hashing stuff to be able to use DenseMap inside the runtime.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't realize we'd actually bitten the bullet and put DenseMap in. Should we just link llvmSupport and let the linker figure it out? ;-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrose-apple, while you propose that (probably) in jest, that would help with another issue that recently came up. The ABI breaking validation for LLVM uses a value in llvmSuppprt. We ended up having to add another build option to disable it and force disable for swift.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth a shot. If linking llvmSupport into the runtime doesn't affect the binary size too much, it'd certainly be preferable to cherry-picking certain definitions we need.

@@ -20,7 +21,7 @@ using namespace swift::reflection;
using namespace swift::remote;

using NativeReflectionContext
= ReflectionContext<External<RuntimeTarget<sizeof(uintptr_t)>>>;
= swift::reflection::ReflectionContext<External<RuntimeTarget<sizeof(uintptr_t)>>>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants