[Base] Check success of sync primitive creation

- Mainly use `assert`s, since failure is very rare
- Forward failure of `CreateSemaphore` to guests because it is more easy
  to trigger with invalid initial parameters.
This commit is contained in:
Joel Linn
2022-02-24 22:37:32 +01:00
committed by Rick Gibbed
parent 6bd1279fc0
commit 986dcf4f65
21 changed files with 96 additions and 26 deletions

View File

@@ -590,7 +590,13 @@ dword_result_t NtCreateSemaphore_entry(lpdword_t handle_ptr,
}
auto sem = object_ref<XSemaphore>(new XSemaphore(kernel_state()));
sem->Initialize((int32_t)count, (int32_t)limit);
if (!sem->Initialize((int32_t)count, (int32_t)limit)) {
if (handle_ptr) {
*handle_ptr = 0;
}
sem->ReleaseHandle();
return X_STATUS_INVALID_PARAMETER;
}
// obj_attributes may have a name inside of it, if != NULL.
if (obj_attributes_ptr) {

View File

@@ -2,7 +2,7 @@
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Copyright 2022 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
@@ -30,6 +30,7 @@ void XEvent::Initialize(bool manual_reset, bool initial_state) {
} else {
event_ = xe::threading::Event::CreateAutoResetEvent(initial_state);
}
assert_not_null(event_);
}
void XEvent::InitializeNative(void* native_ptr, X_DISPATCH_HEADER* header) {
@@ -53,6 +54,7 @@ void XEvent::InitializeNative(void* native_ptr, X_DISPATCH_HEADER* header) {
} else {
event_ = xe::threading::Event::CreateAutoResetEvent(initial_state);
}
assert_not_null(event_);
}
int32_t XEvent::Set(uint32_t priority_increment, bool wait) {
@@ -112,6 +114,7 @@ object_ref<XEvent> XEvent::Restore(KernelState* kernel_state,
} else {
evt->event_ = xe::threading::Event::CreateAutoResetEvent(false);
}
assert_not_null(evt->event_);
if (signaled) {
evt->event_->Set();

View File

@@ -2,7 +2,7 @@
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2020 Ben Vanik. All rights reserved. *
* Copyright 2022 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
@@ -26,10 +26,12 @@ XFile::XFile(KernelState* kernel_state, vfs::File* file, bool synchronous)
file_(file),
is_synchronous_(synchronous) {
async_event_ = threading::Event::CreateAutoResetEvent(false);
assert_not_null(async_event_);
}
XFile::XFile() : XObject(kObjectType) {
async_event_ = threading::Event::CreateAutoResetEvent(false);
assert_not_null(async_event_);
}
XFile::~XFile() {

View File

@@ -2,7 +2,7 @@
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2015 Ben Vanik. All rights reserved. *
* Copyright 2022 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
@@ -15,6 +15,7 @@ namespace kernel {
XIOCompletion::XIOCompletion(KernelState* kernel_state)
: XObject(kernel_state, kObjectType) {
notification_semaphore_ = threading::Semaphore::Create(0, kMaxNotifications);
assert_not_null(notification_semaphore_);
}
XIOCompletion::~XIOCompletion() = default;

View File

@@ -2,7 +2,7 @@
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Copyright 2022 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
@@ -28,6 +28,7 @@ void XMutant::Initialize(bool initial_owner) {
assert_false(mutant_);
mutant_ = xe::threading::Mutant::Create(initial_owner);
assert_not_null(mutant_);
}
void XMutant::InitializeNative(void* native_ptr, X_DISPATCH_HEADER* header) {

View File

@@ -2,13 +2,14 @@
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Copyright 2022 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include "xenia/kernel/xnotifylistener.h"
#include "xenia/base/assert.h"
#include "xenia/base/byte_stream.h"
#include "xenia/base/logging.h"
#include "xenia/kernel/kernel_state.h"
@@ -25,6 +26,7 @@ void XNotifyListener::Initialize(uint64_t mask, uint32_t max_version) {
assert_false(wait_handle_);
wait_handle_ = xe::threading::Event::CreateManualResetEvent(false);
assert_not_null(wait_handle_);
mask_ = mask;
max_version_ = max_version;

View File

@@ -2,7 +2,7 @@
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Copyright 2022 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
@@ -410,7 +410,9 @@ object_ref<XObject> XObject::GetNativeObject(KernelState* kernel_state,
case 5: // SemaphoreObject
{
auto sem = new XSemaphore(kernel_state);
sem->InitializeNative(native_ptr, header);
auto success = sem->InitializeNative(native_ptr, header);
// Can't report failure to the guest at late initialization:
assert_true(success);
object = sem;
} break;
case 3: // ProcessObject

View File

@@ -2,7 +2,7 @@
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Copyright 2022 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
@@ -20,22 +20,24 @@ XSemaphore::XSemaphore(KernelState* kernel_state)
XSemaphore::~XSemaphore() = default;
void XSemaphore::Initialize(int32_t initial_count, int32_t maximum_count) {
bool XSemaphore::Initialize(int32_t initial_count, int32_t maximum_count) {
assert_false(semaphore_);
CreateNative(sizeof(X_KSEMAPHORE));
maximum_count_ = maximum_count;
semaphore_ = xe::threading::Semaphore::Create(initial_count, maximum_count);
return !!semaphore_;
}
void XSemaphore::InitializeNative(void* native_ptr, X_DISPATCH_HEADER* header) {
bool XSemaphore::InitializeNative(void* native_ptr, X_DISPATCH_HEADER* header) {
assert_false(semaphore_);
auto semaphore = reinterpret_cast<X_KSEMAPHORE*>(native_ptr);
maximum_count_ = semaphore->limit;
semaphore_ = xe::threading::Semaphore::Create(semaphore->header.signal_state,
semaphore->limit);
return !!semaphore_;
}
int32_t XSemaphore::ReleaseSemaphore(int32_t release_count) {
@@ -85,6 +87,7 @@ object_ref<XSemaphore> XSemaphore::Restore(KernelState* kernel_state,
sem->semaphore_ =
threading::Semaphore::Create(free_count, sem->maximum_count_);
assert_not_null(sem->semaphore_);
return object_ref<XSemaphore>(sem);
}

View File

@@ -2,7 +2,7 @@
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Copyright 2022 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
@@ -30,8 +30,9 @@ class XSemaphore : public XObject {
explicit XSemaphore(KernelState* kernel_state);
~XSemaphore() override;
void Initialize(int32_t initial_count, int32_t maximum_count);
void InitializeNative(void* native_ptr, X_DISPATCH_HEADER* header);
[[nodiscard]] bool Initialize(int32_t initial_count, int32_t maximum_count);
[[nodiscard]] bool InitializeNative(void* native_ptr,
X_DISPATCH_HEADER* header);
int32_t ReleaseSemaphore(int32_t release_count);

View File

@@ -2,7 +2,7 @@
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2020 Ben Vanik. All rights reserved. *
* Copyright 2022 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
@@ -1057,6 +1057,7 @@ object_ref<XThread> XThread::Restore(KernelState* kernel_state,
// Release the self-reference to the thread.
thread->ReleaseHandle();
});
assert_not_null(thread->thread_);
// Notify processor we were recreated.
thread->emulator()->processor()->OnThreadCreated(

View File

@@ -2,7 +2,7 @@
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Copyright 2022 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
@@ -35,6 +35,7 @@ void XTimer::Initialize(uint32_t timer_type) {
assert_always();
break;
}
assert_not_null(timer_);
}
X_STATUS XTimer::SetTimer(int64_t due_time, uint32_t period_ms,