diff options
| author | Chris Peterson <cpeterson@mozilla.com> | 2013-02-14 00:37:01 -0800 |
|---|---|---|
| committer | Chris Peterson <cpeterson@mozilla.com> | 2013-02-14 22:30:27 -0800 |
| commit | f4320b6195d2704cf5cb5cb7d23f2b6077a0b34c (patch) | |
| tree | f880b688f0e30f39640ea4e30efe3f606ae64b28 | |
| parent | c53150638562959e6020bab887a22cf3d13ecaab (diff) | |
| download | rust-f4320b6195d2704cf5cb5cb7d23f2b6077a0b34c.tar.gz rust-f4320b6195d2704cf5cb5cb7d23f2b6077a0b34c.zip | |
move isaac RNG utility functions to new rust_rng.cpp file
| -rw-r--r-- | mk/rt.mk | 1 | ||||
| -rw-r--r-- | src/rt/rust_globals.h | 1 | ||||
| -rw-r--r-- | src/rt/rust_rng.cpp | 81 | ||||
| -rw-r--r-- | src/rt/rust_rng.h | 34 | ||||
| -rw-r--r-- | src/rt/rust_sched_loop.h | 1 | ||||
| -rw-r--r-- | src/rt/rust_util.h | 59 |
6 files changed, 117 insertions, 60 deletions
diff --git a/mk/rt.mk b/mk/rt.mk index 23dc64dbca5..e6e0f1e0cd7 100644 --- a/mk/rt.mk +++ b/mk/rt.mk @@ -50,6 +50,7 @@ RUNTIME_CXXS_$(1) := \ rt/rust_builtin.cpp \ rt/rust_run_program.cpp \ rt/rust_env.cpp \ + rt/rust_rng.cpp \ rt/rust_sched_loop.cpp \ rt/rust_sched_launcher.cpp \ rt/rust_sched_driver.cpp \ diff --git a/src/rt/rust_globals.h b/src/rt/rust_globals.h index d0116fe2781..3cc8550104a 100644 --- a/src/rt/rust_globals.h +++ b/src/rt/rust_globals.h @@ -37,7 +37,6 @@ #include <math.h> #include <assert.h> -#include "rand.h" #include "uthash.h" #if defined(__WIN32__) diff --git a/src/rt/rust_rng.cpp b/src/rt/rust_rng.cpp new file mode 100644 index 00000000000..5f4522c4978 --- /dev/null +++ b/src/rt/rust_rng.cpp @@ -0,0 +1,81 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#include "rust_globals.h" +#include "rust_rng.h" +#include "rust_util.h" + +// Initialization helpers for ISAAC RNG + +void +isaac_seed(rust_kernel* kernel, uint8_t* dest, size_t size) { +#ifdef __WIN32__ + HCRYPTPROV hProv; + kernel->win32_require + (_T("CryptAcquireContext"), + CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, + CRYPT_VERIFYCONTEXT|CRYPT_SILENT)); + kernel->win32_require + (_T("CryptGenRandom"), CryptGenRandom(hProv, size, (BYTE*) dest)); + kernel->win32_require + (_T("CryptReleaseContext"), CryptReleaseContext(hProv, 0)); +#else + int fd = open("/dev/urandom", O_RDONLY); + if (fd == -1) + kernel->fatal("error opening /dev/urandom: %s", strerror(errno)); + size_t amount = 0; + do { + ssize_t ret = read(fd, dest+amount, size-amount); + if (ret < 0) + kernel->fatal("error reading /dev/urandom: %s", strerror(errno)); + else if (ret == 0) + kernel->fatal("somehow hit eof reading from /dev/urandom"); + amount += (size_t)ret; + } while (amount < size); + int ret = close(fd); + // FIXME #3697: Why does this fail sometimes? + if (ret != 0) + kernel->log(log_warn, "error closing /dev/urandom: %s", + strerror(errno)); +#endif +} + +void +isaac_init(rust_kernel *kernel, randctx *rctx, rust_vec_box* user_seed) { + memset(rctx, 0, sizeof(randctx)); + + char *env_seed = kernel->env->rust_seed; + if (user_seed != NULL) { + // ignore bytes after the required length + size_t seed_len = user_seed->body.fill < sizeof(rctx->randrsl) + ? user_seed->body.fill : sizeof(rctx->randrsl); + memcpy(&rctx->randrsl, user_seed->body.data, seed_len); + } else if (env_seed != NULL) { + ub4 seed = (ub4) atoi(env_seed); + for (size_t i = 0; i < RANDSIZ; i ++) { + memcpy(&rctx->randrsl[i], &seed, sizeof(ub4)); + seed = (seed + 0x7ed55d16) + (seed << 12); + } + } else { + isaac_seed(kernel, (uint8_t*) &rctx->randrsl, sizeof(rctx->randrsl)); + } + + randinit(rctx, 1); +} + +// +// Local Variables: +// mode: C++ +// fill-column: 78; +// indent-tabs-mode: nil +// c-basic-offset: 4 +// buffer-file-coding-system: utf-8-unix +// End: +// diff --git a/src/rt/rust_rng.h b/src/rt/rust_rng.h new file mode 100644 index 00000000000..7f61b615b25 --- /dev/null +++ b/src/rt/rust_rng.h @@ -0,0 +1,34 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#ifndef RUST_RNG_H +#define RUST_RNG_H + +#include "rand.h" + +class rust_kernel; +struct rust_vec_box; + +// Initialization helpers for ISAAC RNG + +void isaac_seed(rust_kernel* kernel, uint8_t* dest, size_t size); +void isaac_init(rust_kernel *kernel, randctx *rctx, rust_vec_box* user_seed); + +// +// Local Variables: +// mode: C++ +// fill-column: 78; +// indent-tabs-mode: nil +// c-basic-offset: 4 +// buffer-file-coding-system: utf-8-unix +// End: +// + +#endif diff --git a/src/rt/rust_sched_loop.h b/src/rt/rust_sched_loop.h index 0105b83e28b..8abec9cf869 100644 --- a/src/rt/rust_sched_loop.h +++ b/src/rt/rust_sched_loop.h @@ -13,6 +13,7 @@ #include "rust_globals.h" #include "rust_log.h" +#include "rust_rng.h" #include "rust_stack.h" #include "rust_signal.h" #include "context.h" diff --git a/src/rt/rust_util.h b/src/rt/rust_util.h index 4b0d87880ef..b5827e0724b 100644 --- a/src/rt/rust_util.h +++ b/src/rt/rust_util.h @@ -136,65 +136,6 @@ inline size_t get_box_size(size_t body_size, size_t body_align) { return total_size; } -// Initialization helpers for ISAAC RNG - -inline void isaac_seed(rust_kernel* kernel, uint8_t* dest, size_t size) -{ -#ifdef __WIN32__ - HCRYPTPROV hProv; - kernel->win32_require - (_T("CryptAcquireContext"), - CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, - CRYPT_VERIFYCONTEXT|CRYPT_SILENT)); - kernel->win32_require - (_T("CryptGenRandom"), CryptGenRandom(hProv, size, (BYTE*) dest)); - kernel->win32_require - (_T("CryptReleaseContext"), CryptReleaseContext(hProv, 0)); -#else - int fd = open("/dev/urandom", O_RDONLY); - if (fd == -1) - kernel->fatal("error opening /dev/urandom: %s", strerror(errno)); - size_t amount = 0; - do { - ssize_t ret = read(fd, dest+amount, size-amount); - if (ret < 0) - kernel->fatal("error reading /dev/urandom: %s", strerror(errno)); - else if (ret == 0) - kernel->fatal("somehow hit eof reading from /dev/urandom"); - amount += (size_t)ret; - } while (amount < size); - int ret = close(fd); - // FIXME #3697: Why does this fail sometimes? - if (ret != 0) - kernel->log(log_warn, "error closing /dev/urandom: %s", - strerror(errno)); -#endif -} - -inline void -isaac_init(rust_kernel *kernel, randctx *rctx, rust_vec_box* user_seed) -{ - memset(rctx, 0, sizeof(randctx)); - - char *env_seed = kernel->env->rust_seed; - if (user_seed != NULL) { - // ignore bytes after the required length - size_t seed_len = user_seed->body.fill < sizeof(rctx->randrsl) - ? user_seed->body.fill : sizeof(rctx->randrsl); - memcpy(&rctx->randrsl, user_seed->body.data, seed_len); - } else if (env_seed != NULL) { - ub4 seed = (ub4) atoi(env_seed); - for (size_t i = 0; i < RANDSIZ; i ++) { - memcpy(&rctx->randrsl[i], &seed, sizeof(ub4)); - seed = (seed + 0x7ed55d16) + (seed << 12); - } - } else { - isaac_seed(kernel, (uint8_t*) &rctx->randrsl, sizeof(rctx->randrsl)); - } - - randinit(rctx, 1); -} - // // Local Variables: // mode: C++ |
