X Tutup
The Wayback Machine - https://web.archive.org/web/20221004051003/https://github.com/PowerShell/PowerShell/pull/15603
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

Use Source Code Generator for PSVersionInfo class #15603

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

iSazonov
Copy link
Collaborator

@iSazonov iSazonov commented Jun 18, 2021

PR Summary

The custom source generator allows us to exclude reflection and to directly get current PowerShell version from MSBuild.

PerfView doesn't work well on current .Net Preview. A fix has just been merged in .Net Runtime repo for this. I hope we get it in .Net 6.0 Preview6 or Preview7. So today I can not share reliable result from PerfView to confirm perf win. Obviously using static string is more faster then reflection.

PR Context

Contribute to #13540
Related #14268

PR Checklist

@iSazonov iSazonov added the CL-Performance Indicates that a PR should be marked as a performance improvement in the Change Log label Jun 18, 2021
@iSazonov iSazonov requested a review from adityapatwardhan as a code owner Jun 18, 2021
@iSazonov
Copy link
Collaborator Author

iSazonov commented Jun 18, 2021

It seems test fails are not related to the PR - pwsh.exe has Preview.8 in product version instead of Preview.7.

@rjmholt @TravisEz13 Could you please look the issue?

@rjmholt
Copy link
Collaborator

rjmholt commented Jun 18, 2021

@iSazonov I opened #15607

var result = CreatePSVersionInfoPartialClass(context);

// We must use specific file name suffix (*.g.cs,*.g, *.i.cs, *.generated.cs, *.designer.cs)
// so that Roslyng analizers skip the file.
Copy link
Collaborator

@rkeithhill rkeithhill Jun 18, 2021

Choose a reason for hiding this comment

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

Typo. Should be - Roslyn analyzers

Copy link
Collaborator Author

@iSazonov iSazonov Jun 18, 2021

Choose a reason for hiding this comment

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

Thanks!

Fixed.

@@ -42,6 +41,13 @@ public class PSVersionInfo

private static readonly PSVersionHashTable s_psVersionTable;

/*
// The property and fields are generated by PSVersionInfoGenerator source generator.
Copy link
Collaborator

@rkeithhill rkeithhill Jun 18, 2021

Choose a reason for hiding this comment

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

Maybe tweak to The following property ...

Copy link
Collaborator Author

@iSazonov iSazonov Jun 19, 2021

Choose a reason for hiding this comment

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

Fixed.

@@ -42,6 +41,13 @@ public class PSVersionInfo

private static readonly PSVersionHashTable s_psVersionTable;

/*
Copy link
Collaborator

@rkeithhill rkeithhill Jun 18, 2021

Choose a reason for hiding this comment

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

Use of multiline comment seems a bit out-of-place. Consider using //

Copy link
Collaborator Author

@iSazonov iSazonov Jun 19, 2021

Choose a reason for hiding this comment

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

Using // raise CodeFactor issue (blank line after the comment).

Copy link
Collaborator

@vexx32 vexx32 Jul 2, 2021

Choose a reason for hiding this comment

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

I think CodeFactor recommends using //// to comment out code specifically, which bypasses the usual warning for that.

Copy link
Collaborator Author

@iSazonov iSazonov Jul 2, 2021

Choose a reason for hiding this comment

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

@xtqqczze Have you thoughts?

Copy link
Member

@SteveL-MSFT SteveL-MSFT Sep 26, 2022

Choose a reason for hiding this comment

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

It seems that for consistency we should use // and ignore the codefactor issue

@iSazonov iSazonov force-pushed the sg-psversion branch 4 times, most recently from cbb19d0 to 5abcd36 Compare Jun 25, 2021
@iSazonov
Copy link
Collaborator Author

iSazonov commented Jun 25, 2021

@rjmholt I resolved the issue with release tag. It turned out that ci.psm1 always assigns ReleaseTag property directly to dotnet.

@msftbot msftbot bot added the Review - Needed The PR is being reviewed label Jul 2, 2021
@msftbot
Copy link

msftbot bot commented Jul 2, 2021

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

const string SourceTemplate = @"
// <auto-generated>
// This file is auto-generated by PSVersionInfoGenerator.
// </auto-generated>

namespace System.Management.Automation
{{
public static partial class PSVersionInfo
{{
// Defined in PowerShell.Common.props as ProductVersion
// '6.0.0-beta.7 Commits: 29 SHA: 52c6b...'
internal static string ProductVersion {{ get; }} = ""{0}"";

// Defined in PowerShell.Common.props as
// git describe --abbrev=60 --long
// 'v6.0.0-beta.7-29-g52c6b...'
private static readonly string _rawGitCommitId = ""{1}"";

// Defined in PowerShell.Common.props as PSCoreBuildVersion
// '6.0.0-beta.7'
private static readonly string _mainVersion = ""{2}"";
}}
}}";
Copy link
Member

@TravisEz13 TravisEz13 Oct 26, 2021

Choose a reason for hiding this comment

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

Tagging for @wg-security review @PaulHigin

Copy link
Collaborator Author

@iSazonov iSazonov Nov 10, 2021

Choose a reason for hiding this comment

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

Notice, the file is written to disk and it is accessible for compliance tools.

@PaulHigin Friendly ping.

Copy link
Collaborator

@PaulHigin PaulHigin Nov 15, 2021

Choose a reason for hiding this comment

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

@TravisEz13 , @iSazonov
The concern, of course, is code injection. I don't immediately see an issue, but this needs to be discussed in the next WG-Security meeting. However, a meeting may not occur until December, due to the holidays. Since this is an optional change, I feel it is not high priority.

Copy link
Collaborator

@PaulHigin PaulHigin Nov 15, 2021

Choose a reason for hiding this comment

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

@WG-Security We looked into this and our main concern is that this is a potential code injection vulnerability. The version properties are all passed in as string types, and there is no validation. In addition the properties are passed in a very non-transparent manner, and unwanted injection would be difficult to detect.

We feel using something like a codedom to generate the source rather than string concatenation, is desirable.

Copy link
Collaborator Author

@iSazonov iSazonov Nov 16, 2021

Choose a reason for hiding this comment

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

@PaulHigin Could you please clarify what code you have reviewed - the source generator itself or resulting code generated by the source generator?
The SG is only used at compile time (not at runtime) and it is not distributed to end PowerShell users. I don't think it can be a security problem like you said.
The generated code contains only static strings and the file (.\src\System.Management.Automation\gen\SourceGeneratedFiles\PSVersionInfoGenerator\System.Management.Automation.Internal.Generators.PSVersionInfoGenerator\PSVersionInfo.generated.cs) is:

// <auto-generated>
// This file is auto-generated by PSVersionInfoGenerator.
// </auto-generated>

    namespace System.Management.Automation
{
    public static partial class PSVersionInfo
    {
        // Defined in PowerShell.Common.props as ProductVersion
        // '6.0.0-beta.7 Commits: 29 SHA: 52c6b...'
        internal static string ProductVersion { get; } = "7.2.0-preview.10 Commits: 169 SHA: 795a1ca556caa9cf90e43e63b9696b2437dc722e";

        // Defined in PowerShell.Common.props as
        // git describe --abbrev=60 --long
        // 'v6.0.0-beta.7-29-g52c6b...'
        private static readonly string _rawGitCommitId = "7.2.0-preview.10-169-g795a1ca556caa9cf90e43e63b9696b2437dc722e";

        // Defined in PowerShell.Common.props as PSCoreBuildVersion
        // '6.0.0-beta.7'
        private static readonly string _mainVersion = "7.2.0-preview.10";
    }
}

Copy link
Collaborator

@PaulHigin PaulHigin Nov 16, 2021

Choose a reason for hiding this comment

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

To be clear, this is the opinion of the @WG-Security group, not just me. We looked at const string SourceTemplate where the above property values are concatenated into the source template as strings. This is a supply chain source code injection opportunity, that would be difficult to detect. Our understanding is that using a CodeDom is the preferred way to build up source code for compilation.

Copy link
Collaborator Author

@iSazonov iSazonov Nov 16, 2021

Choose a reason for hiding this comment

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

@PaulHigin I really don't understand about CodeDom. The best comment from .Net team I saw about using CodeDom in SGs is dotnet/roslyn#48214 (comment)

Copy link
Collaborator

@PaulHigin PaulHigin Nov 16, 2021

Choose a reason for hiding this comment

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

I'm not an expert either, and I'll defer to @TravisEz13.

Copy link
Member

@SteveL-MSFT SteveL-MSFT Sep 26, 2022

Choose a reason for hiding this comment

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

For code injection to happen here, there would need to be a double quote, right? Can we just reject any strings that contain a double quote?

Copy link
Collaborator Author

@iSazonov iSazonov Sep 26, 2022

Choose a reason for hiding this comment

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

I really don't understand. We could implement this as TypeGen and ResGen which we already use. Only difference is the TypeGen and ResGen are our custom utilities but the source generator is .Net feature.
To do injection, someone would have to compromise the entire repository in order to corrupt both build scripts and the source code.

Copy link
Member

@TravisEz13 TravisEz13 left a comment

Pending @wg-security review

@msftbot msftbot bot added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Review - Needed The PR is being reviewed labels Oct 26, 2021
@TravisEz13 TravisEz13 added the WG-Security security related areas such as JEA label Oct 26, 2021
@msftbot msftbot bot removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Oct 29, 2021
@daxian-dbw daxian-dbw assigned daxian-dbw and unassigned rjmholt Nov 3, 2021
@msftbot msftbot bot added the Review - Needed The PR is being reviewed label Nov 10, 2021
@msftbot
Copy link

msftbot bot commented Nov 10, 2021

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@PaulHigin
Copy link
Collaborator

PaulHigin commented Nov 12, 2021

@iSazonov Sorry for the delay. I am unavailable today but will look at this early next week.

@msftbot msftbot bot removed the Review - Needed The PR is being reviewed label Nov 12, 2021
@msftbot msftbot bot added the Review - Needed The PR is being reviewed label Nov 24, 2021
@msftbot
Copy link

msftbot bot commented Nov 24, 2021

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@pull-request-quantifier
Copy link

pull-request-quantifier bot commented Sep 21, 2022

This PR has 85 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Small
Size       : +72 -13
Percentile : 34%

Total files changed: 7

Change summary by file extension:
.spelling : +1 -0
.props : +7 -0
.cs : +33 -13
.csproj : +30 -0
.md : +1 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  👌  👎 (Email)
Customize PullRequestQuantifier for this repository.

@daxian-dbw
Copy link
Member

daxian-dbw commented Sep 27, 2022

@iSazonov I really like you trying out the source generator!
I measured the code you removed from PSVersionInfo.cs by using the source generator, and it takes about 7ms on average on my machine for private build (going through the Contains(" Commit ") code path, and hence slower).

Assembly currentAssembly = typeof(PSVersionInfo).Assembly;
ProductVersion = currentAssembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;

string rawGitCommitId;
string mainVersion = ProductVersion.Substring(0, ProductVersion.IndexOf(' '));

if (ProductVersion.Contains(" Commits: "))
{
    rawGitCommitId = ProductVersion.Replace(" Commits: ", "-").Replace(" SHA: ", "-g");
}
else
{
    rawGitCommitId = mainVersion;
}

The time it takes for pwsh -noprofile -c exit on my machine is about 390ms on average (ranging from ~380 to ~420), and the time improvement is about 1.7%. (an estimate only, didn't do end-to-end measurement)

Not sure if the gain worth the extra complexity though.

@msftbot msftbot bot removed the Review - Needed The PR is being reviewed label Sep 27, 2022
@iSazonov
Copy link
Collaborator Author

iSazonov commented Sep 27, 2022

Not sure if the gain worth the extra complexity though.

It's no more complicated than our ResGen or TypeGen. It's just something new to get used to. 😄

We could replace some of our Regex with a standard .Net Regex SG to get benefits in startup scenario and other scenarios.
And we could also replace TypeGen with our SG, which of course will not speed up PowerShell itself, but will noticeably speed up the CIs and the generated cache will probably be more correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-Performance Indicates that a PR should be marked as a performance improvement in the Change Log Small WG-Security security related areas such as JEA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants
X Tutup