diff --git a/stratosphere/libstratosphere b/stratosphere/libstratosphere index 163d9259a..880bce909 160000 --- a/stratosphere/libstratosphere +++ b/stratosphere/libstratosphere @@ -1 +1 @@ -Subproject commit 163d9259a365c3a89c1a837b28e7609d45795e40 +Subproject commit 880bce9092fbef0bcbf101b8ec2e3d2c5af3fb98 diff --git a/stratosphere/loader/source/ldr_map.cpp b/stratosphere/loader/source/ldr_map.cpp index 4e307937e..c05157bca 100644 --- a/stratosphere/loader/source/ldr_map.cpp +++ b/stratosphere/loader/source/ldr_map.cpp @@ -18,7 +18,6 @@ #include #include "ldr_map.hpp" -#include "ldr_random.hpp" Result MapUtils::LocateSpaceForMap(u64 *out, u64 out_size) { if (kernelAbove200()) { @@ -142,7 +141,7 @@ Result MapUtils::MapCodeMemoryForProcessModern(Handle process_h, u64 base_addres u64 try_address; for (unsigned int i = 0; i < 0x200; i++) { while (true) { - try_address = address_space.addspace_base + (RandomUtils::GetRandomU64((u64)(address_space.addspace_size - size) >> 12) << 12); + try_address = address_space.addspace_base + (StratosphereRandomUtils::GetRandomU64((u64)(address_space.addspace_size - size) >> 12) << 12); if (address_space.heap_size && (address_space.heap_base <= try_address + size - 1 && try_address <= address_space.heap_end - 1)) { continue; } @@ -179,7 +178,7 @@ Result MapUtils::MapCodeMemoryForProcessDeprecated(Handle process_h, bool is_64_ u64 try_address; for (unsigned int i = 0; i < 0x200; i++) { - try_address = addspace_base + (RandomUtils::GetRandomU64((u64)(addspace_size - size) >> 12) << 12); + try_address = addspace_base + (StratosphereRandomUtils::GetRandomU64((u64)(addspace_size - size) >> 12) << 12); rc = svcMapProcessCodeMemory(process_h, try_address, base_address, size); if (rc != ResultKernelInvalidMemoryState) { break; diff --git a/stratosphere/loader/source/ldr_nro.cpp b/stratosphere/loader/source/ldr_nro.cpp index 0f2cfbfda..a28da564a 100644 --- a/stratosphere/loader/source/ldr_nro.cpp +++ b/stratosphere/loader/source/ldr_nro.cpp @@ -23,7 +23,6 @@ #include "ldr_nro.hpp" #include "ldr_registration.hpp" #include "ldr_map.hpp" -#include "ldr_random.hpp" Result NroUtils::ValidateNrrHeader(NrrHeader *header, u64 size, u64 title_id_min) { if (header->magic != MAGIC_NRR0) { diff --git a/stratosphere/loader/source/ldr_nso.cpp b/stratosphere/loader/source/ldr_nso.cpp index 3f4045093..8104e9353 100644 --- a/stratosphere/loader/source/ldr_nso.cpp +++ b/stratosphere/loader/source/ldr_nso.cpp @@ -21,7 +21,6 @@ #include "lz4.h" #include "ldr_nso.hpp" #include "ldr_map.hpp" -#include "ldr_random.hpp" #include "ldr_patcher.hpp" #include "ldr_content_management.hpp" @@ -226,7 +225,7 @@ Result NsoUtils::CalculateNsoLoadExtents(u32 addspace_type, u32 args_size, NsoLo u64 aslr_slide = 0; if (addspace_type & 0x20) { - aslr_slide = RandomUtils::GetRandomU64((addspace_size - extents->total_size) >> 21) << 21; + aslr_slide = StratosphereRandomUtils::GetRandomU64((addspace_size - extents->total_size) >> 21) << 21; } extents->base_address = addspace_start + aslr_slide; diff --git a/stratosphere/loader/source/ldr_random.cpp b/stratosphere/loader/source/ldr_random.cpp deleted file mode 100644 index 197c4bdf2..000000000 --- a/stratosphere/loader/source/ldr_random.cpp +++ /dev/null @@ -1,65 +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 . - */ - -#include - -#include "ldr_random.hpp" - -/* Official HOS uses TinyMT. This is high effort. Let's just use XorShift. */ -/* https://en.wikipedia.org/wiki/Xorshift */ - -static u32 g_random_state[4] = {0}; -static bool g_has_initialized = false; - -static void EnsureRandomState() { - if (g_has_initialized) { - return; - } - - /* Retrieve process entropy with svcGetInfo. */ - u64 val = 0; - for (unsigned int i = 0; i < 4; i++) { - if (R_FAILED(svcGetInfo(&val, 0xB, 0, i))) { - /* TODO: Panic? */ - } - g_random_state[i] = val & 0xFFFFFFFF; - } - - g_has_initialized = true; -} - -u32 RandomUtils::GetNext() { - EnsureRandomState(); - u32 s, t = g_random_state[3]; - t ^= t << 11; - t ^= t >> 8; - g_random_state[3] = g_random_state[2]; g_random_state[2] = g_random_state[1]; g_random_state[1] = (s = g_random_state[0]); - t ^= s; - t ^= s >> 19; - g_random_state[0] = t; - return t; -} - -/* These are slightly biased, but I think that's totally okay. */ -u32 RandomUtils::GetRandomU32(u32 max) { - return GetNext() % max; -} - -u64 RandomUtils::GetRandomU64(u64 max) { - u64 val = GetNext(); - val |= ((u64)GetNext()) << 32; - return val % max; -} diff --git a/stratosphere/loader/source/ldr_random.hpp b/stratosphere/loader/source/ldr_random.hpp deleted file mode 100644 index 9b74cdd74..000000000 --- a/stratosphere/loader/source/ldr_random.hpp +++ /dev/null @@ -1,25 +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 . - */ - -#pragma once -#include - -class RandomUtils { - public: - static u32 GetNext(); - static u32 GetRandomU32(u32 max); - static u64 GetRandomU64(u64 max); -}; \ No newline at end of file