Skip to main content

Module vtable

Module vtable 

Source
Expand description

Helpers for accessing vtables (primary and secondary) of server-owned C++ objects.

In C++ with multiple inheritance, each base class with virtuals results in a distinct vtable. The primary lies at offset 0 of the object; secondaries at offsets that depend on the sizeof of the preceding bases. The offsets are fixed per class and known at compile time (after layout analysis via disasm).

This module centralizes the repeated pattern of:

  1. Adjust the object pointer to point to a subobject (obj + offset).
  2. Read the secondary vtable (*subobject).
  3. Load the slot N pointer (*(vtable + N * sizeof(usize))).

Each specific caller still performs the final transmute to the correct function type, because the calling convention varies (extern "C", extern "thiscall", variadic vs fixed arity).

§Example usage

// ILogger at offset 56 inside ICore; logLn at slot [2].
let (this, f_ptr) = unsafe {
    vtable::secondary_call_target(core, 56, 2)?
};
let f: LogLnFn = unsafe { std::mem::transmute(f_ptr) };
unsafe { f(this, level, fmt, arg) };

Functions§

secondary_call_target
Combines subobject_ptr + vtable_slot in a single helper.
subobject_ptr
Returns the subobject pointer at offset bytes from obj.
vtable_slot
Reads the slot slot pointer from the vtable pointed to by subobject.