'How to check the CRC of the function in C++

Is it possible to count the CRC of code in the memory of the function in the runtime?

I have a function that compares user's password with the secret password. If I check the CRC of all commands in function, I will be able to understand if the code of the function was overwritten in disassembler.

For now, I've tried using the Boost::crc but I'm not sure, how can I pass the function into the crc_32_type object.

This is not working (at least, returns a new result every time I run the program):

void myFunction() {...};

auto GetCrc32() -> decltype(boost::crc_32_type().checksum()){
    std::function<void()> func = myFunction;

    boost::crc_32_type result;
    result.process_bytes(&func, sizeof(func));
    return result.checksum();
}


Solution 1:[1]

No, there is no portable way to find or access the compiled function code during execution.

If code can be modified maliciously, then the code checking the code can also be modified maliciously to not detect changes, making the exercise entirely pointless.

Lastly, a CRC is easily spoofed, so the code could easily be modified in a way that leaves the CRC unchanged.

None of what you're trying to do makes any sense.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mark Adler