Skip to content

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

Merged
siegfriedpammer merged 2 commits intoicsharpcode:masterfrom
ElektroKill:mcs264-pinned-region
Jul 2, 2023
Merged

Add support for MCS 2.6.4 pinned region with string variable#3015
siegfriedpammer merged 2 commits intoicsharpcode:masterfrom
ElektroKill:mcs264-pinned-region

Conversation

@ElektroKill
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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