digitalmars.D.internals - Modifying vtbl in ClassInfo
- Jean-Louis Leroy (27/27) Jun 26 2017 I realize I am doing bad things, anyway...
- Jacob Carlborg (5/31) Jun 27 2017 It works for me on macOS using DMD. Might behave differently on
- Walter Bright (4/4) Jul 21 2017 You could make it work by making a copy of the vtbl[], modifying it as y...
- Jean-Louis Leroy (5/10) Jul 22 2017 Thanks for the suggestion. I am working on open methods, not
I realize I am doing bad things, anyway...
I'd like to modify the vtbl inside ClassInfo. My goal is to stick
a pointer inside each instance of a series of types, and I
thought I'd reserve a slot in the vtbl and put the pointer in
place of the function, like this:
import std.stdio;
class MethObject
{
void methSlotTable() { }
}
int[] a_slots = [ 0, 1 ];
void main()
{
int firstUserVFunc = 5;
assert(MethObject.classinfo.vtbl[firstUserVFunc] ==
&MethObject.methSlotTable);
writeln(MethObject.classinfo.vtbl);
MethObject.classinfo.vtbl[firstUserVFunc] = cast(void*)
a_slots.ptr;
}
Not so surprisingly, I get a segfault:
ldc2 -unittest --run abi.d
[624230, 40CC50, 40C9E0, 40CC70, 40CB70, 40CED0]
Error: abi failed with status: -2
Error: message: Segmentation fault (core dumped)
Error: program received signal 2 (Interrupt)
Is the vtbl mprotected? Is there a chance of making this work?
Jun 26 2017
On 2017-06-27 00:19, Jean-Louis Leroy wrote:
I realize I am doing bad things, anyway...
I'd like to modify the vtbl inside ClassInfo. My goal is to stick a
pointer inside each instance of a series of types, and I thought I'd
reserve a slot in the vtbl and put the pointer in place of the function,
like this:
import std.stdio;
class MethObject
{
void methSlotTable() { }
}
int[] a_slots = [ 0, 1 ];
void main()
{
int firstUserVFunc = 5;
assert(MethObject.classinfo.vtbl[firstUserVFunc] ==
&MethObject.methSlotTable);
writeln(MethObject.classinfo.vtbl);
MethObject.classinfo.vtbl[firstUserVFunc] = cast(void*) a_slots.ptr;
}
Not so surprisingly, I get a segfault:
ldc2 -unittest --run abi.d
[624230, 40CC50, 40C9E0, 40CC70, 40CB70, 40CED0]
Error: abi failed with status: -2
Error: message: Segmentation fault (core dumped)
Error: program received signal 2 (Interrupt)
Is the vtbl mprotected? Is there a chance of making this work?
It works for me on macOS using DMD. Might behave differently on
different systems with different compilers.
--
/Jacob Carlborg
Jun 27 2017
You could make it work by making a copy of the vtbl[], modifying it as you please, and seeing __vptr to it. If you're trying to implement signals and slots, it's already in the library: http://dlang.org/phobos/std_signals.html
Jul 21 2017
On Saturday, 22 July 2017 at 01:18:12 UTC, Walter Bright wrote:You could make it work by making a copy of the vtbl[], modifying it as you please, and seeing __vptr to it. If you're trying to implement signals and slots, it's already in the library: http://dlang.org/phobos/std_signals.htmlThanks for the suggestion. I am working on open methods, not signals. So far I've hijacked the 'deallocator' field but now I am experimenting with a perfect hash of the vptr. Seems to work quite well...
Jul 22 2017









Jacob Carlborg <doob me.com> 