From 5630141ad77e52af4e2c70fa9b9fff8d8268f04e Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Thu, 13 Jun 2019 13:35:16 -0400 Subject: [PATCH] Ensures randomised memory contents at startup. --- Machines/Apple/Macintosh/Macintosh.cpp | 4 ++++ Machines/Apple/Macintosh/RealTimeClock.hpp | 8 ++++++++ Machines/Utility/MemoryFuzzer.cpp | 4 ++++ Machines/Utility/MemoryFuzzer.hpp | 3 +++ OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj | 4 ++-- 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Machines/Apple/Macintosh/Macintosh.cpp b/Machines/Apple/Macintosh/Macintosh.cpp index 7b8a512ec..97da812f8 100644 --- a/Machines/Apple/Macintosh/Macintosh.cpp +++ b/Machines/Apple/Macintosh/Macintosh.cpp @@ -33,6 +33,7 @@ #include "../../../Analyser/Static/Macintosh/Target.hpp" #include "../../Utility/MemoryPacker.hpp" +#include "../../Utility/MemoryFuzzer.hpp" namespace { @@ -100,6 +101,9 @@ template class ConcreteMachin roms[0]->resize(rom_size); Memory::PackBigEndian16(*roms[0], rom_); + // Randomise memory contents. + Memory::Fuzz(ram_, sizeof(ram_)); + // Attach the drives to the IWM. iwm_.iwm.set_drive(0, &drives_[0]); iwm_.iwm.set_drive(1, &drives_[1]); diff --git a/Machines/Apple/Macintosh/RealTimeClock.hpp b/Machines/Apple/Macintosh/RealTimeClock.hpp index 7cb7fe744..5a1f01496 100644 --- a/Machines/Apple/Macintosh/RealTimeClock.hpp +++ b/Machines/Apple/Macintosh/RealTimeClock.hpp @@ -9,6 +9,8 @@ #ifndef RealTimeClock_hpp #define RealTimeClock_hpp +#include "../../Utility/MemoryFuzzer.hpp" + namespace Apple { namespace Macintosh { @@ -21,6 +23,12 @@ namespace Macintosh { */ class RealTimeClock { public: + RealTimeClock() { + // TODO: this should persist, if possible, rather than + // being randomly initialised. + Memory::Fuzz(data_, sizeof(data_)); + } + /*! Advances the clock by 1 second. diff --git a/Machines/Utility/MemoryFuzzer.cpp b/Machines/Utility/MemoryFuzzer.cpp index fdcdaa01e..c423cc5d1 100644 --- a/Machines/Utility/MemoryFuzzer.cpp +++ b/Machines/Utility/MemoryFuzzer.cpp @@ -23,6 +23,10 @@ void Memory::Fuzz(uint8_t *buffer, std::size_t size) { } } +void Memory::Fuzz(uint16_t *buffer, std::size_t size) { + Fuzz(reinterpret_cast(buffer), size * sizeof(uint16_t)); +} + void Memory::Fuzz(std::vector &buffer) { Fuzz(buffer.data(), buffer.size()); } diff --git a/Machines/Utility/MemoryFuzzer.hpp b/Machines/Utility/MemoryFuzzer.hpp index bd097aeed..9beecd2f6 100644 --- a/Machines/Utility/MemoryFuzzer.hpp +++ b/Machines/Utility/MemoryFuzzer.hpp @@ -18,6 +18,9 @@ namespace Memory { /// Stores @c size random bytes from @c buffer onwards. void Fuzz(uint8_t *buffer, std::size_t size); +/// Stores @c size random 16-bit words from @c buffer onwards. +void Fuzz(uint16_t *buffer, std::size_t size); + /// Replaces all existing vector contents with random bytes. void Fuzz(std::vector &buffer); diff --git a/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj b/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj index 446546b3f..a2dc8adfb 100644 --- a/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj +++ b/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj @@ -3132,6 +3132,7 @@ 4B9378E222A199C600973513 /* Audio.cpp */, 4BB4BFAC22A33DE50069048D /* DriveSpeedAccumulator.cpp */, 4BCE0058227CFFCA000CA200 /* Macintosh.cpp */, + 4B0C956F22A74E880015A8F6 /* SonyDrive.cpp */, 4BCE005E227D39AB000CA200 /* Video.cpp */, 4B9378E322A199C600973513 /* Audio.hpp */, 4BB4BFAA22A300710069048D /* DeferredAudio.hpp */, @@ -3139,9 +3140,8 @@ 4BDB3D8522833321002D3CEE /* Keyboard.hpp */, 4BCE0059227CFFCA000CA200 /* Macintosh.hpp */, 4BD0692B22828A2D00D2A54F /* RealTimeClock.hpp */, - 4BCE005F227D39AB000CA200 /* Video.hpp */, - 4B0C956F22A74E880015A8F6 /* SonyDrive.cpp */, 4B0C957022A74E880015A8F6 /* SonyDrive.hpp */, + 4BCE005F227D39AB000CA200 /* Video.hpp */, ); path = Macintosh; sourceTree = "";