Skip to content

Commit

Permalink
Seed PRNGs with actual entropy.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmacnak committed Oct 5, 2024
1 parent 4286aa3 commit 985f41e
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 29 deletions.
1 change: 1 addition & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ executable("vm") {
"vm/port.h",
"vm/primitives.cc",
"vm/primitives.h",
"vm/random.cc",
"vm/random.h",
"vm/snapshot.cc",
"vm/snapshot.h",
Expand Down
1 change: 1 addition & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ def BuildVM(cxx, arch, target_os, debug, sanitize):
'os_win',
'port',
'primitives',
'random',
'snapshot',
'thread_emscripten',
'thread_pool',
Expand Down
9 changes: 4 additions & 5 deletions vm/isolate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ void Isolate::PrintStack() {
interpreter_->PrintStack();
}

Isolate::Isolate(const void* snapshot, size_t snapshot_length, uint64_t seed)
Isolate::Isolate(const void* snapshot, size_t snapshot_length)
: heap_(nullptr),
interpreter_(nullptr),
loop_(nullptr),
snapshot_(snapshot),
snapshot_length_(snapshot_length),
salt_(static_cast<uintptr_t>(seed)),
random_(seed),
random_(),
salt_(static_cast<uintptr_t>(random_.NextUInt64())),
next_(nullptr) {
heap_ = new Heap();
interpreter_ = new Interpreter(heap_, this);
Expand Down Expand Up @@ -204,8 +204,7 @@ class SpawnIsolateTask : public ThreadPool::Task {
initial_message_(initial_message) {}

virtual void Run() {
uint64_t seed = OS::CurrentMonotonicNanos();
Isolate* child_isolate = new Isolate(snapshot_, snapshot_length_, seed);
Isolate* child_isolate = new Isolate(snapshot_, snapshot_length_);
child_isolate->loop()->PostMessage(initial_message_);
initial_message_ = nullptr;
intptr_t exit_code = child_isolate->loop()->Run();
Expand Down
8 changes: 4 additions & 4 deletions vm/isolate.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ThreadPool;

class Isolate {
public:
Isolate(const void* snapshot, size_t snapshot_length, uint64_t seed);
Isolate(const void* snapshot, size_t snapshot_length);
~Isolate();

Heap* heap() const { return heap_; }
Expand Down Expand Up @@ -54,10 +54,10 @@ class Isolate {
Heap* heap_;
Interpreter* interpreter_;
MessageLoop* loop_;
const void* snapshot_;
size_t snapshot_length_;
uintptr_t salt_;
const void* const snapshot_;
const size_t snapshot_length_;
Random random_;
const uintptr_t salt_;
Isolate* next_;

void AddIsolateToList(Isolate* isolate);
Expand Down
3 changes: 1 addition & 2 deletions vm/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ int main(int argc, const char** argv) {
psoup::Isolate::Startup();
void (*defaultSIGINT)(int) = signal(SIGINT, SIGINT_handler);

uint64_t seed = psoup::OS::CurrentMonotonicNanos();
psoup::Isolate* isolate = new psoup::Isolate(snapshot.address(),
snapshot.size(), seed);
snapshot.size());
isolate->loop()->PostMessage(new psoup::IsolateMessage(ILLEGAL_PORT,
argc - 2, &argv[2]));
intptr_t exit_code = isolate->loop()->Run();
Expand Down
3 changes: 1 addition & 2 deletions vm/main_emscripten.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ extern "C" void load_snapshot(const void* snapshot, size_t snapshot_length) {
psoup::Isolate::Startup();
_JS_initializeAliens();

uint64_t seed = psoup::OS::CurrentMonotonicNanos();
isolate = new psoup::Isolate(snapshot, snapshot_length, seed);
isolate = new psoup::Isolate(snapshot, snapshot_length);
int argc = 0;
const char** argv = nullptr;
isolate->loop()->PostMessage(new psoup::IsolateMessage(ILLEGAL_PORT,
Expand Down
9 changes: 4 additions & 5 deletions vm/port.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "vm/port.h"

#include "vm/flags.h"
#include "vm/lockers.h"
#include "vm/message_loop.h"
#include "vm/os.h"
Expand All @@ -16,7 +15,7 @@ namespace psoup {

Mutex* PortMap::mutex_ = nullptr;
PortMap::Entry* PortMap::map_ = nullptr;
MessageLoop* PortMap::deleted_entry_ = reinterpret_cast<MessageLoop*>(1);
MessageLoop* const PortMap::deleted_entry_ = reinterpret_cast<MessageLoop*>(1);
intptr_t PortMap::capacity_ = 0;
intptr_t PortMap::used_ = 0;
intptr_t PortMap::deleted_ = 0;
Expand All @@ -31,7 +30,7 @@ intptr_t PortMap::FindPort(Port port) {
return -1;
}
ASSERT(port != ILLEGAL_PORT);
intptr_t index = port % capacity_;
intptr_t index = static_cast<uintptr_t>(port) % capacity_;
intptr_t start_index = index;
Entry entry = map_[index];
while (entry.loop != nullptr) {
Expand Down Expand Up @@ -103,7 +102,7 @@ Port PortMap::CreatePort(MessageLoop* loop) {
// Search for the first unused slot. Make use of the knowledge that here is
// currently no port with this id in the port map.
ASSERT(FindPort(entry.port) < 0);
intptr_t index = entry.port % capacity_;
intptr_t index = static_cast<uintptr_t>(entry.port) % capacity_;
Entry cur = map_[index];
// Stop the search at the first found unused (free or deleted) slot.
while (cur.port != 0) {
Expand Down Expand Up @@ -182,7 +181,7 @@ void PortMap::CloseAllPorts(MessageLoop* loop) {

void PortMap::Startup() {
mutex_ = new Mutex();
prng_ = new Random(OS::CurrentMonotonicNanos());
prng_ = new Random();

static const intptr_t kInitialCapacity = 8;
// TODO(iposva): Verify whether we want to keep exponentially growing.
Expand Down
2 changes: 1 addition & 1 deletion vm/port.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class PortMap : public AllStatic {
static Mutex* mutex_;

static Entry* map_;
static MessageLoop* deleted_entry_;
static MessageLoop* const deleted_entry_;
static intptr_t capacity_;
static intptr_t used_;
static intptr_t deleted_;
Expand Down
18 changes: 8 additions & 10 deletions vm/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,28 @@
#ifndef VM_RANDOM_H_
#define VM_RANDOM_H_

#include "vm/globals.h"

namespace psoup {

// xorshift128+
// Sebastiano Vigna. "Further scramblings of Marsaglia’s xorshift generators."
class Random {
public:
explicit Random(uint64_t seed) {
state0_ = seed;
state1_ = 0;
}
Random();

uint64_t NextUInt64() {
uint64_t s1 = state0_;
const uint64_t s0 = state1_;
uint64_t s1 = state_[0];
const uint64_t s0 = state_[1];
const uint64_t result = s0 + s1;
state0_ = s0;
state_[0] = s0;
s1 ^= s1 << 23;
state1_ = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5);
state_[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5);
return result;
}

private:
uint64_t state0_;
uint64_t state1_;
uint64_t state_[2];
};

} // namespace psoup
Expand Down

0 comments on commit 985f41e

Please sign in to comment.