Truly independent web browser
AK: Remove the support for scanning the captures of a Function
Andreas Klingf5c6ff18f5dc
diff --git a/AK/Function.h b/AK/Function.hindex b04d0d8c5cb..01c73789d7b 100644--- a/AK/Function.h+++ b/AK/Function.h@@ -31,10 +31,8 @@#include <AK/Assertions.h>#include <AK/Atomic.h>#include <AK/BitCast.h>-#include <AK/Memory.h>#include <AK/Noncopyable.h>#include <AK/ScopeGuard.h>-#include <AK/Span.h>#include <AK/StdLibExtras.h>#include <AK/TypeCasts.h>#include <AK/Types.h>@@ -43,7 +41,6 @@// BlockRuntime methods for Objective-C block closure support.extern "C" void* _Block_copy(void const*);extern "C" void _Block_release(void const*);-extern "C" size_t Block_size(void const*);namespace AK {@@ -65,9 +62,6 @@ inline constexpr bool HaveObjcArc = true;inline constexpr bool HaveObjcArc = false;#endif-// validated in TestFunction.mm-inline constexpr size_t block_layout_size = 32;-}template<typename>@@ -103,34 +97,25 @@ public:clear(false);}- [[nodiscard]] ReadonlyBytes raw_capture_range() const LIFETIME_BOUND- {- if (!m_size)- return {};- if (auto* wrapper = callable_wrapper())- return ReadonlyBytes { wrapper->raw_callable(), m_size };- return {};- }-template<typename CallableType>Function(CallableType&& callable)requires((IsFunctionObject<CallableType> && IsCallableWithArguments<CallableType, Out, In...> && !IsSame<RemoveCVReference<CallableType>, Function>)){- init_with_callable(forward<CallableType>(callable), CallableKind::FunctionObject);+ init_with_callable(forward<CallableType>(callable));}template<typename FunctionType>Function(FunctionType f)requires((IsFunctionPointer<FunctionType> && IsCallableWithArguments<RemovePointer<FunctionType>, Out, In...> && !IsSame<RemoveCVReference<FunctionType>, Function>)){- init_with_callable(move(f), CallableKind::FunctionPointer);+ init_with_callable(move(f));}template<typename BlockType>Function(BlockType b)requires((IsBlockClosure<BlockType> && IsCallableWithArguments<BlockType, Out, In...>)){- init_with_callable(move(b), CallableKind::Block);+ init_with_callable(move(b));}Function(Function&& other)@@ -158,7 +143,7 @@ public:requires((IsFunctionObject<CallableType> && IsCallableWithArguments<CallableType, Out, In...>)){clear();- init_with_callable(forward<CallableType>(callable), CallableKind::FunctionObject);+ init_with_callable(forward<CallableType>(callable));return *this;}@@ -168,7 +153,7 @@ public:{clear();if (f)- init_with_callable(move(f), CallableKind::FunctionPointer);+ init_with_callable(move(f));return *this;}@@ -177,7 +162,7 @@ public:requires((IsBlockClosure<BlockType> && IsCallableWithArguments<BlockType, Out, In...>)){clear();- init_with_callable(static_cast<RemoveCVReference<BlockType>>(block), CallableKind::Block);+ init_with_callable(static_cast<RemoveCVReference<BlockType>>(block));return *this;}@@ -197,12 +182,6 @@ public:}private:- enum class CallableKind {- FunctionPointer,- FunctionObject,- Block,- };-class CallableWrapperBase {AK_ALLOC_WITH_KMALLOC;@@ -211,7 +190,6 @@ private:// Note: This is not const to allow storing mutable lambdas.virtual Out call(In...) = 0;virtual void init_and_swap(u8*, size_t) = 0;- virtual void const* raw_callable() const = 0;};template<typename CallableType>@@ -244,18 +222,9 @@ private:void init_and_swap(u8* destination, size_t size) final override{VERIFY(size >= sizeof(CallableWrapper));- zero_callable_storage(destination, size);new (destination) CallableWrapper { move(m_callable) };}- void const* raw_callable() const final override- {- if constexpr (IsBlockClosure<CallableType>)- return static_cast<u8 const*>(bridge_cast<void>(m_callable)) + Detail::block_layout_size;- else- return &m_callable;- }-private:CallableType m_callable;};@@ -267,17 +236,6 @@ private:Block,};- // A callable is constructed into zeroed storage, since its bytes get scanned conservatively: GC::Function and the- // other cells that visit raw_capture_range() take every pointer-sized word in it that points at a cell for an edge.- // So padding, and storage the callable doesn't use, must not keep whatever the memory held before — a stale pointer- // there would keep alive the cell it points at, and everything that cell reaches. And secure_zero() keeps a- // compiler from dropping the zeroing as a dead store, as GCC's -flifetime-dse otherwise does, since the constructor- // that follows starts the object's lifetime.- static void zero_callable_storage(void* storage, size_t size)- {- secure_zero(storage, size);- }-CallableWrapperBase* callable_wrapper() const{switch (m_kind) {@@ -324,7 +282,7 @@ private:}template<typename Callable>- void init_with_callable(Callable&& callable, CallableKind callable_kind)+ void init_with_callable(Callable&& callable){if constexpr (alignof(Callable) > ExcessiveAlignmentThreshold && !AccommodateExcessiveAlignmentRequirements) {static_assert(@@ -335,28 +293,16 @@ private:}VERIFY(m_call_nesting_level == 0);using WrapperType = CallableWrapper<Callable>;- if (callable_kind == CallableKind::FunctionObject)- m_size = sizeof(Callable);- else- m_size = 0;- if constexpr (IsBlockClosure<Callable>) {- auto block_size = Block_size(bridge_cast<void>(callable));- VERIFY(block_size >= Detail::block_layout_size);- m_size = block_size - Detail::block_layout_size;- }-if constexpr (alignof(Callable) > inline_alignment || sizeof(WrapperType) > inline_capacity) {void* storage;if constexpr (alignof(WrapperType) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)storage = WrapperType::operator new(sizeof(WrapperType), std::align_val_t { alignof(WrapperType) });elsestorage = WrapperType::operator new(sizeof(WrapperType));- zero_callable_storage(storage, sizeof(WrapperType));*bit_cast<CallableWrapperBase**>(&m_storage) = new (storage) WrapperType(forward<Callable>(callable));m_kind = FunctionKind::Outline;} else {static_assert(sizeof(WrapperType) <= inline_capacity);- zero_callable_storage(m_storage, inline_capacity);if constexpr (IsBlockClosure<Callable>) {if constexpr (Detail::HaveObjcArc) {new (m_storage) WrapperType(forward<Callable>(callable));@@ -375,7 +321,6 @@ private:{VERIFY(m_call_nesting_level == 0 && other.m_call_nesting_level == 0);auto* other_wrapper = other.callable_wrapper();- m_size = other.m_size;switch (other.m_kind) {case FunctionKind::NullPointer:break;@@ -395,7 +340,6 @@ private:other.m_kind = FunctionKind::NullPointer;}- size_t m_size { 0 };FunctionKind m_kind { FunctionKind::NullPointer };bool m_deferred_clear { false };mutable Atomic<u16> m_call_nesting_level { 0 };diff --git a/Tests/AK/TestFunction.cpp b/Tests/AK/TestFunction.cppindex 3efc5a09b43..93622d8a474 100644--- a/Tests/AK/TestFunction.cpp+++ b/Tests/AK/TestFunction.cpp@@ -91,91 +91,3 @@ TEST_CASE(outline_wrapper_respects_callable_alignment)Function<bool()> moved = move(function);EXPECT(moved());}--// The GC scans a callable's bytes conservatively (raw_capture_range()), so the padding between captures, and storage-// a callable doesn't use, must not keep whatever stale data the memory held before.-static constexpr u8 stale_byte = 0xaa;--// The captures in these tests put padding after a bool, before a member with a copy constructor of its own, so no copy-// of the captures writes that padding: It keeps whatever the callable's storage held. A callable that's copied as one-// block of bytes would instead bring along the padding of the callable it's copied from, which Function can't clear.-// Check just the padding, since the reference's bytes are an address, and any of them could be the stale byte.-template<typename Captures>-static bool padding_holds_stale_byte(Captures const& captures)-{- auto const* start = reinterpret_cast<u8 const*>(&captures);- auto padding_size = static_cast<size_t>(reinterpret_cast<u8 const*>(&captures.counted) - start) - sizeof(bool);- VERIFY(padding_size > 0);- for (auto value : ReadonlyBytes { start + sizeof(bool), padding_size }) {- if (value == stale_byte)- return true;- }- return false;-}--struct InlineCaptures {- bool flag { true };- CopyOnly counted;-};-using InlineCapturesFunction = Function<InlineCaptures const*()>;--TEST_CASE(inline_callable_padding_is_zeroed)-{- int instance_count = 0;- alignas(InlineCapturesFunction) u8 buffer[sizeof(InlineCapturesFunction)];- __builtin_memset(buffer, stale_byte, sizeof(buffer));-- auto* function = new (buffer) InlineCapturesFunction([captures = InlineCaptures { true, CopyOnly(instance_count) }]() {- return &captures;- });- EXPECT(!padding_holds_stale_byte(*(*function)()));- function->~Function();- EXPECT_EQ(instance_count, 0);-}--TEST_CASE(moved_inline_callable_padding_is_zeroed)-{- int instance_count = 0;- alignas(InlineCapturesFunction) u8 source_buffer[sizeof(InlineCapturesFunction)];- __builtin_memset(source_buffer, stale_byte, sizeof(source_buffer));- auto* source = new (source_buffer) InlineCapturesFunction([captures = InlineCaptures { true, CopyOnly(instance_count) }]() {- return &captures;- });-- alignas(InlineCapturesFunction) u8 buffer[sizeof(InlineCapturesFunction)];- __builtin_memset(buffer, stale_byte, sizeof(buffer));- auto* destination = new (buffer) InlineCapturesFunction(move(*source));- EXPECT(!padding_holds_stale_byte(*(*destination)()));- destination->~Function();- source->~Function();- EXPECT_EQ(instance_count, 0);-}--TEST_CASE(outline_callable_padding_is_zeroed)-{- int instance_count = 0;- struct Captures {- bool flag { true };- CopyOnly counted;- u64 numbers[6] { 1, 2, 3, 4, 5, 6 };- };- static constexpr size_t wrapper_size = sizeof(void*) + sizeof(Captures);-- // Fill enough blocks of the wrapper's size class with stale bytes, and free them, that the allocator hands one of- // them back for the wrapper.- Vector<void*> stale_blocks;- for (size_t i = 0; i < 4096; ++i) {- auto* block = kmalloc(HeapPartition::General, wrapper_size);- __builtin_memset(block, stale_byte, wrapper_size);- stale_blocks.append(block);- }- for (auto* block : stale_blocks)- kfree(block);-- Function<Captures const*()> function = [captures = Captures { true, CopyOnly(instance_count) }]() {- return &captures;- };- auto const* captures = function();- EXPECT_EQ(captures->numbers[5], 6u);- EXPECT(!padding_holds_stale_byte(*captures));-}diff --git a/Tests/AK/TestFunctionObjC.mm b/Tests/AK/TestFunctionObjC.mmindex 3d6f562c96c..b0c22df7bbc 100644--- a/Tests/AK/TestFunctionObjC.mm+++ b/Tests/AK/TestFunctionObjC.mm@@ -6,7 +6,6 @@#include <LibTest/TestCase.h>-#include <AK/ByteReader.h>#include <AK/Function.h>#include <AK/Platform.h>@@ -63,45 +62,6 @@ TEST_CASE(BlockCaptureLongStringAndInt)EXPECT_EQ(x, 2);}-// Struct definitions from llvm-project/compiler-rt/lib/BlocksRuntime/Block_private.h @ d0177670a0e59e9d9719386f85bb78de0929407c-struct Block_descriptor {- unsigned long int reserved;- unsigned long int size;- void (*copy)(void* dst, void* src);- void (*dispose)(void*);-};--struct Block_layout {- void* isa;- int flags;- int reserved;- void (*invoke)(void*, ...);- struct Block_descriptor* descriptor;- /* Imported variables. */-};-// This check is super important for proper tracking of block closure captures-static_assert(sizeof(Block_layout) == AK::Detail::block_layout_size);--TEST_CASE(BlockPointerCaptures)-{- int x = 0;- int* p = &x;-- auto b = ^{- *p = 2;- };-- auto f = Function<void()>(b);- auto span = f.raw_capture_range();-- int* captured_p = ByteReader::load_pointer<int>(span.data());- EXPECT_EQ(captured_p, p);-- f();-- EXPECT_EQ(x, 2);-}-TEST_CASE(AssignBlock){auto b = ^{ };