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

Analyzer processes expression tree construction data flow differently from linker #3172

Open
Tracked by #101149
vitek-karas opened this issue Jan 4, 2023 · 1 comment

Comments

@vitek-karas
Copy link
Member

vitek-karas commented Jan 4, 2023

For example:

			public static void MethodWithSingleAnnotatedParameter (
				[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] Type type)
			{ }

			static void LdToken ()
			{
                                // Linker produces IL2111
                                // Analyzer produces IL2067
				Expression<Action<Type>> _ = (Type t) => MethodWithSingleAnnotatedParameter (t);

                                // Linker produces IL2111
                                // Analyzer doesn't warn
				Expression<Action> _ = () => MethodWithSingleAnnotatedParameter (typeof(string));
			}

The difference is because linker sees ldtoken and not an actual call and doesn't perform true data flow, but analyzer sees this as a normal statement and processes it through data flow.
The linker behavior is correct here, under the hood this creates expression tree with MethodInfo which is publicly accessible. And anybody with the expression tree can invoke the MethodInfo with any input. So this needs to warn always, regardless of data flow.

@vitek-karas vitek-karas changed the title Analyzer doesn't fully process data flow in expression tree construction Analyzer processes expression tree construction data flow differently from linker Jan 4, 2023
@vitek-karas
Copy link
Member Author

In this case the analyzer should probably always produce less warnings than trimmer, which is the better outcome.

The one place where this might be a problem is cases where expression trees are used as a performance optimization and as a convenient way to generate code at runtime. Where the code builds an expression tree which is then compiled and executed.

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

No branches or pull requests

1 participant