Skip to content
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

Fix symbol resolution for malloc_type functions on macOS Sequoia #693

Merged
merged 2 commits into from
Oct 30, 2024
Merged
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
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ jobs:
container:
image: alpine
options: --cap-add=SYS_PTRACE
env:
# Coverage is kind of broken in Alpine
SKIP_COVERAGE_HTML: 1
steps:
- uses: actions/checkout@v4
- name: Set up dependencies
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ pycoverage: ## Run the test suite, with Python code coverage
--cov-append $(PYTEST_ARGS) \
tests
$(PYTHON) -m coverage lcov -i -o pycoverage.lcov
genhtml *coverage.lcov --branch-coverage --output-directory memray-coverage $(GENHTMLOPTS)
if [ -z "$$SKIP_COVERAGE_HTML" ]; then \
genhtml *coverage.lcov --branch-coverage --output-directory memray-coverage $(GENHTMLOPTS); \
fi

.PHONY: valgrind
valgrind: ## Run valgrind, with the correct configuration
Expand Down
1 change: 1 addition & 0 deletions news/693.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a bug that was causing tracking of runtime libraries that are part of the linker cache not work in macOS 15.
19 changes: 17 additions & 2 deletions src/memray/_memray/macho_shenanigans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ patch_symbol(
}
}

static inline const char*
get_canonical_name(const char* name)
{
// In macOS 15 (Sequoia)+, the symbols in the shared cache have a prefix
// "_malloc_type" that we need to remove to match the symbols that we
// are looking for.
const char* prefix = "_malloc_type";
if (strncmp(name, prefix, strlen(prefix)) != 0) {
return name;
}
return name + strlen(prefix);
}

static void
patch_symbols_in_section(
const section_t* section,
Expand All @@ -49,8 +62,9 @@ patch_symbols_in_section(
if (!symbol_name || !(symbol_name[0] == '_' || symbol_name[0] == '.') || !symbol_name[1]) {
continue;
}
const char* canonical_name = get_canonical_name(symbol_name);
#define FOR_EACH_HOOKED_FUNCTION(hookname) \
if (strcmp(MEMRAY_ORIG(hookname).d_symbol, symbol_name + 1) == 0) { \
if (strcmp(MEMRAY_ORIG(hookname).d_symbol, canonical_name + 1) == 0) { \
LOG(DEBUG) << "Patching " << symbol_name << " symbol pointer at " << std::hex << std::showbase \
<< *(symbol_addr_table + i) << " for relocation entry " << (symbol_addr_table + i); \
patch_symbol( \
Expand Down Expand Up @@ -225,9 +239,10 @@ patch_stubs(
if (!symbol_name || !(symbol_name[0] == '_' || symbol_name[0] == '.') || !symbol_name[1]) {
continue;
}
const char* canonical_name = get_canonical_name(symbol_name);
auto stub_addr = reinterpret_cast<uint64_t>(symbol_addr_table + i * element_size);
#define FOR_EACH_HOOKED_FUNCTION(hookname) \
if (strcmp(MEMRAY_ORIG(hookname).d_symbol, symbol_name + 1) == 0) { \
if (strcmp(MEMRAY_ORIG(hookname).d_symbol, canonical_name + 1) == 0) { \
LOG(DEBUG) << "Extracting symbol address for " << symbol_name << " from stub function at " \
<< std::hex << std::showbase << stub_addr; \
void* symbol_addr = reinterpret_cast<void*>(lazy_pointer_from_stub(stub_addr)); \
Expand Down
Loading