Changeset 282899 in webkit


Ignore:
Timestamp:
Sep 22, 2021 5:52:25 PM (3 years ago)
Author:
fpizlo@apple.com
Message:

[libpas] fix DebugHeap
https://bugs.webkit.org/show_bug.cgi?id=230658

Reviewed by Yusuke Suzuki.

The previous DebugHeap integration with libpas assumed we didn't have a jit_heap. Now that
we have a jit_heap, we need to be able to do DebugHeap from the bmalloc_heap while we still
use jit_heap.

This was tricky, since previously, libpas could just follow bmalloc and say that if TLC is
not initialized, then we go slow, and the slow path checks for DebugHeap. Now, we might have
a TLC. This means having to push down the debug heap checks into other slow paths.

  • bmalloc/DebugHeap.cpp:

(pas_debug_heap_is_enabled):
(pas_debug_heap_malloc):
(pas_debug_heap_memalign):
(pas_debug_heap_realloc):
(pas_debug_heap_free):

  • libpas/src/libpas/bmalloc_heap_config.c:

(bmalloc_heap_config_activate):

  • libpas/src/libpas/jit_heap.c:

(jit_heap_add_fresh_memory):
(jit_heap_try_allocate):

  • libpas/src/libpas/jit_heap_config.c:
  • libpas/src/libpas/pas_deallocate.c:

(pas_try_deallocate_slow_no_cache):

  • libpas/src/libpas/pas_deallocate.h:

(pas_try_deallocate_not_small):

  • libpas/src/libpas/pas_debug_heap.h:

(pas_debug_heap_is_enabled):
(pas_debug_heap_allocate):

  • libpas/src/libpas/pas_local_allocator_inlines.h:

(pas_local_allocator_try_allocate_small_segregated_slow_impl):
(pas_local_allocator_try_allocate_slow_impl):
(pas_local_allocator_try_allocate):

  • libpas/src/libpas/pas_thread_local_cache.c:

(allocate_cache):
(pas_thread_local_cache_get_local_allocator_if_can_set_cache_slow):

  • libpas/src/libpas/pas_try_allocate.h:

(pas_try_allocate_impl):

  • libpas/src/libpas/pas_try_allocate_common.h:

(pas_try_allocate_common_impl_slow):

  • libpas/src/libpas/pas_try_allocate_intrinsic_primitive.h:

(pas_try_allocate_intrinsic_primitive_impl_medium_slow_case):
(pas_try_allocate_intrinsic_primitive_impl_inline_only):

  • libpas/src/libpas/pas_try_reallocate.h:

(pas_try_reallocate):

Location:
trunk/Source/bmalloc
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/bmalloc/ChangeLog

    r282850 r282899  
     12021-09-22  Filip Pizlo  <fpizlo@apple.com>
     2
     3        [libpas] fix DebugHeap
     4        https://bugs.webkit.org/show_bug.cgi?id=230658
     5
     6        Reviewed by Yusuke Suzuki.
     7
     8        The previous DebugHeap integration with libpas assumed we didn't have a jit_heap. Now that
     9        we have a jit_heap, we need to be able to do DebugHeap from the bmalloc_heap while we still
     10        use jit_heap.
     11
     12        This was tricky, since previously, libpas could just follow bmalloc and say that if TLC is
     13        not initialized, then we go slow, and the slow path checks for DebugHeap. Now, we might have
     14        a TLC. This means having to push down the debug heap checks into other slow paths.
     15
     16        * bmalloc/DebugHeap.cpp:
     17        (pas_debug_heap_is_enabled):
     18        (pas_debug_heap_malloc):
     19        (pas_debug_heap_memalign):
     20        (pas_debug_heap_realloc):
     21        (pas_debug_heap_free):
     22        * libpas/src/libpas/bmalloc_heap_config.c:
     23        (bmalloc_heap_config_activate):
     24        * libpas/src/libpas/jit_heap.c:
     25        (jit_heap_add_fresh_memory):
     26        (jit_heap_try_allocate):
     27        * libpas/src/libpas/jit_heap_config.c:
     28        * libpas/src/libpas/pas_deallocate.c:
     29        (pas_try_deallocate_slow_no_cache):
     30        * libpas/src/libpas/pas_deallocate.h:
     31        (pas_try_deallocate_not_small):
     32        * libpas/src/libpas/pas_debug_heap.h:
     33        (pas_debug_heap_is_enabled):
     34        (pas_debug_heap_allocate):
     35        * libpas/src/libpas/pas_local_allocator_inlines.h:
     36        (pas_local_allocator_try_allocate_small_segregated_slow_impl):
     37        (pas_local_allocator_try_allocate_slow_impl):
     38        (pas_local_allocator_try_allocate):
     39        * libpas/src/libpas/pas_thread_local_cache.c:
     40        (allocate_cache):
     41        (pas_thread_local_cache_get_local_allocator_if_can_set_cache_slow):
     42        * libpas/src/libpas/pas_try_allocate.h:
     43        (pas_try_allocate_impl):
     44        * libpas/src/libpas/pas_try_allocate_common.h:
     45        (pas_try_allocate_common_impl_slow):
     46        * libpas/src/libpas/pas_try_allocate_intrinsic_primitive.h:
     47        (pas_try_allocate_intrinsic_primitive_impl_medium_slow_case):
     48        (pas_try_allocate_intrinsic_primitive_impl_inline_only):
     49        * libpas/src/libpas/pas_try_reallocate.h:
     50        (pas_try_reallocate):
     51
    1522021-09-21  Basuke Suzuki  <basuke.suzuki@sony.com>
    253
  • trunk/Source/bmalloc/bmalloc/DebugHeap.cpp

    r279867 r282899  
    186186#if BENABLE(LIBPAS)
    187187
    188 // FIXME: Currently, we only use libpas for executable allocator. Thus libpas allocator is always enabled even when Malloc=1 is specified.
    189 // To make it work, we disable debug heap for libpas completely. We need additional code to enable libpas only for executable allocator when
    190 // Malloc=1 is specified when bmalloc is replaced with libpas.
    191 
    192 bool pas_debug_heap_is_enabled(void)
    193 {
     188#if BUSE(LIBPAS)
     189
     190using namespace bmalloc;
     191
     192bool pas_debug_heap_is_enabled(pas_heap_config_kind kind)
     193{
     194    switch (kind) {
     195    case pas_heap_config_kind_bmalloc:
     196        return !!DebugHeap::tryGet();
     197    case pas_heap_config_kind_jit:
     198    case pas_heap_config_kind_pas_utility:
     199        return false;
     200    default:
     201        BCRASH();
     202        return false;
     203    }
     204}
     205
     206void* pas_debug_heap_malloc(size_t size)
     207{
     208    return DebugHeap::getExisting()->malloc(size, FailureAction::ReturnNull);
     209}
     210
     211void* pas_debug_heap_memalign(size_t alignment, size_t size)
     212{
     213    return DebugHeap::getExisting()->memalign(alignment, size, FailureAction::ReturnNull);
     214}
     215
     216void* pas_debug_heap_realloc(void* ptr, size_t size)
     217{
     218    return DebugHeap::getExisting()->realloc(ptr, size, FailureAction::ReturnNull);
     219}
     220
     221void pas_debug_heap_free(void* ptr)
     222{
     223    DebugHeap::getExisting()->free(ptr);
     224}
     225
     226#else // BUSE(LIBPAS) -> so !BUSE(LIBPAS)
     227
     228bool pas_debug_heap_is_enabled(pas_heap_config_kind kind)
     229{
     230    BUNUSED_PARAM(kind);
    194231    return false;
    195232}
     
    227264#pragma clang diagnostic pop
    228265
     266#endif // BUSE(LIBPAS) -> so end of !BUSE(LIBPAS)
     267
    229268#endif // BENABLE(LIBPAS)
    230269
  • trunk/Source/bmalloc/libpas/src/libpas/bmalloc_heap_config.c

    r282556 r282899  
    4646void bmalloc_heap_config_activate(void)
    4747{
    48     // FIXME: Temporarily disable it for now until bmalloc is replaced with libpas.
    49     static const bool register_with_libmalloc = false;
     48    static const bool register_with_libmalloc = true;
    5049   
    5150    pas_designated_intrinsic_heap_initialize(&bmalloc_common_primitive_heap.segregated_heap,
    5251                                             &bmalloc_heap_config);
    5352
    54     if (register_with_libmalloc)
     53    if (register_with_libmalloc && !pas_debug_heap_is_enabled(pas_heap_config_kind_bmalloc))
    5554        pas_root_ensure_for_libmalloc_enumeration();
    5655}
  • trunk/Source/bmalloc/libpas/src/libpas/jit_heap.c

    r282556 r282899  
    5151void jit_heap_add_fresh_memory(pas_range range)
    5252{
     53    static const bool verbose = false;
     54
     55    if (verbose)
     56        pas_log("JIT heap at %p...%p\n", (void*)range.begin, (void*)range.end);
     57   
    5358    pas_heap_lock_lock();
    5459    jit_heap_config_add_fresh_memory(range);
     
    6873void* jit_heap_try_allocate(size_t size)
    6974{
    70     return (void*)jit_try_allocate_common_primitive_impl(size, 1).begin;
     75    static const bool verbose = false;
     76    void* result;
     77    if (verbose)
     78        pas_log("going to allocate in jit\n");
     79    result = (void*)jit_try_allocate_common_primitive_impl(size, 1).begin;
     80    if (verbose)
     81        pas_log("done allocating in jit, returning %p\n", result);
     82    return result;
    7183}
    7284
  • trunk/Source/bmalloc/libpas/src/libpas/jit_heap_config.c

    r282556 r282899  
    3939#include "pas_root.h"
    4040
     41#if defined(PAS_BMALLOC)
     42#include "BPlatform.h"
     43#endif
     44
    4145pas_heap_config jit_heap_config = JIT_HEAP_CONFIG;
    4246
  • trunk/Source/bmalloc/libpas/src/libpas/pas_deallocate.c

    r282556 r282899  
    9999    if (verbose)
    100100        pas_log("Trying to deallocate %p.\n", ptr);
    101     if (PAS_UNLIKELY(pas_debug_heap_is_enabled())) {
     101    if (PAS_UNLIKELY(pas_debug_heap_is_enabled(config_ptr->kind))) {
    102102        if (verbose)
    103103            pas_log("Deallocating %p with debug heap.\n", ptr);
  • trunk/Source/bmalloc/libpas/src/libpas/pas_deallocate.h

    r279867 r282899  
    11/*
    2  * Copyright (c) 2019-2020 Apple Inc. All rights reserved.
     2 * Copyright (c) 2019-2021 Apple Inc. All rights reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
     
    2929#include "pas_bitfit_page_inlines.h"
    3030#include "pas_deallocation_mode.h"
     31#include "pas_debug_heap.h"
    3132#include "pas_heap_config.h"
    3233#include "pas_heap_lock.h"
     
    8485{
    8586    pas_page_base* page_base;
     87
     88    if (pas_debug_heap_is_enabled(config.kind)) {
     89        pas_debug_heap_free((void*)begin);
     90        return true;
     91    }
    8692
    8793    page_base = config.page_header_func(begin);
  • trunk/Source/bmalloc/libpas/src/libpas/pas_debug_heap.h

    r279867 r282899  
    2727#define PAS_DEBUG_HEAP_H
    2828
     29#include "pas_allocation_result.h"
     30#include "pas_heap_config_kind.h"
     31#include "pas_log.h"
    2932#include "pas_utils.h"
    3033
     
    3740
    3841/* The implementations are provided by bmalloc. */
    39 PAS_API extern bool pas_debug_heap_is_enabled(void);
     42PAS_API extern bool pas_debug_heap_is_enabled(pas_heap_config_kind kind);
    4043PAS_API extern void* pas_debug_heap_malloc(size_t size);
    4144PAS_API extern void* pas_debug_heap_memalign(size_t alignment, size_t size);
     
    4548#else /* PAS_BMALLOC -> so !PAS_BMALLOC */
    4649
    47 static inline bool pas_debug_heap_is_enabled(void)
     50static inline bool pas_debug_heap_is_enabled(pas_heap_config_kind kind)
    4851{
     52    PAS_UNUSED_PARAM(kind);
    4953    return false;
    5054}
     
    8185#endif /* PAS_BMALLOC -> so end of !PAS_BMALLOC */
    8286
     87static inline pas_allocation_result pas_debug_heap_allocate(size_t size, size_t alignment)
     88{
     89    static const bool verbose = false;
     90   
     91    pas_allocation_result result;
     92    void* raw_result;
     93   
     94    if (alignment > sizeof(void*)) {
     95        if (verbose)
     96            pas_log("Going down debug memalign path.\n");
     97        raw_result = pas_debug_heap_memalign(alignment, size);
     98    } else {
     99        if (verbose)
     100            pas_log("Going down debug malloc path.\n");
     101        raw_result = pas_debug_heap_malloc(size);
     102    }
     103
     104    if (verbose)
     105        pas_log("raw_result = %p\n", raw_result);
     106
     107    result.did_succeed = !!raw_result;
     108    result.begin = (uintptr_t)raw_result;
     109    result.zero_mode = pas_zero_mode_may_have_non_zero;
     110
     111    return result;
     112}
     113
    83114PAS_END_EXTERN_C;
    84115
  • trunk/Source/bmalloc/libpas/src/libpas/pas_local_allocator_inlines.h

    r282556 r282899  
    3232#include "pas_bitfit_size_class.h"
    3333#include "pas_config.h"
     34#include "pas_debug_heap.h"
    3435#include "pas_epoch.h"
    3536#include "pas_full_alloc_bits_inlines.h"
     
    14901491    pas_allocator_counts* counts)
    14911492{
     1493    PAS_ASSERT(!pas_debug_heap_is_enabled(config.kind));
     1494   
    14921495    for (;;) {
    14931496        pas_allocation_result result;
     
    16141617                pas_local_allocator_config_kind_get_string(allocator->config_kind));
    16151618    }
     1619
     1620    PAS_ASSERT(!pas_debug_heap_is_enabled(config.kind));
    16161621   
    16171622    for (;;) {
     
    17461751    }
    17471752
     1753    if (PAS_UNLIKELY(pas_debug_heap_is_enabled(config.kind)))
     1754        return pas_debug_heap_allocate(size_thunk(size_thunk_arg), alignment);
     1755   
    17481756    if (config.small_segregated_config.base.is_enabled &&
    17491757        allocator->config_kind == pas_local_allocator_config_kind_create_normal(
  • trunk/Source/bmalloc/libpas/src/libpas/pas_thread_local_cache.c

    r282556 r282899  
    102102    pas_thread_local_cache* result;
    103103
    104     PAS_ASSERT(!pas_debug_heap_is_enabled());
    105    
    106104    size = pas_thread_local_cache_size_for_allocator_index_capacity(allocator_index_capacity);
    107105   
     
    309307                                                                 pas_heap_config* heap_config)
    310308{
    311     if (!pas_thread_local_cache_can_set() || pas_debug_heap_is_enabled())
     309    if (!pas_thread_local_cache_can_set() || pas_debug_heap_is_enabled(heap_config->kind))
    312310        return pas_local_allocator_result_create_failure();
    313311
  • trunk/Source/bmalloc/libpas/src/libpas/pas_try_allocate.h

    r282556 r282899  
    6161                      pas_try_allocate_common_slow try_allocate_common_slow)
    6262{
     63    static const bool verbose = false;
     64   
    6365    pas_heap_type* type;
    6466    size_t type_size;
     
    6668    unsigned allocator_index;
    6769    pas_thread_local_cache* cache;
     70    pas_typed_allocation_result result;
    6871
    6972    allocator_index = heap_ref->allocator_index;
     
    7376            cache,
    7477            allocator_index);
     78
     79        if (verbose)
     80            pas_log("Got an allocator.\n");
     81       
    7582        if (PAS_LIKELY(allocator.did_succeed)) {
    7683            pas_allocation_result result;
     
    9198                result, type, config.get_type_size(type));
    9299        }
     100
     101        if (verbose)
     102            pas_log("Did not succeed at getting an allocator.\n");
    93103    }
    94104
     
    96106    type_size = config.get_type_size(type);
    97107
    98     return pas_typed_allocation_result_create_with_intrinsic_allocation_result(
     108    if (verbose)
     109        pas_log("Allocating the slow way.\n");
     110
     111    result = pas_typed_allocation_result_create_with_intrinsic_allocation_result(
    99112        try_allocate_common_slow(heap_ref, 1, type_size, 1),
    100113        type, type_size);
     114
     115    if (verbose)
     116        pas_log("result.ptr = %p, result.did_succeed = %d\n", result.ptr, result.did_succeed);
     117
     118    return result;
    101119}
    102120
  • trunk/Source/bmalloc/libpas/src/libpas/pas_try_allocate_common.h

    r282556 r282899  
    108108    alignment = PAS_MAX(alignment, config.get_type_alignment(type));
    109109   
    110     if (PAS_UNLIKELY(pas_debug_heap_is_enabled())) {
    111         void* raw_result;
    112        
    113         if (alignment != 1) {
    114             PAS_ASSERT(alignment > 1);
    115             raw_result = pas_debug_heap_memalign(alignment, size);
    116         } else
    117             raw_result = pas_debug_heap_malloc(size);
    118 
    119         if (raw_result) {
    120             result.did_succeed = true;
    121             result.begin = (uintptr_t)raw_result;
    122         }
    123 
     110    if (PAS_UNLIKELY(pas_debug_heap_is_enabled(config.kind))) {
     111        if (verbose)
     112            pas_log("Debug heap enabled, asking debug heap.\n");
     113        result = pas_debug_heap_allocate(size, alignment);
     114        if (verbose)
     115            pas_log("Got result.ptr = %p, did_succeed = %d\n", result.begin, result.did_succeed);
    124116        return result;
    125117    }
     118
     119    if (verbose)
     120        pas_log("Not using debug heap.\n");
    126121
    127122    heap = pas_ensure_heap(heap_ref, heap_ref_kind, config.config_ptr, runtime_config);
  • trunk/Source/bmalloc/libpas/src/libpas/pas_try_allocate_intrinsic_primitive.h

    r282556 r282899  
    8181    PAS_ASSERT(alignment == 1 || !designation_mode);
    8282
     83    if (verbose)
     84        pas_log("in impl_medium_slow_case for %s\n", pas_heap_config_kind_get_string(config.kind));
     85
    8386    if (!pas_is_power_of_2(alignment))
    8487        return pas_allocation_result_create_failure();
     88
     89    if (PAS_UNLIKELY(pas_debug_heap_is_enabled(config.kind)))
     90        return pas_debug_heap_allocate(size, alignment);
     91
     92    if (verbose)
     93        pas_log("not doing debug heap in impl_medium_slow_case for %s\n", pas_heap_config_kind_get_string(config.kind));
    8594
    8695    /* In the non-memalign case, we can happily handle zero-sized allocations with aligned_size
     
    185194    PAS_ASSERT(alignment == 1 || !designation_mode);
    186195
     196    if (verbose)
     197        pas_log("in impl_inline_only for %s\n", pas_heap_config_kind_get_string(config.kind));
     198
    187199    if (!pas_is_power_of_2(alignment))
    188200        return pas_allocation_result_create_failure();
  • trunk/Source/bmalloc/libpas/src/libpas/pas_try_reallocate.h

    r282556 r282899  
    312312            return allocate_callback(heap, new_count, allocate_callback_arg);
    313313
    314         if (PAS_UNLIKELY(pas_debug_heap_is_enabled())) {
     314        if (PAS_UNLIKELY(pas_debug_heap_is_enabled(config.kind))) {
    315315            void* raw_result;
    316316            size_t size;
Note: See TracChangeset for help on using the changeset viewer.