From a8c19d9bae9aebb2263c853fe160c2a09286319f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C5=8Dshin?= Date: Fri, 4 Jan 2019 14:15:23 -0800 Subject: [PATCH 1/3] c3_rand cleanup This should work on Linux since kernel 3.17, OS X since 10.12, OpenBSD since 5.6, and FreeBSD since 12.0. It also introduces no external dependencies. Note that getentropy should only be called with at most 256 bytes or it will return EIO. The getrandom variant on Linux doesn't check for this. If called with len > 256, it's allowed to produce a short read if it gets interrupted. This should show up as an error, so everything's fine. --- include/c/defs.h | 5 +++++ include/c/portable.h | 11 ++++++++++- include/vere/vere.h | 5 ----- vere/sist.c | 24 +++++------------------- 4 files changed, 20 insertions(+), 25 deletions(-) diff --git a/include/c/defs.h b/include/c/defs.h index 4f4ba37b7d..1ffb165149 100644 --- a/include/c/defs.h +++ b/include/c/defs.h @@ -55,6 +55,11 @@ int c3_cooked(); + /* Fill 16 words (64 bytes) with high-quality entropy. + */ + void + c3_rand(c3_w* rad_w); + /* Short integers. */ # define c3_s1(a) ( (a) ) diff --git a/include/c/portable.h b/include/c/portable.h index 7595921a91..d4ac5ac7ec 100644 --- a/include/c/portable.h +++ b/include/c/portable.h @@ -33,6 +33,7 @@ # include # include # include +# include # include # include # include @@ -50,6 +51,7 @@ # include # include # include +# include # include # include # include @@ -188,7 +190,14 @@ /* Entropy */ -#define c3_rand u3_sist_rand +# if defined(U3_OS_linux) +# define c3_getentropy(B, L) \ + ((L) == syscall(SYS_getrandom, B, L, 0) ? 0 : -1) +# elif defined(U3_OS_bsd) || defined(U3_OS_osx) +# define c3_getentropy getentropy +# else +# error "port: getentropy" +# endif /* Static assertion */ diff --git a/include/vere/vere.h b/include/vere/vere.h index fe54c6f3a1..c701a4b223 100644 --- a/include/vere/vere.h +++ b/include/vere/vere.h @@ -1180,11 +1180,6 @@ void u3_sist_get(const c3_c* key_c, c3_y* val_y); - /* u3_sist_rand(): fill 8 words (32 bytes) with high-quality entropy. - */ - void - u3_sist_rand(c3_w* rad_w); - /** HTTP client. **/ /* u3_cttp_ef_thus(): send %thus effect to cttp. diff --git a/vere/sist.c b/vere/sist.c index 04f32f6597..ef9bd2087b 100644 --- a/vere/sist.c +++ b/vere/sist.c @@ -12,12 +12,6 @@ #include "all.h" #include "vere/vere.h" -#if defined(U3_OS_linux) -#define DEVRANDOM "/dev/urandom" -#else -#define DEVRANDOM "/dev/random" -#endif - /* u3_sist_pack(): write a blob to disk, transferring. */ c3_d @@ -411,23 +405,15 @@ _sist_bask(c3_c* pop_c, u3_noun may) } #endif -/* u3_sist_rand(): fill a 512-bit (16-word) buffer. +/* c3_rand(): fill a 512-bit (16-word) buffer. */ void -u3_sist_rand(c3_w* rad_w) +c3_rand(c3_w* rad_w) { -#if defined(U3_OS_bsd) && defined(__OpenBSD__) - if (-1 == getentropy(rad_w, 64)) { - c3_assert(!"lo_rand"); + if ( 0 != c3_getentropy(rad_w, 64) ) { + uL(fprintf(uH, "c3_rand getentropy: %s\n", strerror(errno))); + u3_lo_bail(); } -#else - c3_i fid_i = open(DEVRANDOM, O_RDONLY); - - if ( 64 != read(fid_i, (c3_y*) rad_w, 64) ) { - c3_assert(!"lo_rand"); - } - close(fid_i); -#endif } /* _sist_fast(): offer to save passcode by mug in home directory. From e253be75cc8b7243a3526b1ba330cef744f9ab4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C5=8Dshin?= Date: Fri, 4 Jan 2019 14:44:23 -0800 Subject: [PATCH 2/3] Consistent indentation in portable.h Also remove the gcc/C99 comment. --- include/c/portable.h | 173 +++++++++++++++++++++---------------------- 1 file changed, 85 insertions(+), 88 deletions(-) diff --git a/include/c/portable.h b/include/c/portable.h index d4ac5ac7ec..2314e7dedc 100644 --- a/include/c/portable.h +++ b/include/c/portable.h @@ -2,17 +2,15 @@ ** ** This file is in the public domain. */ - /** Must be compiled on gcc with C99 support. - **/ #include "config.h" -# ifndef __GNUC__ -# error "port me" -# endif -# ifndef _GNU_SOURCE -# define _GNU_SOURCE -# endif +# ifndef __GNUC__ +# error "port me" +# endif +# ifndef _GNU_SOURCE +# define _GNU_SOURCE +# endif /** System include files. @@ -79,26 +77,26 @@ /** Address space layout. **/ -# if defined(U3_OS_linux) -# define U3_OS_LoomBase 0x36000000 -# define U3_OS_LoomBits 29 // ie, 2^29 words == 2GB -# elif defined(U3_OS_osx) -# ifdef __LP64__ -# define U3_OS_LoomBase 0x200000000 +# if defined(U3_OS_linux) +# define U3_OS_LoomBase 0x36000000 +# define U3_OS_LoomBits 29 // ie, 2^29 words == 2GB +# elif defined(U3_OS_osx) +# ifdef __LP64__ +# define U3_OS_LoomBase 0x200000000 +# else +# define U3_OS_LoomBase 0x4000000 +# endif +# define U3_OS_LoomBits 29 // ie, 2^29 words == 2GB +# elif defined(U3_OS_bsd) +# ifdef __LP64__ +# define U3_OS_LoomBase 0x200000000 +# else +# define U3_OS_LoomBase 0x4000000 +# endif +# define U3_OS_LoomBits 29 // ie, 2^29 words == 2GB # else -# define U3_OS_LoomBase 0x4000000 +# error "port: LoomBase" # endif -# define U3_OS_LoomBits 29 // ie, 2^29 words == 2GB -# elif defined(U3_OS_bsd) -# ifdef __LP64__ -# define U3_OS_LoomBase 0x200000000 -# else -# define U3_OS_LoomBase 0x4000000 -# endif -# define U3_OS_LoomBits 29 // ie, 2^29 words == 2GB -# else -# error "port: LoomBase" -# endif /** Global variable control. *** @@ -114,7 +112,7 @@ /* The GMP (GNU arbitrary-precision arithmetic) library. ** (Tested with version 4.0.1.) */ -# include +# include /** Private C "extensions." @@ -138,70 +136,69 @@ /* Byte swapping. */ -# if defined(U3_OS_linux) || defined(U3_OS_bsd) -# define c3_bswap_16(x) bswap_16(x) -# define c3_bswap_32(x) bswap_32(x) -# define c3_bswap_64(x) bswap_64(x) +# if defined(U3_OS_linux) || defined(U3_OS_bsd) +# define c3_bswap_16(x) bswap_16(x) +# define c3_bswap_32(x) bswap_32(x) +# define c3_bswap_64(x) bswap_64(x) +# elif defined(U3_OS_osx) +# define c3_bswap_16(x) NXSwapShort(x) +# define c3_bswap_32(x) NXSwapInt(x) +# define c3_bswap_64(x) NXSwapLongLong(x) +# else +# error "port: byte swap" +# endif -# elif defined(U3_OS_osx) -# define c3_bswap_16(x) NXSwapShort(x) -# define c3_bswap_32(x) NXSwapInt(x) -# define c3_bswap_64(x) NXSwapLongLong(x) -# else -# error "port: byte swap" -# endif + /* Sync. + */ +# if defined(U3_OS_linux) +# define c3_sync(fd) (fdatasync(fd)) +# elif defined(U3_OS_osx) +# define c3_sync(fd) (fcntl(fd, F_FULLFSYNC, 0)) +# elif defined(U3_OS_bsd) +# define c3_sync(fd) (fsync(fd)) +# else +# error "port: sync" +# endif -/* Sync - */ -# if defined(U3_OS_linux) -# define c3_sync(fd) (fdatasync(fd)) -# elif defined(U3_OS_osx) -# define c3_sync(fd) (fcntl(fd, F_FULLFSYNC, 0)) -# elif defined(U3_OS_bsd) -# define c3_sync(fd) (fsync(fd)) -# else -# error "port: sync" -# endif + /* Purge. + */ +# if defined(U3_OS_linux) +# include +# define c3_fpurge __fpurge +# elif defined(U3_OS_bsd) || defined(U3_OS_osx) +# define c3_fpurge fpurge +# else +# error "port: fpurge" +# endif -/* Purge - */ -# if defined(U3_OS_linux) -# include -# define c3_fpurge __fpurge -# elif defined(U3_OS_bsd) || defined(U3_OS_osx) -# define c3_fpurge fpurge -# else -# error "port: fpurge" -# endif + /* Stat. + */ +# if defined(U3_OS_linux) +# define c3_stat_mtime(dp) (u3_time_t_in_ts((dp)->st_mtime)) +# elif defined(U3_OS_osx) +# define c3_stat_mtime(dp) (u3_time_in_ts(&((dp)->st_mtimespec))) +# define lseek64 lseek +# elif defined(U3_OS_bsd) +# define c3_stat_mtime(dp) (u3_time_in_ts(&((dp)->st_mtim))) +# define lseek64 lseek +# else +# error "port: timeconvert" +# endif -/* Stat struct - */ -# if defined(U3_OS_linux) -# define c3_stat_mtime(dp) (u3_time_t_in_ts((dp)->st_mtime)) -# elif defined(U3_OS_osx) -# define c3_stat_mtime(dp) (u3_time_in_ts(&((dp)->st_mtimespec))) -# define lseek64 lseek -# elif defined(U3_OS_bsd) -# define c3_stat_mtime(dp) (u3_time_in_ts(&((dp)->st_mtim))) -# define lseek64 lseek -# else -# error "port: timeconvert" -# endif + /* Entropy. + */ +# if defined(U3_OS_linux) +# define c3_getentropy(B, L) \ + ((L) == syscall(SYS_getrandom, B, L, 0) ? 0 : -1) +# elif defined(U3_OS_bsd) || defined(U3_OS_osx) +# define c3_getentropy getentropy +# else +# error "port: getentropy" +# endif -/* Entropy - */ -# if defined(U3_OS_linux) -# define c3_getentropy(B, L) \ - ((L) == syscall(SYS_getrandom, B, L, 0) ? 0 : -1) -# elif defined(U3_OS_bsd) || defined(U3_OS_osx) -# define c3_getentropy getentropy -# else -# error "port: getentropy" -# endif - -/* Static assertion - */ -#define ASSERT_CONCAT_(a, b) a##b -#define ASSERT_CONCAT(a, b) ASSERT_CONCAT_(a, b) -#define STATIC_ASSERT(e,m) \ + /* Static assertion. + */ +# define ASSERT_CONCAT_(a, b) a##b +# define ASSERT_CONCAT(a, b) ASSERT_CONCAT_(a, b) +# define STATIC_ASSERT(e,m) \ ;enum { ASSERT_CONCAT(assert_line_, __LINE__) = 1/(int)(!!(e)) } From 23cc08d2242745150fef6930e8ff23476f1c2aa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C5=8Dshin?= Date: Fri, 4 Jan 2019 14:48:54 -0800 Subject: [PATCH 3/3] xenial in .travis.yml Necessitates pulling in setuptools and wheel explicitly so meson can build. --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 3c645ce07c..32b5bd9f46 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,4 @@ +dist: xenial language: node_js node_js: - 4 @@ -32,6 +33,8 @@ addons: packages: - python3 - python3-pip + - python3-setuptools + - python3-wheel - libgmp3-dev - libsigsegv-dev - openssl