Skip to content

Instantly share code, notes, and snippets.

@tritao
Created August 24, 2018 20:27
Show Gist options
  • Save tritao/0c624e66357ae588089090f8b6b9d92a to your computer and use it in GitHub Desktop.
Save tritao/0c624e66357ae588089090f8b6b9d92a to your computer and use it in GitHub Desktop.
```csharp
private void SetupVTables(bool destructorOnly = false)
{
if (__OriginalVTables != null)
return;
__OriginalVTables = new void*[] { *(void**) (__Instance + 0) };
if (_Thunks == null)
{
_Thunks = new void*[2];
_dtorDelegateInstance += _dtorDelegateHook;
_Thunks[0] = Marshal.GetFunctionPointerForDelegate(_dtorDelegateInstance).ToPointer();
_AbstractDelegateInstance += _AbstractDelegateHook;
_Thunks[1] = Marshal.GetFunctionPointerForDelegate(_AbstractDelegateInstance).ToPointer();
}
if (destructorOnly)
{
if (__ManagedVTablesDtorOnly == null)
{
__ManagedVTablesDtorOnly = new void*[1];
var vtptr = Marshal.AllocHGlobal(5 * 8);
var vfptr0 = vtptr + 2 * 8;
__ManagedVTablesDtorOnly[0] = vfptr0.ToPointer();
*(void**) (vfptr0 + -16) = *(void**) (new IntPtr(*(void**) __Instance) + 0 + -16);
*(void**) (vfptr0 + -8) = *(void**) (new IntPtr(*(void**) __Instance) + 0 + -8);
*(void**) (vfptr0 + 0) = *(void**) (new IntPtr(*(void**) __Instance) + 0 + 0);
*(void**) (vfptr0 + 8) = _Thunks[0];
*(void**) (vfptr0 + 16) = *(void**) (new IntPtr(*(void**) __Instance) + 0 + 16);
}
*(void**) (__Instance + 0) = __ManagedVTablesDtorOnly[0];
}
```
```
Vtable for 'AbstractWithVirtualDtor' (5 entries).
0 | offset_to_top (0)
1 | AbstractWithVirtualDtor RTTI
-- (AbstractWithVirtualDtor, 0) vtable address --
2 | AbstractWithVirtualDtor::~AbstractWithVirtualDtor() [complete]
3 | AbstractWithVirtualDtor::~AbstractWithVirtualDtor() [deleting]
4 | void AbstractWithVirtualDtor::abstract() [pure]
```
```
define void @_ZN23AbstractWithVirtualDtorD1Ev(%class.AbstractWithVirtualDtor*) unnamed_addr #0 align 2 !dbg !5037 {
%2 = alloca %class.AbstractWithVirtualDtor*, align 8
store %class.AbstractWithVirtualDtor* %0, %class.AbstractWithVirtualDtor** %2, align 8
call void @llvm.dbg.declare(metadata %class.AbstractWithVirtualDtor** %2, metadata !5038, metadata !DIExpression()), !dbg !5039
%3 = load %class.AbstractWithVirtualDtor*, %class.AbstractWithVirtualDtor** %2, align 8
call void @llvm.trap() #7, !dbg !5040
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment