Use weak_ptr for TrapHandler Callbacks

The lifetime of the `this` pointer in the trap callbacks could be invalid as the lifetime of the underlying `Buffer`/`Texture` object wasn't guaranteed, this commit fixes that by passing a `weak_ptr` of the objects into the callbacks which is locked during the callbacks and ensures that a destroyed object isn't accessed.

Co-authored-by: Billy Laws <blaws05@gmail.com>
This commit is contained in:
PixelyIon
2022-07-25 22:52:45 +05:30
parent 96d8676d5b
commit 9d294b9ccc
6 changed files with 58 additions and 21 deletions

View File

@ -69,6 +69,7 @@ namespace skyline::gpu {
LockedBuffer newBuffer{std::make_shared<Buffer>(gpu, span<u8>{lowestAddress, highestAddress}), tag}; // If we don't lock the buffer prior to trapping it during synchronization, a race could occur with a guest trap acquiring the lock before we do and mutating the buffer prior to it being ready
newBuffer->SetupGuestMappings();
newBuffer->SynchronizeHost(false); // Overlaps don't necessarily fully cover the buffer so we have to perform a sync here to prevent any gaps
auto copyBuffer{[](auto dstGuest, auto srcGuest, auto dstPtr, auto srcPtr) {
@ -164,6 +165,7 @@ namespace skyline::gpu {
if (overlaps.empty()) {
// If we couldn't find any overlapping buffers, create a new buffer without coalescing
LockedBuffer buffer{std::make_shared<Buffer>(gpu, guestMapping), tag};
buffer->SetupGuestMappings();
buffer->SynchronizeHost();
InsertBuffer(*buffer);
return buffer->GetView(offset, size);