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

Adding variable tracking throughout assembly printout #2067

Merged
merged 8 commits into from
Aug 8, 2020

Conversation

edkazcarlson
Copy link
Contributor

This branch allows for variables to be tracked throughout the assembly, if they are of type VLT_STK, VLT_STK_BYREF, VLT_REG, VLT_REG_BYREF, or VLT_REG_FP.
Before it would look like:

; Void System.Runtime.AmbiguousImplementationException..ctor(String)
; UnwindInfo:
; Version:            1
; Flags:              0x03 EHANDLER UHANDLER
; FrameRegister:      none
; Prolog
000000000074FCD0 55                   push      rbp ; UWOP_PUSH_NONVOL(RBP(5))
000000000074FCD1 4883EC20             sub       rsp,20h ; UWOP_ALLOC_SMALL(32)
000000000074FCD5 488D6C2420           lea       rbp,[rsp+20h]
000000000074FCDA 48894D10             mov       [rbp+10h],rcx
000000000074FCDE 48895518             mov       [rbp+18h],rdx
; IL_0000
000000000074FCE2 488B4D10             mov       rcx,[rbp+10h]
000000000074FCE6 488B5518             mov       rdx,[rbp+18h]
; IL_0002
000000000074FCEA FF15382D8CFF         call      qword [rel 1`2A28h] ; instance void System.Exception::.ctor(string)
; IL_0007
000000000074FCF0 90                   nop
; IL_0008
000000000074FCF1 90                   nop
; IL_0009
000000000074FCF2 488B4D10             mov       rcx,[rbp+10h]
000000000074FCF6 BA6A101380           mov       edx,8013`106Ah
; IL_000f
000000000074FCFB FF151FF48BFF         call      qword [rel 0`F120h] ; instance void System.Exception::set_HResult(int32)
; IL_0014
000000000074FD01 90                   nop
; IL_0015
000000000074FD02 90                   nop
; Epilog
000000000074FD03 488D6500             lea       rsp,[rbp]
000000000074FD07 5D                   pop       rbp
000000000074FD08 C3                   ret

With this branches changes it would look like:

; void System.Runtime.AmbiguousImplementationException..ctor(string)
; UnwindInfo:
; Version:            1
; Flags:              0x03 EHANDLER UHANDLER
; FrameRegister:      none
; Prolog
000000000074FCD0 55                   push      rbp ; UWOP_PUSH_NONVOL(RBP(5))
000000000074FCD1 4883EC20             sub       rsp,20h ; UWOP_ALLOC_SMALL(32)
000000000074FCD5 488D6C2420           lea       rbp,[rsp+20h]
000000000074FCDA 48894D10             mov       [rbp+10h],rcx; RCX = Parameter 0
000000000074FCDE 48895518             mov       [rbp+18h],rdx; RDX = Local 0
; IL_0000
000000000074FCE2 488B4D10             mov       rcx,[rbp+10h]
000000000074FCE6 488B5518             mov       rdx,[rbp+18h]; [RBP+24] = Local 0
; IL_0002
000000000074FCEA FF15382D8CFF         call      qword [rel 1`2A28h] ; instance void System.Exception::.ctor(string)
; IL_0007
000000000074FCF0 90                   nop
; IL_0008
000000000074FCF1 90                   nop
; IL_0009
000000000074FCF2 488B4D10             mov       rcx,[rbp+10h]; [RBP+16] = Parameter 0
000000000074FCF6 BA6A101380           mov       edx,8013`106Ah
; IL_000f
000000000074FCFB FF151FF48BFF         call      qword [rel 0`F120h] ; instance void System.Exception::set_HResult(int32)
; IL_0014
000000000074FD01 90                   nop
; IL_0015
000000000074FD02 90                   nop
; Epilog
000000000074FD03 488D6500             lea       rsp,[rbp]
000000000074FD07 5D                   pop       rbp
000000000074FD08 C3                   ret

This branch uses changes in ILCompiler.Reflection.ReadyToRun, and I'm not entirely sure if those changes are included in this branch completely or if it only added a reference. Also does not track some of the less used variable types such as VLT_REG_REG, as I do not have any examples to test with.

@@ -37,6 +37,9 @@
<ProjectReference Include="..\SharpTreeView\ICSharpCode.TreeView.csproj">
<Private>False</Private>
</ProjectReference>
<Reference Include="ILCompiler.Reflection.ReadyToRun">
<HintPath>..\..\..\Desktop\r2r\ILCompiler.Reflection.ReadyToRun\bin\Debug\netstandard2.0\ILCompiler.Reflection.ReadyToRun.dll</HintPath>
Copy link
Contributor

Choose a reason for hiding this comment

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

This won't work. We need to make a PR to dotnet/runtime, get it merged first.
Once we get that, I can work on getting a new nuget package version 1.0.9-alpha


{
Dictionary<ulong, UnwindCode> unwindCodes = new Dictionary<ulong, UnwindCode>();
Dictionary<ulong, HashSet<UnwindCode>> unwindCodes = new Dictionary<ulong, HashSet<UnwindCode>>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you finding examples that we have more than one unwind opcode associated with a single code offset? Please let us know. This looks like something is wrong with the compiler or my understanding.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for System.io.FileLoadException.GetMessageForHR in System.private.corelib I am finding 2 opcodes with the same offset of 19

Copy link
Contributor

Choose a reason for hiding this comment

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

The case is understood, the root cause is a bug in ILCompiler.Reflection.ReadyToRun. It would be great that we fix the root cause instead of having this hack.

Copy link
Contributor

Choose a reason for hiding this comment

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

This PR should fix it.

foreach (Tuple<DebugInfo, NativeVarInfo> tuple in regSet) {
var debugInfo = tuple.Item1;
var varInfo = tuple.Item2;
if (varInfo.StartOffset < instr.IP - baseInstrIP && varInfo.EndOffset > instr.IP - baseInstrIP &&
Copy link
Contributor

Choose a reason for hiding this comment

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

I need to double-check with this, I think the StartOffset should be inclusive, the EndOffset I am not sure.

DebugInfo.GetPlatformSpecificRegister(debugInfo.Machine, varInfo.VariableLocation.Data1) == usedMemInfo.Base.ToString() &&
adjOffset == usedMemInfo.Displacement) {

output.Write($"; [{usedMemInfo.Base.ToString()}{(negativeOffset ? '-' : '+')}{Math.Abs(stackOffset)}] = {varInfo.Variable.Type} {varInfo.Variable.Index}");
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be nice to show hex for stackOffset to be consistent with the disassembly.

@@ -49,7 +49,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.7.0-2.final" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic" Version="3.7.0-2.final" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<PackageReference Include="System.Collections.Immutable" Version="1.5.0" />
<PackageReference Include="System.Collections.Immutable" Version="1.7.1" />
Copy link
Contributor

Choose a reason for hiding this comment

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

We should not be changing these as part of this change? I think they should already be in master.
The commits are getting convoluted, can we squash them?

@cshung
Copy link
Contributor

cshung commented Jul 29, 2020

@cshung

@cshung
Copy link
Contributor

cshung commented Aug 2, 2020

@christophwille
This change is ready to be reviewed.

@edkazcarlson edkazcarlson marked this pull request as ready for review August 2, 2020 22:47
ILSpy.ReadyToRun/ReadyToRunLanguage.cs Outdated Show resolved Hide resolved
output.WriteLine($" Offset: {DebugInfo.GetPlatformSpecificRegister(debugInfo.Machine, varLoc.VariableLocation.Data1)}");
break;
default:
throw new BadImageFormatException("Unexpected var loc type");
Copy link
Member

Choose a reason for hiding this comment

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

Can we print a user-friendly message instead?

edkazcarlson and others added 3 commits August 8, 2020 10:41
Change how it handle's unexpected var loc types.

Co-authored-by: Siegfried Pammer <siegfriedpammer@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants