void CMemTest::NULLReturn(UINT MinSize, UINT MaxSize, int total_threads) { // find size to guarantee getting NULL for 1024 B allocations const int MAXNUM_1024 = (200+50)*1024; std::vector<MemStruct> PointerList; void *tmp; CountErrors=0; int CountNULL, num_1024; if (FullLog) REPORT("\nNULL return & check errno:\n"); UINT Size; Limit limit_200M(200*total_threads), no_limit(0); void **buf_1024 = (void**)Tmalloc(MAXNUM_1024*sizeof(void*)); ASSERT(buf_1024, NULL); /* We must have space for pointers when memory limit is hit. Reserve enough for the worst case. */ PointerList.reserve(200*MByte/MinSize); /* There is a bug in the specific verion of GLIBC (2.5-12) shipped with RHEL5 that leads to erroneous working of the test on Intel64 and IPF systems when setrlimit-related part is enabled. Switching to GLIBC 2.5-18 from RHEL5.1 resolved the issue. */ if (perProcessLimits) limitBarrier->wait(limit_200M); else limitMem(200); /* regression test against the bug in allocator when it dereference NULL while lack of memory */ for (num_1024=0; num_1024<MAXNUM_1024; num_1024++) { buf_1024[num_1024] = Tcalloc(1024, 1); if (! buf_1024[num_1024]) { ASSERT_ERRNO(errno == ENOMEM, NULL); break; } } for (int i=0; i<num_1024; i++) Tfree(buf_1024[i]); Tfree(buf_1024); do { Size=rand()%(MaxSize-MinSize)+MinSize; tmp=Tmalloc(Size); if (tmp != NULL) { memset(tmp, 0, Size); PointerList.push_back(MemStruct(tmp, Size)); } } while(tmp != NULL); ASSERT_ERRNO(errno == ENOMEM, NULL); if (FullLog) REPORT("\n"); // preparation complete, now running tests // malloc if (FullLog) REPORT("malloc...."); CountNULL = 0; while (CountNULL==0) for (int j=0; j<COUNT_TESTS; j++) { Size=rand()%(MaxSize-MinSize)+MinSize; errno = ENOMEM+j+1; tmp=Tmalloc(Size); if (tmp == NULL) { CountNULL++; if ( CHECK_ERRNO(errno != ENOMEM) ) { CountErrors++; if (ShouldReportError()) REPORT("NULL returned, error: errno (%d) != ENOMEM\n", errno); } } else { // Technically, if malloc returns a non-NULL pointer, it is allowed to set errno anyway. // However, on most systems it does not set errno. bool known_issue = false; #if __linux__ if( CHECK_ERRNO(errno==ENOMEM) ) known_issue = true; #endif /* __linux__ */ if ( CHECK_ERRNO(errno != ENOMEM+j+1) && !known_issue) { CountErrors++; if (ShouldReportError()) REPORT("error: errno changed to %d though valid pointer was returned\n", errno); } memset(tmp, 0, Size); PointerList.push_back(MemStruct(tmp, Size)); } } if (FullLog) REPORT("end malloc\n"); if (CountErrors) REPORT("%s\n",strError); else if (FullLog) REPORT("%s\n",strOk); error_occurred |= ( CountErrors>0 ) ; CountErrors=0; //calloc if (FullLog) REPORT("calloc...."); CountNULL = 0; while (CountNULL==0) for (int j=0; j<COUNT_TESTS; j++) { Size=rand()%(MaxSize-MinSize)+MinSize; errno = ENOMEM+j+1; tmp=Tcalloc(COUNT_ELEM_CALLOC,Size); if (tmp == NULL) { CountNULL++; if ( CHECK_ERRNO(errno != ENOMEM) ){ CountErrors++; if (ShouldReportError()) REPORT("NULL returned, error: errno(%d) != ENOMEM\n", errno); } } else { // Technically, if calloc returns a non-NULL pointer, it is allowed to set errno anyway. // However, on most systems it does not set errno. bool known_issue = false; #if __linux__ if( CHECK_ERRNO(errno==ENOMEM) ) known_issue = true; #endif /* __linux__ */ if ( CHECK_ERRNO(errno != ENOMEM+j+1) && !known_issue ) { CountErrors++; if (ShouldReportError()) REPORT("error: errno changed to %d though valid pointer was returned\n", errno); } PointerList.push_back(MemStruct(tmp, Size)); } } if (FullLog) REPORT("end calloc\n"); if (CountErrors) REPORT("%s\n",strError); else if (FullLog) REPORT("%s\n",strOk); error_occurred |= ( CountErrors>0 ) ; CountErrors=0; if (FullLog) REPORT("realloc...."); CountNULL = 0; if (PointerList.size() > 0) while (CountNULL==0) for (size_t i=0; i<(size_t)COUNT_TESTS && i<PointerList.size(); i++) { errno = 0; tmp=Trealloc(PointerList[i].Pointer,PointerList[i].Size*2); if (PointerList[i].Pointer == tmp) // the same place { bool known_issue = false; #if __linux__ if( errno==ENOMEM ) known_issue = true; #endif /* __linux__ */ if (errno != 0 && !known_issue) { CountErrors++; if (ShouldReportError()) REPORT("valid pointer returned, error: errno not kept\n"); } PointerList[i].Size *= 2; } else if (tmp != PointerList[i].Pointer && tmp != NULL) // another place { bool known_issue = false; #if __linux__ if( errno==ENOMEM ) known_issue = true; #endif /* __linux__ */ if (errno != 0 && !known_issue) { CountErrors++; if (ShouldReportError()) REPORT("valid pointer returned, error: errno not kept\n"); } // newly allocated area have to be zeroed memset((char*)tmp + PointerList[i].Size, 0, PointerList[i].Size); PointerList[i].Pointer = tmp; PointerList[i].Size *= 2; } else if (tmp == NULL) { CountNULL++; if ( CHECK_ERRNO(errno != ENOMEM) ) { CountErrors++; if (ShouldReportError()) REPORT("NULL returned, error: errno(%d) != ENOMEM\n", errno); } // check data integrity BYTE *zer=(BYTE*)PointerList[i].Pointer; for (UINT k=0; k<PointerList[i].Size; k++) if (zer[k] != 0) { CountErrors++; if (ShouldReportError()) REPORT("NULL returned, error: data changed\n"); } } } if (FullLog) REPORT("realloc end\n"); if (CountErrors) REPORT("%s\n",strError); else if (FullLog) REPORT("%s\n",strOk); error_occurred |= ( CountErrors>0 ) ; for (UINT i=0; i<PointerList.size(); i++) { Tfree(PointerList[i].Pointer); } if (perProcessLimits) limitBarrier->wait(no_limit); else limitMem(0); }
void CMemTest::UniquePointer() { CountErrors=0; int **MasPointer = (int **)Tmalloc(sizeof(int*)*COUNT_ELEM); size_t *MasCountElem = (size_t*)Tmalloc(sizeof(size_t)*COUNT_ELEM); if (FullLog) REPORT("\nUnique pointer using 0\n"); ASSERT(MasCountElem && MasPointer, NULL); // //------------------------------------------------------- //malloc for (int i=0; i<COUNT_ELEM; i++) { MasCountElem[i]=rand()%MAX_SIZE; MasPointer[i]=(int*)Tmalloc(MasCountElem[i]*sizeof(int)); if (NULL == MasPointer[i]) MasCountElem[i]=0; memset(MasPointer[i], 0, sizeof(int)*MasCountElem[i]); } if (FullLog) REPORT("malloc...."); for (UINT i=0; i<COUNT_ELEM-1; i++) { if (size_t badOff = NonZero(MasPointer[i], sizeof(int)*MasCountElem[i])) { CountErrors++; if (ShouldReportError()) REPORT("error, detect non-zero at %p\n", (char*)MasPointer[i]+badOff-1); } memset(MasPointer[i], 1, sizeof(int)*MasCountElem[i]); } if (CountErrors) REPORT("%s\n",strError); else if (FullLog) REPORT("%s\n",strOk); error_occurred |= ( CountErrors>0 ) ; //---------------------------------------------------------- //calloc for (int i=0; i<COUNT_ELEM; i++) Tfree(MasPointer[i]); CountErrors=0; for (long i=0; i<COUNT_ELEM; i++) { MasPointer[i]=(int*)Tcalloc(MasCountElem[i]*sizeof(int),2); if (NULL == MasPointer[i]) MasCountElem[i]=0; } if (FullLog) REPORT("calloc...."); for (int i=0; i<COUNT_ELEM-1; i++) { if (size_t badOff = NonZero(MasPointer[i], sizeof(int)*MasCountElem[i])) { CountErrors++; if (ShouldReportError()) REPORT("error, detect non-zero at %p\n", (char*)MasPointer[i]+badOff-1); } memset(MasPointer[i], 1, sizeof(int)*MasCountElem[i]); } if (CountErrors) REPORT("%s\n",strError); else if (FullLog) REPORT("%s\n",strOk); error_occurred |= ( CountErrors>0 ) ; //--------------------------------------------------------- //realloc CountErrors=0; for (int i=0; i<COUNT_ELEM; i++) { MasCountElem[i]*=2; *(MasPointer+i)= (int*)Trealloc(*(MasPointer+i),MasCountElem[i]*sizeof(int)); if (NULL == MasPointer[i]) MasCountElem[i]=0; memset(MasPointer[i], 0, sizeof(int)*MasCountElem[i]); } if (FullLog) REPORT("realloc...."); for (int i=0; i<COUNT_ELEM-1; i++) { if (NonZero(MasPointer[i], sizeof(int)*MasCountElem[i])) CountErrors++; memset(MasPointer[i], 1, sizeof(int)*MasCountElem[i]); } if (CountErrors) REPORT("%s\n",strError); else if (FullLog) REPORT("%s\n",strOk); error_occurred |= ( CountErrors>0 ) ; for (int i=0; i<COUNT_ELEM; i++) Tfree(MasPointer[i]); Tfree(MasCountElem); Tfree(MasPointer); }
void CMemTest::AddrArifm() { PtrSize *arr = (PtrSize*)Tmalloc(COUNT_ELEM*sizeof(PtrSize)); if (FullLog) REPORT("\nUnique pointer using Address arithmetics\n"); if (FullLog) REPORT("malloc...."); ASSERT(arr, NULL); for (int i=0; i<COUNT_ELEM; i++) { arr[i].size=rand()%MAX_SIZE; arr[i].ptr=Tmalloc(arr[i].size); } qsort(arr, COUNT_ELEM, sizeof(PtrSize), cmpAddrs); for (int i=0; i<COUNT_ELEM-1; i++) { if (NULL!=arr[i].ptr && NULL!=arr[i+1].ptr) ASSERT((uintptr_t)arr[i].ptr+arr[i].size <= (uintptr_t)arr[i+1].ptr, "intersection detected"); } //---------------------------------------------------------------- if (FullLog) REPORT("realloc...."); for (int i=0; i<COUNT_ELEM; i++) { size_t count=arr[i].size*2; void *tmpAddr=Trealloc(arr[i].ptr,count); if (NULL!=tmpAddr) { arr[i].ptr = tmpAddr; arr[i].size = count; } else if (count==0) { // becasue realloc(..., 0) works as free arr[i].ptr = NULL; arr[i].size = 0; } } qsort(arr, COUNT_ELEM, sizeof(PtrSize), cmpAddrs); for (int i=0; i<COUNT_ELEM-1; i++) { if (NULL!=arr[i].ptr && NULL!=arr[i+1].ptr) ASSERT((uintptr_t)arr[i].ptr+arr[i].size <= (uintptr_t)arr[i+1].ptr, "intersection detected"); } for (int i=0; i<COUNT_ELEM; i++) { Tfree(arr[i].ptr); } //------------------------------------------- if (FullLog) REPORT("calloc...."); for (int i=0; i<COUNT_ELEM; i++) { arr[i].size=rand()%MAX_SIZE; arr[i].ptr=Tcalloc(arr[i].size,1); } qsort(arr, COUNT_ELEM, sizeof(PtrSize), cmpAddrs); for (int i=0; i<COUNT_ELEM-1; i++) { if (NULL!=arr[i].ptr && NULL!=arr[i+1].ptr) ASSERT((uintptr_t)arr[i].ptr+arr[i].size <= (uintptr_t)arr[i+1].ptr, "intersection detected"); } for (int i=0; i<COUNT_ELEM; i++) { Tfree(arr[i].ptr); } Tfree(arr); }
void CMemTest::UniquePointer() { CountErrors=0; int **MasPointer = (int **)Tmalloc(sizeof(int*)*COUNT_ELEM); size_t *MasCountElem = (size_t*)Tmalloc(sizeof(size_t)*COUNT_ELEM); if (FullLog) REPORT("\nUnique pointer using 0\n"); ASSERT(MasCountElem && MasPointer, NULL); // //------------------------------------------------------- //malloc for (int i=0; i<COUNT_ELEM; i++) { MasCountElem[i]=rand()%MAX_SIZE; MasPointer[i]=(int*)Tmalloc(MasCountElem[i]*sizeof(int)); if (NULL == MasPointer[i]) MasCountElem[i]=0; for (UINT j=0; j<MasCountElem[i]; j++) *(MasPointer[i]+j)=0; } if (FullLog) REPORT("malloc...."); for (UINT i=0; i<COUNT_ELEM-1; i++) { for (UINT j=0; j<MasCountElem[i]; j++) { if (*(*(MasPointer+i)+j)!=0) { CountErrors++; if (ShouldReportError()) REPORT("error, detect 1 with 0x%p\n",(*(MasPointer+i)+j)); } *(*(MasPointer+i)+j)+=1; } } if (CountErrors) REPORT("%s\n",strError); else if (FullLog) REPORT("%s\n",strOk); error_occurred |= ( CountErrors>0 ) ; //---------------------------------------------------------- //calloc for (int i=0; i<COUNT_ELEM; i++) Tfree(MasPointer[i]); CountErrors=0; for (long i=0; i<COUNT_ELEM; i++) { MasPointer[i]=(int*)Tcalloc(MasCountElem[i]*sizeof(int),2); if (NULL == MasPointer[i]) MasCountElem[i]=0; } if (FullLog) REPORT("calloc...."); for (int i=0; i<COUNT_ELEM-1; i++) { for (UINT j=0; j<*(MasCountElem+i); j++) { if (*(*(MasPointer+i)+j)!=0) { CountErrors++; if (ShouldReportError()) REPORT("error, detect 1 with 0x%p\n",(*(MasPointer+i)+j)); } *(*(MasPointer+i)+j)+=1; } } if (CountErrors) REPORT("%s\n",strError); else if (FullLog) REPORT("%s\n",strOk); error_occurred |= ( CountErrors>0 ) ; //--------------------------------------------------------- //realloc CountErrors=0; for (int i=0; i<COUNT_ELEM; i++) { MasCountElem[i]*=2; *(MasPointer+i)= (int*)Trealloc(*(MasPointer+i),MasCountElem[i]*sizeof(int)); if (NULL == MasPointer[i]) MasCountElem[i]=0; for (UINT j=0; j<MasCountElem[i]; j++) *(*(MasPointer+i)+j)=0; } if (FullLog) REPORT("realloc...."); for (int i=0; i<COUNT_ELEM-1; i++) { for (UINT j=0; j<*(MasCountElem+i); j++) { if (*(*(MasPointer+i)+j)!=0) { CountErrors++; } *(*(MasPointer+i)+j)+=1; } } if (CountErrors) REPORT("%s\n",strError); else if (FullLog) REPORT("%s\n",strOk); error_occurred |= ( CountErrors>0 ) ; for (int i=0; i<COUNT_ELEM; i++) Tfree(MasPointer[i]); Tfree(MasCountElem); Tfree(MasPointer); }