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

Restrict embedded XML #1675

Merged
merged 7 commits into from
Dec 10, 2020
Merged

Restrict embedded XML #1675

merged 7 commits into from
Dec 10, 2020

Conversation

sbomer
Copy link
Member

@sbomer sbomer commented Dec 7, 2020

This restricts the embedded XML to prevent modification of assemblies other than the containing assembly. Specifically:

  • Descriptor XML may still mark types in other assemblies since it is purely additive
  • Substitution XML may not rewrite methods/fields defined in other assemblies
  • Attribute XML may not add attributes to other assemblies. (RemoveAttributeInstances may only be specified by embedded XML in the assembly that defines the attribute type)
  • Embedded attribute XML may not use '*' to select all assemblies
  • Embedded XML of any kind may leave out the assemby tag, instead specifying types of the containing assembly directly under <linker>
  • XML specified on the command-line is unchanged

(I'm separating the XML changes out from the rest of #1666.)

Embedded attributes and substitutions may only modify the containing assembly.
@vitek-karas
Copy link
Member

I think we will need a an exceptions to the rule:

  • We need to be able to assembly="*" on linker attributes in CoreLib (nullable attributes)
    • I think it's fine for CoreLib to be excluded from these rules - we will just have to pre-load it to make things simple.
    • CoreLib is special anyway - too many dependencies on how runtime works in the linker. It will have to be S.P.CoreLib for ILLinker and mscorlib for mono, but that should be also easy to do.

@sbomer
Copy link
Member Author

sbomer commented Dec 7, 2020

We need to be able to assembly="*" on linker attributes in CoreLib (nullable attributes)

Actually it's not entirely clear to me why we need this. Is it to support removing an attribute whose definition is compiled into multiple assemblies? Could we just recommend embedding a duplicate attribute XML into such assemblies?

@marek-safar
Copy link
Contributor

Actually it's not entirely clear to me why we need this.

This is needed for compiler inserted attributes. It's not really viable to insert the same XML into all assemblies produced by C# compiler.

We could try to convert that section of XML into XML file argument descriptor as specializing this case will be a pain, we could be in the situation where SPC is not known till later and to ensure we don't have a hole somewhere which would mark attributes before they we read SPC xml.

src/linker/Linker.Steps/ProcessLinkerXmlStepBase.cs Outdated Show resolved Hide resolved
```XML
<!-- IL2101: Embedded XML in assembly 'ContainingAssembly' may not modify other assembly 'OtherAssembly' -->
<linker>
<assembly fullname="OtherAssembly">
Copy link
Contributor

Choose a reason for hiding this comment

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

fullname is now redundant for embedded XML format, should we just remove/ignore it instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it's good to warn when it's used incorrectly in the embedded XML, otherwise people might try to embed existing XML and wonder why it doesn't work.

Since the assembly is redundant for embedded XML, I made it so that you can leave out the assembly entirely:

<linker>
  <type fullname="..." />
</linker>

I kept support for <assembly fullname="ThisAssembly>" since I think it will be easier to reuse existing XML this way, but I could force the embedded XML not to name the assembly if you prefer.

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, I think this is more wording issue than anything else. Your "It is invalid to use these embedded XML files to modify other assemblies." seems to suggest this will be error where in reality this is a warning. Maybe we could go with even simple mode and merge 2100 and 2101 under the simple warning that warns that fullname value is ignored for embedded XML files (just suggestion)

Copy link
Member Author

Choose a reason for hiding this comment

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

I reworded the warnings - hopefully this is better. I'd like to keep them separate because the wildcard can produce a warning for non-embedded XML too.

@sbomer
Copy link
Member Author

sbomer commented Dec 8, 2020

Does the compiler inject attribute type definitions into assemblies, or only attribute instances? This approach still allows specifying RemoveAttributeInstances in embedded XML for the assembly that defines the attribute type, and it will remove instances of the attribute everywhere.

we could be in the situation where SPC is not known till later and to ensure we don't have a hole somewhere which would mark attributes before they we read SPC xml.

The lazy loading change will ensure that attribute XML from the resolved attribute type's assembly is processed before we mark attribute instances.

@vitek-karas
Copy link
Member

Yes - Roslyn adds attribute definitions into each assembly it needs it in. That's the problem - that's the reason we have the * for assemblies. The only alternative would be to modify Roslyn to somehow annotate the attribute definition when it generates it, but that's tricky (would require us to introduce the attribute as a real attribute and not just linker internal)

@sbomer
Copy link
Member Author

sbomer commented Dec 8, 2020

I see, thank you. So it sounds like there are a few options:

  1. for CoreLib to be excluded from these rules - we will just have to pre-load it

    • This keeps the existing XML working and seems like the quickest fix for now, so I'll go ahead with it if there are no objections.
  2. We could try to convert that section of XML into XML file argument descriptor

    • This seems more flexible long-term. If they're not defined in corelib, the corelib XML isn't really the right place to remove them - it's more like a C# language feature that the linker needs to understand. I wonder what the versioning story is for these special attributes. I don't know details about them, but presumably this list doesn't version with corelib, otherwise there wouldn't be a need to inject the definitions.

- Allow '*' in corelib embedded attribute XML
- Reword warning messages
- Avoid calling virtual method from ctor
Macking the core library causes problems for PEVerify:
- Removed reference to corelib creates invalid typerefs
- System types like Object aren't found
@sbomer sbomer merged commit 9c88abd into dotnet:master Dec 10, 2020
sbomer added a commit to sbomer/sdk that referenced this pull request Dec 11, 2020
Avoids modifying other assemblies in embedded XML, in response to
dotnet/linker#1675.

This will unblock dependency flow in dotnet#14925.
sbomer added a commit to sbomer/sdk that referenced this pull request Dec 11, 2020
Avoids modifying other assemblies in embedded XML, in response to
dotnet/linker#1675.

This will unblock dependency flow in dotnet#14925.
sbomer added a commit to dotnet/sdk that referenced this pull request Dec 14, 2020
* Fix illink test

Avoids modifying other assemblies in embedded XML, in response to
dotnet/linker#1675.

This will unblock dependency flow in #14925.

* Avoid deconstructing KeyValuePair

Not implemented in .NET Framework
mrvoorhe pushed a commit to Unity-Technologies/linker that referenced this pull request Feb 8, 2021
* Restrict embedded XML

Embedded attributes and substitutions may only modify the containing assembly.

* Allow specifying resource name in referenced assembly

In the test infrastructure.

* Fix formatting

* PR feedback

- Allow '*' in corelib embedded attribute XML
- Reword warning messages
- Avoid calling virtual method from ctor

* Fix test on mono

Macking the core library causes problems for PEVerify:
- Removed reference to corelib creates invalid typerefs
- System types like Object aren't found

* PR feedback

* PR feedback
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
* Restrict embedded XML

Embedded attributes and substitutions may only modify the containing assembly.

* Allow specifying resource name in referenced assembly

In the test infrastructure.

* Fix formatting

* PR feedback

- Allow '*' in corelib embedded attribute XML
- Reword warning messages
- Avoid calling virtual method from ctor

* Fix test on mono

Macking the core library causes problems for PEVerify:
- Removed reference to corelib creates invalid typerefs
- System types like Object aren't found

* PR feedback

* PR feedback

Commit migrated from dotnet/linker@9c88abd
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.

None yet

3 participants