From 23dee97330ca19edf089e9399bdfb6bc78354a5f Mon Sep 17 00:00:00 2001 From: Jann Horn Date: Thu, 27 Aug 2015 18:17:10 +0200 Subject: [PATCH] commit old stuff --- math/histogram.sh | 2 ++ tools/racyopen_simple.c | 4 ++-- tools/rusage_precise.c | 51 +++++++++++++++++++++++++++++++++++++++++ tools/spin.c | 1 + tools/strscan.c | 33 ++++++++++++++++++++++++-- 5 files changed, 87 insertions(+), 4 deletions(-) create mode 100755 math/histogram.sh create mode 100644 tools/rusage_precise.c create mode 100644 tools/spin.c diff --git a/math/histogram.sh b/math/histogram.sh new file mode 100755 index 0000000..c10fc57 --- /dev/null +++ b/math/histogram.sh @@ -0,0 +1,2 @@ +#!/bin/sh +round "$1" | cut -d'.' -f1 | sort -n | uniq -c | sed 's|^\s*\([0-9]*\)\s\([0-9]*\)\s*$|\2 \1|' diff --git a/tools/racyopen_simple.c b/tools/racyopen_simple.c index e4d5c06..b70520a 100644 --- a/tools/racyopen_simple.c +++ b/tools/racyopen_simple.c @@ -14,11 +14,11 @@ int main(int argc, char *argv[]) { while (1) { int fd = open(argv[2], O_RDWR); if (fd == -1) { - sched_yield(); + //sched_yield(); continue; } printf("Success! Here's your shell with open fd.\n"); - execl("/system/bin/sh", "sh", NULL); + execl("/bin/sh", "sh", NULL); printf("\nshell exited, resuming race\n"); } } diff --git a/tools/rusage_precise.c b/tools/rusage_precise.c new file mode 100644 index 0000000..3545727 --- /dev/null +++ b/tools/rusage_precise.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define eprintf(...) fprintf(stderr, __VA_ARGS__) +#define FARG_TIMEVAL(tv) tv.tv_sec, tv.tv_usec +#define FSTR_TIMEVAL "%li.%06li" + +int main(int argc, char **argv) { + #define CHECK_USAGE if (argc < 2) return eprintf("invocation: rusage_precise [--machine-readable] []\n"), 1; + CHECK_USAGE + int machine_readable = 0; + if (!strcmp(argv[1], "--machine-readable")) machine_readable = 1, argv++, argc--; + CHECK_USAGE + + pid_t child = fork(); + if (child == -1) return perror("fork failed"), 1; + if (child == 0) { + execvp(argv[1], argv+1); + return perror("execvp failed"), 1; + } + + int status; + struct rusage ru; + errno = 0; + if (wait4(child, &status, 0, &ru) != child) return perror("wait4 failed"), 1; + + if (machine_readable) { + eprintf(FSTR_TIMEVAL "\t" FSTR_TIMEVAL "\t%li\t%li\t%li\t%li\t%li\t%li\t%li\n", + FARG_TIMEVAL(ru.ru_utime), FARG_TIMEVAL(ru.ru_stime), + ru.ru_maxrss, ru.ru_minflt, ru.ru_majflt, + ru.ru_inblock, ru.ru_oublock, ru.ru_nvcsw, + ru.ru_nivcsw); + } else { + eprintf(" user CPU (s): " FSTR_TIMEVAL "\n", FARG_TIMEVAL(ru.ru_utime)); + eprintf(" system CPU (s): " FSTR_TIMEVAL "\n", FARG_TIMEVAL(ru.ru_stime)); + eprintf(" max RSS size (kB): %li\n", ru.ru_maxrss); + eprintf(" minor faults: %li\n", ru.ru_minflt); + eprintf(" major faults: %li\n", ru.ru_majflt); + eprintf(" fs inblocks: %li\n", ru.ru_inblock); + eprintf(" fs outblocks: %li\n", ru.ru_oublock); + eprintf(" voluntary switches: %li\n", ru.ru_nvcsw); + eprintf("involuntary switches: %li\n", ru.ru_nivcsw); + } + return 0; +} diff --git a/tools/spin.c b/tools/spin.c new file mode 100644 index 0000000..fa2c4b1 --- /dev/null +++ b/tools/spin.c @@ -0,0 +1 @@ +int main(void) { while (1); } diff --git a/tools/strscan.c b/tools/strscan.c index 6060969..613319a 100644 --- a/tools/strscan.c +++ b/tools/strscan.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include #include #include @@ -31,11 +33,28 @@ int main(int argc, char **argv) { if (argc != 3) xperror("invocation: anondump ", 0); TPRINTF(maps_path, "/proc/%s/maps", argv[1]) char *maps = CHK_PTR(slurp_file(maps_path, NULL, JH_SLURP_NO_STAT), "unable to read /proc/$pid/maps", 1); + +/* + char argbuf[8192]; + size_t arglen = strlen(argv[2])*4; + for (int i=0; ib - mapping->a; char *copy = CHK_PTR(malloc(len), "malloc failed", 1); - if (pread(memfd, copy, len, (off_t)mapping->a) != (ssize_t)len) xperror("pread failed", 0); + ssize_t read_res; + size_t read_done = 0; +read_more:; + if ((read_res=pread(memfd, copy+read_done, len-read_done, (off_t)mapping->a-read_done)) != (ssize_t)len) { + if (read_res <= 0) { + fputs("warning: some read failed\n", stderr); + continue; + } + read_done += read_res; + goto read_more; + } size_t pos = 0; while (1) { char *ptr = memmem(copy + pos, len, argv[2], strlen(argv[2])); -- 2.20.1