Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/cmockery.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chech --> Check

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){
Copy link
Contributor

Choose a reason for hiding this comment

The 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) {"

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand All @@ -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.
Expand Down
15 changes: 13 additions & 2 deletions src/example/allocate_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#endif
#include <sys/types.h>

#if UNIT_TESTING
//#if UNIT_TESTING
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
Expand All @@ -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)] = '!';
Expand Down
17 changes: 17 additions & 0 deletions src/example/allocate_module_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,27 @@
extern void leak_memory();
extern void buffer_overflow();
extern void buffer_underflow();
extern void free_twice_memory();
extern void free_bad_memory();


// Test case that fails as leak_memory() leaks a dynamically allocated block.
void leak_memory_test(void **state) {
leak_memory();
}

// Test case that fails as free_twice_memory() free twice.
void free_twice_memory_test(void **state) {
free_twice_memory();
}

// Test case that fails as free_bad_memory() free bad memory.
void free_bad_memory_test(void **state) {
free_bad_memory();
}



// Test case that fails as buffer_overflow() corrupts an allocated block.
void buffer_overflow_test(void **state) {
buffer_overflow();
Expand All @@ -42,6 +57,8 @@ int main(int argc, char* argv[]) {
unit_test(leak_memory_test),
unit_test(buffer_overflow_test),
unit_test(buffer_underflow_test),
unit_test(free_twice_memory),
unit_test(free_bad_memory)
};
return run_tests(tests);
}