Skip to content

Commit

Permalink
Enable IlasmRoundTrip tests for merged tests (#93368)
Browse files Browse the repository at this point in the history
* Initial work

* Tweak

* Tweak

* Do not generate _ilasmroundtrip.py for tests that have a generated run script

* Reduce imports

* Force fail to see where CI fails

* Revert forcing failure. Run roundtrip on build.

* Trying to fix script

* Backslash tweak

* Do not roundtrip the same assembly

* Fixing

* Remove import glob

* Added is_managed_assembly

* remove print

* Fixed paths

* Support bash. Ignore certain tests for arm. Fix poison test.

* Update CLRTest.Jit.targets

* Feedback. Added AssemblyChecker.

* Fix paths

* Update src/tests/Common/Directory.Build.targets

Co-authored-by: Bruce Forstall <brucefo@microsoft.com>

* Feedback

* Feedback

* Update AssemblyChecker.csproj

* Update Program.cs

* Trying to fix calling python on helix.

* Remove old roundtrip script calls. Added --is-exe option for AssemblyChecker.

* Tweak option:

* Remove check

* Remove imports

* Fix build

* Fix syntax errors. Fixed Popen arguments

* Fixed Popen arguments

* Fixing debug check

* Fixing tests

* Update ILVerificationTests.csproj

* Fixing tests

* Feedback

* Feedback

* Update CLRTest.Jit.targets

* Added help usage flag for AssemblyChecker

* Feedback on assembly-checker

---------

Co-authored-by: Bruce Forstall <brucefo@microsoft.com>
  • Loading branch information
TIHan and BruceForstall committed Oct 30, 2023
1 parent 1a19c09 commit cf9bb86
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 98 deletions.
3 changes: 2 additions & 1 deletion eng/Subsets.props
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@
$(CoreClrProjectRoot)tools\r2rdump\R2RDump.csproj;
$(CoreClrProjectRoot)tools\dotnet-pgo\dotnet-pgo.csproj;
$(CoreClrProjectRoot)tools\aot\ILCompiler\repro\repro.csproj;
$(CoreClrProjectRoot)tools\r2rtest\R2RTest.csproj" Category="clr" Condition="'$(DotNetBuildFromSource)' != 'true'"/>
$(CoreClrProjectRoot)tools\r2rtest\R2RTest.csproj;
$(CoreClrProjectRoot)tools\AssemblyChecker\AssemblyChecker.csproj" Category="clr" Condition="'$(DotNetBuildFromSource)' != 'true'"/>
<ProjectToBuild Include="$(CoreClrProjectRoot)tools\aot\crossgen2\crossgen2.csproj" Category="clr" />
<ProjectToBuild Include="$(CoreClrProjectRoot)tools\aot\ILCompiler.Build.Tasks\ILCompiler.Build.Tasks.csproj" Category="clr" Condition="'$(NativeAotSupported)' == 'true'" />
<ProjectToBuild Include="$(CoreClrProjectRoot)tools\aot\ILCompiler\ILCompiler.csproj" Category="clr" Condition="'$(NativeAotSupported)' == 'true'" />
Expand Down
16 changes: 16 additions & 0 deletions src/coreclr/tools/AssemblyChecker/AssemblyChecker.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>AssemblyChecker</AssemblyName>
<OutputType>Exe</OutputType>
<TargetFramework>$(NetCoreAppToolCurrent)</TargetFramework>
<PlatformTarget>AnyCPU</PlatformTarget>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendTargetFrameworkToOutputPath Condition="'$(BuildingInsideVisualStudio)' == 'true'">true</AppendTargetFrameworkToOutputPath>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<EnableDefaultEmbeddedResourceItems>false</EnableDefaultEmbeddedResourceItems>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputPath>$(RuntimeBinDir)\AssemblyChecker</OutputPath>
<RunAnalyzers>false</RunAnalyzers>
</PropertyGroup>
</Project>
117 changes: 117 additions & 0 deletions src/coreclr/tools/AssemblyChecker/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;

namespace AssemblyChecker
{
/// <summary>
/// This is a simple console application that is designed to answer True or False
/// questions about whether a given file is a managed assembly or not.
/// You can also ask whether or not the assembly is debuggable.
/// Return code of 0 indicates the file is a managed assembly.
/// Return code of 1 indicates the file is not a managed assembly. No errors will be printed for this one.
/// </summary>
public class Program
{
private const string HelpText = @"
Usage:
<filePath>: Check if the file-path is a managed assembly.
--is-debug <filePath>: Check if the file-path is a managed assembly that is built with debuggability.
--is-exe <filePath>: Check if the file-path is a managed assembly that is an executable.
";

static bool IsAssembly(string path)
{
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

// Try to read CLI metadata from the PE file.
using var peReader = new PEReader(fs);

if (!peReader.HasMetadata)
{
return false; // File does not have CLI metadata.
}

// Check that file has an assembly manifest.
MetadataReader reader = peReader.GetMetadataReader();
return reader.IsAssembly;
}

static bool IsDebug(string path)
{
return
Assembly.LoadFrom(path)
.GetCustomAttributes(typeof(DebuggableAttribute), false)
.OfType<DebuggableAttribute>().Any(x => x.IsJITOptimizerDisabled);
}

static bool IsExe(string path)
{
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

// Try to read CLI metadata from the PE file.
using var peReader = new PEReader(fs);

if (!peReader.HasMetadata)
{
return false; // File does not have CLI metadata.
}

return peReader.PEHeaders.IsExe;
}

static int Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine(HelpText);
Console.Error.WriteLine("\nExpected assembly file-path.");
return 2;
}

// Help
if (args.Contains("-h"))
{
Console.WriteLine(HelpText);
return 0;
}

if (args.Length == 1)
{
return IsAssembly(args[0]) ? 0 : 1;
}

if (args.Length == 2)
{
switch (args[0])
{
case "--is-debug":
{
return IsDebug(args[1]) ? 0 : 1;
}

case "--is-exe":
{
return IsExe(args[1]) ? 0 : 1;
}

default:
{
Console.WriteLine(HelpText);
Console.Error.WriteLine("\nInvalid option.");
return 2;
}
}
}

Console.WriteLine(HelpText);
Console.Error.WriteLine("\nToo many arguments.");
return 2;
}
}
}
4 changes: 0 additions & 4 deletions src/tests/Common/CLRTest.Execute.Bash.targets
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,6 @@ else
LAUNCHER="$_DebuggerFullPath $_DebuggerArgsSeparator $(CLRTestRunFile)"
fi
$(BashIlrtTestLaunchCmds)
if [ ! -z ${RunCrossGen2+x} ]%3B then
TakeLock
fi
Expand Down Expand Up @@ -412,8 +410,6 @@ else
LAUNCHER="$_DebuggerFullPath $_DebuggerArgsSeparator $(CLRTestRunFile)"
fi
$(BashIlrtTestLaunchCmds)
if [ ! -z ${RunCrossGen2+x} ]%3B then
TakeLock
fi
Expand Down
2 changes: 0 additions & 2 deletions src/tests/Common/CLRTest.Execute.Batch.targets
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,6 @@ IF NOT "%CLRCustomTestLauncher%"=="" (
set LAUNCHER=%_DebuggerFullPath% $(CLRTestRunFile)
)
$(BatchIlrtTestLaunchCmds)
if defined RunCrossGen2 (
call :TakeLock
)
Expand Down

0 comments on commit cf9bb86

Please sign in to comment.