Skip to content

Commit 9a78dc9

Browse files
committed
reseed rust_rng after generating 32KB
1 parent 665e900 commit 9a78dc9

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

src/rt/rust_builtin.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ rand_new_seeded2(rust_vec_box** seed) {
173173

174174
extern "C" CDECL uint32_t
175175
rand_next(rust_rng *rng) {
176-
return rng_gen_u32(rng);
176+
rust_task *task = rust_get_current_task();
177+
return rng_gen_u32(task->kernel, rng);
177178
}
178179

179180
extern "C" CDECL void

src/rt/rust_rng.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,35 @@ isaac_init(rust_kernel *kernel, randctx *rctx, rust_vec_box* user_seed) {
7373
void
7474
rng_init(rust_kernel* kernel, rust_rng* rng, rust_vec_box* user_seed) {
7575
isaac_init(kernel, &rng->rctx, user_seed);
76+
rng->reseedable = !user_seed && !kernel->env->rust_seed;
77+
}
78+
79+
static void
80+
rng_maybe_reseed(rust_kernel* kernel, rust_rng* rng) {
81+
// If this RNG has generated more than 32KB of random data and was not
82+
// seeded by the user or RUST_SEED, then we should reseed now.
83+
const size_t RESEED_THRESHOLD = 32 * 1024;
84+
size_t bytes_generated = rng->rctx.randc * sizeof(ub4);
85+
if (bytes_generated < RESEED_THRESHOLD || !rng->reseedable) {
86+
return;
87+
}
88+
89+
uint32_t new_seed[RANDSIZ];
90+
rng_gen_seed(kernel, (uint8_t*) new_seed, RANDSIZ * sizeof(uint32_t));
91+
92+
// Stir new seed into PRNG's entropy pool.
93+
for (size_t i = 0; i < RANDSIZ; i++) {
94+
rng->rctx.randrsl[i] ^= new_seed[i];
95+
}
96+
97+
randinit(&rng->rctx, 1);
7698
}
7799

78100
uint32_t
79-
rng_gen_u32(rust_rng* rng) {
80-
return isaac_rand(&rng->rctx);
101+
rng_gen_u32(rust_kernel* kernel, rust_rng* rng) {
102+
uint32_t x = isaac_rand(&rng->rctx);
103+
rng_maybe_reseed(kernel, rng);
104+
return x;
81105
}
82106

83107
//

src/rt/rust_rng.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ struct rust_vec_box;
2020

2121
struct rust_rng {
2222
randctx rctx;
23+
bool reseedable;
2324
};
2425

2526
void rng_gen_seed(rust_kernel* kernel, uint8_t* dest, size_t size);
2627
void rng_init(rust_kernel *kernel, rust_rng *rng, rust_vec_box* user_seed);
27-
uint32_t rng_gen_u32(rust_rng *rng);
28+
uint32_t rng_gen_u32(rust_kernel *kernel, rust_rng *rng);
2829

2930
//
3031
// Local Variables:

src/rt/rust_sched_loop.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ rust_task *
151151
rust_sched_loop::schedule_task() {
152152
lock.must_have_lock();
153153
if (running_tasks.length() > 0) {
154-
size_t k = rng_gen_u32(&rng);
154+
size_t k = rng_gen_u32(kernel, &rng);
155155
size_t i = k % running_tasks.length();
156156
return (rust_task *)running_tasks[i];
157157
}

0 commit comments

Comments
 (0)