Skip to content

Instantly share code, notes, and snippets.

@hagai-lvi
Last active August 29, 2015 14:22
Show Gist options
  • Save hagai-lvi/7ef23de3c4ce2b476c72 to your computer and use it in GitHub Desktop.
Save hagai-lvi/7ef23de3c4ce2b476c72 to your computer and use it in GitHub Desktop.
#include "types.h"
#include "user.h"
#define PGSIZE 4096
#define ASSERT(cond, ...) \
if(!cond){ \
printf(2, "FAIL at %s:%d - ", __FUNCTION__, __LINE__); \
printf(2, "\n"); \
exit(); \
}
int do_malloc() {
malloc(100 * PGSIZE);
return 0;
}
int multiple_pgdir_test() {
malloc(10 * 1024 * PGSIZE);
return -1;
}
int fork_test() {
#define ARR_SIZE 5
int numOfProcs = 10;
int i = 0;
int j = 0;
int pid;
printf(1, "calling malloc\n");
int * arr = malloc(4 * ARR_SIZE);
for (i = 0; i < ARR_SIZE; i++) {
arr[i] = i;
}
for (i = 0; i < numOfProcs; i++) {
printf(1, "forking: %d\n", i);
if ((pid = fork()) == 0) {
for (j = 0; j < ARR_SIZE; j++) {
ASSERT(arr[j] == j);
}
exit();
} else {
ASSERT(wait() == pid);
}
}
return 0;
}
void recursion(int n) {
int i = 1;
printf(1, "%d\n", n);
i++;
recursion(n + 1);
}
int stack_overflow() {
int pid;
if ((pid = fork()) == 0) {
recursion(0);
} else {
ASSERT(wait() == pid);
}
return 0;
}
int malloc_to_kernel() {
unsigned int gb = 1024 * 1024 * 1024;
ASSERT(malloc(gb) != 0);
ASSERT(malloc(gb + 100) == 0);
return 0;
}
int
main(int argc, char *argv[]) {
// do_malloc();
// multiple_pgdir_test();
fork_test();
stack_overflow();
malloc_to_kernel();
exit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment