Skip to content

Instantly share code, notes, and snippets.

@fifty-six
Last active February 18, 2022 21:04
Show Gist options
  • Save fifty-six/dcf0148562792885efa8f2e1b4440214 to your computer and use it in GitHub Desktop.
Save fifty-six/dcf0148562792885efa8f2e1b4440214 to your computer and use it in GitHub Desktop.
using System.Runtime.CompilerServices;
#pragma warning disable CS8604
#pragma warning disable CS8600
#pragma warning disable CS0660
#pragma warning disable CS0661
C local = null;
if (local != null && local.Prop)
{
Console.WriteLine(local.ToString());
}
internal class C
{
public bool Prop
{
get
{
unsafe
{
void** rsp = stackalloc void*[1];
C self = this;
if (sizeof(nuint) != sizeof(void*))
throw new InvalidOperationException("wtf");
var ptr = (void*) Unsafe.As<C, nuint>(ref self);
/*
* Call another method to ensure that rcx != this
* That way when we overwrite values on the stack,
* not only do we null the local in Main, but we
* null the caller-saved value of rcx.
*/
stack(rsp, ptr);
}
return true;
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static unsafe void stack(void** rsp, void* ptr)
{
const int cap = 200;
for (int i = 0; i < cap; i++)
{
if (*rsp == ptr)
*rsp = (void*) 0;
rsp++;
}
}
public static bool operator !=(in C a, in C _)
{
Unsafe.AsRef(a) = new C();
return true;
}
public static bool operator ==(C a, C b) => throw new NotImplementedException();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment