add memfd

This commit is contained in:
Stefanos Kornilios Misis Poiitidis 2022-05-04 16:31:01 +03:00
parent 624065b343
commit 2aa443e114
9 changed files with 40 additions and 0 deletions

BIN
memfd Executable file

Binary file not shown.

BIN
shmid

Binary file not shown.

BIN
smc

Binary file not shown.

BIN
smc-2

Binary file not shown.

BIN
smc-mt

Binary file not shown.

BIN
smc-mt-2

Binary file not shown.

Binary file not shown.

View File

@ -14,3 +14,4 @@ gcc signals-nested.cpp -g -o ../signals-nested -lrt
gcc signals-nested-2.cpp -g -o ../signals-nested-2 -lpthread
gcc smc-shmatdt.cpp -g -o ../smc-shmatdt
gcc prot-growsdown.cpp -g -o ../prot-growsdown
gcc memfd.cpp -g -o ../memfd

39
src/memfd.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <sys/mman.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
void test() {
int fd = memfd_create("/bin/sudo", 0);
printf("pid: %d, fd: %d\n", getpid(), fd);
struct stat st;
fstat(fd, &st);
printf("dev: %lu, ino: %lu\n", st.st_dev, st.st_ino);
char syml[512];
char temp[512];
sprintf(syml, "/proc/%d/fd/%d", getpid(), fd);
auto rv = readlink(syml, temp, 511);
temp[rv] = 0;
printf("file: %s\n", temp);
}
void mmaptest() {
auto ptr = (char*)mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_ANON | MAP_SHARED, 0, 0);
auto ptr2 = (char*)mremap(ptr, 0, 8192, MREMAP_MAYMOVE);
auto ptr3 = (char*)mremap(ptr, 4096, 8192, MREMAP_MAYMOVE);
ptr3[0] = 1;
ptr2[1] = 1;
printf("%p, %p, %p\n", ptr, ptr2, ptr3);
printf("Expecting bus error\n");
ptr2[4096] = 1; // BUS_ERROR
ptr3[4096] = 1; // BUS_ERROR
//printf("%d, %d, %d, %d\n", ptr2[0], ptr3[0], ptr2[0], ptr3[0]);
}
int main() {
test();
mmaptest();
}