frameworks/base
修訂 | f4dacb9e395dc87bf68dc8e5f092af618075876f (tree) |
---|---|
時間 | 2020-04-14 23:46:52 |
作者 | jgu21 <jinghui.gu@inte...> |
Commiter | Chih-Wei Huang |
Fix the memory leak bug introduced by PRC compatibility feature
Fix the memory leak bug introduced by PRC compatibility feature,
introduced by https://android.intel.com/#/c/471490/
Change-Id: Iaf9bd21afa17f3a81ab700c63ae7c0bb0851a594
Tracked-On: https://jira01.devtools.intel.com/browse/OAM-27449
Signed-off-by: jgu21 <jinghui.gu@intel.com>
Reviewed-on: https://android.intel.com:443/489353
@@ -10,14 +10,15 @@ namespace android { | ||
10 | 10 | #define ARR_SIZE(x) (sizeof(x)/sizeof(x[0])) |
11 | 11 | |
12 | 12 | #define SO_NAME_MAX (4096) |
13 | + | |
13 | 14 | #define IMPOSSIBLE_LIB_NAME "/mixed/" |
14 | 15 | #define IMPOSSIBLE_LIB_LEN (sizeof(IMPOSSIBLE_LIB_NAME)-1) |
16 | + | |
15 | 17 | #define ARMABI "armeabi" |
16 | 18 | #define ARMV7ABI "armeabi-v7a" |
17 | 19 | #define ARM64ABI "arm64-v8a" |
18 | 20 | #define X86ABI "x86" |
19 | 21 | #define X8664ABI "x86_64" |
20 | -#define ARMABI_NAME_PREFIX "arm" | |
21 | 22 | |
22 | 23 | #define APK_LIB "lib/" |
23 | 24 | #define APK_LIB_LEN (sizeof(APK_LIB) - 1) |
@@ -28,9 +29,10 @@ namespace android { | ||
28 | 29 | #define P_LOG(...) |
29 | 30 | #endif |
30 | 31 | |
31 | -#define OEMWHITE "/vendor/etc/misc/.OEMWhiteList" | |
32 | -#define OEMBLACK "/vendor/etc/misc/.OEMBlackList" | |
33 | -#define THIRDPARTY "/vendor/etc/misc/.ThirdPartySO" | |
32 | +#define LISTPATH "/vendor/etc/misc/" | |
33 | +#define OEMWHITE LISTPATH ".OEMWhiteList" | |
34 | +#define OEMBLACK LISTPATH ".OEMBlackList" | |
35 | +#define THIRDPARTY LISTPATH ".ThirdPartySO" | |
34 | 36 | |
35 | 37 | // load once, hold until poweroff |
36 | 38 | static Vector <char*> thirdPartySO; |
@@ -42,7 +44,18 @@ static bool blackload = false; | ||
42 | 44 | |
43 | 45 | static const char* iaRelated[] = {"intel", "intl", "atom", "x86", "x64"}; |
44 | 46 | |
45 | -////////////////////////////////////////////////////////////////////// | |
47 | +void freeAllString(Vector<char*>& list) { | |
48 | + Vector<char*>::iterator it = list.begin(); | |
49 | + while (it != list.end()) { | |
50 | + if (*it != NULL) { | |
51 | + P_LOG("freeAllSring %p , %s", it, *it); | |
52 | + free(*it); | |
53 | + *it = NULL; | |
54 | + } | |
55 | + it++; | |
56 | + } | |
57 | +} | |
58 | + | |
46 | 59 | void getConfig(const char* cfgFile , Vector<char*>& cfgVec) { |
47 | 60 | int read = -1; |
48 | 61 | char *line = NULL; |
@@ -53,10 +66,13 @@ void getConfig(const char* cfgFile , Vector<char*>& cfgVec) { | ||
53 | 66 | return; |
54 | 67 | } |
55 | 68 | |
69 | + freeAllString(cfgVec); | |
70 | + cfgVec.clear(); | |
71 | + | |
56 | 72 | while ((read = getline(&line, &len, fp)) != -1) { |
57 | 73 | int i = 0 , j = 0; |
58 | 74 | char *cfgline = (char*)malloc(len); |
59 | - if (!cfgline) { | |
75 | + if (cfgline == NULL) { | |
60 | 76 | P_LOG("malloc error"); |
61 | 77 | break; |
62 | 78 | } |
@@ -73,23 +89,11 @@ void getConfig(const char* cfgFile , Vector<char*>& cfgVec) { | ||
73 | 89 | fclose(fp); |
74 | 90 | } |
75 | 91 | |
76 | -////////////////////////////////////////////////////////////////////// | |
77 | -void freeAllString(Vector<char*>& list) { | |
78 | - Vector<char*>::iterator it = list.begin(); | |
79 | - while (it != list.end()) { | |
80 | - if (*it != NULL) { | |
81 | - P_LOG("freeAllSring %p , %s", it, *it); | |
82 | - free(*it); | |
83 | - *it = NULL; | |
84 | - } | |
85 | - it++; | |
86 | - } | |
87 | -} | |
88 | - | |
89 | -////////////////////////////////////////////////////////////////////// | |
90 | 92 | bool isInOEMWhiteList(const char* pkgName) { |
91 | 93 | bool result = false; |
92 | - if (!pkgName) return result; | |
94 | + if (pkgName == NULL) { | |
95 | + return result; | |
96 | + } | |
93 | 97 | |
94 | 98 | if (!whiteload) { |
95 | 99 | getConfig(OEMWHITE, cfgWhite); |
@@ -108,10 +112,11 @@ bool isInOEMWhiteList(const char* pkgName) { | ||
108 | 112 | return result; |
109 | 113 | } |
110 | 114 | |
111 | -////////////////////////////////////////////////////////////////////// | |
112 | 115 | bool isInOEMBlackList(const char* pkgName) { |
113 | 116 | bool result = false; |
114 | - if (!pkgName) return result; | |
117 | + if (pkgName == NULL) { | |
118 | + return result; | |
119 | + } | |
115 | 120 | |
116 | 121 | if (!blackload) { |
117 | 122 | getConfig(OEMBLACK, cfgBlack); |
@@ -129,8 +134,6 @@ bool isInOEMBlackList(const char* pkgName) { | ||
129 | 134 | return result; |
130 | 135 | } |
131 | 136 | |
132 | - | |
133 | -////////////////////////////////////////////////////////////////////// | |
134 | 137 | bool isReliableLib(Vector<char*>& libList) { |
135 | 138 | unsigned sz = libList.size(); |
136 | 139 | int len = ARR_SIZE(iaRelated); |
@@ -151,8 +154,6 @@ bool isReliableLib(Vector<char*>& libList) { | ||
151 | 154 | return false; |
152 | 155 | } |
153 | 156 | |
154 | - | |
155 | -////////////////////////////////////////////////////////////////////// | |
156 | 157 | static bool isValidELF(char* buffer) { |
157 | 158 | if (buffer[EI_MAG0] != ELFMAG0 && |
158 | 159 | buffer[EI_MAG1] != ELFMAG1 && |
@@ -169,11 +170,13 @@ static bool isMixedLib(char* libCur, char* buffer) { | ||
169 | 170 | uint16_t machine_code = *((uint16_t*)(&buffer[ELF_MACHINE_OFFSET])); |
170 | 171 | bool mixed = false; |
171 | 172 | if (isX86_64) { |
172 | - if (machine_code != EM_X86_64) | |
173 | + if (machine_code != EM_X86_64) { | |
173 | 174 | mixed = true; |
175 | + } | |
174 | 176 | } else { |
175 | - if (machine_code != EM_386) | |
177 | + if (machine_code != EM_386) { | |
176 | 178 | mixed = true; |
179 | + } | |
177 | 180 | } |
178 | 181 | return mixed; |
179 | 182 | } |
@@ -201,6 +204,7 @@ static bool isInThirdPartySOList(char* libName) { | ||
201 | 204 | size_t sz = thirdPartySO.size(); |
202 | 205 | for (size_t i = 0; i < sz; i++) { |
203 | 206 | // thirdPartySO[i] won't be NULL |
207 | + assert(thirdPartySO[i] != NULL); | |
204 | 208 | size_t n = strlen(thirdPartySO[i]); |
205 | 209 | // three char for ".so" |
206 | 210 | int j = libLen - 4; |
@@ -225,9 +229,8 @@ static void insertionSort(Vector<char*>& list) { | ||
225 | 229 | P_LOG("in insertionSort, list size = %d\n", list.size()); |
226 | 230 | |
227 | 231 | for (size_t i = 1; i < list.size(); i++) { |
228 | - char* x = list[i]; | |
229 | - | |
230 | 232 | int j = i - 1; |
233 | + char* x = list[i]; | |
231 | 234 | P_LOG("sort 1. x=%s, i=%d, j=%d\n", x, i, j); |
232 | 235 | while (j >= 0 && (strcmp(list[j], x) > 0)) { |
233 | 236 | list.replaceAt(list[j], j + 1); |
@@ -237,9 +240,6 @@ static void insertionSort(Vector<char*>& list) { | ||
237 | 240 | } |
238 | 241 | } |
239 | 242 | |
240 | - | |
241 | - | |
242 | -////////////////////////////////////////////////////////////////////// | |
243 | 243 | // Use armRef as a reference, compare all libraries of iaRef with all |
244 | 244 | // libraries of armRef.If the two are match or iaRef is more, iaRef |
245 | 245 | // will be returned with *result and true is return value. Or else, |
@@ -392,7 +392,9 @@ bool ABIPicker::compare3rdPartyLibList( | ||
392 | 392 | } |
393 | 393 | result = compareLibList(*iaRef3rdPartyLibList, *armRef3rdPartyLibList); |
394 | 394 | |
395 | + armRef3rdPartyLibList->clear(); | |
395 | 396 | delete armRef3rdPartyLibList; |
397 | + iaRef3rdPartyLibList->clear(); | |
396 | 398 | delete iaRef3rdPartyLibList; |
397 | 399 | return result; |
398 | 400 | } |
@@ -451,9 +453,9 @@ Vector<char*>* ABIPicker::getLibList(const char* abiName) { | ||
451 | 453 | } |
452 | 454 | |
453 | 455 | |
454 | -size_t ABIPicker::getSpecficABILibCount(const char* abiName) { | |
456 | +bool ABIPicker::isABILibValid(const char* abiName) { | |
455 | 457 | Vector<char*>* specificAbiLibList = getLibList(abiName); |
456 | - return specificAbiLibList && specificAbiLibList->size(); | |
458 | + return ((specificAbiLibList && specificAbiLibList->size()) > 0); | |
457 | 459 | } |
458 | 460 | |
459 | 461 | bool ABIPicker::foundMixedELF(const char* abiName) { |
@@ -472,12 +474,10 @@ bool ABIPicker::foundMixedELF(const char* abiName) { | ||
472 | 474 | return true; |
473 | 475 | } |
474 | 476 | |
475 | - | |
476 | -////////////////////////////////////////////////////////////////////// | |
477 | 477 | ABIPicker::ABIPicker(const char* pkgName, Vector<ScopedUtfChars*> abiList) { |
478 | 478 | mLibList = new Vector<struct libInfo*>(); |
479 | 479 | mpkgName = strdup(pkgName); |
480 | - if (!mpkgName) { | |
480 | + if (mpkgName == NULL) { | |
481 | 481 | P_LOG("ABIPicker Construct Allocated space fails"); |
482 | 482 | } |
483 | 483 | Vector<ScopedUtfChars*>::iterator it = abiList.begin(); |
@@ -488,7 +488,7 @@ ABIPicker::ABIPicker(const char* pkgName, Vector<ScopedUtfChars*> abiList) { | ||
488 | 488 | |
489 | 489 | struct libInfo* tmp = (struct libInfo*)calloc(1, |
490 | 490 | sizeof(struct libInfo)); |
491 | - if (!tmp) { | |
491 | + if (tmp == NULL) { | |
492 | 492 | P_LOG("ABIPicker Construct Allocated space fail %s", (*it)->c_str()); |
493 | 493 | break; |
494 | 494 | } |
@@ -517,13 +517,13 @@ ABIPicker::~ABIPicker(void) { | ||
517 | 517 | it++; |
518 | 518 | } |
519 | 519 | mLibList->clear(); |
520 | - delete(mLibList); | |
520 | + delete mLibList; | |
521 | 521 | } |
522 | 522 | |
523 | 523 | bool ABIPicker::buildNativeLibList(void* apkHandle) { |
524 | 524 | bool ret = false; |
525 | 525 | |
526 | - if (!apkHandle) { | |
526 | + if (apkHandle == NULL) { | |
527 | 527 | ALOGE("apkHandle is NULL\n"); |
528 | 528 | return ret; |
529 | 529 | } |
@@ -570,7 +570,7 @@ bool ABIPicker::buildNativeLibList(void* apkHandle) { | ||
570 | 570 | unCompBuff = NULL; |
571 | 571 | |
572 | 572 | unCompBuff = (char*)malloc(unCompLen); |
573 | - if (!unCompBuff) { | |
573 | + if (unCompBuff == NULL) { | |
574 | 574 | ALOGE("malloc failed size %d\n", unCompLen); |
575 | 575 | ret = false; |
576 | 576 | break; |
@@ -683,50 +683,49 @@ int ABIPicker::pickupRightABI(int sysPrefer) { | ||
683 | 683 | bool x8664HasMixedELF = foundMixedELF(X8664ABI); |
684 | 684 | bool x86HasMixedELF = foundMixedELF(X86ABI); |
685 | 685 | |
686 | - size_t armv7LibCount = getSpecficABILibCount(ARMV7ABI); | |
687 | - size_t armv5LibCount = getSpecficABILibCount(ARMABI); | |
688 | - size_t armv8LibCount = getSpecficABILibCount(ARM64ABI); | |
689 | - size_t x86LibCount = x86HasMixedELF ? 0 : getSpecficABILibCount(X86ABI); | |
690 | - size_t x8664LibCount = x8664HasMixedELF ? 0 : getSpecficABILibCount(X8664ABI); | |
691 | - P_LOG("armv7LibCount:%d armv5LibCount:%d armv8LibCount:%d x86LibCount:%d x8664LibCount:%d", armv7LibCount, armv5LibCount, armv8LibCount, x86LibCount, x8664LibCount); | |
686 | + bool armv7LibValid = isABILibValid(ARMV7ABI); | |
687 | + bool armv5LibValid = isABILibValid(ARMABI); | |
688 | + bool armv8LibValid = isABILibValid(ARM64ABI); | |
689 | + bool x86LibValid = x86HasMixedELF ? 0 : isABILibValid(X86ABI); | |
690 | + bool x8664LibValid = x8664HasMixedELF ? 0 : isABILibValid(X8664ABI); | |
692 | 691 | |
693 | 692 | // in OEMBlackList, need to be supported by bt |
694 | 693 | // but in case of armlib doesn't exist, we choose x86 or x86_64 |
695 | 694 | if (isInOEMBlackList(mpkgName)) { |
696 | - if (armv7LibCount > 0) { | |
695 | + if (armv7LibValid) { | |
697 | 696 | return getAbiIndex(ARMV7ABI); |
698 | - } else if (armv5LibCount > 0) { | |
697 | + } else if (armv5LibValid) { | |
699 | 698 | return getAbiIndex(ARMABI); |
700 | - } else if (armv8LibCount > 0) { | |
699 | + } else if (armv8LibValid) { | |
701 | 700 | return getAbiIndex(ARM64ABI); |
702 | 701 | } |
703 | 702 | } |
704 | 703 | |
705 | 704 | char arm64Ref[ABI_NAME_MAX_LENGTH]; |
706 | - if (armv8LibCount > 0) { | |
705 | + if (armv8LibValid) { | |
707 | 706 | snprintf(arm64Ref, sizeof(ARM64ABI), "%s", ARM64ABI); |
708 | 707 | } else { |
709 | 708 | arm64Ref[0] = '\0'; |
710 | 709 | } |
711 | 710 | |
712 | 711 | char arm32Ref[ABI_NAME_MAX_LENGTH]; |
713 | - if (armv7LibCount > 0) { | |
712 | + if (armv7LibValid) { | |
714 | 713 | snprintf(arm32Ref, sizeof(ARMV7ABI), "%s", ARMV7ABI); |
715 | - } else if (armv5LibCount > 0) { | |
714 | + } else if (armv5LibValid) { | |
716 | 715 | snprintf(arm32Ref, sizeof(ARMABI), "%s", ARMABI); |
717 | 716 | } else { |
718 | 717 | arm32Ref[0] = '\0'; |
719 | 718 | } |
720 | 719 | |
721 | 720 | char ia32Ref[ABI_NAME_MAX_LENGTH]; |
722 | - if (x86LibCount > 0) { | |
721 | + if (x86LibValid) { | |
723 | 722 | snprintf(ia32Ref, sizeof(X86ABI), "%s", X86ABI); |
724 | 723 | } else { |
725 | 724 | ia32Ref[0] = '\0'; |
726 | 725 | } |
727 | 726 | |
728 | 727 | char ia64Ref[ABI_NAME_MAX_LENGTH]; |
729 | - if (x8664LibCount > 0) { | |
728 | + if (x8664LibValid) { | |
730 | 729 | snprintf(ia64Ref, ABI_NAME_MAX_LENGTH, "%s", X8664ABI); |
731 | 730 | } else { |
732 | 731 | ia64Ref[0] = '\0'; |
@@ -12,17 +12,6 @@ namespace android { | ||
12 | 12 | // assumption: the length of name of any abi type in abi list, |
13 | 13 | // like armeabi-v7a, armeabi, x86, is not longer than 64 |
14 | 14 | #define ABI_NAME_MAX_LENGTH (64) |
15 | -/* | |
16 | -class LibInfo { | |
17 | - public: | |
18 | - LibInfo(char* s, List<char*> list); | |
19 | - ~LibInfo(void); | |
20 | - | |
21 | - private: | |
22 | - char abiName[ABI_NAME_MAX_LENGTH]; | |
23 | - List<char*> libNameList; | |
24 | -}; | |
25 | -*/ | |
26 | 15 | |
27 | 16 | class ABIPicker { |
28 | 17 | public: |
@@ -46,7 +35,7 @@ class ABIPicker { | ||
46 | 35 | size_t* iaIsvLibCount, size_t* armIsvLibCount); |
47 | 36 | char* getAbiName(int abi); |
48 | 37 | int getAbiIndex(const char* abiName); |
49 | - size_t getSpecficABILibCount(const char* abiName); | |
38 | + bool isABILibValid(const char* abiName); | |
50 | 39 | Vector<char*>* getLibList(const char* abiName); |
51 | 40 | }; |
52 | 41 |
@@ -541,30 +541,34 @@ com_android_internal_content_NativeLibraryHelper_findSupportedAbi_replace( | ||
541 | 541 | return (jint)abiType; |
542 | 542 | } |
543 | 543 | |
544 | + int abiIndex = 0; | |
544 | 545 | abiFlag[apkdir_size] = '/'; |
545 | 546 | abiFlag[apkdir_size + 1] = '.'; |
546 | - for (int i = 0; i < numAbis; i++) { | |
547 | + for (abiIndex = 0; abiIndex < numAbis; abiIndex++) { | |
547 | 548 | ScopedUtfChars* abiName = new ScopedUtfChars(env, |
548 | - (jstring)env->GetObjectArrayElement(javaCpuAbisToSearch, i)); | |
549 | - assert(abiName != NULL); | |
550 | - assert(abiName->c_str() != NULL); | |
551 | - if (strlcpy(abiFlag + apkdir_size + 2, abiName->c_str(), 256 - apkdir_size - 2) | |
552 | - == abiName->size()) { | |
553 | - if (access(abiFlag, F_OK) == 0) { | |
554 | - abiType = i; | |
555 | - for (int j = 0; j < i; ++j) { | |
556 | - delete supportedAbis[j]; | |
557 | - } | |
558 | - delete abiName; | |
559 | - return (jint)abiType; | |
560 | - } | |
549 | + (jstring)env->GetObjectArrayElement(javaCpuAbisToSearch, abiIndex)); | |
550 | + supportedAbis.push_back(abiName); | |
551 | + if (abiName == NULL || abiName->c_str() == NULL || abiName->size() <= 0) { | |
552 | + break; | |
553 | + } | |
554 | + if ((strlcpy(abiFlag + apkdir_size + 2, abiName->c_str(), 256 - apkdir_size - 2) | |
555 | + == abiName->size()) && (access(abiFlag, F_OK) == 0)) { | |
556 | + abiType = abiIndex; | |
557 | + break; | |
561 | 558 | } |
559 | + } | |
562 | 560 | |
563 | - supportedAbis.push_back(abiName); | |
561 | + if (abiIndex < numAbis) { | |
562 | + for (int j = 0; j < abiIndex; ++j) { | |
563 | + if (supportedAbis[j] != NULL) { | |
564 | + delete supportedAbis[j]; | |
565 | + } | |
566 | + } | |
567 | + return (jint)abiType; | |
564 | 568 | } |
565 | 569 | |
566 | 570 | do { |
567 | - if (abiType < 0 || abiType >= numAbis ) { | |
571 | + if (abiType < 0 || abiType >= numAbis) { | |
568 | 572 | break; |
569 | 573 | } |
570 | 574 |
@@ -591,7 +595,10 @@ com_android_internal_content_NativeLibraryHelper_findSupportedAbi_replace( | ||
591 | 595 | if (abiType >= 0 && abiType < numAbis && |
592 | 596 | (strlcpy(abiFlag + apkdir_size + 2, supportedAbis[abiType]->c_str(), |
593 | 597 | 256 - apkdir_size - 2) == supportedAbis[abiType]->size())) { |
594 | - creat(abiFlag, 0644); | |
598 | + int flagFp = creat(abiFlag, 0644); | |
599 | + if (flagFp != -1) { | |
600 | + close(flagFp); | |
601 | + } | |
595 | 602 | } |
596 | 603 | |
597 | 604 | } while(0); |