added a new method rs_malloc that checks its arguments and prints a stacktrace on error/weird call. Changed the code everywhere to use this instead of malloc. Removed some mallocs and replaced with RsTemporaryMemory

This commit is contained in:
csoler 2016-01-12 21:10:11 -05:00
parent 9c6e7dfc13
commit d13526facd
39 changed files with 274 additions and 132 deletions

View file

@ -30,6 +30,7 @@
#include <assert.h>
#include "rscompress.h"
#include "zlib.h"
#include "util/rsmemory.h"
// 16K buffer size.
//
@ -42,7 +43,10 @@ bool RsCompress::compress_memory_chunk(const uint8_t *input_mem,const uint32_t i
uint32_t output_offset = 0 ;
uint32_t input_offset = 0 ;
output_size = 1024 ;
output_mem = (uint8_t*)malloc(output_size) ;
output_mem = (uint8_t*)rs_safe_malloc(output_size) ;
if(!output_mem)
return false ;
int ret, flush;
unsigned have;
@ -113,8 +117,11 @@ bool RsCompress::uncompress_memory_chunk(const uint8_t *input_mem,const uint32_t
output_size = input_size ;
uint32_t output_offset = 0 ;
uint32_t input_offset = 0 ;
output_mem = (uint8_t*)malloc(output_size) ;
output_mem = (uint8_t*)rs_safe_malloc(output_size) ;
if(!output_mem)
return false ;
int ret;
unsigned have;
z_stream strm;

View file

@ -33,6 +33,7 @@
#include "util/rsdir.h"
#include "util/rsstring.h"
#include "util/rsrandom.h"
#include "util/rsmemory.h"
#include "retroshare/rstypes.h"
#include "rsthreads.h"
#include <iostream>
@ -267,7 +268,14 @@ bool RsDirUtil::copyFile(const std::string& source,const std::string& dest)
size_t T=0;
static const int BUFF_SIZE = 10485760 ; // 10 MB buffer to speed things up.
void *buffer = malloc(BUFF_SIZE) ;
RsTemporaryMemory buffer(BUFF_SIZE) ;
if(!buffer)
{
fclose(in) ;
fclose(out) ;
return false ;
}
bool bRet = true;
@ -286,8 +294,6 @@ bool RsDirUtil::copyFile(const std::string& source,const std::string& dest)
fclose(in) ;
fclose(out) ;
free(buffer) ;
return true ;
#endif

View file

@ -0,0 +1,32 @@
#include "util/rsmemory.h"
void *rs_safe_malloc(size_t size)
{
static const size_t SAFE_MEMALLOC_THRESHOLD = 1024*1024*1024 ; // 1Gb should be enough for everything!
if(size == 0)
{
std::cerr << "(EE) Memory allocation error. A chunk of size 0 was requested. Callstack:" << std::endl;
print_stacktrace() ;
return NULL ;
}
if(size > SAFE_MEMALLOC_THRESHOLD)
{
std::cerr << "(EE) Memory allocation error. A chunk of size 0 was requested. Callstack:" << std::endl;
print_stacktrace() ;
return NULL ;
}
void *mem = malloc(size) ;
if(mem == NULL)
{
std::cerr << "(EE) Memory allocation error for a chunk of " << size << " bytes. Callstack:" << std::endl;
print_stacktrace() ;
return NULL ;
}
return mem ;
}

View file

@ -1,6 +1,10 @@
#pragma once
#include <stdlib.h>
#include <iostream>
#include <util/stacktrace.h>
void *rs_safe_malloc(size_t size) ;
// This is a scope guard to release the memory block when going of of the current scope.
// Can be very useful to auto-delete some memory on quit without the need to call free each time.
@ -24,7 +28,7 @@ class RsTemporaryMemory
public:
RsTemporaryMemory(size_t s)
{
_mem = (unsigned char *)malloc(s) ;
_mem = (unsigned char *)rs_safe_malloc(s) ;
if(_mem)
_size = s ;
@ -53,5 +57,3 @@ private:
RsTemporaryMemory& operator=(const RsTemporaryMemory&) { return *this ;}
RsTemporaryMemory(const RsTemporaryMemory&) {}
};

View file

@ -145,7 +145,12 @@ bool RsRecogn::loadSigningKeys(std::map<RsGxsId, RsGxsRecognSignerItem *> &signM
/* store in */
uint32_t datalen = recognSerialiser.size(item);
uint8_t *data = (uint8_t *) malloc(datalen);
RsTemporaryMemory data(datalen) ;
if(!data)
return false ;
uint32_t pktlen = datalen;
int signOk = 0;
@ -181,8 +186,6 @@ bool RsRecogn::loadSigningKeys(std::map<RsGxsId, RsGxsRecognSignerItem *> &signM
#endif // DEBUG_RECOGN
delete item;
}
free(data);
}
/* clean up */
@ -233,7 +236,12 @@ bool RsRecogn::validateTagSignature(RsGxsRecognSignerItem *signer, RsGxsRecognTa
RsGxsRecognSerialiser serialiser;
uint32_t datalen = serialiser.size(item);
uint8_t *data = (uint8_t *) malloc(datalen);
RsTemporaryMemory data(datalen) ;
if(!data)
return false ;
int signOk = 0;
uint32_t pktlen = datalen;
@ -262,8 +270,6 @@ bool RsRecogn::validateTagSignature(RsGxsRecognSignerItem *signer, RsGxsRecognTa
EVP_MD_CTX_destroy(mdctx);
EVP_PKEY_free(signKey);
free(data);
return (signOk == 1);
}

View file

@ -1,6 +1,7 @@
#include <iostream>
#include "smallobject.h"
#include "util/rsthreads.h"
#include "util/rsmemory.h"
using namespace RsMemoryManagement ;
@ -206,7 +207,7 @@ SmallObjectAllocator::~SmallObjectAllocator()
void *SmallObjectAllocator::allocate(size_t bytes)
{
if(bytes > _maxObjectSize)
return malloc(bytes) ;
return rs_safe_malloc(bytes) ;
else if(_lastAlloc != NULL && _lastAlloc->blockSize() == bytes)
return _lastAlloc->allocate() ;
else