Skip to content

Commit 1692e12

Browse files
committed
---
yaml --- r: 53111 b: refs/heads/dist-snap c: 9a78dc9 h: refs/heads/master i: 53109: ca40ee8 53107: 5f88b06 53103: fda0578 v: v3
1 parent a6735f4 commit 1692e12

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c
99
refs/heads/incoming: 44d4d6de762f3f9aae1fedcf454c66b79b3ad58d
10-
refs/heads/dist-snap: 665e900edeb611a7bfc9b0b911489cb802740945
10+
refs/heads/dist-snap: 9a78dc93db551f325b3b3d90540de6ebe7873b4b
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1313
refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0

branches/dist-snap/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

branches/dist-snap/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
//

branches/dist-snap/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:

branches/dist-snap/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)