-
Notifications
You must be signed in to change notification settings - Fork 364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
update memory allocate check #30
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,6 +94,7 @@ typedef struct ListNode { | |
// Debug information for malloc(). | ||
typedef struct MallocBlockInfo { | ||
void* block; // Address of the block returned by malloc(). | ||
void* ptr; // Address returned to caller | ||
size_t allocated_size; // Total size of the allocated block. | ||
size_t size; // Request block size. | ||
SourceLocation location; // Where the block was allocated. | ||
|
@@ -1361,6 +1362,7 @@ void* _test_malloc(const size_t size, const char* file, const int line) { | |
block_info->allocated_size = allocate_size; | ||
block_info->size = size; | ||
block_info->block = block; | ||
block_info->ptr = ptr; | ||
block_info->node.value = block_info; | ||
list_add(block_list, &block_info->node); | ||
return ptr; | ||
|
@@ -1377,6 +1379,22 @@ void* _test_calloc(const size_t number_of_elements, const size_t size, | |
return ptr; | ||
} | ||
|
||
//Chech if the address is really allocated | ||
int check_allocated_exist(void* const ptr){ | ||
const ListNode * const head = get_allocated_blocks_list(); | ||
const ListNode *node = head->next; | ||
int rc = 0; | ||
while(node != head){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We roughly follow google C++ style https://google.github.io/styleguide/cppguide.html in here. So this should have some whitespace... "while (node != head) {" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same sort of this below with the "if" statements |
||
const MallocBlockInfo * const block_info = (const MallocBlockInfo*)node->value; | ||
assert_true(block_info); | ||
if(block_info->ptr == ptr){ | ||
rc = 1; | ||
break; | ||
} | ||
node = node->next; | ||
} | ||
return rc; | ||
} | ||
|
||
// Use the real free in this function. | ||
#undef free | ||
|
@@ -1385,6 +1403,17 @@ void _test_free(void* const ptr, const char* file, const int line) { | |
char *block = (char*)ptr; | ||
MallocBlockInfo *block_info; | ||
_assert_true((int)ptr, "ptr", file, line); | ||
|
||
//is it a allocated block(maybe free twice) | ||
if(!check_allocated_exist((void * const)ptr)){ | ||
print_error( | ||
"the address of 0x%08x in " | ||
SOURCE_LOCATION_FORMAT " is not a allocated block\n", | ||
(size_t)ptr, file, line | ||
); | ||
_fail(file, line); | ||
} | ||
|
||
block_info = (MallocBlockInfo*)(block - (MALLOC_GUARD_SIZE + | ||
sizeof(*block_info))); | ||
// Check the guard blocks. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ | |
#endif | ||
#include <sys/types.h> | ||
|
||
#if UNIT_TESTING | ||
//#if UNIT_TESTING | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to keep this in place so that the wrapper for the allocator is only in place when testing. |
||
extern void* _test_malloc(const size_t size, const char* file, const int line); | ||
extern void* _test_calloc(const size_t number_of_elements, const size_t size, | ||
const char* file, const int line); | ||
|
@@ -30,13 +30,24 @@ extern void _test_free(void* const ptr, const char* file, const int line); | |
#define malloc(size) _test_malloc(size, __FILE__, __LINE__) | ||
#define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__) | ||
#define free(ptr) _test_free(ptr, __FILE__, __LINE__) | ||
#endif // UNIT_TESTING | ||
//#endif // UNIT_TESTING | ||
|
||
void leak_memory() { | ||
int * const temporary = (int*)malloc(sizeof(int)); | ||
*temporary = 0; | ||
} | ||
|
||
void free_bad_memory() { | ||
int counter = 0; | ||
free(&counter); | ||
} | ||
|
||
void free_twice_memory() { | ||
int* temporary = (int*)malloc(sizeof(int)); | ||
free(temporary); | ||
free(temporary); | ||
} | ||
|
||
void buffer_overflow() { | ||
char * const memory = (char*)malloc(sizeof(int)); | ||
memory[sizeof(int)] = '!'; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Chech --> Check