Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Throw an exception when passing strings by-value as out parameters. #21513

Merged
merged 17 commits into from
Feb 6, 2019

Conversation

jkoritzinsky
Copy link
Member

@jkoritzinsky jkoritzinsky commented Dec 12, 2018

Block users from adding an [Out] attribute on strings and modifying them in place to avoid causing crashes with unloadability and string interning. Instead of allowing it, we'll throw a MarshalDirectiveException. Fixes #21506.

Blocked on dotnet/corefx#34091

@janvorli
Copy link
Member

@jkoritzinsky something strange happened to the tests/src/Interop/StringMarshalling/LPTSTR/LPTSTRTest.cs file, git now thinks it is binary file for some reason and doesn't show a diff.

@jkoritzinsky
Copy link
Member Author

Probably an encoding issue. I'll go fix that.

@jkoritzinsky
Copy link
Member Author

Fixed. It was encoded as UTF-16 LE which GitHub has a habit of thinking is binary. Changed it to UTF-8.

@janvorli
Copy link
Member

GitHub still thinks it is binary. Ok, I've just fetched the contents of the file and diffed it locally.

@janvorli
Copy link
Member

The linux-musl x64 build now fails:

15:34:33 /mnt/j/workspace/dotnet_coreclr/master/linux-musl-TGroup_netcoreapp+CGroup_Debug+AGroup_x64+TestOuter_false_prtest/src/vm/ilmarshalers.h:1978:23: error: 'GetNativeType' overrides a member function but is not marked 'override' [-Werror,-Winconsistent-missing-override]
15:34:33     virtual LocalDesc GetNativeType();
15:34:33                       ^
15:34:33 /mnt/j/workspace/dotnet_coreclr/master/linux-musl-TGroup_netcoreapp+CGroup_Debug+AGroup_x64+TestOuter_false_prtest/src/vm/ilmarshalers.h:1293:23: note: overridden virtual function is here
15:34:33     virtual LocalDesc GetNativeType() = 0;

That's because you have added the SupportsArgumentMarshal method with override and so the whole class now requires usage of override (clang's idiosyncracy). Could you please remove the override and add virtual to it?

@jkoritzinsky
Copy link
Member Author

Will do.

Marshal_In was copied back into existence from Marshal_InOut. Clean it up a bit.
@jkoritzinsky
Copy link
Member Author

@dotnet-bot test this please.

@jkoritzinsky
Copy link
Member Author

@dotnet-bot test this please

src/vm/ilmarshalers.cpp Outdated Show resolved Hide resolved
@jkoritzinsky
Copy link
Member Author

@dotnet-bot test Ubuntu arm Cross Checked Innerloop Build and Test

@jkoritzinsky
Copy link
Member Author

@dotnet-bot test Ubuntu x64 Checked Innerloop Build and Test (Jit - TieredCompilation=0)
@dotnet-bot test OSX10.12 x64 Checked Innerloop Build and Test

@janvorli
Copy link
Member

@AaronRobinsonMSFT I've just found this PR from last December was not merged in yet. Since I am not familiar with the marshalling code, I don't feel confident enough to approve the change. Could you please do that?

Copy link
Member

@AaronRobinsonMSFT AaronRobinsonMSFT left a comment

Choose a reason for hiding this comment

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

Please update the docs about P/Invokes and root cause the Linux failure, otherwise LGTM.

@jkoritzinsky
Copy link
Member Author

@AaronRobinsonMSFT I've already updated the docs to advise to not do this, but I'll go an add an update to that saying that it is disallowed in .NET Core 3.0+.

@jkoritzinsky
Copy link
Member Author

@dotnet-bot test this please

@janvorli
Copy link
Member

@jkoritzinsky the Linux-musl has a build break due to the source of the test. It is strange that it doesn't fail for non-MUSL Linux, as it complains about the wcslen and wmemcmp argument mismatch (it says the functions expect wchar_t* while we are passing in char16_t *.
The weird thing is also that other pre-existing functions in the same file used the same string comparison and they don't fail.

@janvorli
Copy link
Member

I am trying to run the build on my local Alpine to see if I can find the real culprit.

@janvorli
Copy link
Member

It is really weird - I can build coreclr with your change on my local Alpine just fine and the affected file is really being compiled. I'll retry the leg here.

@janvorli
Copy link
Member

@dotnet-bot test Linux-musl x64 Debug Build please

1 similar comment
@janvorli
Copy link
Member

janvorli commented Feb 6, 2019

@dotnet-bot test Linux-musl x64 Debug Build please

@janvorli janvorli closed this Feb 6, 2019
@janvorli janvorli reopened this Feb 6, 2019
Co-Authored-By: jkoritzinsky <jkoritzinsky@gmail.com>
@jkoritzinsky jkoritzinsky merged commit 2f88f32 into dotnet:master Feb 6, 2019
@jkoritzinsky jkoritzinsky deleted the block-string-byvalue-out branch February 6, 2019 21:05
@jkoritzinsky jkoritzinsky added the breaking-change Issue or PR that represents a breaking API or functional change over a prerelease. label Mar 30, 2019
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…otnet/coreclr#21513)

* Throw an exception when passing strings by-value as out parameters.

* Fix encoding

* Don't use override in this PR.

* Clean up Marshal_In

Marshal_In was copied back into existence from Marshal_InOut. Clean it up a bit.

* Remove extraneous whitespace.

* Fix failing test.

* Remove out attribute in COM string tests.

* Add back attribute and check for exception thow in COM tests.

* Add block comment to explain the implementation of Reverse_LPWStr_OutAttr in the NETServer.

* Only throw in a CLR->Native marshalling situation.

* Fix asserts from changed code-paths used in ILWSTRMarshaler.

* Add comment and explicitly load in a null value (instead of leaving it uninitialized).

* Apply suggestions from code review

Co-Authored-By: jkoritzinsky <jkoritzinsky@gmail.com>


Co-authored-by: Jan Vorlicek <janvorli@microsoft.com>

Commit migrated from dotnet/coreclr@2f88f32
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Interop breaking-change Issue or PR that represents a breaking API or functional change over a prerelease.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants