-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkernelMM.c
72 lines (58 loc) · 2.06 KB
/
kernelMM.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <errno.h>
#include <string.h>
#define PAGE_SIZE 4096
#define ALLOCATION_SIZE (100 * PAGE_SIZE)
void print_memory_info(const char *stage) {
FILE *statm = fopen("/proc/self/statm", "r");
if (statm == NULL) {
perror("Failed to open /proc/self/statm");
return;
}
long size, resident, share, text, lib, data, dt;
if (fscanf(statm, "%ld %ld %ld %ld %ld %ld %ld", &size, &resident, &share, &text, &lib, &data, &dt) != 7) {
fprintf(stderr, "Failed to read memory info\n");
fclose(statm);
return;
}
fclose(statm);
printf("%s:\n", stage);
printf(" Total program size: %ld pages\n", size);
printf(" Resident set size: %ld pages\n", resident);
printf(" Shared pages: %ld\n", share);
printf(" Text (code) pages: %ld\n", text);
printf(" Data/stack pages: %ld\n", data);
printf("\n");
}
int main() {
print_memory_info("Initial state");
// Allocate memory
void *ptr = mmap(NULL, ALLOCATION_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (ptr == MAP_FAILED) {
perror("mmap failed");
return 1;
}
print_memory_info("After allocation");
// Touch the memory to force page allocation
memset(ptr, 0, ALLOCATION_SIZE);
print_memory_info("After touching memory");
// Free the memory
if (munmap(ptr, ALLOCATION_SIZE) == -1) {
perror("munmap failed");
return 1;
}
print_memory_info("After freeing memory");
// Try to allocate memory in kernel space (this will fail)
void *kernel_ptr = mmap((void *)0xffffffff80000000, PAGE_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (kernel_ptr == MAP_FAILED) {
printf("Failed to allocate in kernel space as expected. Error: %s\n", strerror(errno));
} else {
printf("Unexpectedly succeeded in allocating kernel space memory at %p\n", kernel_ptr);
munmap(kernel_ptr, PAGE_SIZE);
}
return 0;
}