Skip to content

Commit f4320b6

Browse files
committed
move isaac RNG utility functions to new rust_rng.cpp file
1 parent c531506 commit f4320b6

File tree

6 files changed

+117
-60
lines changed

6 files changed

+117
-60
lines changed

mk/rt.mk

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ RUNTIME_CXXS_$(1) := \
5050
rt/rust_builtin.cpp \
5151
rt/rust_run_program.cpp \
5252
rt/rust_env.cpp \
53+
rt/rust_rng.cpp \
5354
rt/rust_sched_loop.cpp \
5455
rt/rust_sched_launcher.cpp \
5556
rt/rust_sched_driver.cpp \

src/rt/rust_globals.h

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#include <math.h>
3838
#include <assert.h>
3939

40-
#include "rand.h"
4140
#include "uthash.h"
4241

4342
#if defined(__WIN32__)

src/rt/rust_rng.cpp

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#include "rust_globals.h"
12+
#include "rust_rng.h"
13+
#include "rust_util.h"
14+
15+
// Initialization helpers for ISAAC RNG
16+
17+
void
18+
isaac_seed(rust_kernel* kernel, uint8_t* dest, size_t size) {
19+
#ifdef __WIN32__
20+
HCRYPTPROV hProv;
21+
kernel->win32_require
22+
(_T("CryptAcquireContext"),
23+
CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
24+
CRYPT_VERIFYCONTEXT|CRYPT_SILENT));
25+
kernel->win32_require
26+
(_T("CryptGenRandom"), CryptGenRandom(hProv, size, (BYTE*) dest));
27+
kernel->win32_require
28+
(_T("CryptReleaseContext"), CryptReleaseContext(hProv, 0));
29+
#else
30+
int fd = open("/dev/urandom", O_RDONLY);
31+
if (fd == -1)
32+
kernel->fatal("error opening /dev/urandom: %s", strerror(errno));
33+
size_t amount = 0;
34+
do {
35+
ssize_t ret = read(fd, dest+amount, size-amount);
36+
if (ret < 0)
37+
kernel->fatal("error reading /dev/urandom: %s", strerror(errno));
38+
else if (ret == 0)
39+
kernel->fatal("somehow hit eof reading from /dev/urandom");
40+
amount += (size_t)ret;
41+
} while (amount < size);
42+
int ret = close(fd);
43+
// FIXME #3697: Why does this fail sometimes?
44+
if (ret != 0)
45+
kernel->log(log_warn, "error closing /dev/urandom: %s",
46+
strerror(errno));
47+
#endif
48+
}
49+
50+
void
51+
isaac_init(rust_kernel *kernel, randctx *rctx, rust_vec_box* user_seed) {
52+
memset(rctx, 0, sizeof(randctx));
53+
54+
char *env_seed = kernel->env->rust_seed;
55+
if (user_seed != NULL) {
56+
// ignore bytes after the required length
57+
size_t seed_len = user_seed->body.fill < sizeof(rctx->randrsl)
58+
? user_seed->body.fill : sizeof(rctx->randrsl);
59+
memcpy(&rctx->randrsl, user_seed->body.data, seed_len);
60+
} else if (env_seed != NULL) {
61+
ub4 seed = (ub4) atoi(env_seed);
62+
for (size_t i = 0; i < RANDSIZ; i ++) {
63+
memcpy(&rctx->randrsl[i], &seed, sizeof(ub4));
64+
seed = (seed + 0x7ed55d16) + (seed << 12);
65+
}
66+
} else {
67+
isaac_seed(kernel, (uint8_t*) &rctx->randrsl, sizeof(rctx->randrsl));
68+
}
69+
70+
randinit(rctx, 1);
71+
}
72+
73+
//
74+
// Local Variables:
75+
// mode: C++
76+
// fill-column: 78;
77+
// indent-tabs-mode: nil
78+
// c-basic-offset: 4
79+
// buffer-file-coding-system: utf-8-unix
80+
// End:
81+
//

src/rt/rust_rng.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#ifndef RUST_RNG_H
12+
#define RUST_RNG_H
13+
14+
#include "rand.h"
15+
16+
class rust_kernel;
17+
struct rust_vec_box;
18+
19+
// Initialization helpers for ISAAC RNG
20+
21+
void isaac_seed(rust_kernel* kernel, uint8_t* dest, size_t size);
22+
void isaac_init(rust_kernel *kernel, randctx *rctx, rust_vec_box* user_seed);
23+
24+
//
25+
// Local Variables:
26+
// mode: C++
27+
// fill-column: 78;
28+
// indent-tabs-mode: nil
29+
// c-basic-offset: 4
30+
// buffer-file-coding-system: utf-8-unix
31+
// End:
32+
//
33+
34+
#endif

src/rt/rust_sched_loop.h

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "rust_globals.h"
1515
#include "rust_log.h"
16+
#include "rust_rng.h"
1617
#include "rust_stack.h"
1718
#include "rust_signal.h"
1819
#include "context.h"

src/rt/rust_util.h

-59
Original file line numberDiff line numberDiff line change
@@ -136,65 +136,6 @@ inline size_t get_box_size(size_t body_size, size_t body_align) {
136136
return total_size;
137137
}
138138

139-
// Initialization helpers for ISAAC RNG
140-
141-
inline void isaac_seed(rust_kernel* kernel, uint8_t* dest, size_t size)
142-
{
143-
#ifdef __WIN32__
144-
HCRYPTPROV hProv;
145-
kernel->win32_require
146-
(_T("CryptAcquireContext"),
147-
CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
148-
CRYPT_VERIFYCONTEXT|CRYPT_SILENT));
149-
kernel->win32_require
150-
(_T("CryptGenRandom"), CryptGenRandom(hProv, size, (BYTE*) dest));
151-
kernel->win32_require
152-
(_T("CryptReleaseContext"), CryptReleaseContext(hProv, 0));
153-
#else
154-
int fd = open("/dev/urandom", O_RDONLY);
155-
if (fd == -1)
156-
kernel->fatal("error opening /dev/urandom: %s", strerror(errno));
157-
size_t amount = 0;
158-
do {
159-
ssize_t ret = read(fd, dest+amount, size-amount);
160-
if (ret < 0)
161-
kernel->fatal("error reading /dev/urandom: %s", strerror(errno));
162-
else if (ret == 0)
163-
kernel->fatal("somehow hit eof reading from /dev/urandom");
164-
amount += (size_t)ret;
165-
} while (amount < size);
166-
int ret = close(fd);
167-
// FIXME #3697: Why does this fail sometimes?
168-
if (ret != 0)
169-
kernel->log(log_warn, "error closing /dev/urandom: %s",
170-
strerror(errno));
171-
#endif
172-
}
173-
174-
inline void
175-
isaac_init(rust_kernel *kernel, randctx *rctx, rust_vec_box* user_seed)
176-
{
177-
memset(rctx, 0, sizeof(randctx));
178-
179-
char *env_seed = kernel->env->rust_seed;
180-
if (user_seed != NULL) {
181-
// ignore bytes after the required length
182-
size_t seed_len = user_seed->body.fill < sizeof(rctx->randrsl)
183-
? user_seed->body.fill : sizeof(rctx->randrsl);
184-
memcpy(&rctx->randrsl, user_seed->body.data, seed_len);
185-
} else if (env_seed != NULL) {
186-
ub4 seed = (ub4) atoi(env_seed);
187-
for (size_t i = 0; i < RANDSIZ; i ++) {
188-
memcpy(&rctx->randrsl[i], &seed, sizeof(ub4));
189-
seed = (seed + 0x7ed55d16) + (seed << 12);
190-
}
191-
} else {
192-
isaac_seed(kernel, (uint8_t*) &rctx->randrsl, sizeof(rctx->randrsl));
193-
}
194-
195-
randinit(rctx, 1);
196-
}
197-
198139
//
199140
// Local Variables:
200141
// mode: C++

0 commit comments

Comments
 (0)