linker: Fix memory leak for reserved memory

When loading a dynamic library, reserved memory is successful, but fail in other steps, such as loading segments, which will generate a memory leak. Because the reserved memory is not released in time.

Bug: https://issuetracker.google.com/issues/263713888

Change-Id: I556ee02e37db5259df0b6c7178cd9a076dab9725
Signed-off-by: huangchaochao <huangchaochao@bytedance.com>
This commit is contained in:
huangchaochao 2022-12-27 19:38:41 +08:00
parent 6f78f756a2
commit bdc3796a87

View file

@ -175,7 +175,8 @@ bool ElfReader::Load(address_space_params* address_space) {
if (did_load_) {
return true;
}
if (ReserveAddressSpace(address_space) && LoadSegments() && FindPhdr() &&
bool reserveSuccess = ReserveAddressSpace(address_space);
if (reserveSuccess && LoadSegments() && FindPhdr() &&
FindGnuPropertySection()) {
did_load_ = true;
#if defined(__aarch64__)
@ -186,6 +187,13 @@ bool ElfReader::Load(address_space_params* address_space) {
}
#endif
}
if (reserveSuccess && !did_load_) {
if (load_start_ != nullptr && load_size_ != 0) {
if (!mapped_by_caller_) {
munmap(load_start_, load_size_);
}
}
}
return did_load_;
}