Skip to content
Permalink
Browse files

Disabled invalid free error (it's not an error necessarily) and added…

… offset checking for overflow detection.
  • Loading branch information...
emeryberger committed Nov 7, 2019
1 parent d65de67 commit 008ac9784d9fd12c95a0023480594aee2e3f2593
Showing with 19 additions and 11 deletions.
  1. +19 −11 src/include/randomminiheap-core.h
@@ -11,7 +11,7 @@ class RandomMiniHeapBase {
virtual void * malloc (size_t) = 0;
virtual bool free (void *) = 0;
virtual size_t getSize (void *) = 0;
virtual void activate (void) = 0;
virtual void activate() = 0;
virtual ~RandomMiniHeapBase () {}
};

@@ -39,15 +39,15 @@ template <int Numerator,
class RandomMiniHeapCore : public RandomMiniHeapBase {
private:

void reportOverflowError(void * ptr) {
void reportOverflowError(void * ptr, int offset) {
if (DieFastOn) {
tprintf::tprintf("DieFast: Overflow detected in object at address: @\n", ptr);
tprintf::tprintf("DieFast: Overflow detected in object at address: @, position: @ (malloc size = @)\n", ptr, offset, ObjectSize);
}
}

void reportDoubleFreeError(void * ptr) {
if (DieFastOn) {
tprintf::tprintf("DieFast: Double free detected in object at address: @\n", ptr);
tprintf::tprintf("DieFast: Double free detected in object at address: @ (malloc size = @)\n", ptr, ObjectSize);
}
}

@@ -85,7 +85,7 @@ class RandomMiniHeapCore : public RandomMiniHeapBase {

/// @return an allocated object of size ObjectSize
/// @param sz requested object size
/// @note May return NULL even though there is free space.
/// @note May return nullptr even though there is free space.
#ifdef NDEBUG
void * malloc (size_t)
#else
@@ -97,15 +97,15 @@ class RandomMiniHeapCore : public RandomMiniHeapBase {
// Ensure size is reasonable.
assert (sz <= ObjectSize);

void * ptr = NULL;
void * ptr = nullptr;

// Try to allocate an object from the bitmap.
unsigned int index = modulo<NObjects> (_random.next());

bool didMalloc = _miniHeapBitmap.tryToSet (index);

if (!didMalloc) {
return NULL;
return nullptr;
}

// Get the address of the indexed object.
@@ -120,7 +120,14 @@ class RandomMiniHeapCore : public RandomMiniHeapBase {
if (DieFastOn) {
// Check to see if this object was overflowed.
if (DieFast::checkNot (ptr, ObjectSize, _freedValue)) {
reportOverflowError(ptr);
auto * l = (size_t *) ptr;
int i;
for (i = 0; i < (int) (ObjectSize / sizeof(size_t)); i++) {
if (l[i] != _freedValue) {
break;
}
}
reportOverflowError(ptr, i * sizeof(size_t));
}
}

@@ -138,7 +145,8 @@ class RandomMiniHeapCore : public RandomMiniHeapBase {

// Return false if the pointer is out of range.
if (!inBounds(ptr)) {
reportInvalidFreeError(ptr);
// Can't do the below because we always check lots of miniheaps...
// reportInvalidFreeError(ptr);
return false;
}

@@ -159,7 +167,7 @@ class RandomMiniHeapCore : public RandomMiniHeapBase {


/// Sanity check.
void check (void) const {
void check() const {
assert ((_check1 == CHECK1) &&
(_check2 == CHECK2));
}
@@ -211,7 +219,7 @@ class RandomMiniHeapCore : public RandomMiniHeapBase {


/// @return true iff heap is currently active.
inline bool isActivated (void) const {
inline bool isActivated() const {
return _isHeapActivated;
}

0 comments on commit 008ac97

Please sign in to comment.
You can’t perform that action at this time.