The trick to prevent the compiler from constantly laying out the expression

I have a string literal in my program, I am trying to create an amateur checksum to ensure that the string literal has not been replaced in a portable executable.

To do this, I create a string literal hash and save it as an integer literal in the program. Now I have two literals: one for the string and one for the hash.

In my code, I implement a checksum using a function that hashes a string literal in the same way, I create a new runtime hash and check that hash for a hash literal.

The problem is that with compiler optimization, it can pre-compute the run-time hash, and then im check the hash literal against the hash literal, and the checksum will always return true.

So, I'm looking for a trick to make the compiler think that a string literal is a dynamic string that can be anything, so it won’t do constant bending optimization during the execution of the hash, and my code will work correctly.

+4
source share
1 answer

Perhaps you can declare a string literal as const volatile, for example.

const volatile char myliteral[] = "some literal string";

, . - , .

, (, file1.c file2.c), , inlining . GCC ( GCC 4.9 5) -flto , , (, CC=gcc -flto -O2 Makefile), .

, . - . Linux . dlopen (3), dlsym (3), dladdr (3), dl_iterate_phdr (5), (5), proc (5)

, (, hash myliteral+random()%strlen(myliteral) ) . , !

, : , .

+7

Source: https://habr.com/ru/post/1606168/


All Articles