From 0220862ba576851e8140516d2c34a5f8540e64f2 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 25 Feb 2019 09:53:18 -0500 Subject: kernel/handle_table: Resolve truncation warnings Avoids implicit truncation warnings from u32 -> u16 (the truncation is desirable behavior here). --- src/core/hle/kernel/handle_table.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/core/hle/kernel/handle_table.cpp') diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp index c8acde5b1..a30038dca 100644 --- a/src/core/hle/kernel/handle_table.cpp +++ b/src/core/hle/kernel/handle_table.cpp @@ -14,11 +14,11 @@ namespace Kernel { namespace { constexpr u16 GetSlot(Handle handle) { - return handle >> 15; + return static_cast(handle >> 15); } constexpr u16 GetGeneration(Handle handle) { - return handle & 0x7FFF; + return static_cast(handle & 0x7FFF); } } // Anonymous namespace -- cgit v1.2.3 From 4f8cd74061464d01e0c3a4ea47a4141e3597cb57 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 25 Feb 2019 10:01:22 -0500 Subject: kernel/handle-table: In-class initialize data members Directly initializes members where applicable. --- src/core/hle/kernel/handle_table.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/core/hle/kernel/handle_table.cpp') diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp index a30038dca..cd3508fa9 100644 --- a/src/core/hle/kernel/handle_table.cpp +++ b/src/core/hle/kernel/handle_table.cpp @@ -23,7 +23,6 @@ constexpr u16 GetGeneration(Handle handle) { } // Anonymous namespace HandleTable::HandleTable() { - next_generation = 1; Clear(); } -- cgit v1.2.3 From 5167d1577d6b4074f46ad90864d6e0d6119089a3 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 25 Feb 2019 10:13:52 -0500 Subject: kernel/handle_table: Allow process capabilities to limit the handle table size The kernel allows restricting the total size of the handle table through the process capability descriptors. Until now, this functionality wasn't hooked up. With this, the process handle tables become properly restricted. In the case of metadata-less executables, the handle table will assume the maximum size is requested, preserving the behavior that existed before these changes. --- src/core/hle/kernel/handle_table.cpp | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'src/core/hle/kernel/handle_table.cpp') diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp index cd3508fa9..84a9ca7fc 100644 --- a/src/core/hle/kernel/handle_table.cpp +++ b/src/core/hle/kernel/handle_table.cpp @@ -28,17 +28,33 @@ HandleTable::HandleTable() { HandleTable::~HandleTable() = default; +ResultCode HandleTable::SetSize(s32 handle_table_size) { + if (static_cast(handle_table_size) > MAX_COUNT) { + return ERR_OUT_OF_MEMORY; + } + + // Values less than or equal to zero indicate to use the maximum allowable + // size for the handle table in the actual kernel, so we ignore the given + // value in that case, since we assume this by default unless this function + // is called. + if (handle_table_size > 0) { + table_size = static_cast(handle_table_size); + } + + return RESULT_SUCCESS; +} + ResultVal HandleTable::Create(SharedPtr obj) { DEBUG_ASSERT(obj != nullptr); - u16 slot = next_free_slot; - if (slot >= generations.size()) { + const u16 slot = next_free_slot; + if (slot >= table_size) { LOG_ERROR(Kernel, "Unable to allocate Handle, too many slots in use."); return ERR_HANDLE_TABLE_FULL; } next_free_slot = generations[slot]; - u16 generation = next_generation++; + const u16 generation = next_generation++; // Overflow count so it fits in the 15 bits dedicated to the generation in the handle. // Horizon OS uses zero to represent an invalid handle, so skip to 1. @@ -79,7 +95,7 @@ bool HandleTable::IsValid(Handle handle) const { std::size_t slot = GetSlot(handle); u16 generation = GetGeneration(handle); - return slot < MAX_COUNT && objects[slot] != nullptr && generations[slot] == generation; + return slot < table_size && objects[slot] != nullptr && generations[slot] == generation; } SharedPtr HandleTable::GetGeneric(Handle handle) const { @@ -96,7 +112,7 @@ SharedPtr HandleTable::GetGeneric(Handle handle) const { } void HandleTable::Clear() { - for (u16 i = 0; i < MAX_COUNT; ++i) { + for (u16 i = 0; i < table_size; ++i) { generations[i] = i + 1; objects[i] = nullptr; } -- cgit v1.2.3 From d29f9e9709b3cab6448b43f00f4f0204680ceee5 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 25 Feb 2019 11:06:00 -0500 Subject: kernel/handle_table: Make local variables as const where applicable Makes immutable state explicit. --- src/core/hle/kernel/handle_table.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/core/hle/kernel/handle_table.cpp') diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp index 84a9ca7fc..bdfaa977f 100644 --- a/src/core/hle/kernel/handle_table.cpp +++ b/src/core/hle/kernel/handle_table.cpp @@ -79,10 +79,11 @@ ResultVal HandleTable::Duplicate(Handle handle) { } ResultCode HandleTable::Close(Handle handle) { - if (!IsValid(handle)) + if (!IsValid(handle)) { return ERR_INVALID_HANDLE; + } - u16 slot = GetSlot(handle); + const u16 slot = GetSlot(handle); objects[slot] = nullptr; @@ -92,8 +93,8 @@ ResultCode HandleTable::Close(Handle handle) { } bool HandleTable::IsValid(Handle handle) const { - std::size_t slot = GetSlot(handle); - u16 generation = GetGeneration(handle); + const std::size_t slot = GetSlot(handle); + const u16 generation = GetGeneration(handle); return slot < table_size && objects[slot] != nullptr && generations[slot] == generation; } -- cgit v1.2.3