From c4c4ae462b090386641a7018d39334abb15288dd Mon Sep 17 00:00:00 2001 From: "julien.cohen-scali" Date: Tue, 9 Apr 2024 12:52:26 +0200 Subject: [PATCH 1/6] Add features to malloc playground --- malloc_playground.c | 105 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 92 insertions(+), 13 deletions(-) diff --git a/malloc_playground.c b/malloc_playground.c index 2ad245f..a89a8a6 100644 --- a/malloc_playground.c +++ b/malloc_playground.c @@ -21,31 +21,108 @@ void report_mcheck_fail(enum mcheck_status s) } #endif + +#define MAX_PTR_NUM 20; + +char **ptrArray; + +int mystrlen(char *s){ + int res; + while (s && *s != 0x0a){ + res += 1; + s += 1; + } + s -= res; + return res; +} + + int main(int argc, char ** argv) { + int ptrNumber = -1; + int maxPtr = MAX_PTR_NUM; fprintf(stderr, "pid: %d\n", getpid()); - + ptrArray = malloc(sizeof(char*) * 20); + for (int i = 0; i < maxPtr; i++){ + ptrArray[i] = 0; + } char buffer[1000]; while (1) { fprintf(stderr, "> "); fgets(buffer, sizeof(buffer), stdin); char cmd[1000]; - intptr_t arg1, arg2; - int num = sscanf(buffer, "%s %"SCNiPTR" %"SCNiPTR, cmd, &arg1, &arg2); + char arg1[100] = {0}; + char arg2[100] = {0}; + printf("%s\n", buffer); + int num = sscanf(buffer, "%s %s %s\n", cmd, arg1, arg2); + printf("%d\n", num); if (strcmp(cmd, "malloc") == 0) { - void* result = malloc(arg1); + int tmpArg = atoi((const char *) &arg1); + void *result = malloc(tmpArg); + ptrNumber++; + ptrArray[ptrNumber] = result; + strcpy(result, "none"); fprintf(stderr, "==> %p\n", result); } else if (strcmp(cmd, "free") == 0) { - free((void*) arg1); - fprintf(stderr, "==> ok\n"); - } else if (strcmp(cmd, "show") == 0) { - if (num == 2) { - arg2 = 1; + if (num == 1){ + free((void*) ptrArray[ptrNumber]); + fprintf(stderr, "==> ok\n"); + ptrNumber -= 1; + } + else if (num == 2){ + if (ptrNumber > -1){ + int tmpArg = atoi((const char *) &arg1); + ptrArray[tmpArg] = 0; + free((void *) ptrArray[tmpArg]); + ptrNumber -= 1; + } + else{ + printf("List Empty :/"); + } + } + } else if (strcmp(cmd, "write") == 0) { + if (num == 1){ + printf("write: write value [pointer index]\n"); + } + else if (num == 2){ + int len = strlen((const char *) &arg1); + strcpy(ptrArray[ptrNumber], (const char *) &arg1); + fprintf(stderr, "==> ok, wrote %s\n", ptrArray[ptrNumber]); + } + else if (num == 3){ + int len = strlen((const char *) &arg1); + int tmpArg2 = atoi((const char *) &arg2); + if (tmpArg2 > ptrNumber){ + strcpy(ptrArray[tmpArg2], (const char *) &arg1); + fprintf(stderr, "==> ok, wrote %s\n", ptrArray[tmpArg2]); + } + else{ + printf("Invalid Index\n"); + } } - long * src = (long*) arg1; - for (int i = 0; i < arg2; i++) { - fprintf(stderr, "%p: %#16.0lx\n", &src[i], src[i]); + } else if (strcmp(cmd, "listp") == 0) { + int tmpIndex = 0; + printf("\n"); + while(ptrArray[tmpIndex]){ + printf("%d - %p - %s\n", tmpIndex, ptrArray[tmpIndex], ptrArray[tmpIndex]); + tmpIndex++; } + fprintf(stderr, "==> ok\n"); + } else if (strcmp(cmd, "listpall") == 0) { + int tmpIndex = 0; + printf("\n"); + for (int i=0; i < maxPtr; i++){ + printf("%d - %p - %s\n", tmpIndex, ptrArray[tmpIndex], ptrArray[tmpIndex]); + tmpIndex++; + } + fprintf(stderr, "==> ok\n"); + } else if (strcmp(cmd, "clearArray") == 0) { + ptrNumber = -1; + for (int i = 0; i < maxPtr; i++){ + free(ptrArray[i]); + ptrArray[i] = 0; + } + fprintf(stderr, "==> ok, array cleared\n"); #ifdef __GLIBC__ } else if (strcmp(cmd, "usable") == 0) { fprintf(stderr, "usable size: %zu\n", malloc_usable_size((void*) arg1)); @@ -53,6 +130,7 @@ int main(int argc, char ** argv) { malloc_stats(); } else if (strcmp(cmd, "info") == 0) { malloc_info(0, stdout); + printf("Ptrptr %d\n", ptrNumber); } else if (strcmp(cmd, "mcheck") == 0) { fprintf(stderr, "==> %s\n", mcheck(report_mcheck_fail) == 0 ? "OK" : "ERROR"); } else if (strcmp(cmd, "mcheck_pedantic") == 0) { @@ -66,7 +144,8 @@ int main(int argc, char ** argv) { } #endif } else { - puts("Commands: malloc n, free p, show p [n], usable p, stats, info, mprobe [p], mcheck, mcheck_pedantic"); + puts("Commands: malloc n, free p, usable p, stats, info, mprobe [p], mcheck, mcheck_pedantic, "); + puts("Commands: [BETA] write str, listp, listpall, clearArray\n"); } } } From 20c6c95cb38e11aed5d512ce5484415d453e31a9 Mon Sep 17 00:00:00 2001 From: "julien.cohen-scali" Date: Tue, 9 Apr 2024 12:55:10 +0200 Subject: [PATCH 2/6] Removed Junks --- malloc_playground.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/malloc_playground.c b/malloc_playground.c index a89a8a6..ebbc39b 100644 --- a/malloc_playground.c +++ b/malloc_playground.c @@ -26,17 +26,6 @@ void report_mcheck_fail(enum mcheck_status s) char **ptrArray; -int mystrlen(char *s){ - int res; - while (s && *s != 0x0a){ - res += 1; - s += 1; - } - s -= res; - return res; -} - - int main(int argc, char ** argv) { int ptrNumber = -1; @@ -53,16 +42,14 @@ int main(int argc, char ** argv) { char cmd[1000]; char arg1[100] = {0}; char arg2[100] = {0}; - printf("%s\n", buffer); int num = sscanf(buffer, "%s %s %s\n", cmd, arg1, arg2); - printf("%d\n", num); if (strcmp(cmd, "malloc") == 0) { int tmpArg = atoi((const char *) &arg1); void *result = malloc(tmpArg); ptrNumber++; ptrArray[ptrNumber] = result; strcpy(result, "none"); - fprintf(stderr, "==> %p\n", result); + fprintf(stderr, "==> OK, %p\n", result); } else if (strcmp(cmd, "free") == 0) { if (num == 1){ free((void*) ptrArray[ptrNumber]); From 203a4e6ba19be70e33596cb6a1c1c67bbf5496bc Mon Sep 17 00:00:00 2001 From: "julien.cohen-scali" Date: Wed, 5 Jun 2024 14:31:33 +0200 Subject: [PATCH 3/6] Corrected misimplementation of listp and listpall --- malloc_playground.c | 59 ++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/malloc_playground.c b/malloc_playground.c index ebbc39b..e4c7170 100644 --- a/malloc_playground.c +++ b/malloc_playground.c @@ -27,32 +27,47 @@ void report_mcheck_fail(enum mcheck_status s) char **ptrArray; int main(int argc, char ** argv) { - + int num; int ptrNumber = -1; int maxPtr = MAX_PTR_NUM; + char sizeTable[maxPtr]; + + char buffer[1000]; + char cmd[1000]; + char arg1[100]; + char arg2[100]; + + memset(sizeTable, 0, maxPtr); + memset(cmd, 0, 1000); + memset(arg1, 0, 100); + memset(arg2, 0, 100); + fprintf(stderr, "pid: %d\n", getpid()); ptrArray = malloc(sizeof(char*) * 20); for (int i = 0; i < maxPtr; i++){ ptrArray[i] = 0; } - char buffer[1000]; while (1) { fprintf(stderr, "> "); fgets(buffer, sizeof(buffer), stdin); - char cmd[1000]; - char arg1[100] = {0}; - char arg2[100] = {0}; - int num = sscanf(buffer, "%s %s %s\n", cmd, arg1, arg2); + num = sscanf(buffer, "%s %s %s\n", cmd, arg1, arg2); if (strcmp(cmd, "malloc") == 0) { - int tmpArg = atoi((const char *) &arg1); - void *result = malloc(tmpArg); - ptrNumber++; - ptrArray[ptrNumber] = result; - strcpy(result, "none"); - fprintf(stderr, "==> OK, %p\n", result); + if (ptrNumber < maxPtr){ + int sizeArg = atoi((const char *) &arg1); + void *result = malloc(sizeArg); + ptrNumber++; + sizeTable[ptrNumber] = sizeArg; + ptrArray[ptrNumber] = result; + strcpy(result, "none"); + fprintf(stderr, "==> OK, %p\n", result); + } + else{ + printf("Max pointer reached, free or restart"); + } } else if (strcmp(cmd, "free") == 0) { if (num == 1){ free((void*) ptrArray[ptrNumber]); + ptrArray[ptrNumber] = 0; fprintf(stderr, "==> ok\n"); ptrNumber -= 1; } @@ -79,27 +94,27 @@ int main(int argc, char ** argv) { else if (num == 3){ int len = strlen((const char *) &arg1); int tmpArg2 = atoi((const char *) &arg2); - if (tmpArg2 > ptrNumber){ + //if (tmpArg2 > ptrNumber){ strcpy(ptrArray[tmpArg2], (const char *) &arg1); fprintf(stderr, "==> ok, wrote %s\n", ptrArray[tmpArg2]); - } - else{ - printf("Invalid Index\n"); - } + //} + //else{ + // printf("Invalid Index\n"); + //} } } else if (strcmp(cmd, "listp") == 0) { - int tmpIndex = 0; printf("\n"); - while(ptrArray[tmpIndex]){ - printf("%d - %p - %s\n", tmpIndex, ptrArray[tmpIndex], ptrArray[tmpIndex]); - tmpIndex++; + for (int i = 0; i < 20; i++){ + if (ptrArray[i]){ + printf("%d - %p - %s - %d\n", i, ptrArray[i], ptrArray[i], sizeTable[i]); + } } fprintf(stderr, "==> ok\n"); } else if (strcmp(cmd, "listpall") == 0) { int tmpIndex = 0; printf("\n"); for (int i=0; i < maxPtr; i++){ - printf("%d - %p - %s\n", tmpIndex, ptrArray[tmpIndex], ptrArray[tmpIndex]); + printf("%d - %p - %s - %d\n", tmpIndex, ptrArray[tmpIndex], ptrArray[tmpIndex], sizeTable[i]); tmpIndex++; } fprintf(stderr, "==> ok\n"); From 07a9b6c144bfb166041851559f5c89fba245fe71 Mon Sep 17 00:00:00 2001 From: "julien.cohen-scali" Date: Wed, 5 Jun 2024 15:12:51 +0200 Subject: [PATCH 4/6] Corrected free to remove size in sizeTable --- malloc_playground.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/malloc_playground.c b/malloc_playground.c index e4c7170..3541898 100644 --- a/malloc_playground.c +++ b/malloc_playground.c @@ -65,23 +65,27 @@ int main(int argc, char ** argv) { printf("Max pointer reached, free or restart"); } } else if (strcmp(cmd, "free") == 0) { - if (num == 1){ - free((void*) ptrArray[ptrNumber]); - ptrArray[ptrNumber] = 0; - fprintf(stderr, "==> ok\n"); - ptrNumber -= 1; - } - else if (num == 2){ - if (ptrNumber > -1){ + if (ptrNumber > -1){ + if (num == 1){ + free((void*) ptrArray[ptrNumber]); + ptrArray[ptrNumber] = 0; + sizeTable[ptrNumber] = 0; + fprintf(stderr, "==> ok\n"); + ptrNumber -= 1; + } + else if (num == 2){ int tmpArg = atoi((const char *) &arg1); ptrArray[tmpArg] = 0; + sizeTable[tmpArg] = 0; free((void *) ptrArray[tmpArg]); ptrNumber -= 1; - } - else{ - printf("List Empty :/"); + fprintf(stderr, "==> ok\n"); } } + else{ + fprintf(stderr, "==> list empty :/\n"); + + } } else if (strcmp(cmd, "write") == 0) { if (num == 1){ printf("write: write value [pointer index]\n"); @@ -150,4 +154,4 @@ int main(int argc, char ** argv) { puts("Commands: [BETA] write str, listp, listpall, clearArray\n"); } } -} +} \ No newline at end of file From ec918ea069ebd6b8da48c009141590600c4f054c Mon Sep 17 00:00:00 2001 From: "julien.cohen-scali" Date: Wed, 5 Jun 2024 15:14:50 +0200 Subject: [PATCH 5/6] Added clear of sizeTable in clearlist --- malloc_playground.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/malloc_playground.c b/malloc_playground.c index 3541898..a6ab252 100644 --- a/malloc_playground.c +++ b/malloc_playground.c @@ -122,11 +122,12 @@ int main(int argc, char ** argv) { tmpIndex++; } fprintf(stderr, "==> ok\n"); - } else if (strcmp(cmd, "clearArray") == 0) { + } else if (strcmp(cmd, "clearlist") == 0) { ptrNumber = -1; for (int i = 0; i < maxPtr; i++){ free(ptrArray[i]); ptrArray[i] = 0; + memset(sizeTable, 0, maxPtr); } fprintf(stderr, "==> ok, array cleared\n"); #ifdef __GLIBC__ @@ -151,7 +152,7 @@ int main(int argc, char ** argv) { #endif } else { puts("Commands: malloc n, free p, usable p, stats, info, mprobe [p], mcheck, mcheck_pedantic, "); - puts("Commands: [BETA] write str, listp, listpall, clearArray\n"); + puts("Commands: [BETA] write str, listp, listpall, clearlist\n"); } } } \ No newline at end of file From 5a082a40624e817f05eabbc0decacbb9c8e67cae Mon Sep 17 00:00:00 2001 From: "julien.cohen-scali" Date: Wed, 5 Jun 2024 15:20:42 +0200 Subject: [PATCH 6/6] Corrected Bugs --- malloc_playground.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/malloc_playground.c b/malloc_playground.c index a6ab252..86dd626 100644 --- a/malloc_playground.c +++ b/malloc_playground.c @@ -30,6 +30,7 @@ int main(int argc, char ** argv) { int num; int ptrNumber = -1; int maxPtr = MAX_PTR_NUM; + int sizeArg; char sizeTable[maxPtr]; char buffer[1000]; @@ -53,7 +54,7 @@ int main(int argc, char ** argv) { num = sscanf(buffer, "%s %s %s\n", cmd, arg1, arg2); if (strcmp(cmd, "malloc") == 0) { if (ptrNumber < maxPtr){ - int sizeArg = atoi((const char *) &arg1); + sizeArg = atoi((const char *) &arg1); void *result = malloc(sizeArg); ptrNumber++; sizeTable[ptrNumber] = sizeArg;