From 911506af9f2c487298e1419f24b1b907364ea770 Mon Sep 17 00:00:00 2001 From: Hendiadyoin1 Date: Fri, 9 Sep 2022 16:47:42 +0200 Subject: [PATCH] LibJS: Align Instructions as void* and roundup variably sized ones sizes Both is indeed needed, the standard alignment would have been 4, but some Instructions, like Jumps need an alignment of 8 Fixes #12127. --- Userland/Libraries/LibJS/Bytecode/Generator.h | 7 +++++-- Userland/Libraries/LibJS/Bytecode/Instruction.h | 2 +- Userland/Libraries/LibJS/Bytecode/Op.h | 5 +++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibJS/Bytecode/Generator.h b/Userland/Libraries/LibJS/Bytecode/Generator.h index 5473e11ca8..85ee15a3b4 100644 --- a/Userland/Libraries/LibJS/Bytecode/Generator.h +++ b/Userland/Libraries/LibJS/Bytecode/Generator.h @@ -65,12 +65,15 @@ public: OpType& emit_with_extra_register_slots(size_t extra_register_slots, Args&&... args) { VERIFY(!is_current_block_terminated()); + + size_t size_to_allocate = round_up_to_power_of_two(sizeof(OpType) + extra_register_slots * sizeof(Register), alignof(void*)); + // If the block doesn't have enough space, switch to another block if constexpr (!OpType::IsTerminator) - ensure_enough_space(sizeof(OpType) + extra_register_slots * sizeof(Register)); + ensure_enough_space(size_to_allocate); void* slot = next_slot(); - grow(sizeof(OpType) + extra_register_slots * sizeof(Register)); + grow(size_to_allocate); new (slot) OpType(forward(args)...); if constexpr (OpType::IsTerminator) m_current_basic_block->terminate({}); diff --git a/Userland/Libraries/LibJS/Bytecode/Instruction.h b/Userland/Libraries/LibJS/Bytecode/Instruction.h index 18fd91de85..ca64c8bfc7 100644 --- a/Userland/Libraries/LibJS/Bytecode/Instruction.h +++ b/Userland/Libraries/LibJS/Bytecode/Instruction.h @@ -92,7 +92,7 @@ namespace JS::Bytecode { -class Instruction { +class alignas(void*) Instruction { public: constexpr static bool IsTerminator = false; diff --git a/Userland/Libraries/LibJS/Bytecode/Op.h b/Userland/Libraries/LibJS/Bytecode/Op.h index a8c4fbf855..db951a337e 100644 --- a/Userland/Libraries/LibJS/Bytecode/Op.h +++ b/Userland/Libraries/LibJS/Bytecode/Op.h @@ -8,6 +8,7 @@ #pragma once +#include #include #include #include @@ -990,9 +991,9 @@ ALWAYS_INLINE void Instruction::replace_references(BasicBlock const& from, Basic ALWAYS_INLINE size_t Instruction::length() const { if (type() == Type::NewArray) - return static_cast(*this).length_impl(); + return round_up_to_power_of_two(static_cast(*this).length_impl(), alignof(void*)); if (type() == Type::CopyObjectExcludingProperties) - return static_cast(*this).length_impl(); + return round_up_to_power_of_two(static_cast(*this).length_impl(), alignof(void*)); #define __BYTECODE_OP(op) \ case Type::op: \