-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ycaro02
committed
Feb 22, 2024
1 parent
9a343d9
commit 52a9074
Showing
10 changed files
with
256 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |