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

@@ -40,7 +40,7 @@ namespace ams {
const u32 build_version = exosphere::GetVersion(ATMOSPHERE_RELEASE_VERSION);
if (runtime_version < build_version) {
R_ASSERT(exosphere::ResultVersionMismatch());
R_ABORT_UNLESS(exosphere::ResultVersionMismatch());
}
}

View File

@@ -29,7 +29,7 @@ namespace ams::dd {
inline uintptr_t GetIoMapping(uintptr_t phys_addr, size_t size) {
const uintptr_t io_mapping = QueryIoMapping(phys_addr, size);
AMS_ASSERT(io_mapping);
AMS_ABORT_UNLESS(io_mapping);
return io_mapping;
}

View File

@@ -83,7 +83,7 @@ namespace ams::fssystem {
/* Iteration API */
template<typename OnEnterDir, typename OnExitDir, typename OnFile>
Result IterateDirectoryRecursively(fs::fsa::IFileSystem *fs, const char *root_path, char *work_path, size_t work_path_size, fs::DirectoryEntry *dir_ent_buf, OnEnterDir on_enter_dir, OnExitDir on_exit_dir, OnFile on_file) {
AMS_ASSERT(work_path_size >= fs::EntryNameLengthMax + 1);
AMS_ABORT_UNLESS(work_path_size >= fs::EntryNameLengthMax + 1);
/* Get size of the root path. */
size_t root_path_len = strnlen(root_path, fs::EntryNameLengthMax + 1);

View File

@@ -66,7 +66,7 @@ namespace ams::kvdb {
Result Initialize(size_t size) {
/* Check that we're not already initialized. */
AMS_ASSERT(this->buffer == nullptr);
AMS_ABORT_UNLESS(this->buffer == nullptr);
/* Allocate a buffer. */
this->buffer = static_cast<u8 *>(std::malloc(size));

View File

@@ -28,7 +28,7 @@ namespace ams::kvdb {
private:
/* Utility. */
static inline void CheckLength(size_t len) {
AMS_ASSERT(len < N);
AMS_ABORT_UNLESS(len < N);
}
public:
/* Constructors. */
@@ -109,8 +109,8 @@ namespace ams::kvdb {
/* Substring utilities. */
void GetSubstring(char *dst, size_t dst_size, size_t offset, size_t length) const {
/* Make sure output buffer can hold the substring. */
AMS_ASSERT(offset + length <= GetLength());
AMS_ASSERT(dst_size > length);
AMS_ABORT_UNLESS(offset + length <= GetLength());
AMS_ABORT_UNLESS(dst_size > length);
/* Copy substring to dst. */
std::strncpy(dst, this->buffer + offset, length);
dst[length] = 0;

View File

@@ -54,7 +54,7 @@ namespace ams::kvdb {
}
private:
void RemoveIndex(size_t i) {
AMS_ASSERT(i < this->GetCount());
AMS_ABORT_UNLESS(i < this->GetCount());
std::memmove(this->keys + i, this->keys + i + 1, sizeof(*this->keys) * (this->GetCount() - (i + 1)));
this->DecrementCount();
}
@@ -71,8 +71,8 @@ namespace ams::kvdb {
Result Initialize(const char *path, void *buf, size_t size) {
/* Only initialize once, and ensure we have sufficient memory. */
AMS_ASSERT(this->keys == nullptr);
AMS_ASSERT(size >= BufferSize);
AMS_ABORT_UNLESS(this->keys == nullptr);
AMS_ABORT_UNLESS(size >= BufferSize);
/* Setup member variables. */
this->keys = static_cast<Key *>(buf);
@@ -127,23 +127,23 @@ namespace ams::kvdb {
}
Key Get(size_t i) const {
AMS_ASSERT(i < this->GetCount());
AMS_ABORT_UNLESS(i < this->GetCount());
return this->keys[i];
}
Key Peek() const {
AMS_ASSERT(!this->IsEmpty());
AMS_ABORT_UNLESS(!this->IsEmpty());
return this->Get(0);
}
void Push(const Key &key) {
AMS_ASSERT(!this->IsFull());
AMS_ABORT_UNLESS(!this->IsFull());
this->keys[this->GetCount()] = key;
this->IncrementCount();
}
Key Pop() {
AMS_ASSERT(!this->IsEmpty());
AMS_ABORT_UNLESS(!this->IsEmpty());
this->RemoveIndex(0);
}

View File

@@ -92,7 +92,7 @@ namespace ams::kvdb {
static_assert(std::is_pod<Value>::value && !std::is_pointer<Value>::value, "Invalid FileKeyValueStore Value!");
size_t size = 0;
R_TRY(this->Get(&size, out_value, sizeof(Value), key));
AMS_ASSERT(size >= sizeof(Value));
AMS_ABORT_UNLESS(size >= sizeof(Value));
return ResultSuccess();
}

View File

@@ -45,7 +45,7 @@ namespace ams::kvdb {
Value *GetValuePointer() {
/* Size check. Note: Nintendo does not size check. */
if constexpr (!std::is_same<Value, void>::value) {
AMS_ASSERT(sizeof(Value) <= this->value_size);
AMS_ABORT_UNLESS(sizeof(Value) <= this->value_size);
/* Ensure we only get pod. */
static_assert(std::is_pod<Value>::value, "KeyValueStore Values must be pod");
}
@@ -56,7 +56,7 @@ namespace ams::kvdb {
const Value *GetValuePointer() const {
/* Size check. Note: Nintendo does not size check. */
if constexpr (!std::is_same<Value, void>::value) {
AMS_ASSERT(sizeof(Value) <= this->value_size);
AMS_ABORT_UNLESS(sizeof(Value) <= this->value_size);
/* Ensure we only get pod. */
static_assert(std::is_pod<Value>::value, "KeyValueStore Values must be pod");
}

View File

@@ -53,7 +53,7 @@ namespace ams::map {
~AutoCloseMap() {
if (this->process_handle != INVALID_HANDLE && R_SUCCEEDED(this->result)) {
R_ASSERT(svcUnmapProcessMemory(this->mapped_address, this->process_handle, this->base_address, this->size));
R_ABORT_UNLESS(svcUnmapProcessMemory(this->mapped_address, this->process_handle, this->base_address, this->size));
}
}
@@ -88,7 +88,7 @@ namespace ams::map {
~MappedCodeMemory() {
if (this->process_handle != INVALID_HANDLE && R_SUCCEEDED(this->result) && this->size > 0) {
R_ASSERT(svcUnmapProcessCodeMemory(this->process_handle, this->dst_address, this->src_address, this->size));
R_ABORT_UNLESS(svcUnmapProcessCodeMemory(this->process_handle, this->dst_address, this->src_address, this->size));
}
}

View File

@@ -50,7 +50,7 @@ namespace ams::os {
NX_INLINE os::ProcessId GetProcessId(::Handle process_handle) {
os::ProcessId process_id;
R_ASSERT(TryGetProcessId(&process_id, process_handle));
R_ABORT_UNLESS(TryGetProcessId(&process_id, process_handle));
return process_id;
}

View File

@@ -47,7 +47,7 @@ namespace ams::os {
}
void Wait(::Mutex *m) {
R_ASSERT(condvarWait(&this->cv, m));
R_ABORT_UNLESS(condvarWait(&this->cv, m));
}
ConditionVariableStatus TimedWait(os::Mutex *m, u64 timeout) {

View File

@@ -28,7 +28,7 @@ namespace ams::os {
ManagedHandle(Handle h) : hnd(h) { /* ... */ }
~ManagedHandle() {
if (this->hnd != INVALID_HANDLE) {
R_ASSERT(svcCloseHandle(this->hnd));
R_ABORT_UNLESS(svcCloseHandle(this->hnd));
this->hnd = INVALID_HANDLE;
}
}

View File

@@ -71,7 +71,7 @@ namespace ams::os {
constexpr StaticThread() : stack_mem{}, thr{} { /* ... */ }
constexpr StaticThread(ThreadFunc entry, void *arg, int prio, int cpuid = -2) : StaticThread() {
R_ASSERT(this->Initialize(entry, arg, prio, cpuid));
R_ABORT_UNLESS(this->Initialize(entry, arg, prio, cpuid));
}
Result Initialize(ThreadFunc entry, void *arg, int prio, int cpuid = -2) {
@@ -103,7 +103,7 @@ namespace ams::os {
NX_INLINE u32 GetCurrentThreadPriority() {
u32 prio;
R_ASSERT(svcGetThreadPriority(&prio, CUR_THREAD_HANDLE));
R_ABORT_UNLESS(svcGetThreadPriority(&prio, CUR_THREAD_HANDLE));
return prio;
}

View File

@@ -61,7 +61,7 @@ namespace ams::ro {
ModuleType GetType() const {
const ModuleType type = static_cast<ModuleType>(this->type);
AMS_ASSERT(type < ModuleType::Count);
AMS_ABORT_UNLESS(type < ModuleType::Count);
return type;
}

View File

@@ -87,7 +87,7 @@ namespace ams::sf::cmif {
inline DomainObjectId GetId(Entry *e) {
const size_t index = e - this->entries;
AMS_ASSERT(index < this->num_entries);
AMS_ABORT_UNLESS(index < this->num_entries);
return DomainObjectId{ u32(index + 1) };
}

View File

@@ -41,8 +41,8 @@ namespace ams::sf::cmif {
ServerMessageRuntimeMetadata impl_metadata;
public:
DomainServiceObjectProcessor(ServerDomainBase *d, DomainObjectId *in_obj_ids, size_t num_in_objs) : domain(d), in_object_ids(in_obj_ids), num_in_objects(num_in_objs) {
AMS_ASSERT(this->domain != nullptr);
AMS_ASSERT(this->in_object_ids != nullptr);
AMS_ABORT_UNLESS(this->domain != nullptr);
AMS_ABORT_UNLESS(this->in_object_ids != nullptr);
this->impl_processor = nullptr;
this->out_object_ids = nullptr;
this->impl_metadata = {};

View File

@@ -83,11 +83,11 @@ namespace ams::sf::hipc {
virtual ~Server() override {
if (this->service_managed) {
if constexpr (IsMitmServer) {
R_ASSERT(sm::mitm::UninstallMitm(this->service_name));
R_ABORT_UNLESS(sm::mitm::UninstallMitm(this->service_name));
} else {
R_ASSERT(sm::UnregisterService(this->service_name));
R_ABORT_UNLESS(sm::UnregisterService(this->service_name));
}
R_ASSERT(svcCloseHandle(this->port_handle));
R_ABORT_UNLESS(svcCloseHandle(this->port_handle));
}
}
@@ -106,7 +106,7 @@ namespace ams::sf::hipc {
/* Get mitm forward session. */
sm::MitmProcessInfo client_info;
R_ASSERT(sm::mitm::AcknowledgeSession(forward_service.get(), &client_info, this->service_name));
R_ABORT_UNLESS(sm::mitm::AcknowledgeSession(forward_service.get(), &client_info, this->service_name));
*out_obj = std::move(cmif::ServiceObjectHolder(std::move(MakeShared(std::shared_ptr<::Service>(forward_service), client_info))));
*out_fsrv = std::move(forward_service);
@@ -149,7 +149,7 @@ namespace ams::sf::hipc {
void RegisterServerImpl(Handle port_handle, sm::ServiceName service_name, bool managed, cmif::ServiceObjectHolder &&static_holder) {
/* Allocate server memory. */
auto *server = this->AllocateServer();
AMS_ASSERT(server != nullptr);
AMS_ABORT_UNLESS(server != nullptr);
new (server) Server<ServiceImpl, MakeShared>(port_handle, service_name, managed, std::forward<cmif::ServiceObjectHolder>(static_holder));
if constexpr (!ServiceObjectTraits<ServiceImpl>::IsMitmServiceObject) {
@@ -273,13 +273,13 @@ namespace ams::sf::hipc {
private:
constexpr inline size_t GetServerIndex(const ServerBase *server) const {
const size_t i = server - GetPointer(this->server_storages[0]);
AMS_ASSERT(i < MaxServers);
AMS_ABORT_UNLESS(i < MaxServers);
return i;
}
constexpr inline size_t GetSessionIndex(const ServerSession *session) const {
const size_t i = session - GetPointer(this->session_storages[0]);
AMS_ASSERT(i < MaxSessions);
AMS_ABORT_UNLESS(i < MaxSessions);
return i;
}
@@ -301,7 +301,7 @@ namespace ams::sf::hipc {
virtual void FreeSession(ServerSession *session) override final {
std::scoped_lock lk(this->resource_mutex);
const size_t index = this->GetSessionIndex(session);
AMS_ASSERT(this->session_allocated[index]);
AMS_ABORT_UNLESS(this->session_allocated[index]);
this->session_allocated[index] = false;
}
@@ -319,7 +319,7 @@ namespace ams::sf::hipc {
virtual void DestroyServer(ServerBase *server) override final {
std::scoped_lock lk(this->resource_mutex);
const size_t index = this->GetServerIndex(server);
AMS_ASSERT(this->server_allocated[index]);
AMS_ABORT_UNLESS(this->server_allocated[index]);
server->~ServerBase();
this->server_allocated[index] = false;
}
@@ -339,8 +339,8 @@ namespace ams::sf::hipc {
std::scoped_lock lk(this->resource_mutex);
DomainStorage *ptr = static_cast<DomainStorage *>(domain);
const size_t index = ptr - this->domain_storages;
AMS_ASSERT(index < ManagerOptions::MaxDomains);
AMS_ASSERT(this->domain_allocated[index]);
AMS_ABORT_UNLESS(index < ManagerOptions::MaxDomains);
AMS_ABORT_UNLESS(this->domain_allocated[index]);
this->domain_allocated[index] = false;
}

View File

@@ -58,14 +58,14 @@ namespace ams::sf::hipc {
this->is_closed = false;
this->has_received = false;
this->forward_service = nullptr;
AMS_ASSERT(!this->IsMitmSession());
AMS_ABORT_UNLESS(!this->IsMitmSession());
}
ServerSession(Handle h, cmif::ServiceObjectHolder &&obj, std::shared_ptr<::Service> &&fsrv) : WaitableHolder(h), srv_obj_holder(std::move(obj)), session_handle(h) {
this->is_closed = false;
this->has_received = false;
this->forward_service = std::move(fsrv);
AMS_ASSERT(this->IsMitmSession());
AMS_ABORT_UNLESS(this->IsMitmSession());
}
bool IsMitmSession() const {

View File

@@ -797,8 +797,8 @@ namespace ams::sf::impl {
return;
}
Handle server_handle, client_handle;
R_ASSERT(sf::hipc::CreateSession(&server_handle, &client_handle));
R_ASSERT(manager->RegisterSession(server_handle, std::move(object)));
R_ABORT_UNLESS(sf::hipc::CreateSession(&server_handle, &client_handle));
R_ABORT_UNLESS(manager->RegisterSession(server_handle, std::move(object)));
response.move_handles[Index] = client_handle;
}
@@ -1013,7 +1013,7 @@ namespace ams::sf::impl {
/* Fake buffer. This is either InData or OutData, but serializing over buffers. */
constexpr auto Attributes = CommandMeta::BufferAttributes[Info.buffer_index];
if constexpr (Attributes & SfBufferAttr_In) {
/* TODO: AMS_ASSERT()? N does not bother. */
/* TODO: AMS_ABORT_UNLESS()? N does not bother. */
return *reinterpret_cast<const T *>(buffers[Info.buffer_index].GetAddress());
} else if constexpr (Attributes & SfBufferAttr_Out) {
return T(buffers[Info.buffer_index]);

View File

@@ -44,7 +44,7 @@ namespace ams::sf {
public:
constexpr Out(uintptr_t p) : ptr(reinterpret_cast<T *>(p)) { /* ... */ }
constexpr Out(T *p) : ptr(p) { /* ... */ }
constexpr Out(const cmif::PointerAndSize &pas) : ptr(reinterpret_cast<T *>(pas.GetAddress())) { /* TODO: Is AMS_ASSERT(pas.GetSize() >= sizeof(T)); necessary? */ }
constexpr Out(const cmif::PointerAndSize &pas) : ptr(reinterpret_cast<T *>(pas.GetAddress())) { /* TODO: Is AMS_ABORT_UNLESS(pas.GetSize() >= sizeof(T)); necessary? */ }
void SetValue(const T& value) const {
*this->ptr = value;

View File

@@ -62,7 +62,7 @@ namespace ams::sm {
}
Result Initialize() {
AMS_ASSERT(!this->has_initialized);
AMS_ABORT_UNLESS(!this->has_initialized);
sm::DoWithSession([&]() {
this->result = Initializer();
@@ -73,7 +73,7 @@ namespace ams::sm {
}
void Finalize() {
AMS_ASSERT(this->has_initialized);
AMS_ABORT_UNLESS(this->has_initialized);
Finalizer();
this->has_initialized = false;
}