Commit 03e3fc04 authored by Davide Galassi's avatar Davide Galassi

Malloc mock set unused memory and new acquired memory to 0xFF.

This shall prevent usage of freed memory and that the user makes
no assumptions about the content of allocation memory.
parent eea60f58
Pipeline #57998954 passed with stages
in 1 minute and 30 seconds
......@@ -4,7 +4,7 @@ CC := gcc
CFLAGS := -O0 -g3 -Wall
CPPFLAGS = -I. -I../include -I../src
LDFLAGS := --coverage -Wl,--wrap=malloc,--wrap=realloc,--wrap=calloc,--wrap=free
LDFLAGS := --coverage -Wl,--wrap=malloc,--wrap=realloc,--wrap=free
objs := \
......
#include "malloc_mock.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int g_malloc_mock_count = -1;
int g_malloc_mock_count;
int g_malloc_mock_state = MALLOC_MOCK_STOPPED;
#define MAGIC 0xABADCAFE
void *__real_malloc(size_t size);
void *__real_realloc(void *ptr, size_t size);
void *__real_calloc(size_t nmemb, size_t size);
void __real_free(void *ptr);
void *__wrap_malloc(size_t size)
static int mock_update(void)
{
if (g_malloc_mock_state == MALLOC_MOCK_ACTIVE) {
if (g_malloc_mock_count == 0) {
g_malloc_mock_state = MALLOC_MOCK_FAILED;
return NULL;
}
g_malloc_mock_count--;
}
return __real_malloc(size);
return g_malloc_mock_state;
}
void *__wrap_realloc(void *ptr, size_t size)
struct mem_head {
unsigned long magic;
size_t size;
};
#define HEAD_SIZE sizeof(struct mem_head)
void *__wrap_malloc(size_t size)
{
struct mem_head *head;
if (mock_update() == MALLOC_MOCK_FAILED)
return NULL;
if (g_malloc_mock_state == MALLOC_MOCK_ACTIVE) {
if (g_malloc_mock_count == 0) {
g_malloc_mock_state = MALLOC_MOCK_FAILED;
......@@ -31,23 +44,50 @@ void *__wrap_realloc(void *ptr, size_t size)
}
g_malloc_mock_count--;
}
return __real_realloc(ptr, size);
head = __real_malloc(HEAD_SIZE + size);
if (head == NULL)
return NULL;
head->magic = MAGIC;
head->size = size;
memset(&head[1], 0xFF, size);
return &head[1];
}
void *__wrap_calloc(size_t nmemb, size_t size)
void *__wrap_realloc(void *ptr, size_t size)
{
if (g_malloc_mock_state == MALLOC_MOCK_ACTIVE) {
if (g_malloc_mock_count == 0) {
g_malloc_mock_state = MALLOC_MOCK_FAILED;
return NULL;
}
g_malloc_mock_count--;
struct mem_head *head;
if (mock_update() == MALLOC_MOCK_FAILED)
return NULL;
if (ptr == NULL)
return __wrap_malloc(size);
head = ((struct mem_head *)ptr) - 1;
if (head->magic != MAGIC) {
printf(">>> Warning: realloc of a not alloced buffer\n");
return NULL;
}
return __real_calloc(nmemb, size);
head = __real_realloc(head, (size != 0) ? size + HEAD_SIZE : 0);
if (size == 0)
return NULL;
if (size > head->size)
memset((unsigned char *)&head[1] + head->size, 0xFF, size - head->size);
else
memset((unsigned char *)&head[1] + size, 0xFF, head->size - size);
head->size = size;
return &head[1];
}
void __wrap_free(void *ptr)
{
__real_free(ptr);
struct mem_head *head;
head = ((struct mem_head *)ptr) - 1;
if (head->magic != MAGIC) {
printf(">>> Warning: free of a not alloced buffer\n");
return;
}
memset(head, 0xFF, HEAD_SIZE + head->size);
__real_free(head);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment