Skip to content

Commit

Permalink
alloc tests: hook posix_memalign & reallocf (#782) (#954)
Browse files Browse the repository at this point in the history
Motivation:

Swift started using posix_memalign very recently (Jan 2019) so we need
to hook that too to get correct results.

Modifications:

Hook posix_memalign, reallocf & valloc

Result:

alloc tests should pass again

(cherry picked from commit de8d3e2)
  • Loading branch information
weissi authored Apr 8, 2019
1 parent 29a9f2a commit b111dfc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ void *replacement_malloc(size_t size);
void replacement_free(void *ptr);
void *replacement_calloc(size_t nmemb, size_t size);
void *replacement_realloc(void *ptr, size_t size);
void *replacement_reallocf(void *ptr, size_t size);
void *replacement_valloc(size_t size);
int replacement_posix_memalign(void **memptr, size_t alignment, size_t size);

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,36 @@ void *replacement_calloc(size_t count, size_t size) {
return ptr;
}

void *replacement_reallocf(void *ptr, size_t size) {
void *new_ptr = replacement_realloc(ptr, size);
if (!new_ptr) {
replacement_free(new_ptr);
}
return new_ptr;
}

void *replacement_valloc(size_t size) {
// not aligning correctly (should be PAGE_SIZE) but good enough
return replacement_malloc(size);
}

int replacement_posix_memalign(void **memptr, size_t alignment, size_t size) {
// not aligning correctly (should be `alignment`) but good enough
void *ptr = replacement_malloc(size);
if (ptr && memptr) {
*memptr = ptr;
return 0;
} else {
return 1;
}
}

#if __APPLE__
DYLD_INTERPOSE(replacement_free, free)
DYLD_INTERPOSE(replacement_malloc, malloc)
DYLD_INTERPOSE(replacement_realloc, realloc)
DYLD_INTERPOSE(replacement_calloc, calloc)
DYLD_INTERPOSE(replacement_reallocf, reallocf)
DYLD_INTERPOSE(replacement_valloc, valloc)
DYLD_INTERPOSE(replacement_posix_memalign, posix_memalign)
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ void *calloc(size_t nmemb, size_t size) {
void *realloc(void *ptr, size_t size) {
return replacement_realloc(ptr, size);
}
void *reallocf(void *ptr, size_t size) {
return replacement_reallocf(ptr, size);
}
void *valloc(size_t size) {
return replacement_valloc(size);
}
int posix_memalign(void **memptr, size_t alignment, size_t size) {
return replacement_posix_memalign(memptr, alignment, size);
}
#endif

void swift_main(void);
Expand Down

0 comments on commit b111dfc

Please sign in to comment.