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

Add support for MCS 2.6.4 pinned region with string variable #3015

Merged

Conversation

ElektroKill
Copy link
Contributor

@ElektroKill ElektroKill commented Jun 18, 2023

Link to issue(s) this covers:
N/A

Problem

Decompilation of the following code compiled with MCS 2.6.4 was not properly supported and resulted in ugly and invalid code:

fixed (char* ptr = text, userName = Environment.UserName, ptr2 = text)
{
	*ptr = 'c';
	*userName = 'd';
	*ptr2 = 'e';
}

would result in:

fixed (string text2 = text)
{
	fixed (char* ptr = &System.Runtime.CompilerServices.Unsafe.AsRef<char>((char*)((long)(IntPtr)text2 + (long)RuntimeHelpers.OffsetToStringData)))
	{
		fixed (string text3 = Environment.UserName)
		{
			fixed (char* ptr2 = &System.Runtime.CompilerServices.Unsafe.AsRef<char>((char*)((long)(IntPtr)text3 + (long)RuntimeHelpers.OffsetToStringData)))
			{
				fixed (string text4 = text)
				{
					fixed (char* ptr3 = &System.Runtime.CompilerServices.Unsafe.AsRef<char>((char*)((long)(IntPtr)text4 + (long)RuntimeHelpers.OffsetToStringData)))
					{
						*ptr = 'c';
						*ptr2 = 'd';
						*ptr3 = 'e';
					}
				}
			}
		}
	}
}
text3 = null;
text4 = null;

The code is now decompiled as:

fixed (char* ptr = text)
{
	fixed (char* ptr2 = Environment.UserName)
	{
		fixed (char* ptr3 = text)
		{
			*ptr = 'c';
			*ptr2 = 'd';
			*ptr3 = 'e';
		}
	}
}

Solution

  • Adjusted login in the DetectPinnedRegion transform to handle the alternate code generation from the MCS 2.6.4 compiler.
  • No unit test was added as the current Pretty tests don't support the MCS 2.6.4 compiler.

@dgrunwald
Copy link
Member

You could add an ILPretty test instead.

@christophwille christophwille changed the title Add support for MSC 2.6.4 pinned region with string variable Add support for MCS 2.6.4 pinned region with string variable Jun 19, 2023
@christophwille christophwille added Decompiler The decompiler engine itself mcs Problems with assemblies generated by the Mono compiler labels Jun 22, 2023
@siegfriedpammer siegfriedpammer merged commit 1758547 into icsharpcode:master Jul 2, 2023
@siegfriedpammer
Copy link
Member

Thank you very much for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Decompiler The decompiler engine itself mcs Problems with assemblies generated by the Mono compiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants