On 11/30/2026 9:23 PM, Chris M. Thomasson wrote:
> Think of storing the original vtable pointer from something, oh, say like in my code:
>
> https://pastebin.com/raw/f52a443b1
>
> struct device {
> struct device_vtable const* vtable;
> };
>
> You make another vtable that acts as a proxy for starters, just intercepts everything then sends along to the stored original vtable:
>
>
>
> struct device_vtable* old = device->vtable;
> device->vtable = &new;
>
> Then in the new table we simply forward things to the old vtable.
>
> Now, we have a man in the middle.
>
> This can be thunked:
Fwiw, here is a stupid crude and quick way to do it. Even though all of the device internals are private and not exposed to the sample application:
/* Sample Application
____________________________________________________________________*/
static int thunk_object_destroy(void* const);
static int thunk_device_read(void* const, void*, size_t);
static int thunk_device_write(void* const, void const*, size_t);
static struct device_vtable const g_thunk_table = {
{ /* object */
thunk_object_destroy
},
{ /* device */
thunk_device_read,
thunk_device_write
}
};
static struct device_vtable const* g_thunk_old = 0;
int thunk_object_destroy(
void* const self_
) {
printf("thunk_object_destroy(%p)\n", (void*)self_);
return g_thunk_old->object.fp_destroy(self_);
}
int thunk_device_read(
void* const self_,
void* buf,
size_t size
) {
printf("thunk_device_read(%p, %p, %lu)\n",
(void*)self_, buf, (unsigned long)size);
return g_thunk_old->device.fp_read(self_, buf, size);
}
int thunk_device_write(
void* const self_,
void const* buf,
size_t size
) {
printf("thunk_device_write(%p, %p, %lu)\n",
(void*)self_, buf, (unsigned long)size);
return g_thunk_old->device.fp_write(self_, buf, size);
}
void read_write(
struct device* const self
) {
char buf[100];
device_read(self, buf, 50);
device_write(self, buf, 5);
}
int main(void) {
struct device* a_device;
if (! usb_drive_create(&a_device)) {
// thunk it...
g_thunk_old = a_device->vtable;
a_device->vtable = &g_thunk_table;
read_write(a_device);
object_destroy(a_device);
}
return 0;
}
I get:
thunk_device_read(0xb8343c4e32b0, 0xffffd82045c8, 50)
usb_drive_device_read(0xb8343c4e32b0, 0xffffd82045c8, 50)
thunk_device_write(0xb8343c4e32b0, 0xffffd82045c8, 5)
usb_drive_device_write(0xb8343c4e32b0, 0xffffd82045c8, 5)
thunk_object_destroy(0xb8343c4e32b0)
usb_drive_object_destroy(0xb8343c4e32b0)