Commit 118b6655 authored by Cyrill Gorcunov's avatar Cyrill Gorcunov

Merge branch 'tests'

* tests:
  test: Add test-pipe-async test
  teste: Add test-shmem-three-async test
  test: Update shmem more frequently
  test: Add printing header
  test: Add test-shmem-async test
  test: Add test-rnd-from-file test
  test: Add test-counter test
  test: Drop old tests
parents 6357ad40 5e5cb3b3
OBJS += testee.o SRCS += test-counter.c
OBJS += testee-static.o SRCS += test-rnd-from-file.c
OBJS += testee-static-syssleep.o SRCS += test-shmem-async.c
OBJS += testee-threads.o SRCS += test-shmem-three-async.c
OBJS += testee-unlinked.o SRCS += test-pipe-async.c
OBJS += asmem.o
PROGS := $(patsubst %.o,%,$(OBJS)) OBJS := $(patsubst %.c,%.o,$(SRCS))
PROGS := $(patsubst %.c,%,$(SRCS))
all: $(PROGS) all: $(PROGS)
asmem: asmem.c $(PROGS): $(OBJS)
$(E) " CC " $(patsubst %.c,%.o,$<)
$(Q) $(CC) -c $(CFLAGS) $< -o $(patsubst %.c,%.o,$<)
$(E) " LINK " $@ $(E) " LINK " $@
$(Q) $(CC) -o $@ $(patsubst %.c,%.o,$<) $(Q) $(CC) $@.o -o $@
testee: testee.c %.o: %.c
$(E) " CC " $(patsubst %.c,%.o,$<) $(E) " CC " $@
$(Q) $(CC) -c $(CFLAGS) $< -o $(patsubst %.c,%.o,$<) $(Q) $(CC) -c $(CFLAGS) $< -o $@
$(E) " LINK " $@
$(Q) $(CC) -o $@ $(patsubst %.c,%.o,$<)
testee-static-syssleep: testee-static-syssleep.c
$(E) " CC " $(patsubst %.c,%.o,$<)
$(Q) $(CC) -c $(CFLAGS) $< -o $(patsubst %.c,%.o,$<)
$(E) " LINK " $@
$(Q) $(CC) -static -o $@ $(patsubst %.c,%.o,$<)
testee-static: testee-static.c
$(E) " CC " $(patsubst %.c,%.o,$<)
$(Q) $(CC) -c $(CFLAGS) $< -o $(patsubst %.c,%.o,$<)
$(E) " LINK " $@
$(Q) $(CC) -static -o $@ $(patsubst %.c,%.o,$<)
testee-threads: testee-threads.c
$(E) " CC " $(patsubst %.c,%.o,$<)
$(Q) $(CC) -c $(CFLAGS) $< -o $(patsubst %.c,%.o,$<)
$(E) " LINK " $@
$(Q) $(CC) -lpthread -o $@ $(patsubst %.c,%.o,$<)
testee-unlinked: testee-unlinked.c
$(E) " CC " $(patsubst %.c,%.o,$<)
$(Q) $(CC) -c $(CFLAGS) $< -o $(patsubst %.c,%.o,$<)
$(E) " LINK " $@
$(Q) $(CC) -o $@ $(patsubst %.c,%.o,$<)
clean: clean:
$(Q) $(RM) -f ./*.o $(Q) $(RM) -f ./*.o
......
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc, char *argv[])
{
int counter = 0;
printf("%s pid %d\n", argv[0], getpid());
while (1) {
printf("Pid: %10d Counter: %10d\n",
getpid(), counter++);
sleep(3);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sched.h>
static void *map;
int main(int argc, char *argv[])
{
int pipefd[2];
pid_t pid;
printf("%s pid %d\n", argv[0], getpid());
if (pipe(pipefd)) {
perror("Can't create pipe");
return -1;
}
pid = fork();
if (pid == -1) {
printf("fork failed\n");
return 1;
} else if (pid == 0) {
long buf;
while (read(pipefd[0], &buf, sizeof(buf)) > 0) {
printf("pipe-r: %08lx\n", buf);
sleep(2);
}
} else {
long buf = 0;
while (1) {
printf("pipe-w: %08lx\n", buf);
write(pipefd[1], &buf, sizeof(buf));
sleep(1);
buf++;
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
const char fname_rnd[] = "random-data.o"; /* so make clean drops it */
const int limit = 10;
int counter, fd, rnd;
printf("%s pid %d\n", argv[0], getpid());
unlink((char *)fname_rnd);
fd = open(fname_rnd, O_RDWR | O_CREAT | O_EXCL, 0644);
if (fd < 0) {
perror("Can't open file");
return fd;
}
counter = 0;
while (counter++ < limit) {
rnd = rand();
write(fd, &rnd, sizeof(rnd));
}
counter = 0;
while (1) {
lseek(fd, 0, SEEK_SET);
while (counter++ < limit) {
read(fd, &rnd, sizeof(rnd));
printf("Pid: %10d Rnd: %10d\n",
getpid(), rnd);
sleep(3);
}
}
return 0;
}
...@@ -22,14 +22,14 @@ int main(int argc, char *argv[]) ...@@ -22,14 +22,14 @@ int main(int argc, char *argv[])
map = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0); map = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
if (map == MAP_FAILED) { if (map == MAP_FAILED) {
printf("mmap failed\n"); printf("%6d: mmap failed\n", getpid());
return 0; return 0;
} }
memset(map, '-', 32); memset(map, '-', 21);
((char *)map)[32] = 0; ((char *)map)[21] = 0;
printf("%d: shmem '%s'\n", getpid(), (char *)map); printf("%6d: Initial shmem pattern '%s'\n", getpid(), (char *)map);
pid = fork(); pid = fork();
if (pid == -1) { if (pid == -1) {
...@@ -40,13 +40,13 @@ int main(int argc, char *argv[]) ...@@ -40,13 +40,13 @@ int main(int argc, char *argv[])
if (pid == 0) { if (pid == 0) {
int cnt = 0; int cnt = 0;
while(1) { while(1) {
printf("%d: shmem '%s'\n", getpid(), (char *)map); printf("%6d: Observed shmem pattern '%s'\n", getpid(), (char *)map);
sprintf(map, "shared-mem-%d", cnt++); sprintf(map, "shared-mem-%010d", cnt++);
sleep(5); sleep(1);
} }
} else { } else {
while(1) { while(1) {
printf("%d: shmem '%s'\n", getpid(), (char *)map); printf("%6d: Observed shmem pattern '%s'\n", getpid(), (char *)map);
sleep(3); sleep(3);
} }
} }
......
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sched.h>
static void *map1;
static void *map2;
int main(int argc, char *argv[])
{
pid_t pid;
printf("%s pid %d\n", argv[0], getpid());
map1 = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
map2 = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
if (map1 == MAP_FAILED || map2 == MAP_FAILED) {
printf("%6d: mmap failed\n", getpid());
return 0;
}
memset(map1, '-', 22);
((char *)map1)[22] = 0;
memset(map2, '+', 22);
((char *)map1)[22] = 0;
printf("%6d: Initial shmem1 pattern '%s'\n", getpid(), (char *)map1);
pid = fork();
if (pid == -1) {
printf("fork1 failed\n");
return 1;
} else if (pid == 0) {
int cnt = 0;
pid = fork();
if (pid == -1) {
printf("fork2 failed\n");
exit(1);
} else if (pid == 0) {
int num = 0;
while(1) {
printf("%6d: Observed shmem2 pattern '%s'\n", getpid(), (char *)map2);
sprintf(map2, "shared-mem2-%010d", num);
sleep(1);
num += 2;
}
}
cnt = -1;
while(1) {
cnt += 2;
printf("%6d: Observed shmem1 pattern '%s'\n", getpid(), (char *)map1);
sprintf(map1, "shared-mem1-%010d", cnt);
sleep(1);
}
} else {
while(1) {
printf("%6d: Observed shmem1 pattern '%s'\n", getpid(), (char *)map1);
printf("%6d: Observed shmem2 pattern '%s'\n", getpid(), (char *)map2);
sleep(1);
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sched.h>
#ifndef always_inline
# define always_inline __always_inline
#endif
#define __NR_write 1
#define __NR_nanosleep 35
static always_inline long syscall2(int nr, unsigned long arg0, unsigned long arg1)
{
long ret;
asm volatile("syscall"
: "=a" (ret)
: "a" (nr), "D" (arg0), "S" (arg1)
: "memory");
return ret;
}
static always_inline long syscall3(int nr, unsigned long arg0, unsigned long arg1,
unsigned long arg2)
{
long ret;
asm volatile(
"movl %1, %%eax \t\n"
"movq %2, %%rdi \t\n"
"movq %3, %%rsi \t\n"
"movq %4, %%rdx \t\n"
"syscall \t\n"
"movq %%rax, %0 \t\n"
: "=r"(ret)
: "g" ((int)nr), "g" (arg0), "g" (arg1), "g" (arg2)
: "rax", "rdi", "rsi", "rdx", "memory");
return ret;
}
static always_inline long sys_nanosleep(struct timespec *req, struct timespec *rem)
{
return syscall2(__NR_nanosleep, (unsigned long)req, (unsigned long)rem);
}
static always_inline long sys_write(unsigned long fd, const void *buf, unsigned long count)
{
return syscall3(__NR_write, fd, (unsigned long)buf, count);
}
static void always_inline local_sleep(long seconds)
{
struct timespec req, rem;
req = (struct timespec){
.tv_sec = seconds,
.tv_nsec = 0,
};
sys_nanosleep(&req, &rem);
}
int main(int argc, char *argv[])
{
const char msg[] = "I'm alive\n";
for (;;) {
sys_write(1, msg, sizeof(msg));
local_sleep(5);
}
return 0;
}
/*
* A simple testee program
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sched.h>
int main(int argc, char *argv[])
{
int pipefd[2];
int fd_shared, fd_private;
const char data_mark[] = "This is a data_mark marker";
void *mmap_shared, *mmap_private, *mmap_anon, *map_unreadable;
void *mmap_anon_shared;
const char sep[] = "----------";
unsigned long buf;
int i;
(void)data_mark;
printf("%s pid %d\n", argv[0], getpid());
if (pipe(pipefd)) {
perror("Can't create pipe");
goto err;
}
fd_shared = open("testee-shared.img", O_RDWR | O_CREAT | O_TRUNC, 0600);
if (fd_shared < 0) {
perror("Can't open fd_shared file");
goto err;
}
fd_private = open("testee-private.img", O_RDWR | O_CREAT | O_TRUNC, 0600);
if (fd_private < 0) {
perror("Can't open fd_private file");
goto err;
}
if (lseek(fd_shared, 1024, SEEK_SET) == -1 ||
lseek(fd_private, 1024, SEEK_SET) == -1) {
perror("Can't llsek");
goto err;
}
write(fd_shared, "", 1);
write(fd_private, "", 1);
mmap_shared = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd_shared, 0);
mmap_private = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_FILE | MAP_PRIVATE, fd_private, 0);
mmap_anon = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
map_unreadable = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
mmap_anon_shared= mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
if (mmap_shared == MAP_FAILED ||
mmap_private == MAP_FAILED ||
mmap_anon_shared == MAP_FAILED ||
mmap_anon == MAP_FAILED ||
map_unreadable == MAP_FAILED) {
perror("mmap failed");
goto err;
}
strcpy((char *)mmap_shared, sep);
strcpy((char *)mmap_private, sep);
strcpy((char *)mmap_anon, sep);
strcpy((char *)map_unreadable, sep);
strcpy((char *)mmap_anon_shared,sep);
for (i = 64; i < 128; i++) {
((char *)mmap_shared)[i] = 0 + i;
((char *)mmap_private)[i] = 64 + i;
((char *)mmap_anon)[i] = 128 + i;
((char *)map_unreadable)[i] = 190 + i;
((char *)mmap_anon_shared)[i] = 0 + i;
}
if (mprotect(map_unreadable, 1024, PROT_NONE)) {
perror("mprotect failed");
goto err;
}
asm volatile("" ::: "memory");
fsync(fd_shared);
fsync(fd_private);
sync();
asm volatile("" ::: "memory");
while (1) {
printf("ping: %d\n", getpid());
write(pipefd[1], &buf, sizeof(buf));
sleep(6);
}
err:
/* resources are released by kernel */
return 0;
}
/*
* A simple testee program with threads
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <pthread.h>
#define __NR_arch_prctl 158
#define ARCH_SET_GS 0x1001
#define ARCH_SET_FS 0x1002
#define ARCH_GET_FS 0x1003
#define ARCH_GET_GS 0x1004
static long syscall2(int nr, unsigned long arg0, unsigned long arg1)
{
long ret;
asm volatile(
"movl %1, %%eax \t\n"
"movq %2, %%rdi \t\n"
"movq %3, %%rsi \t\n"
"syscall \t\n"
"movq %%rax, %0 \t\n"
: "=r"(ret)
: "g" ((int)nr), "g" (arg0), "g" (arg1)
: "rax", "rdi", "rsi", "memory");
return ret;
}
static long sys_arch_prctl(int code, void *addr)
{
return syscall2(__NR_arch_prctl, code, (unsigned long)addr);
}
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static int counter;
static int thread_counter = 1;
static __thread int tls_data;
static void pr_fsgs_base(char *name)
{
unsigned long fsgs_base = -1ul;
int ret;
ret = sys_arch_prctl(ARCH_GET_FS, &fsgs_base);
printf("%8d (%4s): (%2d) fsgs_base %8lx\n",
getpid(), name, ret, fsgs_base);
ret = sys_arch_prctl(ARCH_GET_GS, &fsgs_base);
printf("%8d (%4s): (%2d) fsgs_base %8lx\n",
getpid(), name, ret, fsgs_base);
}
static void *ff1(void *arg)
{
void *map_unreadable = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
(void)map_unreadable;
tls_data = thread_counter++;
pr_fsgs_base("thr3");
while (1) {
pthread_mutex_lock(&mtx);
counter++;
printf("%8d (thr3): Counter value: %4d tls_data = %4d\n",
getpid(), counter, tls_data);
pthread_mutex_unlock(&mtx);
sleep(5);
}
return NULL;
}
static void *f1(void *arg)
{
const char name[] = "f1-file";
pthread_t th;
int fd;
void *map_unreadable = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
(void)map_unreadable;
unlink(name);
fd = open(name, O_CREAT, 0644);
if (fd >= 0)
write(fd, name, sizeof(name));
if (pthread_create(&th, NULL, &ff1, NULL))
perror("Cant create thread");
tls_data = thread_counter++;
pr_fsgs_base("thr1");
while (1) {
pthread_mutex_lock(&mtx);
counter++;
printf("%8d (thr1): Counter value: %4d tls_data = %4d\n",
getpid(), counter, tls_data);
pthread_mutex_unlock(&mtx);
sleep(2);
}
return NULL;
}
static void *f2(void *arg)
{
void *map_unreadable = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
(void)map_unreadable;
tls_data = thread_counter++;
pr_fsgs_base("thr2");
while (1) {
pthread_mutex_lock(&mtx);
counter--;
printf("%8d (thr2): Counter value: %4d tls_data = %4d\n",
getpid(), counter, tls_data);
pthread_mutex_unlock(&mtx);
sleep(3);
}
return NULL;
}
int main(int argc, char *argv[])
{
pthread_t th1, th2;
int rc1, rc2;
printf("%s pid %d\n", argv[0], getpid());
tls_data = thread_counter++;
pr_fsgs_base("main");
printf("%8d (main): Counter value: %4d tls_data = %4d\n",
getpid(), counter, tls_data);
rc1 = pthread_create(&th1, NULL, &f1, NULL);
rc2 = pthread_create(&th2, NULL, &f2, NULL);
if (rc1 | rc2)
exit(1);
while (1) {
printf("%8d (main): Counter value: %4d tls_data = %4d\n",
getpid(), counter, tls_data);
sleep(2);
}
pthread_join(th1, NULL);
pthread_join(th2, NULL);
exit(0);
}
/*
* A simple testee program
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sched.h>
int main(int argc, char *argv[])
{
int fd_shared, fd_private;
const char data_mark[] = "This is a data_mark marker";
void *mmap_shared, *mmap_private, *mmap_anon, *map_unreadable;
const char sep[] = "----------";
pid_t pid, child;
int i;
printf("%s pid %d\n", argv[0], getpid());
fd_shared = open("testee-shared.img", O_RDWR | O_CREAT | O_TRUNC, 0600);
if (fd_shared < 0) {
perror("Can't open fd_shared file");
goto err;
}
fd_private = open("testee-private.img", O_RDWR | O_CREAT | O_TRUNC, 0600);
if (fd_private < 0) {
perror("Can't open fd_private file");
goto err;
}
if (lseek(fd_shared, 1024, SEEK_SET) == -1 ||
lseek(fd_private, 1024, SEEK_SET) == -1) {
perror("Can't llsek");
goto err;
}
write(fd_shared, "", 1);
write(fd_private, "", 1);
mmap_shared = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd_shared, 0);
mmap_private = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_FILE | MAP_PRIVATE, fd_private, 0);
if (mmap_shared == MAP_FAILED ||
mmap_private == MAP_FAILED) {
perror("mmap failed");
goto err;
}
strcpy((char *)mmap_shared, sep);
strcpy((char *)mmap_private, sep);
for (i = 64; i < 128; i++) {
((char *)mmap_shared)[i] = 0 + i;
((char *)mmap_private)[i] = 64 + i;
}
fsync(fd_shared);
fsync(fd_private);
close(fd_shared);
fsync(fd_private);
unlink("testee-shared.img");
unlink("testee-private.img");
for (i = 64; i < 128; i++) {
((char *)mmap_shared)[i] = 0 + i;
((char *)mmap_private)[i] = 64 + i;
}
msync(mmap_shared, 1024, MS_SYNC);
msync(mmap_private, 1024, MS_SYNC);
while (1)
sleep(1);
err:
/* resources are released by kernel */
return 0;
}
/*
* A simple testee program
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sched.h>
static int do_child(void *arg)
{
printf("do_child pid: %d\n", getpid());
void *stack, *mmap_anon;
stack = mmap(0, 4 * 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_GROWSDOWN, 0, 0);
if (stack == MAP_FAILED)
return -1;
mmap_anon = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (mmap_anon == MAP_FAILED)
return -1;
while (1)
sleep(6);
return 0;
}
static int run_clone(void)
{
pid_t pid = 0;
int ret = 0;
void *stack, *mmap_anon;
stack = mmap(0, 4 * 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_GROWSDOWN, 0, 0);
if (stack == MAP_FAILED)
return -1;
mmap_anon = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (mmap_anon == MAP_FAILED)
return -1;
stack += 4 * 4096;
ret = clone(do_child, stack, CLONE_FS, NULL, NULL, NULL, &pid);
if (ret < 0)
perror("Failed clone");
printf("run_clone: %d stack: %p mmap_anon: %p ret %d\n",
pid, stack, mmap_anon, ret);
if (stack == MAP_FAILED)
return -1;
mmap_anon = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (mmap_anon == MAP_FAILED)
return -1;
stack += 4 * 4096;
ret = clone(do_child, stack, CLONE_FS | CLONE_FILES | CLONE_VM, NULL, NULL, NULL, &pid);
if (ret < 0)
perror("Failed clone");
printf("run_clone: %d stack: %p mmap_anon: %p ret %d\n",
pid, stack, mmap_anon, ret);
return ret;
}
int main(int argc, char *argv[])
{
int pipefd[2];
int fd_shared, fd_private;
const char data_mark[] = "This is a data_mark marker";
void *mmap_shared, *mmap_private, *mmap_anon, *map_unreadable;
void *mmap_anon_sh;
const char sep[] = "----------";
pid_t pid, child;
char suided_path[128];
int i;
(void)data_mark;
printf("%s pid %d\n", argv[0], getpid());
if (pipe(pipefd)) {
perror("Can't create pipe");
goto err;
}
fd_shared = open("testee-shared.img", O_RDWR | O_CREAT | O_TRUNC, 0600);
if (fd_shared < 0) {
perror("Can't open fd_shared file");
goto err;
}
fd_private = open("testee-private.img", O_RDWR | O_CREAT | O_TRUNC, 0600);
if (fd_private < 0) {
perror("Can't open fd_private file");
goto err;
}
if (lseek(fd_shared, 1024, SEEK_SET) == -1 ||
lseek(fd_private, 1024, SEEK_SET) == -1) {
perror("Can't llsek");
goto err;
}
write(fd_shared, "", 1);
write(fd_private, "", 1);
mmap_shared = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd_shared, 0);
mmap_private = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_FILE | MAP_PRIVATE, fd_private, 0);
mmap_anon = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
map_unreadable = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
mmap_anon_sh = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
if (mmap_shared == MAP_FAILED ||
mmap_private == MAP_FAILED ||
mmap_anon == MAP_FAILED ||
mmap_anon_sh == MAP_FAILED ||
map_unreadable == MAP_FAILED) {
perror("mmap failed");
goto err;
}
snprintf(suided_path, sizeof(suided_path),
"/proc/%d/map_files/%lx-%lx",
getpid(), (long)mmap_shared,
(long)mmap_shared + 0x1000);
strcpy((char *)mmap_shared, sep);
strcpy((char *)mmap_private, sep);
strcpy((char *)mmap_anon, sep);
strcpy((char *)map_unreadable, sep);
strcpy((char *)mmap_anon_sh, sep);
for (i = 64; i < 128; i++) {
((char *)mmap_shared)[i] = 0 + i;
((char *)mmap_private)[i] = 64 + i;
((char *)mmap_anon)[i] = 128 + i;
((char *)mmap_anon_sh)[i] = 128 + i;
((char *)map_unreadable)[i] = 190 + i;
}
if (mprotect(map_unreadable, 1024, PROT_NONE)) {
perror("mprotect failed");
goto err;
}
asm volatile("" ::: "memory");
fsync(fd_shared);
fsync(fd_private);
close(fd_shared);
if (argc > 1) {
printf("my-uid: %d\n", getuid());
setuid(atoi(argv[1]));
printf("my-uid: %d\n", getuid());
}
fd_shared = open(suided_path, O_RDWR, 0600);
printf("fd_shared for O_RDWR: %d\n", fd_shared);
if (fd_shared >= 0) {
write(fd_shared, "aaaa", sizeof("aaaa"));
close(fd_shared);
}
fd_shared = open(suided_path, O_TRUNC, 0600);
printf("fd_shared for O_TRUNC: %d\n", fd_shared);
if (fd_shared >= 0) {
printf("tunc: %d\n", ftruncate(fd_shared, 512));
close(fd_shared);
}
fd_shared = open(suided_path, O_RDONLY, 0600);
printf("fd_shared for O_RDONLY: %d\n", fd_shared);
if (fd_shared >= 0)
close(fd_shared);
sync();
asm volatile("" ::: "memory");
pid = fork();
if (pid == -1)
goto err;
if (pid == 0) {
long buf;
child = fork();
if (child == -1)
goto err;
if (child == 0) {
printf("first child pid: %d\n", getpid());
while (read(pipefd[0], &buf, sizeof(buf)) > 0) {
printf("pipe-r: %8lx\n", buf);
sleep(3);
}
*(unsigned long *)mmap_anon_sh = 0x11111111;
while (1) {
printf("ping: %d\n", getpid());
sleep(8);
}
} else {
*(unsigned long *)mmap_anon_sh = 0x22222222;
printf("first parent pid: %d\n", getpid());
// run_clone();
while (1) {
printf("pipe-r: %8lx\n", buf);
printf("ping: %d\n", getpid());
sleep(9);
}
}
} else {
long buf = 0xdeadbeef;
while (1) {
float res = 0.9;
*(unsigned long *)mmap_anon_sh = 0x33333333;
printf("ping: %d %f\n", getpid(), res + (float)(unsigned long)mmap_anon_sh);
printf("pipe-w: %8lx\n", buf);
write(pipefd[1], &buf, sizeof(buf));
sleep(10);
}
}
err:
/* resources are released by kernel */
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment