Blame SOURCES/papi-mx.patch

1f35f8
commit 3a6c9a855195e6f6f44ad6dffe2cd4046426ab53
1f35f8
Author: Masahiko, Yamada <yamada.masahiko@fujitsu.com>
1f35f8
Date:   Wed Nov 25 21:46:26 2020 +0900
1f35f8
1f35f8
    fix for performance improvement of _mx_init_component() function
1f35f8
1f35f8
diff --git a/src/components/mx/linux-mx.c b/src/components/mx/linux-mx.c
1f35f8
index 2da406d14..34e6f02c2 100644
1f35f8
--- a/src/components/mx/linux-mx.c
1f35f8
+++ b/src/components/mx/linux-mx.c
1f35f8
@@ -224,26 +224,35 @@ _mx_init_component( int cidx )
1f35f8
 {
1f35f8
 
1f35f8
 	FILE *fff;
1f35f8
-	char test_string[BUFSIZ];
1f35f8
+	char *path;
1f35f8
+	int len, pathlen;
1f35f8
 
1f35f8
 	/* detect if MX available */
1f35f8
 
1f35f8
-	strncpy(mx_counters_exe,"mx_counters 2> /dev/null",BUFSIZ);
1f35f8
-	fff=popen(mx_counters_exe,"r");
1f35f8
-	/* popen only returns NULL if "sh" fails, not the actual command */
1f35f8
-	if (fgets(test_string,BUFSIZ,fff)==NULL) {
1f35f8
-	   pclose(fff);
1f35f8
-	   strncpy(mx_counters_exe,"./components/mx/utils/fake_mx_counters 2> /dev/null",BUFSIZ);
1f35f8
-	   fff=popen(mx_counters_exe,"r");
1f35f8
-	   if (fgets(test_string,BUFSIZ,fff)==NULL) {
1f35f8
-	      pclose(fff);
1f35f8
-	      /* neither real nor fake found */
1f35f8
-	      strncpy(_mx_vector.cmp_info.disabled_reason,
1f35f8
-		      "No MX utilities found",PAPI_MAX_STR_LEN);
1f35f8
-	      return PAPI_ECMP;
1f35f8
+	path = getenv("PATH");
1f35f8
+	pathlen = strlen(path);
1f35f8
+	while(pathlen > 0) {
1f35f8
+	   len = strcspn(path, ":");
1f35f8
+	   strncpy(mx_counters_exe, path, len);
1f35f8
+	   mx_counters_exe[len] = '\0';
1f35f8
+	   strcat(mx_counters_exe, "/mx_counters");
1f35f8
+	   fff = fopen(mx_counters_exe, "r");
1f35f8
+	   if (fff != NULL) {
1f35f8
+	      strcat(mx_counters_exe, " 2> /dev/null");
1f35f8
+	      break;
1f35f8
 	   }
1f35f8
+	   pathlen = pathlen - len - 1;
1f35f8
+	   if (pathlen > 0) {
1f35f8
+	      path = path + len + 1;
1f35f8
+	   }
1f35f8
+	}
1f35f8
+	if (fff == NULL) {
1f35f8
+	   /* neither real nor fake found */
1f35f8
+	   strncpy(_mx_vector.cmp_info.disabled_reason,
1f35f8
+		   "No MX utilities found",PAPI_MAX_STR_LEN);
1f35f8
+	   return PAPI_ECMP;
1f35f8
 	}
1f35f8
-	pclose(fff);
1f35f8
+	fclose(fff);
1f35f8
 
1f35f8
 	num_events=MX_MAX_COUNTERS;
1f35f8
 	_mx_vector.cmp_info.num_native_events=num_events;
1f35f8
commit 3a2560a86be44f4b15d96a45eda8e7f387b9166c
1f35f8
Author: Masahiko, Yamada <yamada.masahiko@fujitsu.com>
1f35f8
Date:   Tue Jan 26 16:30:40 2021 +0900
1f35f8
1f35f8
    Add string length check before strncpy() and strcat() calls in _mx_init_component()
1f35f8
    
1f35f8
    Myrinet Express-related component MX modules are initialized with the _mx_init_component() function,
1f35f8
    which is called from the PAPI_library_init() function.
1f35f8
    The popen(3) call runs a loadable module called "mx_counters",
1f35f8
    and if the loadable module does not exist,
1f35f8
    it attempts to run a loadable module called "./components/mx/utils/fake_mx_counters".
1f35f8
    In an environment where there are no "mx_counters" and "./components/mx/utils/fake_mx_counters" loadable modules,
1f35f8
    popen(3) will be called twice uselessly.
1f35f8
    popen(3) internally calls pipe(2) once, fork(2) twice and exec(2) once.
1f35f8
    
1f35f8
    The size of the user space of the application calling the PAPI_library_init() function affects the performance of fork(2),
1f35f8
    which is called as an extension of popen(3).
1f35f8
    As a result, the performance of the PAPI_library_init() function is affected by the amount of user space in the application
1f35f8
    that called the PAPI_library_init() function.
1f35f8
    
1f35f8
    In the _mx_init_component() function,
1f35f8
    the MX module only needs to be able to verify that a load module named "mx_counters" exists.
1f35f8
    We improved the _mx_init_component() function to call fopen(3) instead of popen(3).
1f35f8
    We add string length check before strncpy() and strcat() calls in _mx_init_component() function.
1f35f8
1f35f8
diff --git a/src/components/mx/linux-mx.c b/src/components/mx/linux-mx.c
1f35f8
index 34e6f02c2..c2920d65b 100644
1f35f8
--- a/src/components/mx/linux-mx.c
1f35f8
+++ b/src/components/mx/linux-mx.c
1f35f8
@@ -225,7 +225,7 @@ _mx_init_component( int cidx )
1f35f8
 
1f35f8
 	FILE *fff;
1f35f8
 	char *path;
1f35f8
-	int len, pathlen;
1f35f8
+	int checklen, len, pathlen;
1f35f8
 
1f35f8
 	/* detect if MX available */
1f35f8
 
1f35f8
@@ -233,13 +233,31 @@ _mx_init_component( int cidx )
1f35f8
 	pathlen = strlen(path);
1f35f8
 	while(pathlen > 0) {
1f35f8
 	   len = strcspn(path, ":");
1f35f8
-	   strncpy(mx_counters_exe, path, len);
1f35f8
+	   if (len < BUFSIZ) {
1f35f8
+	      strncpy(mx_counters_exe, path, len);
1f35f8
+	   } else {
1f35f8
+	      fff = NULL;
1f35f8
+	      break;
1f35f8
+	   }
1f35f8
 	   mx_counters_exe[len] = '\0';
1f35f8
-	   strcat(mx_counters_exe, "/mx_counters");
1f35f8
+	   checklen = len + strlen("/mx_counters");
1f35f8
+	   if (checklen < BUFSIZ) {
1f35f8
+	      strcat(mx_counters_exe, "/mx_counters");
1f35f8
+	   } else {
1f35f8
+	      fff = NULL;
1f35f8
+	      break;
1f35f8
+	   }
1f35f8
 	   fff = fopen(mx_counters_exe, "r");
1f35f8
 	   if (fff != NULL) {
1f35f8
-	      strcat(mx_counters_exe, " 2> /dev/null");
1f35f8
-	      break;
1f35f8
+	      checklen = checklen + strlen(" 2> /dev/null");
1f35f8
+	      if (checklen < BUFSIZ) {
1f35f8
+	         strcat(mx_counters_exe, " 2> /dev/null");
1f35f8
+	         break;
1f35f8
+	      } else {
1f35f8
+	         fclose(fff);
1f35f8
+	         fff = NULL;
1f35f8
+	         break;
1f35f8
+	      }
1f35f8
 	   }
1f35f8
 	   pathlen = pathlen - len - 1;
1f35f8
 	   if (pathlen > 0) {
1f35f8
@@ -247,7 +265,7 @@ _mx_init_component( int cidx )
1f35f8
 	   }
1f35f8
 	}
1f35f8
 	if (fff == NULL) {
1f35f8
-	   /* neither real nor fake found */
1f35f8
+	   /* mx_counters not found */
1f35f8
 	   strncpy(_mx_vector.cmp_info.disabled_reason,
1f35f8
 		   "No MX utilities found",PAPI_MAX_STR_LEN);
1f35f8
 	   return PAPI_ECMP;