Skip to content

Commit

Permalink
AK+Kernel: Don't allow allocations in AK::Function in kernel mode
Browse files Browse the repository at this point in the history
Refs #6369.
Fixes #15053.

Co-authored-by: Brian Gianforcaro <[email protected]>
  • Loading branch information
2 people authored and linusg committed Nov 1, 2022
1 parent 2a840a5 commit e44ccdd
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions AK/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,18 @@ class Function<Out(In...)> {
{
VERIFY(m_call_nesting_level == 0);
using WrapperType = CallableWrapper<Callable>;
#ifndef KERNEL
if constexpr (sizeof(WrapperType) > inline_capacity) {
*bit_cast<CallableWrapperBase**>(&m_storage) = new WrapperType(forward<Callable>(callable));
m_kind = FunctionKind::Outline;
} else {
#endif
static_assert(sizeof(WrapperType) <= inline_capacity);
new (m_storage) WrapperType(forward<Callable>(callable));
m_kind = FunctionKind::Inline;
#ifndef KERNEL
}
#endif
}

void move_from(Function&& other)
Expand All @@ -246,8 +251,13 @@ class Function<Out(In...)> {
FunctionKind m_kind { FunctionKind::NullPointer };
bool m_deferred_clear { false };
mutable Atomic<u16> m_call_nesting_level { 0 };
#ifndef KERNEL
// Empirically determined to fit most lambdas and functions.
static constexpr size_t inline_capacity = 4 * sizeof(void*);
#else
// FIXME: Try to decrease this.
static constexpr size_t inline_capacity = 6 * sizeof(void*);
#endif
alignas(max(alignof(CallableWrapperBase), alignof(CallableWrapperBase*))) u8 m_storage[inline_capacity];
};

Expand Down

0 comments on commit e44ccdd

Please sign in to comment.