From feadea0e91ab4eb6e6c0c79e9bb63c9f2551fec4 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 13 Aug 2024 23:44:17 +0300 Subject: [PATCH] Kernel: Fix AML unaligned integer reads and buffer shifts over 32 --- kernel/include/kernel/ACPI/AML/Buffer.h | 6 ++--- kernel/include/kernel/ACPI/AML/Integer.h | 29 +++++++++++++++--------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/kernel/include/kernel/ACPI/AML/Buffer.h b/kernel/include/kernel/ACPI/AML/Buffer.h index 8b428d62..b1fd7911 100644 --- a/kernel/include/kernel/ACPI/AML/Buffer.h +++ b/kernel/include/kernel/ACPI/AML/Buffer.h @@ -38,7 +38,7 @@ namespace Kernel::ACPI::AML { uint64_t value = 0; for (size_t i = 0; i < BAN::Math::min(buffer.size(), 8); i++) - value |= buffer[i] << (8 * i); + value |= static_cast(buffer[i]) << (8 * i); return MUST(BAN::RefPtr::create(value)); } @@ -119,7 +119,7 @@ namespace Kernel::ACPI::AML for (size_t i = 0; i < BAN::Math::min(field_bit_size, 64); i++) { const size_t bit = field_bit_offset + i; - value |= ((buffer[bit / 8] >> (bit % 8)) & 1) << i; + value |= static_cast((buffer[bit / 8] >> (bit % 8)) & 1) << i; } return MUST(BAN::RefPtr::create(value)); @@ -140,7 +140,7 @@ namespace Kernel::ACPI::AML for (size_t i = 0; i < field_bit_size; i++) { const size_t bit = field_bit_offset + i; - value |= ((buffer[bit / 8] >> (bit % 8)) & 1) << i; + value |= static_cast((buffer[bit / 8] >> (bit % 8)) & 1) << i; } return MUST(BAN::RefPtr::create(value)); diff --git a/kernel/include/kernel/ACPI/AML/Integer.h b/kernel/include/kernel/ACPI/AML/Integer.h index c9457724..baa223a1 100644 --- a/kernel/include/kernel/ACPI/AML/Integer.h +++ b/kernel/include/kernel/ACPI/AML/Integer.h @@ -1,6 +1,5 @@ #pragma once -#include #include #include #include @@ -94,7 +93,7 @@ namespace Kernel::ACPI::AML { if (aml_data.size() < 2) return ParseResult::Failure; - uint8_t value = aml_data[1]; + const uint8_t value = aml_data[1]; aml_data = aml_data.slice(2); return ParseResult(MUST(BAN::RefPtr::create(value))); } @@ -102,9 +101,9 @@ namespace Kernel::ACPI::AML { if (aml_data.size() < 3) return ParseResult::Failure; - uint16_t value = BAN::little_endian_to_host( - *reinterpret_cast(&aml_data[1]) - ); + uint16_t value = 0; + value |= aml_data[1] << 0; + value |= aml_data[2] << 8; aml_data = aml_data.slice(3); return ParseResult(MUST(BAN::RefPtr::create(value))); } @@ -112,9 +111,11 @@ namespace Kernel::ACPI::AML { if (aml_data.size() < 5) return ParseResult::Failure; - uint32_t value = BAN::little_endian_to_host( - *reinterpret_cast(&aml_data[1]) - ); + uint32_t value = 0; + value |= static_cast(aml_data[1]) << 0; + value |= static_cast(aml_data[2]) << 8; + value |= static_cast(aml_data[3]) << 16; + value |= static_cast(aml_data[4]) << 24; aml_data = aml_data.slice(5); return ParseResult(MUST(BAN::RefPtr::create(value))); } @@ -122,9 +123,15 @@ namespace Kernel::ACPI::AML { if (aml_data.size() < 9) return ParseResult::Failure; - uint64_t value = BAN::little_endian_to_host( - *reinterpret_cast(&aml_data[1]) - ); + uint64_t value = 0; + value |= static_cast(aml_data[1]) << 0; + value |= static_cast(aml_data[2]) << 8; + value |= static_cast(aml_data[3]) << 16; + value |= static_cast(aml_data[4]) << 24; + value |= static_cast(aml_data[5]) << 32; + value |= static_cast(aml_data[6]) << 40; + value |= static_cast(aml_data[7]) << 48; + value |= static_cast(aml_data[8]) << 56; aml_data = aml_data.slice(9); return ParseResult(MUST(BAN::RefPtr::create(value))); }