Files
ppsspp/Core/SaveStateRewind.h
T
Henrik RydgårdandClaude Opus 5 9f4dcf359b Fix three more out-of-bounds writes
GetCurrentDrawAsDebugVertices (GE debugger vertex preview) sized its index scratch
buffer at a fixed 65536 and then ran both expanding steps into it: index generation
turns strips/fans into up to 3 indices per input index, and RunSoftwareTransform can
then expand points/lines/rects into 6 more each. A 30000-vertex triangle strip wrote
~90000 entries. Size the buffer from the count instead.

The Expand{Rectangles,Lines,Points} capacity checks were also off: they compared the
expansion against indsSize but write the expanded indices at inds + vertexCount, so
the input count has to be part of the sum.

ControlMapper::Axis wrote rawAxisValue_[axis.axisId] with no bounds check, one line
below an explicit check on axis.deviceId. axisId comes straight from the device -
Android reports AXIS_GENERIC_13..16 as 44..47, against a 44-entry array - so it wrote
into the neighbouring deviceTimestamps_. NativeAxis had the same unchecked write into
HLEPlugins::PluginDataAxis, where it goes out of the object entirely.

Rewind's LockedDecompress computed its copy-from-base block size as
base.size() - result.size() in size_t and truncated to int, so it went negative once
the output grew past the base, and insert() then ran with last < first. That happens
because a state can outlive the base it was compressed against: there are 20 states
but only 2 bases, rotated every 16 saves. Track a generation per base and refuse to
decode a state whose base is gone, and bound the block size against the base itself.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DCPmm7FoQUoqrbMdhfqhQ2
2026-08-30 22:45:50 +02:00

84 lines
2.3 KiB
C++

#pragma once
#include <mutex>
#include <thread>
#include "Common/Serialize/Serializer.h"
#include "Common/CommonTypes.h"
#include "Common/TimeUtil.h"
namespace SaveState {
// This ring buffer of states is for rewind save states, which are kept in RAM.
// Save states are compressed against one of two reference saves (bases_), and the reference
// is switched to a fresh save every N saves, where N is BASE_USAGE_INTERVAL.
// The compression is a simple block based scheme where 0 means to copy a block from the base,
// and 1 means that the following bytes are the next block. See Compress/LockedDecompress.
class StateRingbuffer {
public:
StateRingbuffer() {
size_ = REWIND_NUM_STATES;
states_.resize(size_);
baseMapping_.resize(size_);
}
~StateRingbuffer() {
if (compressThread_.joinable()) {
compressThread_.join();
}
}
CChunkFileReader::Error Save();
CChunkFileReader::Error Restore(std::string *errorString, std::string *metadata);
void ScheduleCompress(std::vector<u8> *result, const std::vector<u8> *state, const std::vector<u8> *base);
void Compress(std::vector<u8> &result, const std::vector<u8> &state, const std::vector<u8> &base);
void LockedDecompress(std::vector<u8> &result, const std::vector<u8> &compressed, const std::vector<u8> &base);
void Clear();
bool Empty() const {
return next_ == first_;
}
void Process();
void NotifyState();
double NextStateTimestamp() const;
private:
const int BLOCK_SIZE = 8192;
const int REWIND_NUM_STATES = 20;
// TODO: Instead, based on size of compressed state?
const int BASE_USAGE_INTERVAL = 15;
typedef std::vector<u8> StateBuffer;
struct RewindState {
StateBuffer stateBuffer;
double savedTime;
bool empty() const { return stateBuffer.empty(); }
void clear() { stateBuffer.clear(); savedTime = 0.0; }
};
int first_ = 0;
int next_ = 0;
int size_;
std::vector<RewindState> states_;
StateBuffer bases_[2];
// Which generation each base slot currently holds, and which generation each state was
// compressed against. There are more states than bases, so states do go stale.
int baseGeneration_[2] = {-1, -1};
int nextBaseGeneration_ = 0;
std::vector<int> baseMapping_;
std::mutex lock_;
std::thread compressThread_;
std::vector<u8> buffer_;
int base_ = -1;
int baseUsage_ = 0;
double rewindLastTime_ = 0.0f;
};
} // namespace SaveState