ams: revamp assertion system

This commit is contained in:
Michael Scire 2020-02-22 23:05:14 -08:00
parent 9572fb2ce3
commit 40400aee1f
168 changed files with 1014 additions and 696 deletions

View file

@ -31,7 +31,7 @@ namespace ams::os::impl {
}
InterProcessEvent::InterProcessEvent(bool autoclear) : is_initialized(false) {
R_ASSERT(this->Initialize(autoclear));
R_ABORT_UNLESS(this->Initialize(autoclear));
}
InterProcessEvent::~InterProcessEvent() {
@ -39,7 +39,7 @@ namespace ams::os::impl {
}
Result InterProcessEvent::Initialize(bool autoclear) {
AMS_ASSERT(!this->is_initialized);
AMS_ABORT_UNLESS(!this->is_initialized);
Handle rh, wh;
R_TRY(CreateEventHandles(&rh, &wh));
this->Initialize(rh, true, wh, true, autoclear);
@ -47,8 +47,8 @@ namespace ams::os::impl {
}
void InterProcessEvent::Initialize(Handle read_handle, bool manage_read_handle, Handle write_handle, bool manage_write_handle, bool autoclear) {
AMS_ASSERT(!this->is_initialized);
AMS_ASSERT(read_handle != INVALID_HANDLE || write_handle != INVALID_HANDLE);
AMS_ABORT_UNLESS(!this->is_initialized);
AMS_ABORT_UNLESS(read_handle != INVALID_HANDLE || write_handle != INVALID_HANDLE);
this->read_handle = read_handle;
this->manage_read_handle = manage_read_handle;
this->write_handle = write_handle;
@ -58,40 +58,40 @@ namespace ams::os::impl {
}
Handle InterProcessEvent::DetachReadableHandle() {
AMS_ASSERT(this->is_initialized);
AMS_ABORT_UNLESS(this->is_initialized);
const Handle handle = this->read_handle;
AMS_ASSERT(handle != INVALID_HANDLE);
AMS_ABORT_UNLESS(handle != INVALID_HANDLE);
this->read_handle = INVALID_HANDLE;
this->manage_read_handle = false;
return handle;
}
Handle InterProcessEvent::DetachWritableHandle() {
AMS_ASSERT(this->is_initialized);
AMS_ABORT_UNLESS(this->is_initialized);
const Handle handle = this->write_handle;
AMS_ASSERT(handle != INVALID_HANDLE);
AMS_ABORT_UNLESS(handle != INVALID_HANDLE);
this->write_handle = INVALID_HANDLE;
this->manage_write_handle = false;
return handle;
}
Handle InterProcessEvent::GetReadableHandle() const {
AMS_ASSERT(this->is_initialized);
AMS_ABORT_UNLESS(this->is_initialized);
return this->read_handle;
}
Handle InterProcessEvent::GetWritableHandle() const {
AMS_ASSERT(this->is_initialized);
AMS_ABORT_UNLESS(this->is_initialized);
return this->write_handle;
}
void InterProcessEvent::Finalize() {
if (this->is_initialized) {
if (this->manage_read_handle && this->read_handle != INVALID_HANDLE) {
R_ASSERT(svcCloseHandle(this->read_handle));
R_ABORT_UNLESS(svcCloseHandle(this->read_handle));
}
if (this->manage_write_handle && this->write_handle != INVALID_HANDLE) {
R_ASSERT(svcCloseHandle(this->write_handle));
R_ABORT_UNLESS(svcCloseHandle(this->write_handle));
}
}
this->read_handle = INVALID_HANDLE;
@ -102,7 +102,7 @@ namespace ams::os::impl {
}
void InterProcessEvent::Signal() {
R_ASSERT(svcSignalEvent(this->GetWritableHandle()));
R_ABORT_UNLESS(svcSignalEvent(this->GetWritableHandle()));
}
void InterProcessEvent::Reset() {
@ -110,7 +110,7 @@ namespace ams::os::impl {
if (handle == INVALID_HANDLE) {
handle = this->GetWritableHandle();
}
R_ASSERT(svcClearEvent(handle));
R_ABORT_UNLESS(svcClearEvent(handle));
}
void InterProcessEvent::Wait() {

View file

@ -25,8 +25,8 @@ namespace ams::os::impl {
/* Nintendo does not check the result of these invocations, but we will for safety. */
/* Nintendo uses entropy values 0, 1 to seed the public TinyMT random, and values */
/* 2, 3 to seed os::detail::RngManager's private TinyMT random. */
R_ASSERT(svcGetInfo(reinterpret_cast<u64 *>(&seed[0]), InfoType_RandomEntropy, INVALID_HANDLE, 0));
R_ASSERT(svcGetInfo(reinterpret_cast<u64 *>(&seed[2]), InfoType_RandomEntropy, INVALID_HANDLE, 1));
R_ABORT_UNLESS(svcGetInfo(reinterpret_cast<u64 *>(&seed[0]), InfoType_RandomEntropy, INVALID_HANDLE, 0));
R_ABORT_UNLESS(svcGetInfo(reinterpret_cast<u64 *>(&seed[2]), InfoType_RandomEntropy, INVALID_HANDLE, 1));
mt->Initialize(seed, util::size(seed));
}

View file

@ -31,7 +31,7 @@ namespace ams::os::impl {
}
virtual Handle GetHandle() const override {
AMS_ASSERT(this->event->is_initialized);
AMS_ABORT_UNLESS(this->event->is_initialized);
return this->event->GetReadableHandle();
}
};

View file

@ -30,7 +30,7 @@ namespace ams::os::impl {
}
virtual Handle GetHandle() const override {
AMS_ASSERT(this->event->is_initialized);
AMS_ABORT_UNLESS(this->event->is_initialized);
return this->event->handle.Get();
}
};

View file

@ -64,7 +64,7 @@ namespace ams::os::impl{
index = WaitTimedOut;
} else {
index = this->WaitSynchronization(object_handles, count, min_timeout);
AMS_ASSERT(index != WaitInvalid);
AMS_ABORT_UNLESS(index != WaitInvalid);
}
switch (index) {
@ -115,7 +115,7 @@ namespace ams::os::impl{
for (WaitableHolderBase &holder_base : this->waitable_list) {
if (Handle handle = holder_base.GetHandle(); handle != INVALID_HANDLE) {
AMS_ASSERT(count < MaximumHandleCount);
AMS_ABORT_UNLESS(count < MaximumHandleCount);
out_handles[count] = handle;
out_objects[count] = &holder_base;
@ -170,7 +170,7 @@ namespace ams::os::impl{
if (this->signaled_holder == nullptr) {
this->signaled_holder = holder_base;
R_ASSERT(svcCancelSynchronization(this->waiting_thread_handle));
R_ABORT_UNLESS(svcCancelSynchronization(this->waiting_thread_handle));
}
}

View file

@ -18,7 +18,7 @@
namespace ams::os {
Result InterruptEvent::Initialize(u32 interrupt_id, bool autoclear) {
AMS_ASSERT(!this->is_initialized);
AMS_ABORT_UNLESS(!this->is_initialized);
this->auto_clear = autoclear;
const auto type = this->auto_clear ? svc::InterruptType_Edge : svc::InterruptType_Level;
@ -29,23 +29,23 @@ namespace ams::os {
}
void InterruptEvent::Finalize() {
AMS_ASSERT(this->is_initialized);
R_ASSERT(svcCloseHandle(this->handle.Move()));
AMS_ABORT_UNLESS(this->is_initialized);
R_ABORT_UNLESS(svcCloseHandle(this->handle.Move()));
this->auto_clear = true;
this->is_initialized = false;
}
InterruptEvent::InterruptEvent(u32 interrupt_id, bool autoclear) {
this->is_initialized = false;
R_ASSERT(this->Initialize(interrupt_id, autoclear));
R_ABORT_UNLESS(this->Initialize(interrupt_id, autoclear));
}
void InterruptEvent::Reset() {
R_ASSERT(svcClearEvent(this->handle.Get()));
R_ABORT_UNLESS(svcClearEvent(this->handle.Get()));
}
void InterruptEvent::Wait() {
AMS_ASSERT(this->is_initialized);
AMS_ABORT_UNLESS(this->is_initialized);
while (true) {
/* Continuously wait, until success. */
@ -65,7 +65,7 @@ namespace ams::os {
}
bool InterruptEvent::TryWait() {
AMS_ASSERT(this->is_initialized);
AMS_ABORT_UNLESS(this->is_initialized);
if (this->auto_clear) {
/* Auto-clear. Just try to reset. */
@ -86,7 +86,7 @@ namespace ams::os {
}
bool InterruptEvent::TimedWait(u64 ns) {
AMS_ASSERT(this->is_initialized);
AMS_ABORT_UNLESS(this->is_initialized);
TimeoutHelper timeout_helper(ns);
while (true) {

View file

@ -29,7 +29,7 @@ namespace ams::os {
void MessageQueue::SendInternal(uintptr_t data) {
/* Ensure we don't corrupt the queue, but this should never happen. */
AMS_ASSERT(this->count < this->capacity);
AMS_ABORT_UNLESS(this->count < this->capacity);
/* Write data to tail of queue. */
this->buffer[(this->count++ + this->offset) % this->capacity] = data;
@ -37,7 +37,7 @@ namespace ams::os {
void MessageQueue::SendNextInternal(uintptr_t data) {
/* Ensure we don't corrupt the queue, but this should never happen. */
AMS_ASSERT(this->count < this->capacity);
AMS_ABORT_UNLESS(this->count < this->capacity);
/* Write data to head of queue. */
this->offset = (this->offset + this->capacity - 1) % this->capacity;
@ -47,7 +47,7 @@ namespace ams::os {
uintptr_t MessageQueue::ReceiveInternal() {
/* Ensure we don't corrupt the queue, but this should never happen. */
AMS_ASSERT(this->count > 0);
AMS_ABORT_UNLESS(this->count > 0);
uintptr_t data = this->buffer[this->offset];
this->offset = (this->offset + 1) % this->capacity;
@ -57,7 +57,7 @@ namespace ams::os {
inline uintptr_t MessageQueue::PeekInternal() {
/* Ensure we don't corrupt the queue, but this should never happen. */
AMS_ASSERT(this->count > 0);
AMS_ABORT_UNLESS(this->count > 0);
return this->buffer[this->offset];
}

View file

@ -65,7 +65,7 @@ namespace ams::os {
void Semaphore::Release() {
std::scoped_lock lk(this->mutex);
AMS_ASSERT(this->count + 1 <= this->max_count);
AMS_ABORT_UNLESS(this->count + 1 <= this->max_count);
this->count++;
this->condvar.Signal();
@ -75,7 +75,7 @@ namespace ams::os {
void Semaphore::Release(int count) {
std::scoped_lock lk(this->mutex);
AMS_ASSERT(this->count + count <= this->max_count);
AMS_ABORT_UNLESS(this->count + count <= this->max_count);
this->count += count;
this->condvar.Broadcast();

View file

@ -20,9 +20,9 @@ namespace ams::os {
SystemEvent::SystemEvent(bool inter_process, bool autoclear) : state(SystemEventState::Uninitialized) {
if (inter_process) {
R_ASSERT(this->InitializeAsInterProcessEvent(autoclear));
R_ABORT_UNLESS(this->InitializeAsInterProcessEvent(autoclear));
} else {
R_ASSERT(this->InitializeAsEvent(autoclear));
R_ABORT_UNLESS(this->InitializeAsEvent(autoclear));
}
}
@ -35,34 +35,34 @@ namespace ams::os {
}
Event &SystemEvent::GetEvent() {
AMS_ASSERT(this->state == SystemEventState::Event);
AMS_ABORT_UNLESS(this->state == SystemEventState::Event);
return GetReference(this->storage_for_event);
}
const Event &SystemEvent::GetEvent() const {
AMS_ASSERT(this->state == SystemEventState::Event);
AMS_ABORT_UNLESS(this->state == SystemEventState::Event);
return GetReference(this->storage_for_event);
}
impl::InterProcessEvent &SystemEvent::GetInterProcessEvent() {
AMS_ASSERT(this->state == SystemEventState::InterProcessEvent);
AMS_ABORT_UNLESS(this->state == SystemEventState::InterProcessEvent);
return GetReference(this->storage_for_inter_process_event);
}
const impl::InterProcessEvent &SystemEvent::GetInterProcessEvent() const {
AMS_ASSERT(this->state == SystemEventState::InterProcessEvent);
AMS_ABORT_UNLESS(this->state == SystemEventState::InterProcessEvent);
return GetReference(this->storage_for_inter_process_event);
}
Result SystemEvent::InitializeAsEvent(bool autoclear) {
AMS_ASSERT(this->state == SystemEventState::Uninitialized);
AMS_ABORT_UNLESS(this->state == SystemEventState::Uninitialized);
new (GetPointer(this->storage_for_event)) Event(autoclear);
this->state = SystemEventState::Event;
return ResultSuccess();
}
Result SystemEvent::InitializeAsInterProcessEvent(bool autoclear) {
AMS_ASSERT(this->state == SystemEventState::Uninitialized);
AMS_ABORT_UNLESS(this->state == SystemEventState::Uninitialized);
new (GetPointer(this->storage_for_inter_process_event)) impl::InterProcessEvent();
this->state = SystemEventState::InterProcessEvent;
@ -77,7 +77,7 @@ namespace ams::os {
}
void SystemEvent::AttachHandles(Handle read_handle, bool manage_read_handle, Handle write_handle, bool manage_write_handle, bool autoclear) {
AMS_ASSERT(this->state == SystemEventState::Uninitialized);
AMS_ABORT_UNLESS(this->state == SystemEventState::Uninitialized);
new (GetPointer(this->storage_for_inter_process_event)) impl::InterProcessEvent();
this->state = SystemEventState::InterProcessEvent;
this->GetInterProcessEvent().Initialize(read_handle, manage_read_handle, write_handle, manage_write_handle, autoclear);
@ -92,22 +92,22 @@ namespace ams::os {
}
Handle SystemEvent::DetachReadableHandle() {
AMS_ASSERT(this->state == SystemEventState::InterProcessEvent);
AMS_ABORT_UNLESS(this->state == SystemEventState::InterProcessEvent);
return this->GetInterProcessEvent().DetachReadableHandle();
}
Handle SystemEvent::DetachWritableHandle() {
AMS_ASSERT(this->state == SystemEventState::InterProcessEvent);
AMS_ABORT_UNLESS(this->state == SystemEventState::InterProcessEvent);
return this->GetInterProcessEvent().DetachWritableHandle();
}
Handle SystemEvent::GetReadableHandle() const {
AMS_ASSERT(this->state == SystemEventState::InterProcessEvent);
AMS_ABORT_UNLESS(this->state == SystemEventState::InterProcessEvent);
return this->GetInterProcessEvent().GetReadableHandle();
}
Handle SystemEvent::GetWritableHandle() const {
AMS_ASSERT(this->state == SystemEventState::InterProcessEvent);
AMS_ABORT_UNLESS(this->state == SystemEventState::InterProcessEvent);
return this->GetInterProcessEvent().GetWritableHandle();
}

View file

@ -20,7 +20,7 @@ namespace ams::os {
WaitableHolder::WaitableHolder(Handle handle) {
/* Don't allow invalid handles. */
AMS_ASSERT(handle != INVALID_HANDLE);
AMS_ABORT_UNLESS(handle != INVALID_HANDLE);
/* Initialize appropriate holder. */
new (GetPointer(this->impl_storage)) impl::WaitableHolderOfHandle(handle);
@ -98,7 +98,7 @@ namespace ams::os {
auto holder_base = reinterpret_cast<impl::WaitableHolderBase *>(GetPointer(this->impl_storage));
/* Don't allow destruction of a linked waitable holder. */
AMS_ASSERT(!holder_base->IsLinkedToManager());
AMS_ABORT_UNLESS(!holder_base->IsLinkedToManager());
holder_base->~WaitableHolderBase();
}
@ -107,7 +107,7 @@ namespace ams::os {
auto holder_base = reinterpret_cast<impl::WaitableHolderBase *>(GetPointer(this->impl_storage));
/* Don't allow unlinking of an unlinked holder. */
AMS_ASSERT(holder_base->IsLinkedToManager());
AMS_ABORT_UNLESS(holder_base->IsLinkedToManager());
holder_base->GetManager()->UnlinkWaitableHolder(*holder_base);
holder_base->SetManager(nullptr);

View file

@ -27,7 +27,7 @@ namespace ams::os {
auto &impl = GetReference(this->impl_storage);
/* Don't allow destruction of a non-empty waitable holder. */
AMS_ASSERT(impl.IsEmpty());
AMS_ABORT_UNLESS(impl.IsEmpty());
impl.~WaitableManagerImpl();
}
@ -38,7 +38,7 @@ namespace ams::os {
auto &impl = GetReference(this->impl_storage);
/* Don't allow waiting on empty list. */
AMS_ASSERT(!impl.IsEmpty());
AMS_ABORT_UNLESS(!impl.IsEmpty());
return reinterpret_cast<WaitableHolder *>(impl.WaitAny());
}
@ -47,7 +47,7 @@ namespace ams::os {
auto &impl = GetReference(this->impl_storage);
/* Don't allow waiting on empty list. */
AMS_ASSERT(!impl.IsEmpty());
AMS_ABORT_UNLESS(!impl.IsEmpty());
return reinterpret_cast<WaitableHolder *>(impl.TryWaitAny());
}
@ -56,7 +56,7 @@ namespace ams::os {
auto &impl = GetReference(this->impl_storage);
/* Don't allow waiting on empty list. */
AMS_ASSERT(!impl.IsEmpty());
AMS_ABORT_UNLESS(!impl.IsEmpty());
return reinterpret_cast<WaitableHolder *>(impl.TimedWaitAny(timeout));
}
@ -67,7 +67,7 @@ namespace ams::os {
auto holder_base = reinterpret_cast<impl::WaitableHolderBase *>(GetPointer(holder->impl_storage));
/* Don't allow double-linking a holder. */
AMS_ASSERT(!holder_base->IsLinkedToManager());
AMS_ABORT_UNLESS(!holder_base->IsLinkedToManager());
impl.LinkWaitableHolder(*holder_base);
holder_base->SetManager(&impl);