 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Does the same happen to you when doing this? |
|
|
| Total Votes : 0 |
|
| Author |
Message |
linhbeo0708 How do I cheat?
Reputation: 0
Joined: 25 Aug 2019 Posts: 2
|
Posted: Sun Aug 25, 2019 6:47 am Post subject: Cheat Engine and const |
|
|
I have found out that it's impossible to modify a const value (even through 3rd party sources, like CE)
Basically, I made a program in C++ that declares 3 variables and print them.
| Code: |
#include <iostream>
int main()
{
int a{5}; // let's say we have C++14
const int a2{6}; // const lvalue (this is a runtime constant)
constexpr int a3{7}; // constexpr lvalue (compile-time constant)
// also constexpr is a new feature in C++11
while(true) // print our values
{
std::cout << &a << "\n" << a << "\n";
std::cout << &a2 << "\n" << a2 << "\n";
std::cout << &a3 << "\n" << a3 << "\n";
// these three lines print the address of the 3 variables,
// then prints the value themselves, seperated by newlines
std::cin.get(); // waits for input
}
}
|
After connecting the program to CE, I modified the value of @a to prove that CE was working. Afterwards, I tried to modify @a2, which doesn't work. It might seem to work, but when i press enter to print the values again, the value of @a2 doesn't change. The same goes for @a3.
I guess const values do live up to their name after all...
|
|
| Back to top |
|
 |
Dark Byte Site Admin
Reputation: 470
Joined: 09 May 2003 Posts: 25807 Location: The netherlands
|
Posted: Sun Aug 25, 2019 7:38 am Post subject: |
|
|
don't forget compiler optimizations.
since it is const the compiler may have optimized it into:
| Code: |
while(true)
{
std::cout << &a << "\n" << a << "\n";
std::cout << &a2 << "\n" << 6 << "\n";
std::cout << &a3 << "\n" << 7 << "\n";
}
|
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
| Back to top |
|
 |
FreeER Grandmaster Cheater Supreme
Reputation: 53
Joined: 09 Aug 2013 Posts: 1091
|
Posted: Sun Aug 25, 2019 7:52 am Post subject: |
|
|
Go look at the assembly code for main, this is one of the advantages you get practicing on small self-made programs use it. I'm going to show it using printf
| Code: |
mov DWORD PTR [rbp-4], 5
mov DWORD PTR [rbp-8], 6
mov DWORD PTR [rbp-12], 7
...
mov edx, DWORD PTR [rbp-4]
lea rax, [rbp-4]
mov rsi, rax
mov edi, OFFSET FLAT:.LC1 // "%p %d" string literal
mov eax, 0
call printf
lea rax, [rbp-8]
mov edx, 6
mov rsi, rax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call printf
lea rax, [rbp-12]
mov edx, 7
mov rsi, rax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call printf |
As you can see, it does exactly what DB mentioned, even without specifying optimization flags it's optimizing out the const variable values and hardcoding them into the assembly
the cout code looks like | Code: | lea rax, [rbp-12]
mov rsi, rax
mov edi, OFFSET FLAT:_ZSt4cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(void const*)
mov esi, OFFSET FLAT:.LC0
mov rdi, rax
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov esi, 7
mov rdi, rax
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov esi, OFFSET FLAT:.LC0
mov rdi, rax
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) | (that's for just 1 of them)
copied from https://godbolt.org/z/jg5Oy-
_________________
|
|
| Back to top |
|
 |
ParkourPenguin I post too much
Reputation: 152
Joined: 06 Jul 2014 Posts: 4706
|
Posted: Sun Aug 25, 2019 9:45 am Post subject: |
|
|
Compilers are permitted to and generally will evaluate constant expressions at compile time regardless of the optimization level. In this case, a3 is encoded into an instruction as an immediate value (aka literal).
A const variable initialized w/ a constant expression is permitted to be used in constant expressions as if it were constexpr. a2 behaves in the same manner as a3.
Even though the first one isn't const, it's trivial to see nothing writes to it, and the c++ standard allows enough leeway for the compiler to treat it similar to the other two.
Try using the volatile keyword.
_________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
| Back to top |
|
 |
linhbeo0708 How do I cheat?
Reputation: 0
Joined: 25 Aug 2019 Posts: 2
|
Posted: Mon Aug 26, 2019 8:18 am Post subject: |
|
|
Updated the code (in my IDE) to make all 3 variables volatile. The printing process is the same, however you needed to cast the address-of the volatile value to void* to print it.
After testing, I found out that the const variable can actually be modified, however constexpr cannot. (I have read online, and volatile constexpr exists on purpose (the report about volatile constexpr has been marked with NAD (Not A Defect).
So that means constexpr values cannot be modified even with 3rd party tools.
Either that, or optimization still replace a3 with the literal 7.
(Note: The test was performed with the lowest optimization (-O0))
| Code: |
#include <iostream>
int main()
{
volatile int a{5};
volatile const int a2{6};
volatile constexpr int a3{7};
while(true)
{
std::cout << (void*)&a << "\n" << a << "\n";
std::cout << (void*)&a2 << "\n" << a2 << "\n";
std::cout << (void*)&a3 << "\n" << a3 << "\n";
std::cin.get();
}
}
|
|
|
| Back to top |
|
 |
ParkourPenguin I post too much
Reputation: 152
Joined: 06 Jul 2014 Posts: 4706
|
Posted: Mon Aug 26, 2019 9:01 am Post subject: |
|
|
Look up the section [expr.const] in the c++ standard, as well as defect report 1688:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4093.html#1688
tl;dr: volatile constexpr is valid, but it can't be used in a constant expression. If the compiler is treating it like that, then it's a compiler bug. Update it and/or make it use a newer version of the standard.
A recent version of gcc works fine:
https://godbolt.org/z/zBUS_j
| linhbeo0708 wrote: | | So that means constexpr values cannot be modified even with 3rd party tools. | Change the immediate in the instruction. Don't blame your tools if you don't know how to use them.
_________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|