added support for MALLOC_PERTURB_ environment variable

This commit is contained in:
Thomas Rieg 2025-12-08 15:48:24 +01:00
parent 0fd85cf568
commit 6d84b2f72a
17 changed files with 453 additions and 404 deletions

Binary file not shown.

View file

@ -6,7 +6,7 @@
/* By: thrieg < thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/25 11:47:16 by thrieg #+# #+# */
/* Updated: 2025/11/28 16:41:16 by thrieg ### ########.fr */
/* Updated: 2025/12/08 15:36:52 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */

Binary file not shown.

View file

@ -6,7 +6,7 @@
/* By: thrieg < thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/19 15:44:05 by thrieg #+# #+# */
/* Updated: 2025/11/25 16:21:10 by thrieg ### ########.fr */
/* Updated: 2025/12/08 15:34:11 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
@ -65,6 +65,8 @@ int defrag_zone(t_zone *zone)
void free(void *ptr)
{
pthread_mutex_lock(&g_mut);
if (!g_state.is_init)
init_env_variables(); // shouldn't ever happen because free can't be called first, but it protects the program
if (!ptr)
return ((void)pthread_mutex_unlock(&g_mut));
t_header *header = ((t_header *)ptr) - 1;
@ -84,6 +86,8 @@ void free(void *ptr)
void *ptr = header->zone;
size_t len = header->zone->size;
munmap(ptr, len);
pthread_mutex_unlock(&g_mut);
return;
}
else if (header->zone->type == E_SMALL)
{
@ -95,5 +99,12 @@ void free(void *ptr)
header->occupied = false;
defrag_zone(header->zone);
}
else
{
pthread_mutex_unlock(&g_mut);
return; // we didn't allocate this ptr
}
if (g_state.patern)
ft_memset(ptr, g_state.patern ^ 0xFF, header->size); // doesn't need to lock because we only modify the user pointer and the user doesn't have accces to it yet
pthread_mutex_unlock(&g_mut);
}

Binary file not shown.

View file

@ -6,7 +6,7 @@
/* By: thrieg < thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/17 15:02:55 by thrieg #+# #+# */
/* Updated: 2025/11/25 14:30:44 by thrieg ### ########.fr */
/* Updated: 2025/12/08 15:37:27 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,6 +15,8 @@
void *malloc(size_t size)
{
pthread_mutex_lock(&g_mut);
if (!g_state.is_init)
init_env_variables();
if (!size || size % ALLIGN_BYTES)
size = ((size / ALLIGN_BYTES) + 1) * ALLIGN_BYTES;
void *ret = NULL;
@ -31,5 +33,7 @@ void *malloc(size_t size)
ret = add_tiny(size);
}
pthread_mutex_unlock(&g_mut);
if (g_state.patern)
ft_memset(ret, g_state.patern, size); // doesn't need to lock because we only modify the user pointer and the user doesn't have accces to it yet
return (ret);
}

Binary file not shown.

View file

@ -6,7 +6,7 @@
/* By: thrieg < thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/25 11:47:23 by thrieg #+# #+# */
/* Updated: 2025/11/28 16:37:22 by thrieg ### ########.fr */
/* Updated: 2025/12/08 15:37:18 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
@ -39,6 +39,11 @@ void *realloc(void *ptr, size_t size)
size = ((size / ALLIGN_BYTES) + 1) * ALLIGN_BYTES;
void *ret = NULL;
pthread_mutex_lock(&g_mut);
if (!g_state.is_init)
{
init_env_variables(); // realloc shouldn't ever be called first with a non-NULL ptr
return (NULL); // should I just let it segfault here like the real realloc?
}
t_header *header = ((t_header *)ptr) - 1;
if (header->zone->type == E_LARGE)
{
@ -80,6 +85,7 @@ void *realloc(void *ptr, size_t size)
if (next_header && !next_header->occupied && (header->size + next_header->size + sizeof(t_header)) >= size)
{
const size_t available_space = (header->size + next_header->size + sizeof(t_header));
const size_t original_space = header->size;
if (available_space - size > sizeof(t_header))
{
// Split the block: create a new header in the remaining space
@ -96,7 +102,10 @@ void *realloc(void *ptr, size_t size)
{
header->size = available_space;
}
ret = ptr;
pthread_mutex_unlock(&g_mut);
if (g_state.patern)
ft_memset((char *)ptr + original_space, g_state.patern, header->size - original_space); // should I use original_size to only perturb the actually requested bytes instead of the bytes I allocate?
return (ptr);
}
else
{

Binary file not shown.

View file

@ -6,14 +6,27 @@
/* By: thrieg < thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/18 16:44:13 by thrieg #+# #+# */
/* Updated: 2025/11/28 16:41:55 by thrieg ### ########.fr */
/* Updated: 2025/12/08 15:32:55 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/ft_malloc.h"
pthread_mutex_t g_mut = PTHREAD_MUTEX_INITIALIZER;
t_state g_state = {.tiny_zone = NULL, .small_zone = NULL, .large_zone = NULL};
t_state g_state = {.tiny_zone = NULL, .small_zone = NULL, .large_zone = NULL, .patern = 0, .is_init = false};
void init_env_variables()
{
char *env = getenv("MALLOC_PERTURB_"); // this doesn't call malloc, returns a pointer from **environ
if (env)
{
int overflow = 0;
int v = ft_atoi(env, &overflow);
if (!overflow && v > 0 && v <= 255)
g_state.patern = (unsigned char)v;
}
g_state.is_init = true;
}
// only call this for TINY or SMALL
void *add_page(t_type type)

Binary file not shown.