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 ResourceWarning for open file stream leaks #1538

Merged
merged 1 commit into from
Oct 21, 2019
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
4 changes: 4 additions & 0 deletions manticore/binary/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ def __init__(self, filename):
assert self.interpreter.arch == self.arch
assert self.interpreter.elf.header.e_type in ["ET_DYN", "ET_EXEC"]

def __del__(self):
if self.elf is not None:
self.elf.stream.close()

def maps(self):
for elf_segment in self.elf.iter_segments():
if elf_segment.header.p_type != "PT_LOAD" or elf_segment.header.p_memsz == 0:
Expand Down
14 changes: 14 additions & 0 deletions manticore/platforms/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,12 +950,22 @@ def load(self, filename, env):

# Get interpreter elf
interpreter = None

# Need to clean up when we are done
def _clean_interp_stream():
if interpreter is not None:
try:
interpreter.stream.close()
except IOError as e:
logger.error(str(e))
Comment on lines +956 to +960
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is based on what was done here

if elf is not None:
try:
# Prevents a ResourceWarning
elf.stream.close()
except IOError as e:
logger.error(str(e))


for elf_segment in elf.iter_segments():
if elf_segment.header.p_type != "PT_INTERP":
continue
interpreter_filename = elf_segment.data()[:-1]
logger.info(f"Interpreter filename: {interpreter_filename}")
if os.path.exists(interpreter_filename.decode("utf-8")):
_clean_interp_stream()
interpreter = ELFFile(open(interpreter_filename, "rb"))
elif "LD_LIBRARY_PATH" in env:
for mpath in env["LD_LIBRARY_PATH"].split(":"):
Expand All @@ -964,6 +974,7 @@ def load(self, filename, env):
)
logger.info(f"looking for interpreter {interpreter_path_filename}")
if os.path.exists(interpreter_path_filename):
_clean_interp_stream()
interpreter = ELFFile(open(interpreter_path_filename, "rb"))
break
break
Expand Down Expand Up @@ -1173,6 +1184,9 @@ def load(self, filename, env):
"AT_EXECFN": at_execfn, # Filename of executable.
}

# Clean up interpreter ELFFile
_clean_interp_stream()

def _to_signed_dword(self, dword):
arch_width = self.current.address_bit_size
if arch_width == 32:
Expand Down