Recently, we have used ollvm to compile some code. We need to perform some obfuscation operations on so, and found a bug, which is recorded as follows:
Code segment 1
jint begin_antidebug(){ pthread_t antidebugtid; int ret = 0; ret = pthread_create(&antidebugtid, NULL, antidebug_listen_thread, NULL); if(ret!=0) { LOGANTI("Create pthread error!\n"); exit (1); } return 0; }
The above code snippet completes the logic of creating a thread. If the thread fails to be created, the Code directly exits the application.
The android ndk default compiler or ollvm compiler runs according to the code logic.
However, if code segment 2 is as follows:
jint begin_antidebug(){ pthread_t antidebugtid; int ret = 0; ret = pthread_create(&antidebugtid, NULL, antidebug_listen_thread, NULL); if(ret!=0) { LOGANTI("Create pthread error!\n"); exit (1); } }
This code is compared with the above Code, with a return 0 missing; this statement
At this time, the compilation results of the two compilers are different.
The Code Compiled by the android ndk compiler by default can still run according to the code logic.
The Code Compiled by the ollvm compiler is optimized to 0 by ret. Although the thread is successfully created, the process also executes exit (1 );
Exited
To prevent this type of bad code, add the following line to Android. mk.
Local_cflags + =-werror
Strengthen the check type, and handle the same issue as warning and error.
Ollvm Compiler Optimization bug