Skip to content

Commit

Permalink
write and implement rtest
Browse files Browse the repository at this point in the history
  • Loading branch information
Ycaro02 committed Feb 22, 2024
1 parent 9a343d9 commit 52a9074
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ ${NAME}: $(OBJS)
@echo "\033[7;32m ----- Compiling malloc done ----- \033[0m"
@ln -sf ${NAME} ${LINK_NAME}

rtest:
@"${CALL_TESTER}" rtest

test : ${NAME}
@"${CALL_TESTER}" test0
@"${CALL_TESTER}" test1
Expand Down
6 changes: 5 additions & 1 deletion tester/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ test4: ${NAME}
test5:
@gcc src/test5.c ../libft_malloc.so -o ${LAST_TEST} 2> /dev/null && ./${LAST_TEST}

rtest: ${NAME}
@${CHECK_LIB}
./correct.sh

clean:
@${RM} ${NAME} ${REAL_TEST} ${LAST_TEST}

fclean: clean
@${RM} ${NAME} ${REAL_TEST} ${LAST_TEST}
@${RM} ${NAME} ${REAL_TEST} ${LAST_TEST} libft_malloc.so

re: fclean all

Expand Down
71 changes: 71 additions & 0 deletions tester/correct.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/bash

# Color definition
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
BLUE="\e[34m"
PURPLE="\e[35m"
CYAN="\e[36m"
RESET="\e[0m"
# linux run script
LINUX_RUN=./real_test/run_linux.sh
# test source code
TEST0_SRC=real_test/test0.c
TEST1_SRC=real_test/test1.c
TEST2_SRC=real_test/test2.c
TEST3_SRC=real_test/test3.c
TEST4_SRC=real_test/test4.c
TEST5_SRC=real_test/test5.c

compite_test_with_lib() {
FILE_SRC=${1}
FILE_EXEC=${2}
echo -e "${CYAN}Compile ${FILE_SRC} ${RESET}"
gcc -o ${FILE_EXEC} ${FILE_SRC} -L. -lft_malloc
}

compile_real_test_get_minor() {
FILE_SRC=${1}
FILE_EXEC=${2}
echo -e "${YELLOW}Compile and test ${FILE_SRC} real malloc ${RESET}"
gcc -o ${FILE_EXEC} ${FILE_SRC} && /usr/bin/time -v ./${FILE_EXEC} 2> .tmp/out_${FILE_EXEC}
MALLOC_PAGE=$(cat .tmp/out_${FILE_EXEC} | grep Minor | rev | cut -d ' ' -f 1 | rev)
echo -e "${GREEN}${MALLOC_PAGE}${RESET}"
}

test_libft_malloc_get_minor() {
echo -e "${PURPLE}Test ${FILE_SRC} libft malloc ${RESET}"
${LINUX_RUN} /usr/bin/time -v ./${1} 2> .tmp/out_${1}
MALLOC_PAGE=$(cat .tmp/out_${1} | grep Minor | rev | cut -d ' ' -f 1 | rev)
echo -e "${RED}${MALLOC_PAGE}${RESET}"
}

test_libft_malloc() {
echo -e "${CYAN}Test ${FILE_SRC} libft malloc ${RESET}"
${LINUX_RUN} ./${1}
}
# TEST
mkdir -p .tmp
cp ../libft_malloc.so .

compile_real_test_get_minor ${TEST0_SRC} test0
compile_real_test_get_minor ${TEST1_SRC} test1

test_libft_malloc_get_minor test0
test_libft_malloc_get_minor test1

compite_test_with_lib ${TEST2_SRC} test2
test_libft_malloc_get_minor test2

compite_test_with_lib ${TEST3_SRC} test3
test_libft_malloc test3

# dev null cause implicit declaration of function ‘show_alloc_mem’
compite_test_with_lib ${TEST4_SRC} test4 2> /dev/null
test_libft_malloc test4

compite_test_with_lib ${TEST5_SRC} test5
test_libft_malloc test5

rm -rf .tmp test0 test1 test2 test3 test4 test5
4 changes: 4 additions & 0 deletions tester/real_test/run_linux.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
export LD_LIBRARY_PATH=. && export LD_PRELOAD=libft_malloc.so && export DYLD_FORCE_FLAT_NAMESPACE=1 && $@

# https://theevilbit.github.io/posts/dyld_insert_libraries_dylib_injection_in_macos_osx_deep_dive/
14 changes: 14 additions & 0 deletions tester/real_test/test0.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdlib.h>

int main(void)
{
int i;
char *addr;

i = 0;
while (i < 1024)
{
i++;
}
return (0);
}
28 changes: 28 additions & 0 deletions tester/real_test/test1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void print(char *s)
{
write(1, s, strlen(s));
}

int main(void)
{
int i;
char *addr;

i = 0;
while (i < 1024)
{
addr = (char*)malloc(1024);
if (addr == NULL)
{
print("Failed to allocate memory\n");
return (1);
}
addr[0] = 42;
i++;
}
return (0);
}
29 changes: 29 additions & 0 deletions tester/real_test/test2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void print(char *s)
{
write(1, s, strlen(s));
}

int main(void)
{
int i;
char *addr;

i = 0;
while (i < 1024)
{
addr = (char*)malloc(1024);
if (addr == NULL)
{
print("Failed to allocate memory\n");
return (1);
}
addr[0] = 42;
free(addr);
i++;
}
return (0);
}
41 changes: 41 additions & 0 deletions tester/real_test/test3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define M (1024 * 1024)

void print(char *s)
{
write(1, s, strlen(s));
}

int main()
{
char *addr1;
char *addr2;
char *addr3;

addr1 = (char*)malloc(16*M);
if (addr1 == NULL)
{
print("Failed to allocate memory\n");
exit(1);
}
strcpy(addr1, "Hello world!\n");
print(addr1);
addr2 = (char*)malloc(16*M);
if (addr2 == NULL)
{
print("Failed to allocate memory\n");
exit(1);
}
addr3 = (char*)realloc(addr1, 128*M);
if (addr3 == NULL)
{
print("Failed to reallocate memory\n");
exit(1);
}
addr3[127*M] = 42;
print(addr3);
return (0);
}
26 changes: 26 additions & 0 deletions tester/real_test/test4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# include "../include/malloc.h"

#define M (1024 * 1024)

int main()
{
void* a = malloc(1);
void* b = malloc(2);
void* c = malloc(4);
void* d = malloc(8);
void* e = malloc(16);
void* f = malloc(32);
void* g = malloc(64);
void* h = malloc(128);
void* i = malloc(256);
void* j = malloc(512);
void* k = malloc(1024);
void* l = malloc(1024 * 2);
void* m = malloc(1024 * 4);
void* n = malloc(1024 * 32);
void* o = malloc(M);
void* p = malloc(16*M);
void* q = malloc(128*M);
show_alloc_mem();
return (0);
}
35 changes: 35 additions & 0 deletions tester/real_test/test5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void print(char *s)
{
write(1, s, strlen(s));
}

int main()
{
int i;
int alignment;
char *addr;

i = 1;
alignment = 2 * sizeof(size_t);
while (i <= 100)
{
addr = (char*)malloc(i);
if (addr == NULL)
{
print("Failed to allocate memory\n");
exit(1);
}
if ((((unsigned long) (addr)) % alignment) != 0)
{
print("malloc returned a non aligned boundary\n");
exit(1);
}
i++;
free(addr);
}
print("Alignement OK\n");
}

0 comments on commit 52a9074

Please sign in to comment.