Skip to content
54 changes: 32 additions & 22 deletions src/components/coretemp/linux-coretemp.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ generateEventList(char *base_dir)
struct dirent *hwmonx;
int i,pathnum;
int retlen;
int err = PAPI_OK;

#define NUM_PATHS 2
char paths[NUM_PATHS][PATH_MAX]={
Expand Down Expand Up @@ -133,6 +134,7 @@ generateEventList(char *base_dir)
base_dir, hwmonx->d_name,paths[pathnum]);
if (retlen <= 0 || PATH_MAX <= retlen) {
SUBDBG("Path length is too long.\n");
closedir(dir);
return PAPI_EINVAL;
}
SUBDBG("Trying to open %s\n",path);
Expand All @@ -146,7 +148,8 @@ generateEventList(char *base_dir)
retlen = snprintf(filename, PAPI_MAX_STR_LEN, "%s/name",path);
if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) {
SUBDBG("Module name too long.\n");
return PAPI_EINVAL;
err = PAPI_EINVAL;
goto done_error;
}
fff=fopen(filename,"r");
if (fff==NULL) {
Expand Down Expand Up @@ -176,7 +179,8 @@ generateEventList(char *base_dir)
path,i);
if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) {
SUBDBG("Failed to construct location label.\n");
return PAPI_EINVAL;
err = PAPI_EINVAL;
goto done_error;
}
fff=fopen(filename,"r");
if (fff==NULL) {
Expand All @@ -194,7 +198,8 @@ generateEventList(char *base_dir)
path,i);
if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) {
SUBDBG("Failed input temperature string.\n");
return PAPI_EINVAL;
err = PAPI_EINVAL;
goto done_error;
}
fff=fopen(filename,"r");
if (fff==NULL) continue;
Expand All @@ -203,9 +208,8 @@ generateEventList(char *base_dir)
retlen = snprintf(name, PAPI_MAX_STR_LEN, "%s:in%i_input", hwmonx->d_name, i);
if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) {
SUBDBG("Unable to generate name %s:in%i_input\n", hwmonx->d_name, i);
closedir(dir);
closedir(d);
return ( PAPI_EINVAL );
err = PAPI_EINVAL;
goto done_error;
}

snprintf(units, PAPI_MIN_STR_LEN, "V");
Expand All @@ -214,10 +218,12 @@ generateEventList(char *base_dir)
location);
if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) {
SUBDBG("snprintf failed.\n");
return PAPI_EINVAL;
err = PAPI_EINVAL;
goto done_error;
}

if (insert_in_list(name,units,description,filename)!=PAPI_OK) {
err = PAPI_ECMP;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

insert_in_list can also return PAPI_ENOMEM. Can you change this check such that you set err equal to insert_in_list and then if a failure does occur you can jump to done_error?

goto done_error;
}

Expand All @@ -236,7 +242,8 @@ generateEventList(char *base_dir)
path,i);
if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) {
SUBDBG("Location label string failed.\n");
return PAPI_EINVAL;
err = PAPI_EINVAL;
goto done_error;
}
fff=fopen(filename,"r");
if (fff==NULL) {
Expand All @@ -254,7 +261,8 @@ generateEventList(char *base_dir)
path,i);
if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) {
SUBDBG("Input temperature string failed.\n");
return PAPI_EINVAL;
err = PAPI_EINVAL;
goto done_error;
}
fff=fopen(filename,"r");
if (fff==NULL) continue;
Expand All @@ -263,9 +271,8 @@ generateEventList(char *base_dir)
retlen = snprintf(name, PAPI_MAX_STR_LEN, "%s:temp%i_input", hwmonx->d_name, i);
if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) {
SUBDBG("Unable to generate name %s:temp%i_input\n", hwmonx->d_name, i);
closedir(d);
closedir(dir);
return ( PAPI_EINVAL );
err = PAPI_EINVAL;
goto done_error;
}

snprintf(units, PAPI_MIN_STR_LEN, "degrees C");
Expand All @@ -274,10 +281,12 @@ generateEventList(char *base_dir)
location);
if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) {
SUBDBG("snprintf failed.\n");
return PAPI_EINVAL;
err = PAPI_EINVAL;
goto done_error;
}

if (insert_in_list(name,units,description,filename)!=PAPI_OK) {
err = PAPI_ECMP;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

goto done_error;
}

Expand All @@ -295,7 +304,8 @@ generateEventList(char *base_dir)
path,i);
if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) {
SUBDBG("Failed to write fan label string.\n");
return PAPI_EINVAL;
err = PAPI_EINVAL;
goto done_error;
}
fff=fopen(filename,"r");
if (fff==NULL) {
Expand All @@ -312,9 +322,8 @@ generateEventList(char *base_dir)
retlen = snprintf(filename, PAPI_MAX_STR_LEN, "%s/fan%d_input", path,i);
if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) {
SUBDBG("Unable to generate filename %s/fan%d_input\n", path,i);
closedir(d);
closedir(dir);
return ( PAPI_EINVAL );
err = PAPI_EINVAL;
goto done_error;
}

fff=fopen(filename,"r");
Expand All @@ -324,9 +333,8 @@ generateEventList(char *base_dir)
retlen = snprintf(name, PAPI_MAX_STR_LEN, "%s:fan%i_input", hwmonx->d_name, i);
if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) {
SUBDBG("Unable to generate name %s:fan%i_input\n", hwmonx->d_name, i);
closedir(d);
closedir(dir);
return ( PAPI_EINVAL );
err = PAPI_EINVAL;
goto done_error;
}

snprintf(units, PAPI_MIN_STR_LEN, "RPM");
Expand All @@ -335,10 +343,12 @@ generateEventList(char *base_dir)
location);
if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) {
SUBDBG("snprintf failed.\n");
return PAPI_EINVAL;
err = PAPI_EINVAL;
goto done_error;
}

if (insert_in_list(name,units,description,filename)!=PAPI_OK) {
err = PAPI_ECMP;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

goto done_error;
}

Expand All @@ -356,7 +366,7 @@ generateEventList(char *base_dir)
done_error:
closedir(d);
closedir(dir);
return PAPI_ECMP;
return err;
}

static long long
Expand Down
24 changes: 16 additions & 8 deletions src/components/pcp/linux-pcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,9 @@ static int _pcp_init_component(int cidx)
{
int retval = PAPI_OK;
char *reason = _papi_hwd[cidx]->cmp_info.disabled_reason; // For error messages.
char **allNames = NULL;
pmID *allPMID = NULL;
pmResult *allFetch = NULL; // result of pmFetch.
int rLen = PAPI_HUGE_STR_LEN; // Most I will print.
reason[rLen-1]=0; // last resort terminator.

Expand Down Expand Up @@ -941,17 +944,24 @@ static int _pcp_init_component(int cidx)
}

int i,j,k;
char **allNames=calloc(sEventCount, sizeof(char*)); // Make an array for all names.
allNames=calloc(sEventCount, sizeof(char*)); // Make an array for all names.
if (allNames == NULL) {
snprintf(reason, rLen, "memory alloc denied for allNames; "
"size=%i.\n", sEventCount);
int strErr=snprintf(reason, rLen, "Could not allocate %lu bytes of memory for allNames.", sEventCount*sizeof(allNames));
if (strErr > rLen) HANDLE_STRING_ERROR;
retval = PAPI_ENOMEM;
goto fn_fail;
}
for (i=0; i<sEventCount; i++) { // ..
allNames[i] = pcp_event_info[i].name; // copy pointer into array.
} // end for each event.

pmID *allPMID=calloc(sEventCount, sizeof(pmID)); // Make an array for results.
allPMID=calloc(sEventCount, sizeof(pmID)); // Make an array for results.
if (allPMID == NULL) { // If we failed,
snprintf(reason, rLen, "memory alloc denied for allPMID; "
"size=%i.\n", sEventCount);
int strErr=snprintf(reason, rLen, "Could not allocate %lu bytes of memory for allPMID.", sEventCount*sizeof(pmID));
free(allNames);
if (strErr > rLen) HANDLE_STRING_ERROR;
retval = PAPI_ENOMEM;
goto fn_fail;
Expand Down Expand Up @@ -1003,7 +1013,6 @@ static int _pcp_init_component(int cidx)

for (i=0; i<sEventCount; i++) pcp_event_info[i].pmid = allPMID[i]; // copy all the pmid over to array.

pmResult *allFetch = NULL; // result of pmFetch.
_time_gettimeofday(&t1, NULL);
ret = pcp_pmFetch(sEventCount, allPMID, &allFetch); // Fetch (read) all the events.
_time_gettimeofday(&t2, NULL);
Expand Down Expand Up @@ -1217,10 +1226,6 @@ static int _pcp_init_component(int cidx)
// *************************************** END DEBUG REPORT INFORMATION DISCOVERED **************************************
//-----------------------------------------------------------------------------------------------------------------------

free(allNames); // Locals allocations not needed anymore.
free(allPMID); // .. the pmIDs we read.
pcp_pmFreeResult(allFetch); // .. release the results we fetched.

// For PCP, we can read any number of events at once
// in a single event set. Set vector elements for PAPI.

Expand All @@ -1230,6 +1235,9 @@ static int _pcp_init_component(int cidx)
_pcp_vector.cmp_info.CmpIdx = cidx; // export the component ID.

fn_exit:
free(allNames); // Locals allocations not needed anymore.
free(allPMID); // .. the pmIDs we read.
pcp_pmFreeResult(allFetch); // .. release the results we fetched.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From documentation:

As a general rule, if the called function returns an error status, then no allocation is done, the pointer to the variable sized result is undefined, and free, pmFreeLabelSets, or pmFreeResult should not be called.


Therefore, before calling pcp_pmFreeResult we need to check:

if (all_fetch != NULL) {
    pcp_pmFreeResult(allFetch); 
}

_papi_hwd[cidx]->cmp_info.disabled = retval;
return retval;
fn_fail:
Expand Down
9 changes: 9 additions & 0 deletions src/components/powercap/linux-powercap.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ static char * _local_strlcpy( char *dst, const char *src, size_t size )
static long long read_powercap_value( int index )
{
int sz = pread(event_fds[index], read_buff, PAPI_MAX_STR_LEN, 0);
if (sz == -1) {
perror("Error in pread(): ");
return (long long) 0;
}
read_buff[sz] = '\0';

return atoll(read_buff);
Expand Down Expand Up @@ -207,6 +211,7 @@ static int _powercap_init_component( int cidx )
if (event_fd == -1) { break; }

int sz = pread(event_fd, read_buff, PAPI_MAX_STR_LEN, 0);
if (sz == -1) { HANDLE_STRING_ERROR; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formatting here for HANDLE_STRING_ERROR is different from the rest below. I did not an counter any error if the formatting here mimics the formatting below. Can you change the formatting here to be like below or vice versa?

read_buff[sz] = '\0';
close(event_fd);

Expand Down Expand Up @@ -254,6 +259,7 @@ static int _powercap_init_component( int cidx )

if(powercap_ntv_events[num_events].type == PKG_NAME) {
int sz = pread(event_fds[num_events], read_buff, PAPI_MAX_STR_LEN, 0);
if (sz == -1) HANDLE_STRING_ERROR;
read_buff[sz] = '\0';
strErr=snprintf(powercap_ntv_events[num_events].description, sizeof(powercap_ntv_events[num_events].description), "%s", read_buff);
powercap_ntv_events[num_events].description[sizeof(powercap_ntv_events[num_events].description)-1]=0;
Expand All @@ -262,6 +268,7 @@ static int _powercap_init_component( int cidx )

if(powercap_ntv_events[num_events].type == PKG_MAX_ENERGY_RANGE) {
int sz = pread(event_fds[num_events], read_buff, PAPI_MAX_STR_LEN, 0);
if (sz == -1) HANDLE_STRING_ERROR;
read_buff[sz] = '\0';
max_pkg_energy_count = atoll(read_buff);
}
Expand Down Expand Up @@ -302,6 +309,7 @@ static int _powercap_init_component( int cidx )

if(powercap_ntv_events[num_events].type == COMPONENT_NAME) {
int sz = pread(event_fds[num_events], read_buff, PAPI_MAX_STR_LEN, 0);
if (sz == -1) HANDLE_STRING_ERROR;
read_buff[sz] = '\0';
strErr=snprintf(powercap_ntv_events[num_events].description, sizeof(powercap_ntv_events[num_events].description), "%s", read_buff);
powercap_ntv_events[num_events].description[sizeof(powercap_ntv_events[num_events].description)-1]=0;
Expand All @@ -310,6 +318,7 @@ static int _powercap_init_component( int cidx )

if(powercap_ntv_events[num_events].type == COMPONENT_MAX_ENERGY_RANGE) {
int sz = pread(event_fds[num_events], read_buff, PAPI_MAX_STR_LEN, 0);
if (sz == -1) HANDLE_STRING_ERROR;
read_buff[sz] = '\0';
max_component_energy_count = atoll(read_buff);
}
Expand Down
2 changes: 1 addition & 1 deletion src/linux-memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ generic_get_memory_info( PAPI_hw_info_t *hw_info )
{

int type=0,level,result;
int size,line_size,associativity,sets;
int size,line_size=0,associativity=0,sets=0;
int write_policy,allocation_policy;
DIR *dir;
FILE *fff;
Expand Down
1 change: 1 addition & 0 deletions src/papi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,7 @@ PAPI_event_name_to_code( const char *in, int *out )
char *evt_base_name = strtok(evt_name_copy, ":");
if( NULL == evt_base_name ) {
PAPIERROR("Failed to allocate space for base name of native event used in preset.\n");
free(evt_name_copy);
papi_return( PAPI_EINVAL );
}

Expand Down
Loading