Skip to content

317.40.8 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions featureflags/libmalloc.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
<key>Enabled</key>
<false/>
</dict>
<key>ProbGuard</key>
<key>PGuardViaLaunchd</key>
<dict>
<key>Enabled</key>
<true/>
<false/>
</dict>
<key>ProbGuardAllProcesses</key>
<key>PGuardAllProcesses</key>
<dict>
<key>Enabled</key>
<false/>
Expand Down
4 changes: 0 additions & 4 deletions libmalloc.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,13 @@
4DD5962D23E22A3D00D573D2 /* pguard_internals.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = pguard_internals.c; sourceTree = "<group>"; tabWidth = 2; };
4DF318FF23D796550064A673 /* pguard_malloc.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = pguard_malloc.c; sourceTree = "<group>"; tabWidth = 2; };
4DF3190023D796550064A673 /* pguard_malloc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = pguard_malloc.h; sourceTree = "<group>"; };
4DFE482725BA55C5004A50E8 /* malloc_zone_unregister_test.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = malloc_zone_unregister_test.c; sourceTree = "<group>"; };
875E02E32125B62300A7FE8A /* aligned_alloc_test.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = aligned_alloc_test.c; sourceTree = "<group>"; };
875E02E42125C1D100A7FE8A /* posix_memalign_test.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = posix_memalign_test.c; sourceTree = "<group>"; };
8CB962B01F7E9F610046942E /* asan.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = asan.c; sourceTree = "<group>"; };
8CB962B11F7E9FD00046942E /* tsan.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = tsan.c; sourceTree = "<group>"; };
925383D01BD03B4A00F745DB /* Makefile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.make; path = Makefile; sourceTree = "<group>"; };
925383D11BD03B4A00F745DB /* stress_test.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = stress_test.c; sourceTree = "<group>"; };
925383D31BD03B8F00F745DB /* manpages.lst */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = manpages.lst; sourceTree = "<group>"; };
96E1C8ED2653373700B23906 /* magazine_medium_test.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = magazine_medium_test.c; sourceTree = "<group>"; };
B61341DD20114B070038D163 /* ktrace.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ktrace.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.Internal.sdk/System/Library/PrivateFrameworks/ktrace.framework; sourceTree = DEVELOPER_DIR; };
B629CF29202BA3C2007719B9 /* libmalloc_resolver.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = libmalloc_resolver.xcconfig; sourceTree = "<group>"; };
B629CF42202BB337007719B9 /* libmalloc_alt.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libmalloc_alt.a; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -640,7 +638,6 @@
875E02E32125B62300A7FE8A /* aligned_alloc_test.c */,
875E02E42125C1D100A7FE8A /* posix_memalign_test.c */,
B6A414EA1FBDF01C0038DC53 /* malloc_claimed_address_tests.c */,
4DFE482725BA55C5004A50E8 /* malloc_zone_unregister_test.c */,
B6726EC92092473D00E8AF5A /* malloc_heap_check_test.c */,
C9F8C2681D70B521008C4044 /* magazine_small_test.c */,
B64E100A205311DC004C4BA6 /* malloc_size_test.c */,
Expand All @@ -660,7 +657,6 @@
B6D59B0E225EA90E009E5896 /* reallocarray.c */,
B68C985C2180BEB5003DAF36 /* region_cookie_test.c */,
925383D11BD03B4A00F745DB /* stress_test.c */,
96E1C8ED2653373700B23906 /* magazine_medium_test.c */,
);
path = tests;
sourceTree = "<group>";
Expand Down
5 changes: 2 additions & 3 deletions private/malloc_implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@

/********* Libsystem initializers ************/

struct _malloc_late_init {
struct _malloc_functions {
unsigned long version;
/* The following functions are included in version 1 of this structure */
void * (*dlopen) (const char *path, int mode);
void * (*dlsym) (void *handle, const char *symbol);
bool internal_diagnostics; /* os_variant_has_internal_diagnostics() */
};

void __malloc_init(const char *apple[]);
void __malloc_late_init(const struct _malloc_late_init *);
void __stack_logging_early_finished(const struct _malloc_functions *);



Expand Down
13 changes: 0 additions & 13 deletions src/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
#endif
#include <os/tsd.h>
#include <paths.h>
#include <pthread/pthread.h> // _pthread_threadid_self_np_direct()
#include <pthread/private.h> // _pthread_threadid_self_np_direct()
#include <pthread/tsd_private.h> // TSD keys
#include <signal.h>
Expand Down Expand Up @@ -146,18 +145,6 @@ malloc_traced(void)
return malloc_tracing_enabled;
}

static inline uint32_t
_malloc_cpu_number(void)
{
#if TARGET_OS_SIMULATOR
size_t n;
pthread_cpu_number_np(&n);
return (uint32_t)n;
#else
return _os_cpu_number();
#endif
}

/*
* Copies the malloc library's _malloc_msl_lite_hooks_t structure to a given
* location. Size is passed to allow the structure to grow. Since this is
Expand Down
63 changes: 27 additions & 36 deletions src/magazine_medium.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ medium_mag_get_thread_index(void)
{
#if CONFIG_MEDIUM_USES_HYPER_SHIFT
if (os_likely(_os_cpu_number_override == -1)) {
return _malloc_cpu_number() >> hyper_shift;
return _os_cpu_number() >> hyper_shift;
} else {
return _os_cpu_number_override >> hyper_shift;
}
#else // CONFIG_MEDIUM_USES_HYPER_SHIFT
if (os_likely(_os_cpu_number_override == -1)) {
return _malloc_cpu_number();
return _os_cpu_number();
} else {
return _os_cpu_number_override;
}
Expand Down Expand Up @@ -866,7 +866,6 @@ medium_free_scan_madvise_free(rack_t *rack, magazine_t *depot_ptr, region_t r)
medium_advisory_t mat = (medium_advisory_t)pgLo;
mat->next = advisories;
mat->size = pgHi - pgLo;
advisories = mat;
}
break;
}
Expand Down Expand Up @@ -894,7 +893,6 @@ medium_free_scan_madvise_free(rack_t *rack, magazine_t *depot_ptr, region_t r)
medium_advisory_t mat = (medium_advisory_t)pgLo;
mat->next = advisories;
mat->size = pgHi - pgLo;
advisories = mat;
}

memset(&madv_headers[index], 0, sizeof(uint16_t) * alloc_msize);
Expand Down Expand Up @@ -1072,31 +1070,19 @@ medium_madvise_pressure_relief(rack_t *rack)
for (mag_index = 0; mag_index < rack->num_magazines; mag_index++) {
size_t index;
for (index = 0; index < rack->region_generation->num_regions_allocated; ++index) {
rack_region_lock(rack);
SZONE_LOCK(MEDIUM_SZONE_FROM_RACK(rack));

region_t medium = rack->region_generation->hashed_regions[index];
if (!medium || medium == HASHRING_REGION_DEALLOCATED) {
rack_region_unlock(rack);
SZONE_UNLOCK(MEDIUM_SZONE_FROM_RACK(rack));
continue;
}

region_trailer_t *trailer =
REGION_TRAILER_FOR_MEDIUM_REGION(medium);
// Make sure that the owning magazine doesn't try and take this out
// from under our feet.
trailer->dispose_flags |= RACK_DISPOSE_DELAY;
rack_region_unlock(rack);

magazine_t *mag_ptr = mag_lock_zine_for_region_trailer(rack->magazines,
trailer, MAGAZINE_INDEX_FOR_MEDIUM_REGION(medium));
REGION_TRAILER_FOR_MEDIUM_REGION(medium),
MAGAZINE_INDEX_FOR_MEDIUM_REGION(medium));

// If acquiring the region lock was enough to prevent the owning
// magazine from deallocating the region, free it now so we don't
// do wasted work.
if (rack_region_maybe_dispose(rack, medium, MEDIUM_REGION_SIZE, trailer)) {
SZONE_MAGAZINE_PTR_UNLOCK(mag_ptr);
continue;
}
SZONE_UNLOCK(MEDIUM_SZONE_FROM_RACK(rack));

/* Ordering is important here, the magazine of a region may potentially change
* during mag_lock_zine_for_region_trailer, so src_mag_index must be taken
Expand Down Expand Up @@ -1170,6 +1156,7 @@ medium_madvise_free_range_conditional_no_lock(rack_t *rack, magazine_t *mag_ptr,
{
region_trailer_t *node = REGION_TRAILER_FOR_MEDIUM_REGION(region);
msize_t *madvh = MEDIUM_MADVISE_HEADER_FOR_PTR(ptr);

msize_t trigger_msize = trigger_level >> SHIFT_MEDIUM_QUANTUM;

size_t free_header_size = sizeof(medium_inplace_free_entry_s) + sizeof(msize_t);
Expand Down Expand Up @@ -1218,7 +1205,7 @@ medium_madvise_free_range_conditional_no_lock(rack_t *rack, magazine_t *mag_ptr,
}

msize_t right_dirty_msz = 0;
if (right_end_idx > src_end_idx) {
if (right_end_idx < src_end_idx) {
// Same as above, if we had trailing data coalesced with this entry
// and that was not madvised, consider it, too.
right_dirty_msz = medium_madvise_header_dirty_len(madvh, right_start_idx);
Expand All @@ -1231,7 +1218,7 @@ medium_madvise_free_range_conditional_no_lock(rack_t *rack, magazine_t *mag_ptr,
medium_madvise_header_mark_middle(madvh, right_end_idx);
}

// We absolutely can't madvise lower than the free-list entry pointer plus
// We absolutely can't madvise lower the the free-list entry pointer plus
// the header size. When the entry is OOB, there's no header or footer to
// store in memory.
uintptr_t safe_start_ptr = round_page_kernel(rangep + free_header_size);
Expand All @@ -1248,16 +1235,14 @@ medium_madvise_free_range_conditional_no_lock(rack_t *rack, magazine_t *mag_ptr,
MEDIUM_BYTES_FOR_MSIZE(range_msz), safe_end_ptr);

// The page that contains the freelist entry needs to be marked as not
// having been madvised. Note that the quantum is larger than the kernel page size
// so if safe_start_ptr and rangep are on different pages, we just mark
// the whole block as clean.
// having been madvised.
if (range_idx < MEDIUM_META_INDEX_FOR_PTR(safe_start_ptr)) {
medium_madvise_header_mark_dirty(madvh, range_idx,
MEDIUM_META_INDEX_FOR_PTR(safe_start_ptr) - range_idx);
}
if (range_idx + range_msz > MEDIUM_META_INDEX_FOR_PTR(safe_end_ptr)) {
medium_madvise_header_mark_dirty(madvh,
MEDIUM_META_INDEX_FOR_PTR(safe_end_ptr), range_idx +
MEDIUM_META_INDEX_FOR_PTR(safe_end_ptr) + 1, range_idx +
range_msz - MEDIUM_META_INDEX_FOR_PTR(safe_end_ptr));
}

Expand All @@ -1282,12 +1267,10 @@ medium_madvise_free_range_conditional_no_lock(rack_t *rack, magazine_t *mag_ptr,
// We chose not to madvise, we need to re-mark the region as dirty
// for when we come back to it later.
if (left_dirty_msz < left_msz) {
/* The preceding block was clean. */
medium_madvise_header_mark_clean(madvh, range_idx,
left_msz - left_dirty_msz);
}
if (right_dirty_msz < right_msz) {
/* The trailing block was clean. */
medium_madvise_header_mark_clean(madvh, right_start_idx +
right_dirty_msz, right_msz - right_dirty_msz);
}
Expand Down Expand Up @@ -1374,10 +1357,24 @@ medium_free_try_depot_unmap_no_lock(rack_t *rack, magazine_t *depot_ptr, region_
int objects_in_use = medium_free_detach_region(rack, depot_ptr, sparse_region);

if (0 == objects_in_use) {
if (!rack_region_remove(rack, sparse_region, node)) {
// Invalidate the hash table entry for this region with HASHRING_REGION_DEALLOCATED.
// Using HASHRING_REGION_DEALLOCATED preserves the collision chain, using HASHRING_OPEN_ENTRY (0) would not.
rgnhdl_t pSlot = hash_lookup_region_no_lock(rack->region_generation->hashed_regions,
rack->region_generation->num_regions_allocated,
rack->region_generation->num_regions_allocated_shift,
sparse_region);
if (NULL == pSlot) {
malloc_zone_error(rack->debug_flags, true, "medium_free_try_depot_unmap_no_lock hash lookup failed: %p\n", sparse_region);
return NULL;
}
*pSlot = HASHRING_REGION_DEALLOCATED;
depot_ptr->num_bytes_in_magazine -= MEDIUM_REGION_PAYLOAD_BYTES;
// Atomically increment num_regions_dealloc
#ifdef __LP64___
OSAtomicIncrement64(&rack->num_regions_dealloc);
#else
OSAtomicIncrement32((int32_t *)&rack->num_regions_dealloc);
#endif

// Caller will transfer ownership of the region back to the OS with no locks held
MAGMALLOC_DEALLOCREGION(MEDIUM_SZONE_FROM_RACK(rack), (void *)sparse_region, (int)MEDIUM_REGION_SIZE); // DTrace USDT Probe
Expand Down Expand Up @@ -1937,12 +1934,6 @@ medium_try_realloc_in_place(rack_t *rack, void *ptr, size_t old_size, size_t new
/* there's some left, so put the remainder back */
leftover = (unsigned char *)ptr + MEDIUM_BYTES_FOR_MSIZE(new_msize);
medium_free_list_add_ptr(rack, medium_mag_ptr, leftover, leftover_msize);
msize_t leftover_index = MEDIUM_META_INDEX_FOR_PTR(leftover);
if (madv_headers[leftover_index] & MEDIUM_IS_ADVISED) {
medium_madvise_header_mark_clean(madv_headers, leftover_index, leftover_msize);
} else {
medium_madvise_header_mark_dirty(madv_headers, leftover_index, leftover_msize);
}
}
medium_meta_header_set_in_use(meta_headers, index, new_msize);
medium_madvise_header_mark_dirty(madv_headers, index, new_msize);
Expand Down
63 changes: 0 additions & 63 deletions src/magazine_rack.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,66 +159,3 @@ rack_region_insert(rack_t *rack, region_t region)
rack->num_regions++;
_malloc_lock_unlock(&rack->region_lock);
}

bool
rack_region_remove(rack_t *rack, region_t region, region_trailer_t *trailer)
{
bool rv = true;

rack_region_lock(rack);
rgnhdl_t pSlot = hash_lookup_region_no_lock(
rack->region_generation->hashed_regions,
rack->region_generation->num_regions_allocated,
rack->region_generation->num_regions_allocated_shift,
region);

if ((trailer->dispose_flags & RACK_DISPOSE_DELAY) != 0) {
// Still remove this region from the hash table but don't allow the
// current caller to deallocate the region until the pressure thread is
// done with it.
trailer->dispose_flags |= RACK_DISPOSE_NEEDED;
rv = false;
}

if (NULL == pSlot) {
malloc_zone_error(rack->debug_flags, true,
"tiny_free_try_depot_unmap_no_lock hash lookup failed: %p\n",
region);
rv = false;
} else {
// Invalidate the hash table entry for this region with
// HASHRING_REGION_DEALLOCATED. Using HASHRING_REGION_DEALLOCATED
// preserves the collision chain, using HASHRING_OPEN_ENTRY (0) would not.
*pSlot = HASHRING_REGION_DEALLOCATED;

// Atomically increment num_regions_dealloc
#ifdef __LP64__
OSAtomicIncrement64((int64_t *)&rack->num_regions_dealloc);
#else
OSAtomicIncrement32((int32_t *)&rack->num_regions_dealloc);
#endif
}

rack_region_unlock(rack);
return rv;
}

bool
rack_region_maybe_dispose(rack_t *rack, region_t region, size_t region_size,
region_trailer_t *trailer)
{
bool rv = false;
rack_region_lock(rack);

if ((trailer->dispose_flags & RACK_DISPOSE_NEEDED) != 0) {
// We tried to dispose of this region while the pressure thread was
// using it, so now that it's finished we can deallocate it now.
mvm_deallocate_pages((void *)region, region_size,
MALLOC_FIX_GUARD_PAGE_FLAGS(rack->debug_flags));
rv = true;
} else {
trailer->dispose_flags &= ~RACK_DISPOSE_DELAY;
}
rack_region_unlock(rack);
return rv;
}
24 changes: 0 additions & 24 deletions src/magazine_rack.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

typedef void *region_t;
typedef region_t *rgnhdl_t; /* A pointer into hashed_regions array. */
typedef struct region_trailer region_trailer_t;

#define INITIAL_NUM_REGIONS_SHIFT 6 // log2(INITIAL_NUM_REGIONS)
#define INITIAL_NUM_REGIONS (1 << INITIAL_NUM_REGIONS_SHIFT) // Must be a power of 2!
Expand Down Expand Up @@ -96,27 +95,4 @@ MALLOC_NOEXPORT
void
rack_region_insert(rack_t *rack, region_t region);

MALLOC_NOEXPORT
bool
rack_region_remove(rack_t *rack, region_t region, region_trailer_t *trailer);

MALLOC_NOEXPORT
bool
rack_region_maybe_dispose(rack_t *rack, region_t region, size_t region_size,
region_trailer_t *trailer);

MALLOC_NOEXPORT MALLOC_ALWAYS_INLINE
static void
rack_region_lock(rack_t *rack)
{
_malloc_lock_lock(&rack->region_lock);
}

MALLOC_NOEXPORT MALLOC_ALWAYS_INLINE
static void
rack_region_unlock(rack_t *rack)
{
_malloc_lock_unlock(&rack->region_lock);
}

#endif // __MAGAZINE_RACK_H
Loading