Open the game to run 1.98 billion if statements? Hackers ridicule Rockstar’s game code for being too bad

Time:2021-7-29

Transfer from: qubit

A cigarette,GTA 5 OnlineFinally opened.

Open the game to run 1.98 billion if statements? Hackers ridicule Rockstar's game code for being too bad

“Seven years! GTA 5 online version loading is still so slow? “

Open the game to run 1.98 billion if statements? Hackers ridicule Rockstar's game code for being too bad
*Please wait forever to play

Game player make complaints about Reddit, Steam and HackerNews.

If you enter the game, you can wait as little as 5 or 6 minutes and as much as 20 minutes.

Finally, a big brother hacker couldn’t bear it. He used the inverse compiler to check the operation one by one, and finally found the reason.

It turns out that the code written by R star (game developer Rockstar) is too inefficient. When loading, an IF statement actually loops 1.98 billion times

Behind the scenes: who takes up a lot of time?

How slow is it to load GTA 5 online?

Open the game to run 1.98 billion if statements? Hackers ridicule Rockstar's game code for being too bad

Local tyrant players with full hardware, please ignore

In the survey launched by reddit related sections, more than 80 \% of players have to wait more than 3 minutes, and some even more than 15 minutes.

Moreover, this situation has not improved since online was launched seven years ago.

Grumpy, has scolded dirty words

Open the game to run 1.98 billion if statements? Hackers ridicule Rockstar's game code for being too bad

But strangely, if you choose story mode (stand-alone version), the load will be much faster, and it even feels like a game developed by two different studios.

For the example of the hacker, his own hardware configuration is as follows:

Open the game to run 1.98 billion if statements? Hackers ridicule Rockstar's game code for being too bad

CPU is an old and firm AMD fx-83502012. It was launched in 2012 and adopts “bulldozer” architecture, with amazing overclocking potential.

Graphics card or GTX 1070.

For today’s old configuration, it takes 1 minute and 10 seconds to open the stand-alone GTA 5, while it takes 6 minutes to load the online version.

The hacker used the simplest Windows Task Manager to determine which computer resources were called when the online GTA 5 was started.

Open the game to run 1.98 billion if statements? Hackers ridicule Rockstar's game code for being too bad

On the 1-minute time dividing line, the basic content common to the stand-alone and online versions is loaded before, and then the content unique to the online version.

It can be seen that the online version of GTA 5 calls a large amount of CPU resources for at least 4 minutes when loading.

At the same time, the use of memory, GPU and hard disk has hardly changed significantly.

Therefore, the problem probably lies in the code.

“R star code is too bad!”

Before hacking the R-star code, the hacker said:

I smell bad code

To find out which part of the program is stuck in the CPU, he uses a toolLuke Stackwalker, sample and analyze the CPU task stack.

For closed source applications, Luke stackwalker can transfer the running process stack and the position of the current instruction pointer to establish a call tree at a certain time interval.

Finally, the statistical data of program operation can be obtained by integrating the data.

From the results, there are two functions that “jam” the CPU:

Open the game to run 1.98 billion if statements? Hackers ridicule Rockstar's game code for being too bad

So he used a professional code disassembly tool to “open the belly” of GTA 5.

Walk down the call stack and find that the problem lies in onesscanfFunction.

Open the game to run 1.98 billion if statements? Hackers ridicule Rockstar's game code for being too bad

The function of sscanf is to read the data in the formatted string. In GTA 5, it is reading a JSON file with about 10m and more than 63000 entries.

What is this file for? Hacker brother speculated that this may beIn game shopping store related content.

Open the game to run 1.98 billion if statements? Hackers ridicule Rockstar's game code for being too bad

During the specific operation, sscanf reads each character one by one for each valid value, and then returns the result. Then, the pointer moves to the next value, and repeats… Until all 10m files are scanned.

Look at the second problem. This is a storage command. The object isitem, what exactly is unknown.

However, before saving, there is an IF statement that compares the hash values of items in item one by one to check whether they appear in a list.

According to his calculation, this step if is to execute (63000 ^ 2 + 63000) / 2=1984531500Times!

Yes, in the ten minutes before loading, GTA 5 executed 1.98 billion if commands with your CPU.

Such a simple and crude programming idea makes the old brother cry and laugh:

Since the object has a unique hash value, why nothash map???

Open the game to run 1.98 billion if statements? Hackers ridicule Rockstar's game code for being too bad

(HashMap stores data according to the hashcode value. In most cases, it can directly locate its value, so it has fast access speed, but the traversal order is uncertain.)

The problem is solved and the loading time is saved by 70 \%

As for the first question, the hacker uses the hook method. Instead of reading strings one by one, he:

hook strlen
Cache string start and current length.
If the function is called here within the string range, it returns the cached value

As for the if statement, it’s more direct — skip the duplicate check completely and insert the item with the hash map, because these values are unique.

The final results are as follows:

Open the game to run 1.98 billion if statements? Hackers ridicule Rockstar's game code for being too bad

Now, GTA 5 online version is loaded, down from 6 minutes to 1 minute and 50 seconds! Moreover, it still uses the hardware configuration of seven or eight years ago.

Here, you should manually \ @ r Star: have you failed?

The hacker didn’t leave any identity information in his blog, nor did he disclose the decompilation tools he used, but he did a good job without leaving his name. He uploaded the packaged tools to GitHub, and players can download them through one line of code:

git clone —recurse-submodules https://github.com/tostercx/GTAO_Booster_PoC

After that, paste the DLL file into the root directory of the game!

Blog original
https://nee.lv/2021/02/28/How…

GitHub address:

https://github.com/tostercx/GTAO\_Booster\_PoC

Open source outpostShare popular, interesting and practical open source projects on a daily basis. Participate in maintaining the open source technology resource library of 100000 + star, including python, Java, C / C + +, go, JS, CSS, node.js, PHP,. Net, etc.