about summary refs log tree commit diff
path: root/src/rt/rust_util.h
blob: 4b0d87880ef9e938758d713bd82c4cf8234a0a1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#ifndef RUST_UTIL_H
#define RUST_UTIL_H

#include <limits.h>
#include "rust_task.h"
#include "rust_env.h"

extern struct type_desc str_body_tydesc;

// Inline fn used regularly elsewhere.

static inline size_t
next_power_of_two(size_t s)
{
    size_t tmp = s - 1;
    tmp |= tmp >> 1;
    tmp |= tmp >> 2;
    tmp |= tmp >> 4;
    tmp |= tmp >> 8;
    tmp |= tmp >> 16;
#ifdef _LP64
    tmp |= tmp >> 32;
#endif
    return tmp + 1;
}

// Rounds |size| to the nearest |alignment|. Invariant: |alignment| is a power
// of two.
template<typename T>
static inline T
align_to(T size, size_t alignment) {
    assert(alignment);
    T x = (T)(((uintptr_t)size + alignment - 1) & ~(alignment - 1));
    return x;
}

// Interior vectors (rust-user-code level).

struct
rust_vec
{
    size_t fill;    // in bytes; if zero, heapified
    size_t alloc;   // in bytes
    uint8_t data[0];
};

struct
rust_vec_box
{
    rust_opaque_box header;
    rust_vec body;
};

template <typename T>
inline size_t vec_size(size_t elems) {
    return sizeof(rust_vec_box) + sizeof(T) * elems;
}

template <typename T>
inline T *
vec_data(rust_vec *v) {
    return reinterpret_cast<T*>(v->data);
}

inline void reserve_vec_exact_shared(rust_task* task, rust_vec_box** vpp,
                                     size_t size) {
    rust_opaque_box** ovpp = (rust_opaque_box**)vpp;
    if (size > (*vpp)->body.alloc) {
        *vpp = (rust_vec_box*)task->boxed.realloc(
            *ovpp, size + sizeof(rust_vec));
        (*vpp)->body.alloc = size;
    }
}

inline void reserve_vec_exact(rust_task* task, rust_vec_box** vpp,
                              size_t size) {
    if (size > (*vpp)->body.alloc) {
        *vpp = (rust_vec_box*)task->kernel
            ->realloc(*vpp, size + sizeof(rust_vec_box));
        (*vpp)->body.alloc = size;
    }
}

inline void reserve_vec(rust_task* task, rust_vec_box** vpp, size_t size) {
    reserve_vec_exact(task, vpp, next_power_of_two(size));
}

typedef rust_vec_box rust_str;

inline rust_str *
make_str(rust_kernel* kernel, const char* c, size_t strlen,
         const char* name) {
    size_t str_fill = strlen + 1;
    size_t str_alloc = str_fill;
    rust_str *str = (rust_str *)
        kernel->malloc(vec_size<char>(str_fill), name);
    str->header.td = &str_body_tydesc;
    str->body.fill = str_fill;
    str->body.alloc = str_alloc;
    memcpy(&str->body.data, c, strlen);
    str->body.data[strlen] = '\0';
    return str;
}

inline rust_vec_box *
make_str_vec(rust_kernel* kernel, size_t nstrs, char **strs) {
    rust_vec_box *v = (rust_vec_box *)
        kernel->malloc(vec_size<rust_vec_box*>(nstrs),
                       "str vec interior");
    // FIXME: should have a real td (Issue #2639)
    v->header.td = NULL;
    v->body.fill = v->body.alloc = sizeof(rust_vec_box*) * nstrs;
    for (size_t i = 0; i < nstrs; ++i) {
        rust_str *str = make_str(kernel, strs[i],
                                 strlen(strs[i]),
                                 "str");
        ((rust_str**)&v->body.data)[i] = str;
    }
    return v;
}

inline size_t get_box_size(size_t body_size, size_t body_align) {
    size_t header_size = sizeof(rust_opaque_box);
    // FIXME (#2699): This alignment calculation is suspicious. Is it right?
    size_t total_size = align_to(header_size, body_align) + body_size;
    return total_size;
}

// Initialization helpers for ISAAC RNG

inline void isaac_seed(rust_kernel* kernel, uint8_t* dest, size_t size)
{
#ifdef __WIN32__
    HCRYPTPROV hProv;
    kernel->win32_require
        (_T("CryptAcquireContext"),
         CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
                             CRYPT_VERIFYCONTEXT|CRYPT_SILENT));
    kernel->win32_require
        (_T("CryptGenRandom"), CryptGenRandom(hProv, size, (BYTE*) dest));
    kernel->win32_require
        (_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));
    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");
        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));
#endif
}

inline void
isaac_init(rust_kernel *kernel, randctx *rctx, rust_vec_box* user_seed)
{
    memset(rctx, 0, sizeof(randctx));

    char *env_seed = kernel->env->rust_seed;
    if (user_seed != NULL) {
        // ignore bytes after the required length
        size_t seed_len = user_seed->body.fill < sizeof(rctx->randrsl)
            ? user_seed->body.fill : sizeof(rctx->randrsl);
        memcpy(&rctx->randrsl, user_seed->body.data, seed_len);
    } else if (env_seed != NULL) {
        ub4 seed = (ub4) atoi(env_seed);
        for (size_t i = 0; i < RANDSIZ; i ++) {
            memcpy(&rctx->randrsl[i], &seed, sizeof(ub4));
            seed = (seed + 0x7ed55d16) + (seed << 12);
        }
    } else {
        isaac_seed(kernel, (uint8_t*) &rctx->randrsl, sizeof(rctx->randrsl));
    }

    randinit(rctx, 1);
}

//
// Local Variables:
// mode: C++
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//

#endif