mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
start moving malloc and friends in
This commit is contained in:
@@ -3,14 +3,14 @@
|
||||
#include <lkmc.h>
|
||||
|
||||
LKMC_PROLOGUE
|
||||
mov $0, %rax
|
||||
mov $1, %rbx
|
||||
mov $0, %r12
|
||||
mov $1, %r13
|
||||
|
||||
xchg %rbx, %rax
|
||||
LKMC_ASSERT_EQ(%rax, $1)
|
||||
LKMC_ASSERT_EQ(%rbx, $0)
|
||||
xchg %r13, %r12
|
||||
LKMC_ASSERT_EQ(%r12, $1)
|
||||
LKMC_ASSERT_EQ(%r13, $0)
|
||||
|
||||
xchg %rbx, %rax
|
||||
LKMC_ASSERT_EQ(%rax, $0)
|
||||
LKMC_ASSERT_EQ(%rbx, $1)
|
||||
xchg %r13, %r12
|
||||
LKMC_ASSERT_EQ(%r12, $0)
|
||||
LKMC_ASSERT_EQ(%r13, $1)
|
||||
LKMC_EPILOGUE
|
||||
|
||||
22
userland/c/malloc.c
Normal file
22
userland/c/malloc.c
Normal file
@@ -0,0 +1,22 @@
|
||||
/* https://cirosantilli.com/linux-kernel-module-cheat#malloc */
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void) {
|
||||
size_t bytes = sizeof(int) * 2;
|
||||
/* Allocate 2 ints. */
|
||||
int *is = malloc(bytes);
|
||||
/* This can happen for example if we ask for too much memory. */
|
||||
if (is == NULL) {
|
||||
perror("malloc");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
is[0] = 1;
|
||||
assert(is[0] == 1);
|
||||
/* Free the allocated memory. */
|
||||
free(is);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Let's see how much memory Linux lets us allocate. */
|
||||
/* https://cirosantilli.com/linux-kernel-module-cheat#malloc */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// https://github.com/cirosantilli/linux-kernel-module-cheat#cpp-multithreading
|
||||
// https://cirosantilli.com/linux-kernel-module-cheat#cpp-multithreading
|
||||
//
|
||||
// The non-atomic counters have undefined values which get printed:
|
||||
// they are extremely likely to be less than the correct value due to
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Count to infinity sleeping one second per number.
|
||||
//
|
||||
// https://github.com/cirosantilli/linux-kernel-module-cheat#cpp-multithreading
|
||||
// https://cirosantilli.com/linux-kernel-module-cheat#cpp-multithreading
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
1
userland/glibc/README.adoc
Normal file
1
userland/glibc/README.adoc
Normal file
@@ -0,0 +1 @@
|
||||
This directory contains glibc extensions to POSIX / ANSI C.
|
||||
24
userland/glibc/brk.c
Normal file
24
userland/glibc/brk.c
Normal file
@@ -0,0 +1,24 @@
|
||||
/* https://cirosantilli.com/linux-kernel-module-cheat#brk */
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void) {
|
||||
void *b = sbrk(0);
|
||||
int *p = (int *)b;
|
||||
|
||||
/* Move it 2 ints forward */
|
||||
brk(p + 2);
|
||||
|
||||
/* Use the ints. */
|
||||
*p = 1;
|
||||
*(p + 1) = 2;
|
||||
assert(*p == 1);
|
||||
assert(*(p + 1) == 2);
|
||||
|
||||
/* Deallocate back. */
|
||||
brk(b);
|
||||
|
||||
return 0;
|
||||
}
|
||||
1
userland/glibc/build
Symbolic link
1
userland/glibc/build
Symbolic link
@@ -0,0 +1 @@
|
||||
../build
|
||||
1
userland/glibc/test
Symbolic link
1
userland/glibc/test
Symbolic link
@@ -0,0 +1 @@
|
||||
../test
|
||||
@@ -1,4 +1,4 @@
|
||||
/* https://github.com/cirosantilli/linux-kernel-module-cheat#sysconf */
|
||||
/* https://cirosantilli.com/linux-kernel-module-cheat#sysconf */
|
||||
|
||||
#define _XOPEN_SOURCE 700
|
||||
#include <stdio.h>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* Count to infinity with 1 second sleep between each increment.
|
||||
*
|
||||
* https://github.com/cirosantilli/linux-kernel-module-cheat#unistd-h
|
||||
* https://cirosantilli.com/linux-kernel-module-cheat#unistd-h
|
||||
*
|
||||
* Sample application: https://cirosantilli.com/linux-kernel-module-cheat#gdb-step-debug-userland-custom-init
|
||||
*/
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* Count up to a given number 1 second sleep between each increment.
|
||||
*
|
||||
* https://github.com/cirosantilli/linux-kernel-module-cheat#unistd-h
|
||||
* https://cirosantilli.com/linux-kernel-module-cheat#unistd-h
|
||||
*
|
||||
* We need a separate program for this from count.c because count.c
|
||||
* is also usable as an init process, where we can't control the CLI
|
||||
|
||||
118
userland/posix/mmap_file.c
Normal file
118
userland/posix/mmap_file.c
Normal file
@@ -0,0 +1,118 @@
|
||||
/* https://cirosantilli.com/linux-kernel-module-cheat#mmap
|
||||
*
|
||||
* Example of mmap on files.
|
||||
*
|
||||
* Create a file, mmap to it, write to maped memory, close.
|
||||
*
|
||||
* Then read the file and confirm it was written to.
|
||||
*
|
||||
* Implemented in Linux by the mmap syscall.
|
||||
*/
|
||||
|
||||
#define _XOPEN_SOURCE 700
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <lkmc.h>
|
||||
|
||||
int main(void) {
|
||||
char *filepath = LKMC_TMP_FILE;
|
||||
enum Constexpr { NUMINTS = 4 };
|
||||
size_t filesize = NUMINTS * sizeof(int);
|
||||
|
||||
int i;
|
||||
int fd;
|
||||
int result;
|
||||
/* mmapped array of int's */
|
||||
int *map;
|
||||
|
||||
/* Write to file with mmap. */
|
||||
{
|
||||
/* `O_WRONLY` is not sufficient when mmaping, need `O_RDWR`.*/
|
||||
fd = open(filepath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
|
||||
if (fd == -1) {
|
||||
perror("open");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Set fd position and write to it to strech the file. */
|
||||
result = lseek(fd, filesize - 1, SEEK_SET);
|
||||
if (result == -1) {
|
||||
close(fd);
|
||||
perror("lseek");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Write something to the file to actually strech it. */
|
||||
result = write(fd, "", 1);
|
||||
if (result != 1) {
|
||||
close(fd);
|
||||
perror("write");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Do the actual mapping call. */
|
||||
map = mmap(NULL, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (map == MAP_FAILED) {
|
||||
close(fd);
|
||||
perror("mmap");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Write int's to the file as if it were memory because MAP_SHARED was used. */
|
||||
for (i = 0; i < NUMINTS; ++i) {
|
||||
map[i] = i;
|
||||
}
|
||||
|
||||
/* Free mmapped memory. */
|
||||
if (munmap(map, filesize) == -1) {
|
||||
perror("munmap");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Un-mmaping doesn't close the file, so we still need to do that. */
|
||||
if (close(fd) == -1) {
|
||||
perror("close");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
/* Read result back in. */
|
||||
{
|
||||
fd = open(filepath, O_RDONLY, 0);
|
||||
if (fd == -1) {
|
||||
perror("open");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
map = mmap(0, filesize, PROT_READ, MAP_SHARED, fd, 0);
|
||||
if (map == MAP_FAILED) {
|
||||
close(fd);
|
||||
perror("mmap");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
assert(map[1] == 1);
|
||||
|
||||
/* Segmentation fault because no `PROT_WRITE`: */
|
||||
/*map[1] = 2;*/
|
||||
|
||||
if (munmap(map, filesize) == -1) {
|
||||
perror("munmap");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (close(fd) == -1) {
|
||||
perror("close");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/* count to infinity in n threads.
|
||||
*
|
||||
* https://github.com/cirosantilli/linux-kernel-module-cheat#pthreads
|
||||
* https://cirosantilli.com/linux-kernel-module-cheat#pthreads
|
||||
*
|
||||
* Useful if you need to keep several threads around
|
||||
* to test something.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* Let's see a trivial deadlock in action to feel the joys of multithreading.
|
||||
*
|
||||
* https://github.com/cirosantilli/linux-kernel-module-cheat#pthreads
|
||||
* https://cirosantilli.com/linux-kernel-module-cheat#pthreads
|
||||
*
|
||||
* Exit successfully immediately without any arguments:
|
||||
*
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/* Spawn N threads that print their TID with pthread_self and other
|
||||
* ID-like information for multiple threads.
|
||||
*
|
||||
* https://github.com/cirosantilli/linux-kernel-module-cheat#pthreads
|
||||
* https://cirosantilli.com/linux-kernel-module-cheat#pthreads
|
||||
*
|
||||
* Sample usage:
|
||||
*
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* https://github.com/cirosantilli/linux-kernel-module-cheat#sysconf */
|
||||
/* https://cirosantilli.com/linux-kernel-module-cheat#sysconf */
|
||||
|
||||
#define _XOPEN_SOURCE 700
|
||||
#include <stdio.h>
|
||||
|
||||
Reference in New Issue
Block a user