summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/random.c
blob: 01a1dab3a6aff04136e2db0047d2dce6d5d71e94 (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
// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM
// Exceptions. See
// https://github.com/bytecodealliance/wasmtime/blob/main/LICENSE for license
// information.
//
// Significant parts of this file are derived from cloudabi-utils. See
// https://github.com/bytecodealliance/wasmtime/blob/main/lib/wasi/sandboxed-system-primitives/src/LICENSE
// for license information.
//
// The upstream file contains the following copyright notice:
//
// Copyright (c) 2016 Nuxi, https://nuxi.nl/

#include "ssp_config.h"
#include "bh_platform.h"
#include "random.h"

#if CONFIG_HAS_ARC4RANDOM_BUF

void
random_buf(void *buf, size_t len)
{
    arc4random_buf(buf, len);
}

#elif CONFIG_HAS_GETRANDOM

#ifndef BH_PLATFORM_LINUX_SGX
#include <sys/random.h>
#endif

void
random_buf(void *buf, size_t len)
{
    for (;;) {
        ssize_t x = getrandom(buf, len, 0);
        if (x < 0) {
            if (errno == EINTR)
                continue;
            os_printf("getrandom failed: %s", strerror(errno));
            abort();
        }
        if ((size_t)x == len)
            return;
        buf = (void *)((unsigned char *)buf + x);
        len -= (size_t)x;
    }
}

#else

static int urandom;

static void
open_urandom(void)
{
    urandom = open("/dev/urandom", O_RDONLY);
    if (urandom < 0) {
        os_printf("Failed to open /dev/urandom\n");
        abort();
    }
}

void
random_buf(void *buf, size_t len)
{
    static pthread_once_t open_once = PTHREAD_ONCE_INIT;
    pthread_once(&open_once, open_urandom);

    if ((size_t)read(urandom, buf, len) != len) {
        os_printf("Short read on /dev/urandom\n");
        abort();
    }
}

#endif

// Calculates a random number within the range [0, upper - 1] without
// any modulo bias.
//
// The function below repeatedly obtains a random number from
// arc4random() until it lies within the range [2^k % upper, 2^k). As
// this range has length k * upper, we can safely obtain a number
// without any modulo bias.
uintmax_t
random_uniform(uintmax_t upper)
{
    // Compute 2^k % upper
    //      == (2^k - upper) % upper
    //      == -upper % upper.
    uintmax_t lower = -upper % upper;
    for (;;) {
        uintmax_t value;
        random_buf(&value, sizeof(value));
        if (value >= lower)
            return value % upper;
    }
}