Kernel: Fix AML unaligned integer reads and buffer shifts over 32

This commit is contained in:
Bananymous 2024-08-13 23:44:17 +03:00
parent f71a29b6c4
commit feadea0e91
2 changed files with 21 additions and 14 deletions

View File

@ -38,7 +38,7 @@ namespace Kernel::ACPI::AML
{ {
uint64_t value = 0; uint64_t value = 0;
for (size_t i = 0; i < BAN::Math::min<size_t>(buffer.size(), 8); i++) for (size_t i = 0; i < BAN::Math::min<size_t>(buffer.size(), 8); i++)
value |= buffer[i] << (8 * i); value |= static_cast<uint64_t>(buffer[i]) << (8 * i);
return MUST(BAN::RefPtr<Integer>::create(value)); return MUST(BAN::RefPtr<Integer>::create(value));
} }
@ -119,7 +119,7 @@ namespace Kernel::ACPI::AML
for (size_t i = 0; i < BAN::Math::min<size_t>(field_bit_size, 64); i++) for (size_t i = 0; i < BAN::Math::min<size_t>(field_bit_size, 64); i++)
{ {
const size_t bit = field_bit_offset + i; const size_t bit = field_bit_offset + i;
value |= ((buffer[bit / 8] >> (bit % 8)) & 1) << i; value |= static_cast<uint64_t>((buffer[bit / 8] >> (bit % 8)) & 1) << i;
} }
return MUST(BAN::RefPtr<Integer>::create(value)); return MUST(BAN::RefPtr<Integer>::create(value));
@ -140,7 +140,7 @@ namespace Kernel::ACPI::AML
for (size_t i = 0; i < field_bit_size; i++) for (size_t i = 0; i < field_bit_size; i++)
{ {
const size_t bit = field_bit_offset + i; const size_t bit = field_bit_offset + i;
value |= ((buffer[bit / 8] >> (bit % 8)) & 1) << i; value |= static_cast<uint64_t>((buffer[bit / 8] >> (bit % 8)) & 1) << i;
} }
return MUST(BAN::RefPtr<AML::Integer>::create(value)); return MUST(BAN::RefPtr<AML::Integer>::create(value));

View File

@ -1,6 +1,5 @@
#pragma once #pragma once
#include <BAN/Endianness.h>
#include <BAN/Optional.h> #include <BAN/Optional.h>
#include <BAN/String.h> #include <BAN/String.h>
#include <BAN/Vector.h> #include <BAN/Vector.h>
@ -94,7 +93,7 @@ namespace Kernel::ACPI::AML
{ {
if (aml_data.size() < 2) if (aml_data.size() < 2)
return ParseResult::Failure; return ParseResult::Failure;
uint8_t value = aml_data[1]; const uint8_t value = aml_data[1];
aml_data = aml_data.slice(2); aml_data = aml_data.slice(2);
return ParseResult(MUST(BAN::RefPtr<Integer>::create(value))); return ParseResult(MUST(BAN::RefPtr<Integer>::create(value)));
} }
@ -102,9 +101,9 @@ namespace Kernel::ACPI::AML
{ {
if (aml_data.size() < 3) if (aml_data.size() < 3)
return ParseResult::Failure; return ParseResult::Failure;
uint16_t value = BAN::little_endian_to_host<uint16_t>( uint16_t value = 0;
*reinterpret_cast<const uint16_t*>(&aml_data[1]) value |= aml_data[1] << 0;
); value |= aml_data[2] << 8;
aml_data = aml_data.slice(3); aml_data = aml_data.slice(3);
return ParseResult(MUST(BAN::RefPtr<Integer>::create(value))); return ParseResult(MUST(BAN::RefPtr<Integer>::create(value)));
} }
@ -112,9 +111,11 @@ namespace Kernel::ACPI::AML
{ {
if (aml_data.size() < 5) if (aml_data.size() < 5)
return ParseResult::Failure; return ParseResult::Failure;
uint32_t value = BAN::little_endian_to_host<uint32_t>( uint32_t value = 0;
*reinterpret_cast<const uint32_t*>(&aml_data[1]) value |= static_cast<uint32_t>(aml_data[1]) << 0;
); value |= static_cast<uint32_t>(aml_data[2]) << 8;
value |= static_cast<uint32_t>(aml_data[3]) << 16;
value |= static_cast<uint32_t>(aml_data[4]) << 24;
aml_data = aml_data.slice(5); aml_data = aml_data.slice(5);
return ParseResult(MUST(BAN::RefPtr<Integer>::create(value))); return ParseResult(MUST(BAN::RefPtr<Integer>::create(value)));
} }
@ -122,9 +123,15 @@ namespace Kernel::ACPI::AML
{ {
if (aml_data.size() < 9) if (aml_data.size() < 9)
return ParseResult::Failure; return ParseResult::Failure;
uint64_t value = BAN::little_endian_to_host<uint64_t>( uint64_t value = 0;
*reinterpret_cast<const uint64_t*>(&aml_data[1]) value |= static_cast<uint64_t>(aml_data[1]) << 0;
); value |= static_cast<uint64_t>(aml_data[2]) << 8;
value |= static_cast<uint64_t>(aml_data[3]) << 16;
value |= static_cast<uint64_t>(aml_data[4]) << 24;
value |= static_cast<uint64_t>(aml_data[5]) << 32;
value |= static_cast<uint64_t>(aml_data[6]) << 40;
value |= static_cast<uint64_t>(aml_data[7]) << 48;
value |= static_cast<uint64_t>(aml_data[8]) << 56;
aml_data = aml_data.slice(9); aml_data = aml_data.slice(9);
return ParseResult(MUST(BAN::RefPtr<Integer>::create(value))); return ParseResult(MUST(BAN::RefPtr<Integer>::create(value)));
} }