Compare commits

...

2 Commits

Author SHA1 Message Date
Mathieu Maret bee58d9642 Add a (bad)free in userspace 2023-11-11 00:08:08 +01:00
Mathieu Maret 1120b40655 Rename Yolo syscall to Helo 2023-11-11 00:07:26 +01:00
6 changed files with 40 additions and 22 deletions

View File

@ -22,8 +22,8 @@ int syscallExecute(int syscallId, const struct cpu_state *userCtx)
assert(0); assert(0);
break; break;
} }
case SYSCALL_ID_YOLO: case SYSCALL_ID_HELO:
ret = printf("YOLO FROM USERSPACE\n"); ret = printf("HELLO FROM USERSPACE\n");
break; break;
case SYSCALL_ID_PUTC: { case SYSCALL_ID_PUTC: {
unsigned int c; unsigned int c;

View File

@ -4,7 +4,7 @@
#endif #endif
#define SYSCALL_ID_EXIT 1 #define SYSCALL_ID_EXIT 1
#define SYSCALL_ID_YOLO 2 #define SYSCALL_ID_HELO 2
#define SYSCALL_ID_PUTC 3 #define SYSCALL_ID_PUTC 3
#define SYSCALL_ID_READ 4 #define SYSCALL_ID_READ 4
#define SYSCALL_ID_TEST 5 #define SYSCALL_ID_TEST 5

View File

@ -399,7 +399,7 @@ static void userProgramm()
"int %5\n" "int %5\n"
"movl %%eax, %0" "movl %%eax, %0"
: "=g"(ret) : "=g"(ret)
: "g"(SYSCALL_ID_YOLO), "g"(0), "g"(0), "g"(0), "i"(SYSCALL_INTR_NB) : "g"(SYSCALL_ID_HELO), "g"(0), "g"(0), "g"(0), "i"(SYSCALL_INTR_NB)
: "eax", "ebx", "ecx", "edx"); : "eax", "ebx", "ecx", "edx");
asm volatile("movl %1,%%eax \n" asm volatile("movl %1,%%eax \n"
"movl %2,%%ebx \n" "movl %2,%%ebx \n"

View File

@ -4,7 +4,7 @@
#endif #endif
#define SYSCALL_ID_EXIT 1 #define SYSCALL_ID_EXIT 1
#define SYSCALL_ID_YOLO 2 #define SYSCALL_ID_HELO 2
#define SYSCALL_ID_PUTC 3 #define SYSCALL_ID_PUTC 3
#define SYSCALL_ID_READ 4 #define SYSCALL_ID_READ 4
#define SYSCALL_ID_TEST 5 #define SYSCALL_ID_TEST 5

View File

@ -443,25 +443,28 @@ int vasprintf(char **strp, const char *fmt, va_list ap)
{ {
int n = 0; int n = 0;
size_t size = 0; size_t size = 0;
char *p = malloc(256); char *p = malloc(256);
/* Determine required size */ /* Determine required size */
n = vsnprintf(p, size, fmt, ap); n = vsnprintf(p, size, fmt, ap);
if (n < 0) if (n < 0){
free(p);
return -1; return -1;
}
/* One extra byte for '\0' */ /* One extra byte for '\0' */
size = min(256U,(size_t)n + 1); size = min(256U, (size_t)n + 1);
n = vsnprintf(p, size, fmt, ap); n = vsnprintf(p, size, fmt, ap);
if (n < 0) { if (n < 0) {
free(p);
return -1; return -1;
} }
*strp = p; *strp = p;
return size; return size;
} }
@ -521,9 +524,9 @@ int putc(const int c){
return syscall1(SYSCALL_ID_PUTC, c); return syscall1(SYSCALL_ID_PUTC, c);
} }
void yolo() void helo()
{ {
syscall0(SYSCALL_ID_YOLO); syscall0(SYSCALL_ID_HELO);
} }
int testSycall5(uint arg1, uint arg2, uint arg3, uint arg4, uint arg5) int testSycall5(uint arg1, uint arg2, uint arg3, uint arg4, uint arg5)
@ -571,23 +574,38 @@ void *brk(void *addr)
return (void *)syscall1(SYSCALL_ID_BRK, (unsigned int)addr); return (void *)syscall1(SYSCALL_ID_BRK, (unsigned int)addr);
} }
static char *heapTop = 0;
static char *heapFree = 0;
static char *lastAlloc = 0;
void *malloc(size_t size) void *malloc(size_t size)
{ {
void *heapTop = 0;
static void *free = 0;
if (heapTop == 0) { if (heapTop == 0) {
heapTop = brk(0); heapTop = heapFree = brk(0);
free = heapTop;
} else { } else {
heapTop = brk(0); heapTop = brk(0);
} }
if (free + size > heapTop) { if (heapFree + size + sizeof(size) > heapTop) {
if (brk(heapTop + size)) if (brk(heapTop + size + sizeof(size)))
return NULL; return NULL;
} }
free += size; *((size_t *)heapFree) = size;
return (free - size); heapFree += sizeof(size);
lastAlloc = heapFree;
heapFree += size;
return lastAlloc;
}
void free(void *ptr)
{
void *size_addr = ((char *)ptr - sizeof(size_t));
size_t size = *(size_t *)size_addr;
if (heapFree - size == ptr) {
heapFree = size_addr;
}
//TODO ELSE
return;
} }

View File

@ -28,7 +28,6 @@ int vsnprintf(char *str, size_t size, const char *format, va_list ap) __attribut
int vprintf(const char *format, va_list ap) __attribute__ ((__format__ (printf, 1, 0))); int vprintf(const char *format, va_list ap) __attribute__ ((__format__ (printf, 1, 0)));
int printf(const char *format, ...) __attribute__ ((__format__ (printf, 1, 2))); int printf(const char *format, ...) __attribute__ ((__format__ (printf, 1, 2)));
// Could be used after malloc is available
int asprintf(char **strp, const char *fmt, ...) __attribute__ ((__format__ (printf, 2, 3))); int asprintf(char **strp, const char *fmt, ...) __attribute__ ((__format__ (printf, 2, 3)));
int vasprintf(char **strp, const char *fmt, va_list ap) __attribute__ ((__format__ (printf, 2, 0))); int vasprintf(char **strp, const char *fmt, va_list ap) __attribute__ ((__format__ (printf, 2, 0)));
@ -42,10 +41,11 @@ int syscall1(int id, unsigned int arg1);
int syscall0(int id); int syscall0(int id);
void _exit(int status); void _exit(int status);
void yolo(); void helo();
int testSycall5(uint arg1, uint arg2, uint arg3, uint arg4, uint arg5); int testSycall5(uint arg1, uint arg2, uint arg3, uint arg4, uint arg5);
char readc(); char readc();
char getchar(); char getchar();
int readline(char *buf, int size); int readline(char *buf, int size);
void *brk(void *addr); void *brk(void *addr);
void *malloc(size_t size); void *malloc(size_t size);
void free(void *ptr);