Fix use-after-free in Cocoa GHOST system creation

The use-after-free is triggered when the GHOST system is created
multiple timers during the application timelife which happens in
the integration tests.

The solution is to release the application delegate and set it
to nil when the GHOST system is being destroyed. This ensures that
all subsequent GHOST systems properly initialize application
delegate, and that there is no application delegate which points
to a freed system.

The original issue was noticed by a flackey behavior of the
bf_gpu_tests test which was failing at random. The issue could
be reliably reproduced by running this test with ASAN enabled.

Pull Request: https://projects.blender.org/blender/blender/pulls/116717
This commit is contained in:
Sergey Sharybin 2024-01-02 17:19:54 +01:00 committed by Sergey Sharybin
parent cd8f1853ed
commit 4657d541c8
1 changed files with 13 additions and 1 deletions

View File

@ -565,7 +565,19 @@ GHOST_SystemCocoa::GHOST_SystemCocoa()
m_last_warp_timestamp = 0;
}
GHOST_SystemCocoa::~GHOST_SystemCocoa() {}
GHOST_SystemCocoa::~GHOST_SystemCocoa()
{
/* The application delegate integrates the Cocoa application with the GHOST system.
*
* Since the GHOST system is about to be fully destroyed release the application delegate as
* well, so it does not point back to a freed system, forcing the delegate to be created with the
* new GHOST system in init(). */
CocoaAppDelegate *appDelegate = (CocoaAppDelegate *)[NSApp delegate];
if (appDelegate) {
[NSApp setDelegate:nil];
[appDelegate release];
}
}
GHOST_TSuccess GHOST_SystemCocoa::init()
{