Skip to content
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

Redirect DllImport of hostpolicy to the main executable when it's embedded #40014

Merged
merged 3 commits into from
Jul 29, 2020

Conversation

vitek-karas
Copy link
Member

For singlefilehost the hostpolicy is statically linked into the executable, so we need to apply similar logic to other statically linked libraries by redirecting DllImport loads to the main executable module.

Because this happens also on Windows, the coreclr itself doesn't know statically (unlike on Linux where we link coreclr statically as well, so we can hardcode this knowledge). So the hostpolicy must pass this information to the runtime via a new runtime property.

Tested both on Windows and Linux.

Fixes #39907

@vitek-karas vitek-karas added this to the 5.0.0 milestone Jul 28, 2020
@vitek-karas vitek-karas requested a review from VSadov July 28, 2020 14:59
@vitek-karas vitek-karas self-assigned this Jul 28, 2020
@ghost
Copy link

ghost commented Jul 28, 2020

Tagging subscribers to this area: @swaroop-sridhar, @agocke
See info in area-owners.md if you want to be subscribed.

@@ -6328,6 +6331,19 @@ namespace
}
#endif

if (g_hostpolicy_embedded)
{
#if defined(TARGET_LINUX)
Copy link
Member

Choose a reason for hiding this comment

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

Would it be a good idea to invert this condition to make this logic future proof; such that all OS except Windows get libhostpolicy?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I think everything but Windows should use libhostpolicy

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 actually the name used in this context? - not libhostpolicy.so or something with a path?

In the case above we could, in theory, see different file names, but we intentionally only recognize the form used in Interop.Libraries.cs.
Jut wonder whether there is anything to worry about hostpolicy naming.

Copy link
Member Author

Choose a reason for hiding this comment

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

The story is the same - we don't expect any other code but corelib to call into hostpolicy directly, so these strings need to match what's in Interop.Libraries, but nothing else.

I fixed the condition to be for Windows - should also fix the build failures in CI

Copy link
Member

@VSadov VSadov left a comment

Choose a reason for hiding this comment

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

LGTM

if (g_hostpolicy_embedded)
{
#ifdef TARGET_WINDOWS
if (wcscmp(wszLibName, W("hostpolicy.dll")) == 0) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: { on its own line for consistency.

@vitek-karas
Copy link
Member Author

@BruceForstall can you please take a look why the formatting job is failing on this PR? The changes don't touch JIT code. And the generated patch file is empty (0 bytes).

@am11
Copy link
Member

am11 commented Jul 28, 2020

Windows formatting job failure is unrelated to PR changes, failing in every PR. See #40034.

@vitek-karas
Copy link
Member Author

Thanks @am11 - I'm trying to figure out the other failures now...

@vitek-karas
Copy link
Member Author

Something weird is happening. One of the tests is failing with:

Launching 'C:\h\w\B8170A18\w\AC3D09A7\e\Interop\COM\NativeClients\Primitives\COMClientPrimitives.exe'...

Expected: 100
Actual: 3
END EXECUTION - FAILED
FAILED

But looking at the code

int __cdecl main()
{
ComMTA init;
if (FAILED(init.Result))
return -1;
try
{
Run_NumericTests();
Run_ArrayTests();
Run_StringTests();
Run_ErrorTests();
Run_ColorTests();
}
catch (HRESULT hr)
{
::printf("Test Failure: 0x%08x\n", hr);
return 101;
}
return 100;
}

There's no way it returns exit code 3.

@am11
Copy link
Member

am11 commented Jul 28, 2020

The Expected: 3 is indeed very fishy. Going by this investigation: #33733 (comment), it might be a similar intermittent assert is failing in runtime, GUI popping up on the build agent causing exit code 3. Maybe rerunning the CI will fix this error.
git commit -m "Re-run CI" --alow-empty && git push

@vitek-karas
Copy link
Member Author

It is an assert - but seems valid - debugging through it right now.

@vitek-karas
Copy link
Member Author

The test is mocking hostpolicy - so maybe there's something going on there (although I would not expect to affect non-single-file builds at all, given that the embedded variables should be false in that case)

@vitek-karas
Copy link
Member Author

Found it - uninitialized variable (I've been working in C# for way too long now, forgot all of the C++ gotchas)... I can only wish for default compiler settings which would definitely show this as a warning.

@am11
Copy link
Member

am11 commented Jul 28, 2020

Heh, it's C++ which first says Values of type bool are either true or false. then later ... uninitialized automatic variable, might cause it to behave as if it is neither true nor false. 🙈

@vitek-karas
Copy link
Member Author

In this case (on my machine anyway) it initialized to magical 204 (literally) - and since conditions treat anything non-zero as true....
Anyway - WARNINGS - but I guess that would be a project for like 3 months to clean up coreclr to be able to turn on at least some C++ warnings.

@vitek-karas
Copy link
Member Author

Networking test failures are being fixed in #40070.

@vitek-karas vitek-karas merged commit b2c7585 into dotnet:master Jul 29, 2020
@vitek-karas vitek-karas deleted the DllImportStaticHostPolicy branch July 29, 2020 09:47
@jkotas
Copy link
Member

jkotas commented Jul 29, 2020

WARNINGS - but I guess that would be a project for like 3 months to clean up coreclr to be able to turn on at least some C++ warnings.

We have all reasonable C++ warnings turned on. C++ compilers do not seem to provide warning to catch this type of problem. Neither MSVC w/ /W4 nor clang w/ -Wall warn for this code:

extern void g(bool* p);

void f()
{
   bool x;
   g(&x);
}

@vitek-karas
Copy link
Member Author

It's great that we have warnings on... and not so great that the compiler doesn't have a way to warn on this (I guess it is a hard problem in C++). I definitely didn't mean to downplay our efforts on the codebase...

Jacksondr5 pushed a commit to Jacksondr5/runtime that referenced this pull request Aug 10, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Single-File host does not correctly redirect hostpolicy PInvokes
4 participants