Article View: comp.lang.c
Article #2047Re: C and its peasant 'ancestors'
From: Chris M. Thomasson
Date: Tue, 01 Dec 2026 05:42
Date: Tue, 01 Dec 2026 05:42
115 lines
4166 bytes
4166 bytes
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)
Technical Details
Message-ID:
<11elmnb$19l5l$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11dup6q$v4j9$1@dont-email.me> <11ehuri$3ps8n$1@raubtier-asyl.eternal-september.org> <11ei9tp$sps$1@reader2.panix.com> <11eiqbe$332f$1@raubtier-asyl.eternal-september.org> <11eisjs$f7p$1@reader2.panix.com> <11ej2k8$5cc4$1@raubtier-asyl.eternal-september.org> <11ellkg$196o4$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> Newsgroups: comp.lang.c Subject: Re: C and its peasant 'ancestors' Date: Mon, 30 Nov 2026 21:42:00 -0800 Organization: A noiseless patient Spider Lines: 115 Message-ID: <11elmnb$19l5l$1@dont-email.me> References: <11dup6q$v4j9$1@dont-email.me> <11ehuri$3ps8n$1@raubtier-asyl.eternal-september.org> <11ei9tp$sps$1@reader2.panix.com> <11eiqbe$332f$1@raubtier-asyl.eternal-september.org> <11eisjs$f7p$1@reader2.panix.com> <11ej2k8$5cc4$1@raubtier-asyl.eternal-september.org> <11ellkg$196o4$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Tue, 01 Dec 2026 05:42:04 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1365173"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18CiJTEapKagaGe622ISIAZmx2uHMslJHk="; posting-host="5898cded6d2676b56560081c09d52a4a" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:FfayDGhAyRMgVlvmoZatfQfLLV4= sha256:gfN4K+/BmfkUy4oMrTF9RQ3cyjA0ykN0VGBRf8HnnH0= sha1:Q2a84wGVahNJ5Br0myWIIXesFOw= sha256:1IemoZm7vyT5/vlyFGdZvTJgxKfj7z20xaN7ZNZeiuA= In-Reply-To: <11ellkg$196o4$1@dont-email.me> Content-Language: en-US