From 4f86eae6f880dbdc8fe0377cf44ec8543571bccc Mon Sep 17 00:00:00 2001 From: Todd tiantuo Li Date: Mon, 6 Jun 2022 05:42:19 -0700 Subject: [PATCH] SWDEV-339551 - Prevent ICD from loading outdated amdocl binary 1.Keep track of library name in KHRicdVendorRec 2.Check if library with the same name has already been loaded before adding it in ICD list CL#1760530 Change-Id: I1e0356422c71963788b76019b0d8db5b8542f9ee [ROCm/clr commit: ed40c24fa39f1a7d5ceee8c0057f228cb76a9515] --- projects/clr/opencl/khronos/icd/loader/icd.c | 18 ++++++++++++++++++ projects/clr/opencl/khronos/icd/loader/icd.h | 3 +++ .../khronos/icd/loader/linux/icd_linux.c | 10 ++++++++++ .../khronos/icd/loader/windows/icd_windows.c | 11 +++++++++++ 4 files changed, 42 insertions(+) diff --git a/projects/clr/opencl/khronos/icd/loader/icd.c b/projects/clr/opencl/khronos/icd/loader/icd.c index 385e61c6c1..7e0a6118de 100644 --- a/projects/clr/opencl/khronos/icd/loader/icd.c +++ b/projects/clr/opencl/khronos/icd/loader/icd.c @@ -57,6 +57,17 @@ void khrIcdVendorAdd(const char *libraryName) goto Done; } + // get the library's file name + const char *libName = libraryName; + const char *c; + for (c = libraryName; *c; ++c) + { + if ((*c == '\\') || (*c == '/')) + { + libName = c + 1; + } + } + // ensure that we haven't already loaded this vendor for (vendorIterator = khrIcdVendors; vendorIterator; vendorIterator = vendorIterator->next) { @@ -65,6 +76,11 @@ void khrIcdVendorAdd(const char *libraryName) KHR_ICD_TRACE("already loaded vendor %s, nothing to do here\n", libraryName); goto Done; } + if (!strcmp(vendorIterator->libName, libName)) + { + KHR_ICD_TRACE("already loaded library %s, nothing to do here\n", libName); + goto Done; + } } // get the library's clGetExtensionFunctionAddress pointer @@ -162,6 +178,8 @@ void khrIcdVendorAdd(const char *libraryName) KHR_ICD_TRACE("failed get platform handle to library\n"); continue; } + vendor->libName = (char *)malloc(strlen(libName) + 1); + strcpy(vendor->libName, libName); vendor->clGetExtensionFunctionAddress = p_clGetExtensionFunctionAddress; vendor->platform = platforms[i]; vendor->suffix = suffix; diff --git a/projects/clr/opencl/khronos/icd/loader/icd.h b/projects/clr/opencl/khronos/icd/loader/icd.h index 34751e958c..439340fca6 100644 --- a/projects/clr/opencl/khronos/icd/loader/icd.h +++ b/projects/clr/opencl/khronos/icd/loader/icd.h @@ -67,6 +67,9 @@ struct KHRicdVendorRec // the loaded library object (true type varies on Linux versus Windows) void *library; + // the file name of the library + char *libName; + // the extension suffix for this platform char *suffix; diff --git a/projects/clr/opencl/khronos/icd/loader/linux/icd_linux.c b/projects/clr/opencl/khronos/icd/loader/linux/icd_linux.c index e4798063f7..14b471329e 100644 --- a/projects/clr/opencl/khronos/icd/loader/linux/icd_linux.c +++ b/projects/clr/opencl/khronos/icd/loader/linux/icd_linux.c @@ -135,6 +135,16 @@ void khrIcdOsVendorsEnumerate(void) } closedir(dir); + + KHRicdVendor *vendorIterator; + for (vendorIterator = khrIcdVendors; vendorIterator; vendorIterator = vendorIterator->next) + { + if (vendorIterator->libName != NULL) + { + free(vendorIterator->libName); + vendorIterator->libName = NULL; + } + } } if (NULL != envPath) diff --git a/projects/clr/opencl/khronos/icd/loader/windows/icd_windows.c b/projects/clr/opencl/khronos/icd/loader/windows/icd_windows.c index 9b2ce0296b..817b50ca2e 100644 --- a/projects/clr/opencl/khronos/icd/loader/windows/icd_windows.c +++ b/projects/clr/opencl/khronos/icd/loader/windows/icd_windows.c @@ -235,6 +235,17 @@ BOOL CALLBACK khrIcdOsVendorsEnumerate(PINIT_ONCE InitOnce, PVOID Parameter, PVO { KHR_ICD_TRACE("Failed to close platforms key %s, ignoring\n", platformsName); } + + KHRicdVendor *vendorIterator; + for (vendorIterator = khrIcdVendors; vendorIterator; vendorIterator = vendorIterator->next) + { + if (vendorIterator->libName != NULL) + { + free(vendorIterator->libName); + vendorIterator->libName = NULL; + } + } + return status; }