Unchecked mallocs
stephand opened this issue · 2 comments
Similar to my recent issue in XSbench, RSbench has also unchecked mallocs that can cause segfaults.
$ grep -n malloc c
init.c:56: Pole * R = (Pole *) malloc( input.n_nuclides * sizeof( Pole *));
init.c:57: Pole * contiguous = (Pole *) malloc( input.n_nuclides * input.avg_n_poles * sizeof(Pole));
init.c:89: Window * R = (Window *) malloc( input.n_nuclides * sizeof( Window *));
init.c:90: Window * contiguous = (Window *) malloc( input.n_nuclides * input.avg_n_windows * sizeof(Window));
init.c:128: double * R = (double *) malloc( input.n_nuclides * sizeof( double * ));
init.c:129: double * contiguous = (double *) malloc( input.n_nuclides * input.numL * sizeof(double));
main.c:120: (complex double *) malloc( input.numL * sizeof(complex double) );
material.c:17: int * num_nucs = (int)malloc(12_sizeof(int));
material.c:44: int *_ mats = (int *) malloc( 12 * sizeof(int *) );
material.c:46: mats[i] = (int *) malloc(num_nucs[i] * sizeof(int) );
material.c:112: double * concs = (double **)malloc( 12 * sizeof( double *) );
material.c:115: concs[i] = (double *)malloc( num_nucs[i] * sizeof(double) );
papi.c:252: int * events = malloc(num_papi_events * sizeof(int));
papi.c:257: long_long * values = malloc( num_papi_events * sizeof(long_long));
Not quite as bad as in XSbench, because the default allocation is smaller (~250MB), but it would still be good to have checked mallocs.
Thanks for pointing this out!
The malloc return value was originally not checked as it's not actually guaranteed to return NULL if the system is out of memory. This is dependent on the particular OS and how it handles memory allocation requests. OSX Darwin, for instance, will usually not return a NULL malloc, even for ridiculous sized allocations. To combat this issue, you'll notice that the code actually prints out the estimated memory usage requirements at the beginning of execution, so if an OOM seg fault occurs it should give the user a heads up as to what the issue might be.
Given that the memory requirements are so low for RSBench, I'd rather not bloat the code given that a seg fault is very unlikely to occur and the checks may not work on some OS's anyway. However, if you feel that this feature would have tangible benefits to your use or development, I'd be more than happy to add it in anyway, just let me know!
Thanks again for bringing this up though!
Makes sense, thanks!