Mono x86-32 forge MonoVTable reference of MonoObject

Started by RawCode, May 01, 2016, 05:16:31 AM

Previous topic - Next topic

RawCode

subj:


[MethodImpl(MethodImplOptions.NoInlining)]
static public void ForgeObjectType
(
object Target,
Type Newtype
)
{
MethodInfo _CII = typeof(Activator).GetMethod ("CreateInstanceInternal", (BindingFlags)60);
object vtablesource = _CII.Invoke (null, new object[]{ Newtype });
UnionHack.Cast (Target)[0] = UnionHack.Cast (vtablesource)[0];
}


union hack definition:


[StructLayout(LayoutKind.Explicit)]
public unsafe struct UnionHack
{
[FieldOffset(0)] public object arg;
[FieldOffset(0)] public int*   ret;

static private UnionHack INSTANCE = new UnionHack ();

static public int* Cast(object o)
{
INSTANCE.arg = o;
return INSTANCE.ret;
}

private void illayer()
{
DynamicMethod dm = new DynamicMethod("void",typeof(int),new Type[]{typeof(object)});
ILGenerator ilg = dm.GetILGenerator ();
ilg.Emit (OpCodes.Ldarg_0);
ilg.Emit (OpCodes.Conv_I);
ilg.Emit (OpCodes.Ret);
}
}


1) unsafe cast, string to ArrayList anyone
2) full access to private fields with native performance levels
3) memcopy for objects
4) other funny applications

1000101

Interesting bit of code.  Could you provide a simple example of usage?

I had heard (via a third party's testing) that the mono library / unity engine that RimWorld ships with, there was no access to the compiler services library.  I have tried similar things in the past which worked just fine when compiling against mono/.Net 3.5 but failed entirely within RimWorld.
(2*b)||!(2*b) - That is the question.
There are 10 kinds of people in this world - those that understand binary and those that don't.

Powered By

RawCode

illayer() is not used and provided for reference only.

mono library provided with game have large number of methods removed from export table, including mono_reflection_create_dynamic_method essential for dynamic code.

But...
this is not safety measure and just filter for lazy hackers, who care about dynamic methods when you allowed to load arbitrary DLLs?