Skip to content

Commit

Permalink
backend, steamcompmgr: fix use-after-free issue at exit
Browse files Browse the repository at this point in the history
all: also release the xwayland_server_guard lock before calling pthread_exit() to prevent gamescope from hanging at exit
  • Loading branch information
sharkautarch committed Dec 1, 2024
1 parent 5cdf460 commit 1e1ee69
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
19 changes: 7 additions & 12 deletions src/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,29 @@ namespace gamescope
// IBackend
/////////////

static IBackend *s_pBackend = nullptr;
static std::atomic<IBackend*> s_pBackend = nullptr;

IBackend *IBackend::Get()
{
return s_pBackend;
}



bool IBackend::Set( IBackend *pBackend )
{
if ( s_pBackend )
{
delete s_pBackend;
s_pBackend = nullptr;
}

if ( pBackend )
{
s_pBackend = pBackend;
if ( !s_pBackend->Init() )
if ( !GetBackend()->Init() )
{
delete s_pBackend;
s_pBackend = nullptr;
s_pBackend = nullptr;
return false;
}
}

return true;
}


/////////////////
// CBaseBackendFb
Expand Down Expand Up @@ -184,4 +179,4 @@ namespace gamescope

GetBackend()->DumpDebugInfo();
});
}
}
8 changes: 5 additions & 3 deletions src/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ namespace gamescope
wlr_buffer *m_pClientBuffer = nullptr;
std::shared_ptr<CReleaseTimelinePoint> m_pReleasePoint;
};

class IBackend
{
public:
Expand Down Expand Up @@ -206,6 +206,7 @@ namespace gamescope

virtual bool UsesModifiers() const = 0;
virtual std::span<const uint64_t> GetSupportedModifiers( uint32_t uDrmFormat ) const = 0;

inline bool SupportsFormat( uint32_t uDrmFormat ) const
{
return Algorithm::Contains( this->GetSupportedModifiers( uDrmFormat ), DRM_FORMAT_MOD_INVALID );
Expand Down Expand Up @@ -263,6 +264,8 @@ namespace gamescope

virtual void OnBackendBlobDestroyed( BackendBlob *pBlob ) = 0;
private:

void DestroyBackend();
};


Expand Down Expand Up @@ -347,5 +350,4 @@ namespace gamescope
inline gamescope::IBackend *GetBackend()
{
return gamescope::IBackend::Get();
}

}
8 changes: 5 additions & 3 deletions src/steamcompmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5896,7 +5896,7 @@ error(Display *dpy, XErrorEvent *ev)
}

[[noreturn]] static void
steamcompmgr_exit(void)
steamcompmgr_exit(std::optional<std::unique_lock<std::mutex>> lock = std::nullopt)
{
g_ImageWaiter.Shutdown();

Expand Down Expand Up @@ -5936,7 +5936,9 @@ steamcompmgr_exit(void)
wlserver_lock();
wlserver_shutdown();
wlserver_unlock(false);


if (lock)
lock->unlock();
pthread_exit(NULL);
}

Expand Down Expand Up @@ -8042,7 +8044,7 @@ steamcompmgr_main(int argc, char **argv)
vblank = false;
}

steamcompmgr_exit();
steamcompmgr_exit(std::optional<std::unique_lock<std::mutex>> {std::in_place_t{}, std::move(xwayland_server_guard)} );
}

void steamcompmgr_send_frame_done_to_focus_window()
Expand Down

0 comments on commit 1e1ee69

Please sign in to comment.