aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDavid Timber <mieabby@gmail.com>2020-08-10 13:57:00 +0930
committerDavid Timber <mieabby@gmail.com>2020-08-10 13:57:00 +0930
commit195946d11f8a7870b435e0eba32f2299aa1b96a1 (patch)
tree62470aff372efb7cb66d9a90c1fc33c60b19738f /src
parentd61c0f9ddba9176c09ed751587343f3268ac6812 (diff)
Changes for xcomp ...
* Add 'pthsem' dependency * Remove m68k, spc arch * Add proone-stress, proone-arch-test
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am19
-rw-r--r--src/config.c22
-rw-r--r--src/proone-arch-test.c30
-rw-r--r--src/proone-mask.c3
-rw-r--r--src/proone-stress.c325
-rw-r--r--src/proone-test_proto.c2
-rw-r--r--src/protocol.c46
-rw-r--r--src/protocol.h10
-rw-r--r--src/resolv_worker.c4
-rw-r--r--src/rnd.c121
-rw-r--r--src/rnd.h29
-rw-r--r--src/util_ct.h6
-rw-r--r--src/util_rt.c16
-rw-r--r--src/util_rt.h3
14 files changed, 427 insertions, 209 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 2bbb740..1bb594d 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -4,11 +4,12 @@ if DEBUG
TARGET_FLAGS += -g -O0
DEV_FLAGS += -g -O0 -DPRNE_DEBUG
else
-TARGET_FLAGS += -Os
-DEV_FLAGS += -Os
+TARGET_FLAGS += -Os -g
+DEV_FLAGS += -Os -g
endif
-AM_CFLAGS = -std=c11 -pedantic -Wall -Wextra -Wno-switch -Wno-unused-parameter -D_GNU_SOURCE $(DEV_FLAGS)
+# TODO: Use -D_POSIX_C_SOURCE=200112L or -D_POSIX_C_SOURCE=2
+AM_CFLAGS = -std=c11 -pedantic -Wall -Wextra -Wno-switch -D_GNU_SOURCE -Wno-unused-parameter $(DEV_FLAGS)
noinst_LIBRARIES = libproone.a
bin_PROGRAMS =\
@@ -18,7 +19,9 @@ bin_PROGRAMS =\
proone-list-arch\
proone-mask\
proone-print-all-data\
- proone-resolv
+ proone-resolv\
+ proone-arch-test\
+ proone-stress
libproone_a_SOURCES =\
config.c\
@@ -63,6 +66,14 @@ proone_resolv_LDADD = libproone.a
proone_resolv_LDFLAGS = $(LIBS) $(DEP_PKGCFG_LIBS)
proone_resolv_SOURCES = proone-resolv.c
+proone_arch_test_LDADD = libproone.a -lrt -lpthread $(LIBS) $(DEP_PKGCFG_LIBS)
+proone_arch_test_SOURCES = proone-arch-test.c
+proone_arch_test_LDFLAGS = -static
+
+proone_stress_LDADD = libproone.a -lrt -lpthread $(LIBS) $(DEP_PKGCFG_LIBS)
+proone_stress_LDFLAGS = -static
+proone_stress_SOURCES = proone-stress.c
+
if TESTS
bin_PROGRAMS += proone-test_proto proone-test_util
diff --git a/src/config.c b/src/config.c
index 63b568b..7e6512d 100644
--- a/src/config.c
+++ b/src/config.c
@@ -5,14 +5,16 @@ const uint8_t PRNE_PROG_VER[16] = { 0x11, 0xf7, 0x6b, 0x87, 0x62, 0x1a, 0x47, 0x
const prne_arch_t prne_host_arch =
#ifdef __GNUC__
- #if defined(__ARM_ARCH_4T__)
+ #if defined(__i386__)
+ PRNE_ARCH_I686
+ #elif defined(__x86_64__)
+ PRNE_ARCH_X86_64
+ #elif defined(__ARM_ARCH_4T__)
PRNE_ARCH_ARMV4T
#elif defined(__ARM_ARCH_7A__)
PRNE_ARCH_ARMV7
- #elif defined(__x86_64__) || defined(__i386__)
- PRNE_ARCH_I686
- #elif defined(__m68k__)
- PRNE_ARCH_M68K
+ #elif defined(__aarch64__)
+ PRNE_ARCH_AARCH64
#elif defined(__mips__)
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
PRNE_ARCH_MIPS
@@ -23,18 +25,8 @@ const prne_arch_t prne_host_arch =
#endif
#elif defined(__powerpc__)
PRNE_ARCH_PPC
- #elif defined(__riscv) || defined(__riscv__)
- #if __riscv_xlen == 32
- PRNE_ARCH_RV32
- #elif __riscv_xlen == 64
- PRNE_ARCH_RV64
- #else
- #error "FIXME!"
- #endif
#elif defined(__SH4__)
PRNE_ARCH_SH4
- #elif defined(__sparc__)
- PRNE_ARCH_SPC
#else
#error "FIXME!"
#endif
diff --git a/src/proone-arch-test.c b/src/proone-arch-test.c
new file mode 100644
index 0000000..8390ca8
--- /dev/null
+++ b/src/proone-arch-test.c
@@ -0,0 +1,30 @@
+#include <stdio.h>
+#include <math.h>
+
+#include "protocol.h"
+#include "config.h"
+
+
+int main (void) {
+ int exit_code;
+ volatile double mat[9] = {
+ 1, 0, 0,
+ 0, 1, 0,
+ 0, 0, 1
+ };
+ volatile double arr[3] = {
+ 1, 2, 3
+ };
+ volatile double d;
+
+ arr[0] = arr[0] * mat[0] + arr[1] * mat[1] + arr[2] * mat[2];
+ arr[1] = arr[0] * mat[3] + arr[1] * mat[4] + arr[2] * mat[5];
+ arr[2] = arr[0] * mat[6] + arr[1] * mat[7] + arr[2] * mat[8];
+ d = arr[0] + arr[1] + arr[2];
+
+ exit_code = isnan(d) ? 1 : 0;
+
+ printf("%s\n", prne_arch_tostr(prne_host_arch));
+
+ return exit_code;
+}
diff --git a/src/proone-mask.c b/src/proone-mask.c
index 3385c5a..aa39d56 100644
--- a/src/proone-mask.c
+++ b/src/proone-mask.c
@@ -9,6 +9,7 @@
#include <sys/random.h>
#include "dvault.h"
+#include "util_rt.h"
int main (const int argc, char **args) {
@@ -43,7 +44,7 @@ int main (const int argc, char **args) {
}
}
else {
- getrandom(&salt, sizeof(salt), 0);
+ prne_geturandom(&salt, sizeof(salt));
}
type = prne_data_type_fstr(args[1]);
diff --git a/src/proone-stress.c b/src/proone-stress.c
new file mode 100644
index 0000000..31ab1b8
--- /dev/null
+++ b/src/proone-stress.c
@@ -0,0 +1,325 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <string.h>
+#include <time.h>
+#include <assert.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <sys/random.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/shm.h>
+#include <sys/mman.h>
+#include <signal.h>
+#include <errno.h>
+
+#include <mbedtls/ctr_drbg.h>
+#include <mbedtls/entropy.h>
+
+#include "util_ct.h"
+#include "util_rt.h"
+#include "llist.h"
+
+
+typedef struct {
+ bool good;
+} shared_t;
+
+typedef struct {
+ uint64_t nb_cycles;
+ uint64_t last_cycles;
+} shared_ctx_t;
+
+typedef struct {
+ mbedtls_entropy_context ent;
+ mbedtls_ctr_drbg_context ctx;
+} priv_ctx_t;
+
+shared_t *shared = NULL;
+shared_ctx_t *ctx_arr = NULL;
+struct timespec last_report;
+size_t pagesize;
+unsigned int nb_pages_min = 1;
+unsigned int nb_pages_max = 1;
+pid_t parent;
+unsigned int nproc;
+
+static void child_main (shared_ctx_t *ctx);
+static void handle_signal (const int sn);
+static void report (shared_ctx_t *arr);
+static void child_signal_handler (const int sn);
+static void sendall(const int sn);
+
+int main (const int argc, const char **args) {
+ static const size_t ALIGNED_SHARED_SIZE = prne_malign_to(sizeof(shared_t), 8);
+#define END_ON_ERR(retval, val, fname, eq)\
+ if ((eq && retval != val) || (!eq && retval == val)) {\
+ perror(fname);\
+ exit_code = 1;\
+ goto END;\
+ }
+
+ unsigned int nb_proc_started = 0;
+ int exit_code = 0;
+ int zfd = -1;
+ size_t shm_size;
+ uint8_t *shm_ptr = MAP_FAILED;
+ bool is_parent = true;
+
+ parent = getpid();
+
+ {
+ const long ps = sysconf(_SC_PAGESIZE);
+
+ if (ps < 0) {
+ perror("sysconf(_SC_PAGESIZE) returned error");
+ abort();
+ }
+ else if (ps == 0) {
+ fprintf(stderr, "sysconf(_SC_PAGESIZE) returned 0");
+ abort();
+ }
+ else {
+ pagesize = (size_t)ps;
+ }
+
+ assert(pagesize % sizeof(unsigned long) == 0);
+ }
+
+ if (argc < 2) {
+ fprintf(stderr,
+ "Usage: %s <nproc> [page num range]\n"
+ "\t<nproc>: number of processes to spawn\n"
+ "\t[page num range]: number of page to use. '1-5' for 1 to 5 pages, '1' for just one page. Defaults to '1'\n",
+ args[0]);
+ return 2;
+ }
+ if (sscanf(args[1], "%u", &nproc) != 1 || nproc == 0) {
+ fprintf(stderr, "Invalid <nproc> value %u\n", nproc);
+ return 2;
+ }
+ if (argc >= 3) {
+ if (sscanf(args[2], "%u-%u", &nb_pages_min, &nb_pages_max) == 2) {
+ if (nb_pages_min > nb_pages_max || nb_pages_min == 0) {
+ goto DROP;
+ }
+ }
+ else if (sscanf(args[2], "%u", &nb_pages_min) == 1) {
+ if (nb_pages_min == 0) {
+ goto DROP;
+ }
+ nb_pages_max = nb_pages_min;
+ }
+ else {
+ goto DROP;
+ }
+
+ if (false) {
+DROP:
+ fprintf(stderr, "Invalid page num range.\n");
+ return 2;
+ }
+ }
+
+ // init mem
+ shm_size = ALIGNED_SHARED_SIZE + sizeof(shared_ctx_t) * nproc;
+ zfd = open("/dev/zero", O_RDWR);
+ if (zfd < 0) {
+ perror("open()");
+ exit_code = 1;
+ goto END;
+ }
+ shm_ptr = mmap(NULL, shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, zfd, 0);
+ END_ON_ERR(shm_ptr, MAP_FAILED, "mmap()", false);
+ close(zfd);
+ zfd = -1;
+
+ // prep shared
+ shared = (shared_t*)shm_ptr;
+ shm_ptr += ALIGNED_SHARED_SIZE;
+ shared->good = true;
+
+ ctx_arr = (shared_ctx_t*)shm_ptr;
+
+ for (unsigned int i = 0; i < nproc; i += 1) {
+ const pid_t f_ret = fork();
+
+ if (f_ret < 0) {
+ perror("fork()");
+ exit_code = 1;
+ goto END;
+ }
+ else if (f_ret == 0) {
+ struct sigaction sa;
+
+ memzero(&sa, sizeof(struct sigaction));
+ sa.sa_handler = child_signal_handler;
+ sigaction(SIGINT, &sa, NULL);
+
+ is_parent = false;
+
+ child_main(ctx_arr + i);
+ goto END;
+ }
+ else {
+ nb_proc_started += 1;
+ }
+ }
+
+ {
+ struct sigaction sa;
+
+ memzero(&sa, sizeof(struct sigaction));
+ sa.sa_handler = handle_signal;
+
+ sigaction(SIGINT, &sa, NULL);
+ sigaction(SIGTERM, &sa, NULL);
+ sigaction(SIGCHLD, &sa, NULL);
+ sigaction(SIGUSR1, &sa, NULL);
+ }
+
+ clock_gettime(CLOCK_MONOTONIC, &last_report);
+
+
+ while (shared->good) {
+ pause();
+ }
+
+END:
+ sigaction(SIGCHLD, NULL, NULL);
+
+ if (is_parent) {
+ if (shm_ptr != MAP_FAILED) {
+ shared->good = false;
+
+ if (nb_proc_started > 0) {
+ printf("Reaping child processes ...\n");
+
+ for (unsigned int i = 0; i < nb_proc_started; i += 1) {
+ wait(NULL);
+ }
+ }
+
+ munmap(shm_ptr, shm_size);
+ }
+ if (zfd >= 0) {
+ close(zfd);
+ }
+ }
+
+ return exit_code;
+}
+
+static void handle_signal (const int sn) {
+ const int save_errno = errno;
+
+ switch (sn) {
+ case SIGINT:
+ case SIGTERM:
+ shared->good = false;
+ if (sn == SIGTERM) {
+ sendall(SIGTERM);
+ }
+ sigaction(SIGINT, NULL, NULL);
+ sigaction(SIGTERM, NULL, NULL);
+ break;
+ case SIGCHLD:
+ if (shared->good) {
+ fprintf(stderr, "Death of a child!\n");
+ kill(0, SIGABRT);
+ raise(SIGABRT);
+ }
+ break;
+ case SIGUSR1:
+ report(ctx_arr);
+ break;
+ }
+
+ errno = save_errno;
+}
+
+static void report (shared_ctx_t *arr) {
+ struct timespec ts_now, ts_delta;
+ const time_t now = time(NULL);
+ uint64_t sum_delta = 0, sum_all = 0;
+
+ clock_gettime(CLOCK_MONOTONIC, &ts_now);
+
+ printf(
+ "=== proone-stress ===\n"
+ "%s",
+ ctime(&now));
+ for (unsigned int i = 0; i < nproc; i += 1) {
+ shared_ctx_t *ctx = arr + i;
+ uint64_t delta;
+
+ delta = ctx->nb_cycles - ctx->last_cycles;
+ sum_delta += delta;
+ sum_all += ctx->nb_cycles;
+ ctx->last_cycles = ctx->nb_cycles;
+
+ printf("%"PRIu64" ", delta);
+ }
+
+ ts_delta = prne_sub_timespec(ts_now, last_report);
+ printf(
+ "\n"
+ "Delta: %"PRIu64"\n"
+ "Sum: %"PRIu64"\n"
+ "Last report: %"PRIdMAX".%03lds ago\n"
+ "Thoughput: %.1lf events per second\n",
+ sum_delta,
+ sum_all,
+ (intmax_t)ts_delta.tv_sec,
+ ts_delta.tv_nsec / 1000000,
+ (double)sum_delta / prne_real_timespec(ts_delta));
+
+ fflush(stdout);
+
+ last_report = ts_now;
+}
+
+static void do_cycle (priv_ctx_t *priv_ctx, shared_ctx_t *ctx) {
+ unsigned int pages;
+ unsigned long *arr;
+ size_t nb_elements;
+
+ assert(mbedtls_ctr_drbg_random(&priv_ctx->ctx, (unsigned char*)&pages, sizeof(pages)) == 0);
+
+ pages = (pages % (nb_pages_max - nb_pages_min + 1)) + nb_pages_min;
+ nb_elements = pagesize / sizeof(unsigned long) * pages;
+
+ arr = (unsigned long*)prne_malloc(pagesize, pages);
+ assert(mbedtls_ctr_drbg_random(&priv_ctx->ctx, (unsigned char*)arr, 2 * sizeof(unsigned int)) == 0);
+
+ for (size_t i = 2; i < nb_elements; i += 1) {
+ arr[i] = arr[i - 2] + arr[i - 1];
+ }
+
+ prne_free(arr);
+}
+
+static void child_main (shared_ctx_t *ctx) {
+ priv_ctx_t priv_ctx;
+
+ mbedtls_entropy_init(&priv_ctx.ent);
+ mbedtls_ctr_drbg_init(&priv_ctx.ctx);
+ assert(mbedtls_ctr_drbg_seed(&priv_ctx.ctx, mbedtls_entropy_func, &priv_ctx.ent, NULL, 0) == 0);
+
+ while (shared->good) {
+ do_cycle(&priv_ctx, ctx);
+ ctx->nb_cycles += 1;
+ }
+}
+
+static void child_signal_handler (const int sn) {}
+
+static void sendall(const int sn) {
+ for (unsigned int i = 0; i < nproc; i += 1) {
+
+ }
+}
diff --git a/src/proone-test_proto.c b/src/proone-test_proto.c
index b0f2345..e4bb31b 100644
--- a/src/proone-test_proto.c
+++ b/src/proone-test_proto.c
@@ -89,7 +89,7 @@ static void test_ser (void) {
test_pw[255] = test_id[255] = 0;
assert(strlen(test_id) == 255 && strlen(test_pw) == 255);
- assert(getrandom(&salt, 1, 0) == 1);
+ assert(prne_geturandom(&salt, 1) == 1);
// free functions should accept NULL
prne_htbt_free_msg_head(NULL);
diff --git a/src/protocol.c b/src/protocol.c
index b49c6ba..97259f4 100644
--- a/src/protocol.c
+++ b/src/protocol.c
@@ -15,67 +15,57 @@
const char *prne_arch_tostr (const prne_arch_t x) {
switch (x){
+ case PRNE_ARCH_AARCH64:
+ return "aarch64";
case PRNE_ARCH_ARMV4T:
return "armv4t";
case PRNE_ARCH_ARMV7:
return "armv7";
+ case PRNE_ARCH_X86_64:
+ return "x86_64";
case PRNE_ARCH_I686:
return "i686";
- case PRNE_ARCH_M68K:
- return "m68k";
case PRNE_ARCH_MIPS:
return "mips";
case PRNE_ARCH_MPSL:
return "mpsl";
case PRNE_ARCH_PPC:
return "ppc";
- case PRNE_ARCH_RV32:
- return "rv32";
- case PRNE_ARCH_RV64:
- return "rv64";
case PRNE_ARCH_SH4:
return "sh4";
- case PRNE_ARCH_SPC:
- return "spc";
}
return NULL;
}
prne_arch_t prne_arch_fstr (const char *str) {
+ if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_AARCH64))) {
+ return PRNE_ARCH_AARCH64;
+ }
if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_ARMV4T))) {
return PRNE_ARCH_ARMV4T;
}
- else if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_ARMV7))) {
+ if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_ARMV7))) {
return PRNE_ARCH_ARMV7;
}
- else if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_I686))) {
+ if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_I686))) {
return PRNE_ARCH_I686;
}
- else if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_M68K))) {
- return PRNE_ARCH_M68K;
+ if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_X86_64))) {
+ return PRNE_ARCH_X86_64;
}
- else if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_MIPS))) {
+ if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_MIPS))) {
return PRNE_ARCH_MIPS;
}
- else if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_MPSL))) {
+ if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_MPSL))) {
return PRNE_ARCH_MPSL;
}
- else if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_PPC))) {
+ if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_PPC))) {
return PRNE_ARCH_PPC;
}
- else if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_RV32))) {
- return PRNE_ARCH_RV32;
- }
- else if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_RV64))) {
- return PRNE_ARCH_RV64;
- }
- else if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_SH4))) {
+ if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_SH4))) {
return PRNE_ARCH_SH4;
}
- else if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_SPC))) {
- return PRNE_ARCH_SPC;
- }
return PRNE_ARCH_NONE;
}
@@ -200,13 +190,13 @@ prne_htbt_ser_rc_t prne_dec_host_cred (const uint8_t *data, const size_t len, pr
char *id = NULL, *pw = NULL;
if (!(3 <= len && len <= 3 + 255 + 255)) {
- return PRNE_PACK_RC_FMT_ERR;
+ return PRNE_HTBT_SER_RC_FMT_ERR;
}
in_len = len - 1;
in = (uint8_t*)prne_malloc(1, in_len);
if (in == NULL) {
- return PRNE_PACK_RC_ERRNO;
+ return PRNE_HTBT_SER_RC_ERRNO;
}
memcpy(in, data + 1, in_len);
@@ -858,7 +848,7 @@ prne_htbt_ser_rc_t prne_htbt_dser_cmd (const uint8_t *data, const size_t len, si
args = (char**)prne_malloc(sizeof(char*), argc + 1);
mem = (char*)prne_malloc(1, args_len);
if (args == NULL || mem == NULL) {
- ret = PRNE_HTBT_STATUS_ERRNO;
+ ret = PRNE_HTBT_SER_RC_ERRNO;
goto END;
}
diff --git a/src/protocol.h b/src/protocol.h
index fbc9dba..4243336 100644
--- a/src/protocol.h
+++ b/src/protocol.h
@@ -19,18 +19,16 @@ typedef struct prne_htbt_bin_meta prne_htbt_bin_meta_t;
typedef enum {
PRNE_ARCH_NONE = -1,
-
+
+ PRNE_ARCH_I686,
+ PRNE_ARCH_X86_64,
PRNE_ARCH_ARMV4T,
PRNE_ARCH_ARMV7,
- PRNE_ARCH_I686,
- PRNE_ARCH_M68K,
+ PRNE_ARCH_AARCH64,
PRNE_ARCH_MIPS,
PRNE_ARCH_MPSL,
PRNE_ARCH_PPC,
- PRNE_ARCH_RV32,
- PRNE_ARCH_RV64,
PRNE_ARCH_SH4,
- PRNE_ARCH_SPC,
NB_PRNE_ARCH
} prne_arch_t;
diff --git a/src/resolv_worker.c b/src/resolv_worker.c
index 38135d9..72c3cb0 100644
--- a/src/resolv_worker.c
+++ b/src/resolv_worker.c
@@ -324,7 +324,7 @@ static size_t resolv_next_pool_ptr (prne_resolv_wkr_ctx_t ctx, const size_t cnt)
size_t ret = 0;
if (mbedtls_ctr_drbg_random(ctx->ssl.ctr_drbg, (unsigned char*)&ret, sizeof(size_t)) != 0) {
- getrandom(&ret, sizeof(size_t), 0);
+ abort();
}
return ret % cnt;
}
@@ -334,7 +334,7 @@ static uint16_t resolv_next_qid (prne_resolv_wkr_ctx_t ctx) {
for (i = 0; i < UINT16_MAX; i += 1) {
if (mbedtls_ctr_drbg_random(ctx->ssl.ctr_drbg, (unsigned char*)&ret, sizeof(uint16_t)) != 0) {
- getrandom(&ret, sizeof(uint16_t), 0);
+ abort();
}
ret = (ret % UINT16_MAX) + 1;
if (prne_imap_lookup(&ctx->qid_map, ret) == NULL) {
diff --git a/src/rnd.c b/src/rnd.c
deleted file mode 100644
index 09aedf7..0000000
--- a/src/rnd.c
+++ /dev/null
@@ -1,121 +0,0 @@
-#include "rnd.h"
-#include "util_rt.h"
-
-#include <stdlib.h>
-#include <time.h>
-
-#include <unistd.h>
-#include <sys/random.h>
-
-#define N ((size_t)624)
-
-
-struct prne_rnd_engine {
- size_t mti;
- uint32_t mt[N];
-};
-
-
-void prne_init_alloc_rnd_engine_result (prne_rnd_engnie_alloc_result_t *r) {
- r->engine = NULL;
- r->result = PRNE_RND_ENGINE_ALLOC_OK;
-}
-
-prne_rnd_engnie_alloc_result_t prne_alloc_rnd_engine (const uint32_t *s) {
- prne_rnd_engnie_alloc_result_t ret;
- uint32_t seed;
-
- prne_init_alloc_rnd_engine_result(&ret);
-
- if (s == NULL) {
- seed = 4357;
- }
- else {
- if (*s == 0) {
- ret.result = PRNE_RND_ENGINE_ALLOC_INVALID_SEED;
- return ret;
- }
- seed = *s;
- }
-
- ret.engine = (prne_rnd_engine_t*)prne_malloc(sizeof(prne_rnd_engine_t), 1);
- if (ret.engine == NULL) {
- ret.result = PRNE_RND_ENGINE_ALLOC_MEM_ERR;
- return ret;
- }
-
- ret.engine->mt[0] = seed;
- for (ret.engine->mti = 1; ret.engine->mti < N; ret.engine->mti += 1) {
- ret.engine->mt[ret.engine->mti] = 69069 * ret.engine->mt[ret.engine->mti - 1];
- }
-
- return ret;
-}
-
-void prne_free_rnd_engine (prne_rnd_engine_t *engine) {
- prne_free(engine);
-}
-
-uint32_t prne_rnd_gen_int (prne_rnd_engine_t *engine) {
- static const size_t M = 397;
- static const uint32_t
- UPPER_MASK = 0x80000000,
- LOWER_MASK = 0x7fffffff,
- TEMPERING_MASK_B = 0x9d2c5680,
- TEMPERING_MASK_C = 0xefc60000;
- static const uint32_t mag01[2] = {0, 0x9908b0df};
- uint32_t y;
-
- if (engine->mti >= N) {
- size_t kk;
-
- for (kk = 0; kk < N - M; kk += 1) {
- y = (engine->mt[kk] & UPPER_MASK) | (engine->mt[kk + 1] & LOWER_MASK);
- engine->mt[kk] = engine->mt[kk + M] ^ (y >> 1) ^ mag01[y & 1];
- }
- for (; kk < N - 1; kk += 1) {
- y = (engine->mt[kk] & UPPER_MASK) | (engine->mt[kk + 1] & LOWER_MASK);
- engine->mt[kk] = engine->mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 1];
- }
- y = (engine->mt[N - 1] & UPPER_MASK) | (engine->mt[0] & LOWER_MASK);
- engine->mt[N - 1] = engine->mt[M - 1] ^ (y >> 1) ^ mag01[y & 1];
-
- engine->mti = 0;
- }
-
- y = engine->mt[engine->mti];
- engine->mti += 1;
- y ^= y >> 11;
- y ^= (y << 7) & TEMPERING_MASK_B;
- y ^= (y << 15) & TEMPERING_MASK_C;
- y ^= y >> 18;
-
- return y;
-}
-
-double prne_rnd_gen_double (prne_rnd_engine_t *engine) {
- return (double)prne_rnd_gen_int(engine) * 2.3283064370807974e-10;
-}
-
-prne_rnd_engine_t *prne_mk_rnd_engine (void) {
- uint32_t seed = 0;
- prne_rnd_engnie_alloc_result_t ret;
-
- getrandom(&seed, sizeof(uint32_t), 0);
-
- if (seed == 0) {
- // fall back to seeding with what's available.
- seed =
- (uint32_t)(time(NULL) % 0xFFFFFFFF) ^
- (uint32_t)(getpid() % 0xFFFFFFFF) ^
- (uint32_t)(getppid() % 0xFFFFFFFF) ^
- (uint32_t)(clock() % 0xFFFFFFFF);
- }
-
- ret = prne_alloc_rnd_engine(seed == 0 ? NULL : &seed);
- if (ret.result != PRNE_RND_ENGINE_ALLOC_OK) {
- return NULL;
- }
-
- return ret.engine;
-}
diff --git a/src/rnd.h b/src/rnd.h
deleted file mode 100644
index a496e9e..0000000
--- a/src/rnd.h
+++ /dev/null
@@ -1,29 +0,0 @@
-#pragma once
-#include <stddef.h>
-#include <stdint.h>
-#include <stdbool.h>
-
-
-struct prne_rnd_engine;
-typedef struct prne_rnd_engine prne_rnd_engine_t;
-typedef struct prne_rnd_engnie_alloc_result prne_rnd_engnie_alloc_result_t;
-
-typedef enum {
- PRNE_RND_ENGINE_ALLOC_OK,
- PRNE_RND_ENGINE_ALLOC_INVALID_SEED,
- PRNE_RND_ENGINE_ALLOC_MEM_ERR
-} prne_rnd_engine_alloc_result_code_t;
-
-struct prne_rnd_engnie_alloc_result {
- prne_rnd_engine_alloc_result_code_t result;
- prne_rnd_engine_t *engine;
-};
-
-
-void prne_init_alloc_rnd_engine_result (prne_rnd_engnie_alloc_result_t *r);
-prne_rnd_engnie_alloc_result_t prne_alloc_rnd_engine (const uint32_t *seed);
-void prne_free_rnd_engine (prne_rnd_engine_t *engine);
-uint32_t prne_rnd_gen_int (prne_rnd_engine_t *engine);
-double prne_rnd_gen_double (prne_rnd_engine_t *engine);
-
-prne_rnd_engine_t *prne_mk_rnd_engine (void);
diff --git a/src/util_ct.h b/src/util_ct.h
index c8068c7..134ac57 100644
--- a/src/util_ct.h
+++ b/src/util_ct.h
@@ -1,13 +1,15 @@
#pragma once
#include <assert.h>
+#include <stdint.h>
-
-#define PRNE_LIMIT_ENUM(t,x,l) _Static_assert(x<=l,"enum overflow: "#t)
+#define PRNE_LIMIT_ENUM(t,x,l) _Static_assert(x <= l,"enum overflow: "#t)
#define prne_op_min(a, b) (a < b ? a : b)
#define prne_op_max(a, b) (a > b ? a : b)
#define prne_op_spaceship(a, b) (a == b ? 0 : a < b ? -1 : 1)
+#define prne_malign_to(x, align) ((x % align == 0) ? x : (x / align + 1) * align)
+
#if !defined(memzero)
#define memzero(addr, len) memset(addr, 0, len)
#endif
diff --git a/src/util_rt.c b/src/util_rt.c
index c582d08..cdf4526 100644
--- a/src/util_rt.c
+++ b/src/util_rt.c
@@ -446,3 +446,19 @@ bool prne_set_pipe_size (const int fd, const int size) {
#endif
;
}
+
+ssize_t prne_geturandom (void *buf, const size_t len) {
+ const int fd = open("/dev/urandom", O_RDONLY);
+ ssize_t ret;
+ int save_errno;
+
+ if (fd < 0) {
+ return -1;
+ }
+ ret = read(fd, buf, len);
+ save_errno = errno;
+ close(fd);
+ errno = save_errno;
+
+ return ret;
+}
diff --git a/src/util_rt.h b/src/util_rt.h
index 2531f80..9878064 100644
--- a/src/util_rt.h
+++ b/src/util_rt.h
@@ -72,3 +72,6 @@ void prne_free_stdin_base64_rf_ctx (prne_stdin_base64_rf_ctx_t *ctx);
prne_pack_ret_t prne_stdin_base64_rf (void *ctx, const size_t req, uint8_t *out, size_t *out_len);
bool prne_set_pipe_size (const int fd, const int size);
+
+// getrandom polyfill
+ssize_t prne_geturandom (void *buf, const size_t len);