Skip to content

Commit

Permalink
Fix excessive memory use in inspect_linkages_lief (#5348)
Browse files Browse the repository at this point in the history
* Fix excessive memory use in inspect_linkages_lief
introduced in gh-5228 which replaced binary.name with str(file).
The former returns the path to the binary whereas the latter returns the
contents of the parsed binary, naturally causing high memory usage.
That semantic change was unintended (misread upstream changes).
Since binary's path is no longer part of the object, pass it separately.

* Use consistent var name in inspect_linkages_lief to avoid variable shadowing.

* Remove redundant get_uniqueness_key call

---------

Signed-off-by: Marcel Bargull <marcel.bargull@udo.edu>
  • Loading branch information
mbargull committed May 22, 2024
1 parent fa90c11 commit 515aa8f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
30 changes: 15 additions & 15 deletions conda_build/os_utils/liefldd.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,12 @@ def _get_path_dirs(prefix):
yield "/".join((prefix, "bin"))


def get_uniqueness_key(file):
def get_uniqueness_key(filename, file):
binary = ensure_binary(file)
if not binary:
return EXE_FORMATS.UNKNOWN
elif binary.format == EXE_FORMATS.MACHO:
return str(file)
return filename
elif binary.format == EXE_FORMATS.ELF and ( # noqa
binary.type == lief.ELF.ELF_CLASS.CLASS32
or binary.type == lief.ELF.ELF_CLASS.CLASS64
Expand All @@ -369,8 +369,8 @@ def get_uniqueness_key(file):
]
if result:
return result[0]
return str(file)
return str(file)
return filename
return filename


def _get_resolved_location(
Expand Down Expand Up @@ -505,13 +505,13 @@ def inspect_linkages_lief(
for element in todo:
todo.pop(0)
filename2 = element[0]
binary = element[1]
if not binary:
binary2 = element[1]
if not binary2:
continue
uniqueness_key = get_uniqueness_key(binary)
uniqueness_key = get_uniqueness_key(filename2, binary2)
if uniqueness_key not in already_seen:
parent_exe_dirname = None
if binary.format == EXE_FORMATS.PE:
if binary2.format == EXE_FORMATS.PE:
tmp_filename = filename2
while tmp_filename:
if (
Expand All @@ -527,17 +527,17 @@ def inspect_linkages_lief(
if ".pyd" in filename2 or (os.sep + "DLLs" + os.sep) in filename2:
parent_exe_dirname = envroot.replace(os.sep, "/") + "/DLLs"
rpaths_by_binary[filename2] = get_rpaths(
binary, parent_exe_dirname, envroot.replace(os.sep, "/"), sysroot
binary2, parent_exe_dirname, envroot.replace(os.sep, "/"), sysroot
)
tmp_filename = filename2
rpaths_transitive = []
if binary.format == EXE_FORMATS.PE:
if binary2.format == EXE_FORMATS.PE:
rpaths_transitive = rpaths_by_binary[tmp_filename]
else:
while tmp_filename:
rpaths_transitive[:0] = rpaths_by_binary[tmp_filename]
tmp_filename = parents_by_filename[tmp_filename]
libraries = get_libraries(binary)
libraries = get_libraries(binary2)
if filename2 in libraries: # Happens on macOS, leading to cycles.
libraries.remove(filename2)
# RPATH is implicit everywhere except macOS, make it explicit to simplify things.
Expand All @@ -546,14 +546,14 @@ def inspect_linkages_lief(
"$RPATH/" + lib
if not lib.startswith("/")
and not lib.startswith("$")
and binary.format != EXE_FORMATS.MACHO # noqa
and binary2.format != EXE_FORMATS.MACHO # noqa
else lib
)
for lib in libraries
]
for lib, orig in zip(libraries, these_orig):
resolved = _get_resolved_location(
binary,
binary2,
orig,
exedir,
exedir,
Expand All @@ -568,7 +568,7 @@ def inspect_linkages_lief(
# can be run case-sensitively if the user wishes.
#
"""
if binary.format == EXE_FORMATS.PE:
if binary2.format == EXE_FORMATS.PE:
import random
path_fixed = (
os.path.dirname(path_fixed)
Expand Down Expand Up @@ -596,7 +596,7 @@ def inspect_linkages_lief(
if recurse:
if os.path.exists(resolved[0]):
todo.append([resolved[0], lief.parse(resolved[0])])
already_seen.add(get_uniqueness_key(binary))
already_seen.add(uniqueness_key)
return results


Expand Down
19 changes: 19 additions & 0 deletions news/5348-fix-excessive-memory-use-in-inspect_linkages_lief
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* <news item>

### Bug fixes

* Fix excessive memory use in `inspect_linkages_lief` (#5267 via #5348)

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>

0 comments on commit 515aa8f

Please sign in to comment.