mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-29 21:14:27 +01:00
mmap anonymous
This commit is contained in:
44
README.adoc
44
README.adoc
@@ -12535,11 +12535,26 @@ Programs under link:userland/c/[] are examples of https://en.wikipedia.org/wiki/
|
|||||||
|
|
||||||
Allocate memory! Vs using the stack: https://stackoverflow.com/questions/4584089/what-is-the-function-of-the-push-pop-instructions-used-on-registers-in-x86-ass/33583134#33583134
|
Allocate memory! Vs using the stack: https://stackoverflow.com/questions/4584089/what-is-the-function-of-the-push-pop-instructions-used-on-registers-in-x86-ass/33583134#33583134
|
||||||
|
|
||||||
* link:userland/c/malloc.c[]: `malloc` hello world: allocate two ints and use them.
|
link:userland/c/malloc.c[]: `malloc` hello world: allocate two ints and use them.
|
||||||
* link:userland/c/out_of_memory.c[]: test how much memory Linux lets us allocate
|
|
||||||
|
|
||||||
LInux 5.1 / glibc 2.29 implements it with the <<mmap,`mmap` system call>>.
|
LInux 5.1 / glibc 2.29 implements it with the <<mmap,`mmap` system call>>.
|
||||||
|
|
||||||
|
===== malloc out o fmemory
|
||||||
|
|
||||||
|
Test how much memory Linux lets us allocate:
|
||||||
|
|
||||||
|
....
|
||||||
|
./run --userland userland/c/out_of_memory.c
|
||||||
|
....
|
||||||
|
|
||||||
|
Source: link:userland/c/out_of_memory.c[]
|
||||||
|
|
||||||
|
Outcome at c03d5d18ea971ae85d008101528d84c2ff25eb27 on Ubuntu 19.04 <<p51>> host: prints up to `0x1000000000` (64GiB).
|
||||||
|
|
||||||
|
TODO dive into source code.
|
||||||
|
|
||||||
|
Bibliography: https://stackoverflow.com/questions/2798330/maximum-memory-which-malloc-can-allocate
|
||||||
|
|
||||||
==== GCC C extensions
|
==== GCC C extensions
|
||||||
|
|
||||||
===== C empty struct
|
===== C empty struct
|
||||||
@@ -12638,19 +12653,36 @@ getconf -a
|
|||||||
|
|
||||||
==== mmap
|
==== mmap
|
||||||
|
|
||||||
The mmap system call allows advanced memory operations:
|
The mmap system call allows advanced memory operations.
|
||||||
|
|
||||||
* link:userland/posix/mmap_file.c[]: memory mapped file example
|
|
||||||
|
|
||||||
mmap is notably used to implement the <<malloc,malloc ANSI C>> function, replacing the previously used break system call.
|
mmap is notably used to implement the <<malloc,malloc ANSI C>> function, replacing the previously used break system call.
|
||||||
|
|
||||||
Linux adds has several POSIX extension flags to it.
|
Linux adds has several POSIX extension flags to it.
|
||||||
|
|
||||||
|
[[mmap-map-anonymous]]
|
||||||
|
===== mmap MAP_ANONYMOUS
|
||||||
|
|
||||||
|
Basic `mmap` example, do the same as link:userland/c/malloc.c[], but with `mmap`.
|
||||||
|
|
||||||
|
Example: userland/linux/mmap_anonymous.c[]
|
||||||
|
|
||||||
|
In POSIX 7 mmap always maps to a file.
|
||||||
|
|
||||||
|
If we add the MAP_ANONYMOUS Linux extension however, this is not required, and mmap can be used to allocate memory like malloc.
|
||||||
|
|
||||||
|
===== mmap file
|
||||||
|
|
||||||
|
Memory mapped file example: link:userland/posix/mmap_file.c[]
|
||||||
|
|
||||||
|
The example creates a file, mmaps to it, writes to maped memory, and then closes the file.
|
||||||
|
|
||||||
|
We then read the file and confirm it was written to.
|
||||||
|
|
||||||
===== brk
|
===== brk
|
||||||
|
|
||||||
Previously <<posix>>, but was deprecated in favor of <<malloc>>
|
Previously <<posix>>, but was deprecated in favor of <<malloc>>
|
||||||
|
|
||||||
Example: link:userland/glibc/brk.c[]
|
Example: link:userland/linux/brk.c[]
|
||||||
|
|
||||||
The example allocates two ints and uses them, and then deallocates back.
|
The example allocates two ints and uses them, and then deallocates back.
|
||||||
|
|
||||||
|
|||||||
@@ -5,18 +5,27 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
size_t bytes = sizeof(int) * 2;
|
int *is;
|
||||||
/* Allocate 2 ints. */
|
size_t nbytes = 2 * sizeof(*is);
|
||||||
int *is = malloc(bytes);
|
|
||||||
|
/* Allocate 2 ints. Note that unlike traditional stack arrays (non-VLA)
|
||||||
|
* this value does not have to be determined at compile time! */
|
||||||
|
is = malloc(nbytes);
|
||||||
|
|
||||||
/* This can happen for example if we ask for too much memory. */
|
/* This can happen for example if we ask for too much memory. */
|
||||||
if (is == NULL) {
|
if (is == NULL) {
|
||||||
perror("malloc");
|
perror("malloc");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Write to and read from the allocated memory. */
|
||||||
is[0] = 1;
|
is[0] = 1;
|
||||||
|
is[1] = 2;
|
||||||
assert(is[0] == 1);
|
assert(is[0] == 1);
|
||||||
|
assert(is[1] == 2);
|
||||||
|
|
||||||
/* Free the allocated memory. */
|
/* Free the allocated memory. */
|
||||||
free(is);
|
free(is);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
This directory contains glibc extensions to POSIX / ANSI C.
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../build
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../test
|
|
||||||
40
userland/linux/mmap_anonymous.c
Normal file
40
userland/linux/mmap_anonymous.c
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
/* https://cirosantilli.com/linux-kernel-module-cheat#mmap-map-anonymous */
|
||||||
|
|
||||||
|
#define _GNU_SOURCE
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int *is;
|
||||||
|
size_t nbytes = 2 * sizeof(*is);
|
||||||
|
|
||||||
|
/* Allocate 2 ints. */
|
||||||
|
is = mmap(
|
||||||
|
NULL,
|
||||||
|
nbytes,
|
||||||
|
PROT_READ | PROT_WRITE,
|
||||||
|
MAP_SHARED | MAP_ANONYMOUS,
|
||||||
|
-1,
|
||||||
|
0
|
||||||
|
);
|
||||||
|
|
||||||
|
/* This can happen for example if we ask for too much memory. */
|
||||||
|
if (is == NULL) {
|
||||||
|
perror("mmap");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Write to and read from the allocated memory. */
|
||||||
|
is[0] = 1;
|
||||||
|
is[1] = 2;
|
||||||
|
assert(is[0] == 1);
|
||||||
|
assert(is[1] == 2);
|
||||||
|
|
||||||
|
/* Free the allocated memory. */
|
||||||
|
munmap(is, nbytes);
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
@@ -1,13 +1,4 @@
|
|||||||
/* https://cirosantilli.com/linux-kernel-module-cheat#mmap
|
/* https://cirosantilli.com/linux-kernel-module-cheat#mmap-file */
|
||||||
*
|
|
||||||
* 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
|
#define _XOPEN_SOURCE 700
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|||||||
Reference in New Issue
Block a user