| .. | .. |
|---|
| 86 | 86 | .help = "disable\tturn off latent entropy instrumentation\n", |
|---|
| 87 | 87 | }; |
|---|
| 88 | 88 | |
|---|
| 89 | | -static unsigned HOST_WIDE_INT seed; |
|---|
| 90 | | -/* |
|---|
| 91 | | - * get_random_seed() (this is a GCC function) generates the seed. |
|---|
| 92 | | - * This is a simple random generator without any cryptographic security because |
|---|
| 93 | | - * the entropy doesn't come from here. |
|---|
| 94 | | - */ |
|---|
| 89 | +static unsigned HOST_WIDE_INT deterministic_seed; |
|---|
| 90 | +static unsigned HOST_WIDE_INT rnd_buf[32]; |
|---|
| 91 | +static size_t rnd_idx = ARRAY_SIZE(rnd_buf); |
|---|
| 92 | +static int urandom_fd = -1; |
|---|
| 93 | + |
|---|
| 95 | 94 | static unsigned HOST_WIDE_INT get_random_const(void) |
|---|
| 96 | 95 | { |
|---|
| 97 | | - unsigned int i; |
|---|
| 98 | | - unsigned HOST_WIDE_INT ret = 0; |
|---|
| 99 | | - |
|---|
| 100 | | - for (i = 0; i < 8 * sizeof(ret); i++) { |
|---|
| 101 | | - ret = (ret << 1) | (seed & 1); |
|---|
| 102 | | - seed >>= 1; |
|---|
| 103 | | - if (ret & 1) |
|---|
| 104 | | - seed ^= 0xD800000000000000ULL; |
|---|
| 96 | + if (deterministic_seed) { |
|---|
| 97 | + unsigned HOST_WIDE_INT w = deterministic_seed; |
|---|
| 98 | + w ^= w << 13; |
|---|
| 99 | + w ^= w >> 7; |
|---|
| 100 | + w ^= w << 17; |
|---|
| 101 | + deterministic_seed = w; |
|---|
| 102 | + return deterministic_seed; |
|---|
| 105 | 103 | } |
|---|
| 106 | 104 | |
|---|
| 107 | | - return ret; |
|---|
| 105 | + if (urandom_fd < 0) { |
|---|
| 106 | + urandom_fd = open("/dev/urandom", O_RDONLY); |
|---|
| 107 | + gcc_assert(urandom_fd >= 0); |
|---|
| 108 | + } |
|---|
| 109 | + if (rnd_idx >= ARRAY_SIZE(rnd_buf)) { |
|---|
| 110 | + gcc_assert(read(urandom_fd, rnd_buf, sizeof(rnd_buf)) == sizeof(rnd_buf)); |
|---|
| 111 | + rnd_idx = 0; |
|---|
| 112 | + } |
|---|
| 113 | + return rnd_buf[rnd_idx++]; |
|---|
| 108 | 114 | } |
|---|
| 109 | 115 | |
|---|
| 110 | 116 | static tree tree_get_random_const(tree type) |
|---|
| .. | .. |
|---|
| 549 | 555 | tree type, id; |
|---|
| 550 | 556 | int quals; |
|---|
| 551 | 557 | |
|---|
| 552 | | - seed = get_random_seed(false); |
|---|
| 553 | | - |
|---|
| 554 | 558 | if (in_lto_p) |
|---|
| 555 | 559 | return; |
|---|
| 556 | 560 | |
|---|
| .. | .. |
|---|
| 585 | 589 | const struct plugin_argument * const argv = plugin_info->argv; |
|---|
| 586 | 590 | int i; |
|---|
| 587 | 591 | |
|---|
| 592 | + /* |
|---|
| 593 | + * Call get_random_seed() with noinit=true, so that this returns |
|---|
| 594 | + * 0 in the case where no seed has been passed via -frandom-seed. |
|---|
| 595 | + */ |
|---|
| 596 | + deterministic_seed = get_random_seed(true); |
|---|
| 597 | + |
|---|
| 588 | 598 | static const struct ggc_root_tab gt_ggc_r_gt_latent_entropy[] = { |
|---|
| 589 | 599 | { |
|---|
| 590 | 600 | .base = &latent_entropy_decl, |
|---|