rt: Eliminate the dependency on rust_kernel from rust_rng
This commit is contained in:
parent
4cd51c416b
commit
21aaa7fb13
|
@ -88,8 +88,7 @@ rand_seed_size() {
|
|||
|
||||
extern "C" CDECL void
|
||||
rand_gen_seed(uint8_t* dest, size_t size) {
|
||||
rust_task *task = rust_get_current_task();
|
||||
rng_gen_seed(task->kernel, dest, size);
|
||||
rng_gen_seed(dest, size);
|
||||
}
|
||||
|
||||
extern "C" CDECL void *
|
||||
|
@ -101,14 +100,14 @@ rand_new_seeded(uint8_t* seed, size_t seed_size) {
|
|||
task->fail();
|
||||
return NULL;
|
||||
}
|
||||
rng_init(task->kernel, rng, seed, seed_size);
|
||||
char *env_seed = task->kernel->env->rust_seed;
|
||||
rng_init(rng, env_seed, seed, seed_size);
|
||||
return rng;
|
||||
}
|
||||
|
||||
extern "C" CDECL uint32_t
|
||||
rand_next(rust_rng *rng) {
|
||||
rust_task *task = rust_get_current_task();
|
||||
return rng_gen_u32(task->kernel, rng);
|
||||
return rng_gen_u32(rng);
|
||||
}
|
||||
|
||||
extern "C" CDECL void
|
||||
|
|
|
@ -41,7 +41,7 @@ rng_seed_size() {
|
|||
// Initialization helpers for ISAAC RNG
|
||||
|
||||
void
|
||||
rng_gen_seed(rust_kernel* kernel, uint8_t* dest, size_t size) {
|
||||
rng_gen_seed(uint8_t* dest, size_t size) {
|
||||
#ifdef __WIN32__
|
||||
HCRYPTPROV hProv;
|
||||
win32_require
|
||||
|
@ -54,31 +54,37 @@ rng_gen_seed(rust_kernel* kernel, uint8_t* dest, size_t size) {
|
|||
(_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));
|
||||
if (fd == -1) {
|
||||
fprintf(stderr, "error opening /dev/urandom: %s", strerror(errno));
|
||||
abort();
|
||||
}
|
||||
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");
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "error reading /dev/urandom: %s", strerror(errno));
|
||||
abort();
|
||||
}
|
||||
else if (ret == 0) {
|
||||
fprintf(stderr, "somehow hit eof reading from /dev/urandom");
|
||||
abort();
|
||||
}
|
||||
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));
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "error closing /dev/urandom: %s", strerror(errno));
|
||||
// FIXME #3697: Why does this fail sometimes?
|
||||
// abort();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static void
|
||||
isaac_init(rust_kernel *kernel, randctx *rctx,
|
||||
isaac_init(randctx *rctx, char *env_seed,
|
||||
uint8_t* user_seed, size_t seed_len) {
|
||||
memset(rctx, 0, sizeof(randctx));
|
||||
|
||||
char *env_seed = kernel->env->rust_seed;
|
||||
if (user_seed != NULL) {
|
||||
// ignore bytes after the required length
|
||||
if (seed_len > sizeof(rctx->randrsl)) {
|
||||
|
@ -92,8 +98,7 @@ isaac_init(rust_kernel *kernel, randctx *rctx,
|
|||
seed = (seed + 0x7ed55d16) + (seed << 12);
|
||||
}
|
||||
} else {
|
||||
rng_gen_seed(kernel,
|
||||
(uint8_t*)&rctx->randrsl,
|
||||
rng_gen_seed((uint8_t*)&rctx->randrsl,
|
||||
sizeof(rctx->randrsl));
|
||||
}
|
||||
|
||||
|
@ -101,14 +106,14 @@ isaac_init(rust_kernel *kernel, randctx *rctx,
|
|||
}
|
||||
|
||||
void
|
||||
rng_init(rust_kernel* kernel, rust_rng* rng,
|
||||
rng_init(rust_rng* rng, char* env_seed,
|
||||
uint8_t *user_seed, size_t seed_len) {
|
||||
isaac_init(kernel, &rng->rctx, user_seed, seed_len);
|
||||
rng->reseedable = !user_seed && !kernel->env->rust_seed;
|
||||
isaac_init(&rng->rctx, env_seed, user_seed, seed_len);
|
||||
rng->reseedable = !user_seed && !env_seed;
|
||||
}
|
||||
|
||||
static void
|
||||
rng_maybe_reseed(rust_kernel* kernel, rust_rng* rng) {
|
||||
rng_maybe_reseed(rust_rng* rng) {
|
||||
// If this RNG has generated more than 32KB of random data and was not
|
||||
// seeded by the user or RUST_SEED, then we should reseed now.
|
||||
const size_t RESEED_THRESHOLD = 32 * 1024;
|
||||
|
@ -116,16 +121,15 @@ rng_maybe_reseed(rust_kernel* kernel, rust_rng* rng) {
|
|||
if (bytes_generated < RESEED_THRESHOLD || !rng->reseedable) {
|
||||
return;
|
||||
}
|
||||
rng_gen_seed(kernel,
|
||||
(uint8_t*)rng->rctx.randrsl,
|
||||
rng_gen_seed((uint8_t*)rng->rctx.randrsl,
|
||||
sizeof(rng->rctx.randrsl));
|
||||
randinit(&rng->rctx, 1);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
rng_gen_u32(rust_kernel* kernel, rust_rng* rng) {
|
||||
rng_gen_u32(rust_rng* rng) {
|
||||
uint32_t x = isaac_rand(&rng->rctx);
|
||||
rng_maybe_reseed(kernel, rng);
|
||||
rng_maybe_reseed(rng);
|
||||
return x;
|
||||
}
|
||||
|
||||
|
|
|
@ -23,11 +23,10 @@ struct rust_rng {
|
|||
};
|
||||
|
||||
size_t rng_seed_size();
|
||||
void rng_gen_seed(rust_kernel* kernel,
|
||||
uint8_t* dest, size_t size);
|
||||
void rng_init(rust_kernel *kernel, rust_rng *rng,
|
||||
void rng_gen_seed(uint8_t* dest, size_t size);
|
||||
void rng_init(rust_rng *rng, char *env_seed,
|
||||
uint8_t *user_seed, size_t seed_len);
|
||||
uint32_t rng_gen_u32(rust_kernel *kernel, rust_rng *rng);
|
||||
uint32_t rng_gen_u32(rust_rng *rng);
|
||||
|
||||
//
|
||||
// Local Variables:
|
||||
|
|
|
@ -43,7 +43,7 @@ rust_sched_loop::rust_sched_loop(rust_scheduler *sched, int id, bool killed) :
|
|||
name("main")
|
||||
{
|
||||
LOGPTR(this, "new dom", (uintptr_t)this);
|
||||
rng_init(kernel, &rng, NULL, 0);
|
||||
rng_init(&rng, kernel->env->rust_seed, NULL, 0);
|
||||
|
||||
if (!tls_initialized)
|
||||
init_tls();
|
||||
|
@ -154,7 +154,7 @@ rust_sched_loop::schedule_task() {
|
|||
lock.must_have_lock();
|
||||
size_t tasks = running_tasks.length();
|
||||
if (tasks > 0) {
|
||||
size_t i = (tasks > 1) ? (rng_gen_u32(kernel, &rng) % tasks) : 0;
|
||||
size_t i = (tasks > 1) ? (rng_gen_u32(&rng) % tasks) : 0;
|
||||
return running_tasks[i];
|
||||
}
|
||||
return NULL;
|
||||
|
|
Loading…
Reference in New Issue