標籤:android 編譯器 create error return
近來用ollvm來編譯一些代碼,主要是需要對so進行一些混淆的操作,發現了一個bug,記錄如下:
程式碼片段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; }
上面的程式碼片段就是完成一個建立線程的邏輯,如果線程建立失敗,直接退出應用,這段代碼不管用
android ndk預設的編譯器或者是ollvm編譯器,運行結果都是按照代碼邏輯的。
但是如果程式碼片段2,如下
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;這條語句
這個時候,兩種編譯器對於編譯的結果就有差異了
android ndk預設編譯器 編譯出來的代碼依然能夠按照代碼邏輯運行。
而 ollvm編譯器編譯出來的代碼,ret最佳化成0了,雖然線程建立成功了,但是進程也執行 exit(1);
退出了
防止這類bad code的方法,在Android.mk加入下面這行。
LOCAL_CFLAGS += -Werror
加強檢查類型,出現warning和error同樣處理
ollvm 編譯器最佳化的bug