StratosphereRandomUtils --> sts::rnd

This commit is contained in:
Michael Scire 2019-06-24 11:20:27 -07:00
parent 1d81da1230
commit 269765a3bc
7 changed files with 163 additions and 98 deletions

View File

@ -16,7 +16,7 @@ include $(DEVKITPRO)/libnx/switch_rules
# INCLUDES is a list of directories containing header files # INCLUDES is a list of directories containing header files
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
TARGET := $(notdir $(CURDIR)) TARGET := $(notdir $(CURDIR))
SOURCES := source source/spl source/spl/smc source/updater source/patcher source/map SOURCES := source source/spl source/spl/smc source/updater source/patcher source/map source/rnd
DATA := data DATA := data
INCLUDES := include INCLUDES := include

View File

@ -45,4 +45,4 @@
#include "stratosphere/on_crash.hpp" #include "stratosphere/on_crash.hpp"
#include "stratosphere/random.hpp" #include "stratosphere/rnd.hpp"

View File

@ -0,0 +1,20 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <switch.h>
#include "rnd/rnd_api.hpp"

View File

@ -17,12 +17,11 @@
#pragma once #pragma once
#include <switch.h> #include <switch.h>
class StratosphereRandomUtils { namespace sts::rnd {
public:
static u32 GetRandomU32(u32 max);
static u64 GetRandomU64(u64 max);
static void GetRandomBytes(void* out, size_t size);
template<typename T> /* Random utilities. */
static T GetRandom(T max); void GenerateRandomBytes(void* out, size_t size);
}; u32 GenerateRandomU32(u32 max = std::numeric_limits<u32>::max());
u64 GenerateRandomU64(u64 max = std::numeric_limits<u64>::max());
}

View File

@ -116,7 +116,7 @@ namespace sts::map {
uintptr_t try_address; uintptr_t try_address;
for (unsigned int i = 0; i < LocateRetryCount; i++) { for (unsigned int i = 0; i < LocateRetryCount; i++) {
try_address = address_space.aslr_base + (StratosphereRandomUtils::GetRandomU64(static_cast<u64>(address_space.aslr_size - size) >> 12) << 12); try_address = address_space.aslr_base + (rnd::GenerateRandomU64(static_cast<u64>(address_space.aslr_size - size) >> 12) << 12);
MappedCodeMemory tmp_mcm(process_handle, try_address, base_address, size); MappedCodeMemory tmp_mcm(process_handle, try_address, base_address, size);
R_TRY_CATCH(tmp_mcm.GetResult()) { R_TRY_CATCH(tmp_mcm.GetResult()) {
@ -148,7 +148,7 @@ namespace sts::map {
uintptr_t try_address; uintptr_t try_address;
for (unsigned int i = 0; i < LocateRetryCount; i++) { for (unsigned int i = 0; i < LocateRetryCount; i++) {
while (true) { while (true) {
try_address = address_space.aslr_base + (StratosphereRandomUtils::GetRandomU64(static_cast<u64>(address_space.aslr_size - size) >> 12) << 12); try_address = address_space.aslr_base + (rnd::GenerateRandomU64(static_cast<u64>(address_space.aslr_size - size) >> 12) << 12);
if (address_space.heap_size && (address_space.heap_base <= try_address + size - 1 && try_address <= address_space.heap_end - 1)) { if (address_space.heap_size && (address_space.heap_base <= try_address + size - 1 && try_address <= address_space.heap_end - 1)) {
continue; continue;
} }

View File

@ -1,86 +0,0 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <switch.h>
#include <stratosphere.hpp>
#include <random>
/* Official HOS uses TinyMT. This is high effort. Let's just use XorShift. */
/* https://en.wikipedia.org/wiki/Xorshift */
class XorShiftGenerator {
public:
using result_type = uint32_t;
static constexpr result_type (min)() { return 0; }
static constexpr result_type (max)() { return UINT32_MAX; }
static constexpr size_t num_seed_dwords = 4;
private:
result_type random_state[num_seed_dwords];
public:
explicit XorShiftGenerator() {
/* Seed using process entropy. */
u64 val = 0;
for (size_t i = 0; i < num_seed_dwords; i++) {
R_ASSERT(svcGetInfo(&val, 0xB, 0, i));
this->random_state[i] = result_type(val);
}
}
explicit XorShiftGenerator(std::random_device &rd) {
for (size_t i = 0; i < num_seed_dwords; i++) {
this->random_state[i] = result_type(rd());
}
}
result_type operator()() {
result_type s, t = this->random_state[3];
t ^= t << 11;
t ^= t >> 8;
this->random_state[3] = this->random_state[2]; this->random_state[2] = this->random_state[1]; this->random_state[1] = (s = this->random_state[0]);
t ^= s;
t ^= s >> 19;
this->random_state[0] = t;
return t;
}
void discard(size_t n) {
for (size_t i = 0; i < n; i++) {
operator()();
}
}
};
static XorShiftGenerator g_rnd_generator;
template<typename T>
T StratosphereRandomUtils::GetRandom(T max) {
std::uniform_int_distribution<T> rnd(0, max);
return rnd(g_rnd_generator);
}
/* These are slightly biased, but I think that's totally okay. */
u32 StratosphereRandomUtils::GetRandomU32(u32 max) {
return GetRandom<u32>(max);
}
u64 StratosphereRandomUtils::GetRandomU64(u64 max) {
return GetRandom<u64>(max);
}
void StratosphereRandomUtils::GetRandomBytes(void* out, size_t size) {
std::generate(reinterpret_cast<u8*>(out), reinterpret_cast<u8*>(out) + size, std::bind(GetRandom<u8>, 0xFF));
}

132
source/rnd/rnd_api.cpp Normal file
View File

@ -0,0 +1,132 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <random>
#include <switch.h>
#include <stratosphere.hpp>
#include <stratosphere/rnd.hpp>
namespace sts::rnd {
namespace {
/* Generator type. */
/* Official HOS uses TinyMT. This is high effort. Let's just use XorShift. */
/* https://en.wikipedia.org/wiki/Xorshift */
class XorShiftGenerator {
public:
using ResultType = uint32_t;
using result_type = ResultType;
static constexpr ResultType (min)() { return std::numeric_limits<ResultType>::min(); }
static constexpr ResultType (max)() { return std::numeric_limits<ResultType>::max(); }
static constexpr size_t SeedSize = 4;
private:
ResultType random_state[SeedSize];
public:
explicit XorShiftGenerator() {
/* Seed using process entropy. */
u64 val = 0;
for (size_t i = 0; i < SeedSize; i++) {
R_ASSERT(svcGetInfo(&val, InfoType_RandomEntropy, INVALID_HANDLE, i));
this->random_state[i] = ResultType(val);
}
}
explicit XorShiftGenerator(std::random_device &rd) {
for (size_t i = 0; i < SeedSize; i++) {
this->random_state[i] = ResultType(rd());
}
}
ResultType operator()() {
ResultType s, t = this->random_state[3];
t ^= t << 11;
t ^= t >> 8;
this->random_state[3] = this->random_state[2]; this->random_state[2] = this->random_state[1]; this->random_state[1] = (s = this->random_state[0]);
t ^= s;
t ^= s >> 19;
this->random_state[0] = t;
return t;
}
void discard(size_t n) {
for (size_t i = 0; i < n; i++) {
operator()();
}
}
};
/* Generator global. */
XorShiftGenerator g_rnd_generator;
/* Templated helpers. */
template<typename T>
T GenerateRandom(T max = std::numeric_limits<T>::max()) {
std::uniform_int_distribution<T> rnd(std::numeric_limits<T>::min(), max);
return rnd(g_rnd_generator);
}
}
void GenerateRandomBytes(void* _out, size_t size) {
uintptr_t out = reinterpret_cast<uintptr_t>(_out);
uintptr_t end = out + size;
/* Force alignment. */
if (out % sizeof(u16) && out < end) {
*reinterpret_cast<u8 *>(out) = GenerateRandom<u8>();
out += sizeof(u8);
}
if (out % sizeof(u32) && out < end) {
*reinterpret_cast<u16 *>(out) = GenerateRandom<u16>();
out += sizeof(u16);
}
if (out % sizeof(u64) && out < end) {
*reinterpret_cast<u32 *>(out) = GenerateRandom<u32>();
out += sizeof(u32);
}
/* Perform as many aligned writes as possible. */
while (out + sizeof(u64) <= end) {
*reinterpret_cast<u64 *>(out) = GenerateRandom<u64>();
out += sizeof(u64);
}
/* Do remainder writes. */
if (out + sizeof(u32) <= end) {
*reinterpret_cast<u32 *>(out) = GenerateRandom<u32>();
out += sizeof(u32);
}
if (out + sizeof(u16) <= end) {
*reinterpret_cast<u16 *>(out) = GenerateRandom<u16>();
out += sizeof(u16);
}
if (out + sizeof(u8) <= end) {
*reinterpret_cast<u8 *>(out) = GenerateRandom<u8>();
out += sizeof(u8);
}
}
u32 GenerateRandomU32(u32 max) {
return GenerateRandom<u32>(max);
}
u64 GenerateRandomU64(u64 max) {
return GenerateRandom<u64>(max);
}
}