Skip to content

Instantly share code, notes, and snippets.

@sitano
Last active October 27, 2021 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sitano/f3e88bab492bd42b04411ea81fe86913 to your computer and use it in GitHub Desktop.
Save sitano/f3e88bab492bd42b04411ea81fe86913 to your computer and use it in GitHub Desktop.
testing mmap
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdint.h>
#include <pthread.h>
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
void errExit(const char *str) {
printf("error: %s\n", str);
exit(1);
}
struct task {
pthread_t tid;
char *addr;
off_t size;
};
static void *
thread_start(void *arg) {
struct task *t = arg;
size_t x = 0;
srand(t->tid);
printf("firing at %p+%ld\n", t->addr, t->size);
while (1) {
off_t off = (rand() % t->size);
x += *(t->addr + off);
if (rand() % 100 < 1) {
t->addr[off] ++;
}
}
return (void *)x;
}
int
main(int argc, char *argv[]) {
char *addr, *base;
int fd;
struct stat sb;
const size_t threads = 4;
struct task ptt[threads] = {};
if (argc != 2 || strcmp(argv[1], "--help") == 0)
errExit("invalid file");
fd = open(argv[1], O_RDONLY);
if (fd == -1)
errExit("open");
if (fstat(fd, &sb) == -1)
errExit("fstat");
base = addr = mmap(NULL, sb.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
if (addr == MAP_FAILED)
errExit("mmap");
printf("mapped, addr = %p, size = %ld\n", base, sb.st_size);
off_t size = 1ll << 29;
for (size_t i = 0; i < threads; i ++) {
ptt[i].addr = addr + (rand() % (sb.st_size / size)) * size;
// ptt[i].addr = addr;
ptt[i].size = size;
int s = pthread_create(&ptt[i].tid, NULL, &thread_start, &ptt[i]);
if (s != 0)
handle_error_en(s, "pthread_create");
}
printf("started %zu\n", threads);
for (int i = 0; i < threads; i++) {
void *res;
int s = pthread_join(ptt[i].tid, &res);
if (s != 0)
handle_error_en(s, "pthread_join");
printf("Joined with thread %ld; returned value was %ld\n", ptt[i].tid, (size_t) res);
}
printf("done\n");
getchar();
exit(EXIT_SUCCESS);
return 0;
}
@sitano
Copy link
Author

sitano commented Oct 26, 2021

$ fallocate -l 10G bigfile
$ clang mmap.c && ./a.out bigfile
$ cat /proc/$(pgrep -f a.out)/status
$ cat /proc/$(pgrep -f a.out)/status

Name:  a.out
Umask:  0022
State:  R (running)
Tgid:  9418
Ngid:  0
Pid:  9418
PPid:  7910
TracerPid:  0
Uid:  1000  1000  1000  1000
Gid:  1001  1001  1001  1001
FDSize:  64
Groups:  209 974 1000 1001 
NStgid:  9418
NSpid:  9418
NSpgid:  7910
NSsid:  7477
VmPeak:  10488128 kB
VmSize:  10488128 kB
VmLck:         0 kB
VmPin:         0 kB
VmHWM:   7586752 kB
VmRSS:   7586752 kB
RssAnon:        96 kB
RssFile:      1212 kB
RssShmem:   7585444 kB
VmData:       208 kB
VmStk:       136 kB
VmExe:         4 kB
VmLib:      1476 kB
VmPTE:     14900 kB
VmSwap:         0 kB
HugetlbPages:         0 kB
CoreDumping:  0
THP_enabled:  1
Threads:  1
SigQ:  0/128000
SigPnd:  0000000000000000
ShdPnd:  0000000000000000
SigBlk:  0000000000000000
SigIgn:  0000000000000000
SigCgt:  0000000000000000
CapInh:  0000000000000000
CapPrm:  0000000000000000
CapEff:  0000000000000000
CapBnd:  000001ffffffffff
CapAmb:  0000000000000000
NoNewPrivs:  0
Seccomp:  0
Seccomp_filters:  0
Speculation_Store_Bypass:  thread vulnerable
SpeculationIndirectBranch:  conditional enabled
Cpus_allowed:  ff
Cpus_allowed_list:  0-7
Mems_allowed:  00000001
Mems_allowed_list:  0
voluntary_ctxt_switches:  170
nonvoluntary_ctxt_switches:  68

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment