-
Notifications
You must be signed in to change notification settings - Fork 29
feat(major): [sc-23696] replace jemalloc dependency with custom malloc interposer #333
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
supersonicbyte
wants to merge
18
commits into
main
Choose a base branch
from
feature/sc-23696/replace-jemalloc-dependency-with-custom
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6e95843
resolve conflicts
supersonicbyte 2ee5b64
linux fix
supersonicbyte 5d45061
drop malloc small & large
supersonicbyte d17af85
resolve
supersonicbyte b1912c4
fix linux env path
supersonicbyte ba18ccd
add comment
supersonicbyte 3a3e0f2
rebase with main
supersonicbyte 8372094
remove dynamic test
supersonicbyte 95c613c
run swift format
supersonicbyte 1dc55b4
remove test target
supersonicbyte 7aa8368
fix tests
supersonicbyte 675f682
fix swift lint
supersonicbyte 6858ed0
resolve pr comments
supersonicbyte 2bbc2ad
resolve pr comments
supersonicbyte 87e6ec1
fixes for malloc interposer
supersonicbyte 59deb29
fix malloc size linux
supersonicbyte 35924ef
Merge branch 'main' into feature/sc-23696/replace-jemalloc-dependency…
hassila c884d7b
run swift-format
supersonicbyte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| .DS_Store | ||
| /.build | ||
| /Packages | ||
| xcuserdata/ | ||
| DerivedData/ | ||
| .swiftpm/configuration/registries.json | ||
| .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
| .netrc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // swift-tools-version: 5.10 | ||
| // The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
|
||
| import PackageDescription | ||
|
|
||
| let package = Package( | ||
| name: "MallocInterposer", | ||
| products: [ | ||
| // Products define the executables and libraries a package produces, making them visible to other packages. | ||
| .library( | ||
| name: "MallocInterposerC", | ||
| type: .dynamic, | ||
| targets: ["MallocInterposerC"]) | ||
| ], | ||
| targets: [ | ||
| // Targets are the basic building blocks of a package, defining a module or a test suite. | ||
| // Targets can depend on other targets in this package and products from dependencies. | ||
| .target( | ||
| name: "MallocInterposerC", | ||
| linkerSettings: [ | ||
| .linkedLibrary("dl") | ||
| ]) | ||
| ] | ||
| ) | ||
99 changes: 99 additions & 0 deletions
99
LocalPackages/MallocInterposerC/Sources/MallocInterposerC/include/interposer.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // | ||
| // Copyright (c) 2022 Ordo One AB. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
|
|
||
| #ifndef INTERPOSER_H | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general for all new source files, add Apache license header: // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// |
||
| #define INTERPOSER_H | ||
|
|
||
| #include <stdlib.h> | ||
| #include <sys/socket.h> | ||
| #include <sys/types.h> | ||
| #if __APPLE__ | ||
| # include <malloc/malloc.h> | ||
| #endif | ||
|
|
||
| // Hook function types | ||
| typedef void (*malloc_hook_t)(size_t size); | ||
| typedef void (*free_hook_t)(void* ptr); | ||
| typedef void (*calloc_hook_t)(size_t nmemb, size_t size); | ||
| typedef void (*realloc_hook_t)(void* ptr, size_t size); | ||
| typedef void (*valloc_hook_t)(size_t size); | ||
| typedef void (*posix_memalign_hook_t)(void **memptr, size_t alignment, size_t size); | ||
|
|
||
| #if __APPLE__ | ||
| typedef void (*malloc_zone_hook_t)(malloc_zone_t *zone, size_t size); | ||
| typedef void (*malloc_zone_calloc_hook_t)(malloc_zone_t *zone, size_t num_items, size_t size); | ||
| typedef void (*malloc_zone_realloc_hook_t)(malloc_zone_t *zone, void *ptr, size_t size); | ||
| typedef void (*malloc_zone_memalign_hook_t)(malloc_zone_t *zone, size_t alignment, size_t size); | ||
| typedef void (*malloc_zone_valloc_hook_t)(malloc_zone_t *zone, size_t size); | ||
| typedef void (*malloc_zone_free_hook_t)(malloc_zone_t *zone, void *ptr); | ||
| #endif | ||
|
|
||
| // Hook management functions | ||
| void set_malloc_hook(malloc_hook_t hook); | ||
| void set_free_hook(free_hook_t hook); | ||
| void set_calloc_hook(calloc_hook_t hook); | ||
| void set_realloc_hook(realloc_hook_t hook); | ||
| void set_posix_memalign_hook(posix_memalign_hook_t hook); | ||
|
|
||
| #if __APPLE__ | ||
| void set_malloc_zone_hook(malloc_zone_hook_t hook); | ||
| void set_malloc_zone_calloc_hook(malloc_zone_calloc_hook_t hook); | ||
| void set_malloc_zone_realloc_hook(malloc_zone_realloc_hook_t hook); | ||
| void set_malloc_zone_memalign_hook(malloc_zone_memalign_hook_t hook); | ||
| void set_malloc_zone_valloc_hook(malloc_zone_valloc_hook_t hook); | ||
| void set_malloc_zone_free_hook(malloc_zone_free_hook_t hook); | ||
| #endif | ||
|
|
||
| void clear_malloc_hook(void); | ||
| void clear_free_hook(void); | ||
| void clear_calloc_hook(void); | ||
| void clear_realloc_hook(void); | ||
|
|
||
| #if __APPLE__ | ||
| void clear_malloc_zone_hook(void); | ||
| void clear_malloc_zone_calloc_hook(void); | ||
| void clear_malloc_zone_realloc_hook(void); | ||
| void clear_malloc_zone_memalign_hook(void); | ||
| void clear_malloc_zone_valloc_hook(void); | ||
| void clear_malloc_zone_free_hook(void); | ||
| #endif | ||
|
|
||
| // Replacement functions | ||
| 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); | ||
|
|
||
| // On Linux we use LD_PRELOAD to interpose the standard malloc functions | ||
| // and we have to declare them ourselves | ||
| #if !__APPLE__ | ||
| void free(void *ptr); | ||
| void *malloc(size_t size); | ||
| void *calloc(size_t nmemb, size_t size); | ||
| void *realloc(void *ptr, size_t size); | ||
| void *reallocf(void *ptr, size_t size); | ||
| void *valloc(size_t size); | ||
| int posix_memalign(void **memptr, size_t alignment, size_t size); | ||
| #endif | ||
|
|
||
|
|
||
| #if __APPLE__ | ||
| void *replacement_malloc_zone_malloc(malloc_zone_t *zone, size_t size); | ||
| void *replacement_malloc_zone_calloc(malloc_zone_t *zone, size_t num_items, size_t size); | ||
| void *replacement_malloc_zone_valloc(malloc_zone_t *zone, size_t size); | ||
| void *replacement_malloc_zone_realloc(malloc_zone_t *zone, void *ptr, size_t size); | ||
| void *replacement_malloc_zone_memalign(malloc_zone_t *zone, size_t alignment, size_t size); | ||
| void replacement_malloc_zone_free(malloc_zone_t *zone, void *ptr); | ||
| #endif | ||
|
|
||
| #endif | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
dllibrary is Linux-specific and doesn't exist on macOS. This will cause build failures on macOS. The linker setting should be conditional:Spotted by Graphite Agent

Is this helpful? React 👍 or 👎 to let us know.