ILSA.Core 1.0.0.1

dotnet add package ILSA.Core --version 1.0.0.1
NuGet\Install-Package ILSA.Core -Version 1.0.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ILSA.Core" Version="1.0.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ILSA.Core --version 1.0.0.1
#r "nuget: ILSA.Core, 1.0.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ILSA.Core as a Cake Addin
#addin nuget:?package=ILSA.Core&version=1.0.0.1

// Install ILSA.Core as a Cake Tool
#tool nuget:?package=ILSA.Core&version=1.0.0.1

Simple static analysis(SA) library for .Net

Library for creating simple static analysis(SA) diagnostics and analyzing .Net assemblies at the IL-code level. Library provides a predefined subset of patterns for analyzing security\performance or compatibility threats.

Powered by ILReader library.

How to create a simple pattern:

At first, create a class library and reference to ILReader.Core and ILSA.Core nuget packages:

  <ItemGroup>
    <PackageReference Include="ILReader.Core" Version="1.0.0.*" />
    <PackageReference Include="ILSA.Core" Version="1.0.0.*" />
  </ItemGroup>

Once you have the class library set up and the necessary nuget packages referenced, you can create a static class for diagnostic with a single static Match method. This method will define a pattern for specific rule and pass this pattern into the static MethodBodyPattern.Match method available from the ILSA.Core library:

public static class BoxingOnStringMethodCalls {
  static readonly Func<IInstruction, bool>[] matches = new Func<IInstruction, bool>[] {
      new Func<IInstruction, bool>(i => Boxing.IsBoxing(i.OpCode) && Boxing.IsValueType(i.Operand)),
      new Func<IInstruction, bool>(i => Call.IsCall(i.OpCode) && IsFormatOrConcatMethod(i.Operand)),
  };
  static bool IsFormatOrConcatMethod(object? operand) {
      if(!(operand is MethodBase method) || method.DeclaringType != typeof(string))
          return false;
      return method.Name == "Format" || method.Name == "Concat";
  }
  public static bool Match(IILReader instructions, StringBuilder errors, out int[] captures) {
    return MethodBodyPattern.Match(matches, instructions, errors, out captures);
  }
}

To implement some rules you can also use some helper methods from the ILSA.Core library:

static readonly Type hashsetType = typeof(HashSet<>);
static bool IsHashSetMethod(object? operand) {
  return operand is MethodBase method && Call.IsMethodOfGenericType(method, hashsetType);
}

With the use of the annotation attribute Display, you can decorate your diagnostic with a name and a descriptive text (in markdown format) for further displaying in visual client.

[Display(Order = (int)ProcessingSeverity.Informational, 
  Name = @"Avoid using HashSet.Add\HashSet.Remove after HashSet.Contains call",
  Description = "ILSA.Core.Assets.MD.AvoidUsingHashSetAddRemoveAfterContainsCheck.md")]
public static bool Match(IILReader instructions, StringBuilder errors, out int[] captures) { ... }

The corresponding markdown file should be embedded into you library as EmbeddedResource.

You can test your diagnostic by downloading a Visual Client (free) and loading a library with diagnostic into patterns section.

Real usage

Read the Why simple Static Analysys tools are important and how easy to use they are article to learn how it works and how to use this tool effectively.

NuGet

To install ILSA.Core, run the following command in the Package Manager Console:

Install-Package ILSA.Core

License

The ILSA.Core library is licensed under the MIT license.

Product Compatible and additional computed target framework versions.
.NET Framework net472 is compatible.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0.1 275 1/27/2023