[llvm-dev] Question: How to access c++ vtable pointer to use as Value* in LLVM pass

K Jelesnianski via llvm-dev llvm-dev at lists.llvm.org
Thu Apr 18 22:33:39 PDT 2019

Thanks for the information,

I already knew theory side of ""where"" a vtable is located in a C++
object, I need more information on how to access/manipulate it using
the LLVM API. Can you confirm then that the LLVM API treats the object
struct ptr also as the vtable ptr: so if I were to put it into a
function call argument, the function would then be able to manipulate
that vtable ptr specifically.



On Fri, Apr 19, 2019 at 12:49 AM Das, Dibyendu <Dibyendu.Das at amd.com> wrote:
> Hi-
> I don’t know whether you are asking this question but if you want to know where the vtable ptr is within a class its usually the first pointer which means that the object ptr is also your vtable ptr. This was a paper I wrote long back in Dr. Dobb's. You may find it useful - http://www.drdobbs.com/class-hierarchy-graphs-function-devirtua/184401938.
> -Thx
> Dibyendu
> -----Original Message-----
> From: llvm-dev <llvm-dev-bounces at lists.llvm.org> On Behalf Of K Jelesnianski via llvm-dev
> Sent: Friday, April 19, 2019 9:43 AM
> To: llvm-dev <llvm-dev at lists.llvm.org>
> Subject: [llvm-dev] Question: How to access c++ vtable pointer to use as Value* in LLVM pass
> Dear Mailing List,
> This might sound unconventional, but I am trying to access a C++ objects vtable to pass as an argument to a function call for a library function I created. Creating & inserting a function call at the correct location in LLVM is done.
> I have learned that C++ objects are represented as struct types. But I'm just not quite sure how to get at the vtable pointer within, when looking at the interface of Value:: class. clang, more specifically CGClass.cpp, deals with C++ initializing constructors and destructors and its API is straightforward while I can't find the similar API calls in the LLVM counter part.
> So far I am able to get the class object itself from a loadInst or CallInst and I can iterate through the StructType, and the structs "Types" contained within via element_begin()/element_end() to confirm what I am looking at is the object. e.g.:
> i32 (...)***    (this is how vtable is represented according to online
> sources as a generic pointer)
> i32              (class member in this case an int)
> But this doesn't give me a Value* handle i can grab to and use later.
> How can I leverage this Value to get that contained ??
> 2nd question: What happens if the struct object is from a derived class; iterating over the struct again, it looks like the vtable ptr is tangled even deeper within the object:
> %class.Base.base = type <{i32 (...)**, i32 }>
> i32
> I looked at the ThreadSanitizer.cpp pass for inspiration, and it seems they are also using MD_tbaa as hints for whether a load/store isVTableAccess(), but doesn't need the Value. Maybe MDNode metadata could be of use here?
> TLDR: How can I leverage a Value that is of StructType generated from a C++ object to get its vtable ptr in LLVM to use as a Value for a to-be-inserted function call??
> Thank you in advance!
> Sincerely,
> Christopher Jelesnianski
> Graduate Research Assistant, Virginia Tech _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

More information about the llvm-dev mailing list