ASM Tricks Bonus Vid: C++ Calling Member Methods

preview_player
Показать описание
In this video we look at how we can call C++ member methods from our Assembly code. The main tricks involved here are getting the actual name of the function (the name will usually be mangled by the compiler), and then being careful to ensure the first parameter is a pointer to the object, the *this pointer.

Support What's a Creel? on Patreon:

FaceBook:

Music Channel:

Another channel with random things:

Software used to make this vid:

Visual Studio 2019 Community:

Audacity:

OBS:

Davinci Resolve 16:

OpenOffice:

Gimp:
Рекомендации по теме
Комментарии
Автор

You're one of the few people I enjoy learning from. Keep up the good work.

WingsOfAltair
Автор

Great video once again! Thanks for putting this up! Looking forward to part 2 of this video with virtual tables and static functions :)

giladreich
Автор

Calling static and virtual C++ methods is bit different. Static methods do not have the "this" pointer since they are do not associate with any particular object and virtual ones do some pointer chasing.

sakari_n
Автор

You can also get the mangled names in a listing file output without forcing an error. Just turn on the option for a listing file and option for assembled output. You'll find all the mangled names and a lot of other 'interesting' features done by the compiler this way. I've done this sometimes when trying to fix problems with unresolved externals and such.

mikefochtman
Автор

Thanks for all the great videos! Super clear explanation always and easy to follow. Wondering if you could create a new video about shadow space and stack alignment requirements if possible as those two topics are the most troublesome for me. Cheers.

bartekkawalkowski
Автор

he's back. Come self-isolate with us

him
Автор

You forgot to say "don't actually do this if you can help it, because c++ doesn't have a portable abi"

odomobo
Автор

I think you actually need at 32 bytes of shadow space no matter how many parameters you have. The stack also needs to be aligned by 16 before the call, so it should be sub rsp, 40.

AAA-degt
Автор

It's a wrong code because there is an undefined behavior depends on the called member function's complexity. According the Microsoft's X64 calling convention the caller must provide 32 bytes shadow space ( for the rcx rdx r8 r9) even if the called function doesn't have 4 parameters. And while the called function is executed (the GetI here) it allowed to use that shadow space as general purpose storage. Your SomeFunction is violated the calling convention, but the GetI can use the shadwo space to store the state of some registers, so it possible that the SomeFunction overrides the return address or the main's stackframe, and i can cause a crash after the return instruction of GetI. The rigth way to call the GetI from the SomeFunction is just simply jmp GetI_mangled or sub rsp 0x28, 8 for the 16 byte alignment and 32 for the 4 registers

Nyufu
Автор

Please please please, everyone who is watching this and now thinking: "wow I have learnt something new!" Please do NOT just copy this.

It is conceptually not good to hardwire the code to a specific compiler that way. And in particular it is done incorrectly, because if you want to code against the Microsoft C++ compiler, you are always required to provide exactly 32 bytes of shadow space and not 8. *If you are working with anything that is not windows, you do not have this shadow space at all!* So you cannot transfer this "knowledge" to other platforms and you certainly should NOT think "oh wow this is how we do it!" - no it is not. And I really recommend to change the video titel to "C++ Calling Member Methods in Microsoft Window x64 ABI (only)" - of course after correcting the shadow space length.

Additionally the name mangling is not standardized and every compiler (even different versions of the same compiler) may do it differntly. What you would really try to do if you really really really need to call a method from assembly is to provide an unmangled extern "C" wrapper function that calls the method in C++ code. Then you can call this function from assembly more naturally with C linkage. The wrapper will take care of calling the method correctly. However the above problem of different ABIs still remains. So you are very likely required to provide different implementations for windows and non-windows or you disable support for other platforms at all, making your software inaccessible for a wide range of users.

But to be more general on this: you are cool when you speak some assembly, that's nice. You are absolutely not cool (in particular for code reviewers), if you don't understand it properly and just hack something in what seemed to work in a youtube video. And again: never copy code from the internet! NEVER!

iUniversEi
Автор

Please do a video on shadow space. It sounds most intriguing. Also, space is the place.

LukeAvedon
Автор

question, why do so many c++ programmers use *.h instead of *.hpp which is designated for c++ specifically???

zxuiji
Автор

Great videos. Super informative. Can you explain how can you call a function with more parameters (say more than there are registers?) ?. I found it weird that python explicitly states `self` as first parameter for class methods, but it makes more sense now. Cheers

RaduCruceru
Автор

Thanks bro, really useful vid.
I'll try to see if it is possible to automate the "Mangled Name Getting" procedure

gideonmaxmerling
Автор

Could you call a private method this way as well, or is that prevented by linkage or something?

pat
Автор

The fact that the only way to get the mangled name out of your IDE is to force it into an error state... Does not spark joy.

delphicdescant
Автор

I have a problem with my code, I'm trying to use malloc and free in my assembly code.
I've figured out that their mangled names are actually __imp_malloc and __imp_free.
I've included corecrt_malloc.h, which is the malloc& free header and I've referenced them in assembly using extern but trying to build causes an "unresolved external symbol" error.
please help.

gideonmaxmerling
Автор

Awesome vid, but you simply changing the end picture of the code instead of showing how you type it in, is somewhat disorienting. Thus next time please include how you type it in :)

wothin
welcome to shbcf.ru