# HG changeset patch # User Paul Boddie # Date 1677930129 -3600 # Node ID 406b46fcc62fbabd68e065c32bc1b837c482ad1c # Parent 360bebaf5edd3a4827c3bd066da1e376bf49bde1 Fixed deletion of segments when payloads are deallocated. diff -r 360bebaf5edd -r 406b46fcc62f libexec/include/exec/elf.h --- a/libexec/include/exec/elf.h Fri Mar 03 19:29:33 2023 +0100 +++ b/libexec/include/exec/elf.h Sat Mar 04 12:42:09 2023 +0100 @@ -1,7 +1,7 @@ /* * ELF payload decoding support. * - * Copyright (C) 2022 Paul Boddie + * Copyright (C) 2022, 2023 Paul Boddie * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -50,9 +50,6 @@ class Payload { -protected: - Segment **_segments = NULL; - public: virtual ~Payload(); @@ -68,10 +65,13 @@ { protected: HEADER *_header; + Segment **_segments = NULL; public: explicit PayloadVariant(HEADER *header); + virtual ~PayloadVariant(); + l4_addr_t entry_point(); offset_t header_extent(); offset_t program_header_extent(); diff -r 360bebaf5edd -r 406b46fcc62f libexec/include/exec/segment.h --- a/libexec/include/exec/segment.h Fri Mar 03 19:29:33 2023 +0100 +++ b/libexec/include/exec/segment.h Sat Mar 04 12:42:09 2023 +0100 @@ -1,7 +1,7 @@ /* * Program memory segment support. * - * Copyright (C) 2022 Paul Boddie + * Copyright (C) 2022, 2023 Paul Boddie * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -43,6 +43,8 @@ char *_buf; l4re_ds_t _ds; + file_t *_file; + bool _allocated = false, _attached = false; /* Segment region base. */ diff -r 360bebaf5edd -r 406b46fcc62f libexec/lib/src/elf.cc --- a/libexec/lib/src/elf.cc Fri Mar 03 19:29:33 2023 +0100 +++ b/libexec/lib/src/elf.cc Sat Mar 04 12:42:09 2023 +0100 @@ -1,7 +1,7 @@ /* * ELF payload decoding support. * - * Copyright (C) 2022 Paul Boddie + * Copyright (C) 2022, 2023 Paul Boddie * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -85,11 +85,6 @@ Payload::~Payload() { - if (_segments != NULL) - { - for (unsigned int i = 0; i < segments(); i++) - delete _segments[i]; - } } unsigned int Payload::segments() @@ -107,6 +102,18 @@ { } +/* Specific payload destruction. */ + +template +PayloadVariant::~PayloadVariant() +{ + if (_segments != NULL) + { + for (unsigned int i = 0; i < segments(); i++) + delete _segments[i]; + } +} + template l4_addr_t PayloadVariant::entry_point() { diff -r 360bebaf5edd -r 406b46fcc62f libexec/lib/src/segment.cc --- a/libexec/lib/src/segment.cc Fri Mar 03 19:29:33 2023 +0100 +++ b/libexec/lib/src/segment.cc Sat Mar 04 12:42:09 2023 +0100 @@ -1,7 +1,7 @@ /* * Program memory segment support. * - * Copyright (C) 2022 Paul Boddie + * Copyright (C) 2022, 2023 Paul Boddie * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -21,6 +21,7 @@ #include +#include #include #include @@ -31,10 +32,25 @@ -/* Obligatory destructor. */ +/* Free the allocated resources. */ Segment::~Segment() { + if (_allocated) + { + if (_attached) + ipc_detach_dataspace(_buf); + + /* Also unmap the dataspace, not apparently done by the underlying + functions. */ + + if (l4_is_valid_cap(_ds)) + ipc_cap_free_um(_ds); + } + else if (client_opened(_file)) + { + client_close(_file); + } } /* Common initialisation. */ @@ -74,6 +90,9 @@ long Segment::allocate(bool attach) { + _allocated = true; + _attached = attach; + if (attach) return ipc_allocate_align(_region_allocated_size, L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RW, @@ -113,6 +132,7 @@ return err; } + _file = file; _ds = file->ref; return L4_EOK; }