Skip to content

Commit 061f041

Browse files
committed
Pass callback parameters by value to avoid dangling references
Resolves #3483 Resolves #3487
1 parent 706c7c4 commit 061f041

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/AddressSpace.cc

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -886,11 +886,10 @@ void AddressSpace::protect(Task* t, remote_ptr<void> addr, size_t num_bytes,
886886
<< ")";
887887

888888
MemoryRange last_overlap;
889-
auto protector = [this, prot, &last_overlap](const Mapping& mm,
890-
const MemoryRange& rem) {
889+
auto protector = [this, prot, &last_overlap](Mapping m,
890+
MemoryRange rem) {
891891
LOG(debug) << " protecting (" << rem << ") ...";
892892

893-
Mapping m = std::move(mm);
894893
remove_from_map(m.map);
895894

896895
// PROT_GROWSDOWN means that if this is a grows-down segment
@@ -1305,10 +1304,9 @@ void AddressSpace::unmap_internal(Task*, remote_ptr<void> addr,
13051304
ssize_t num_bytes) {
13061305
LOG(debug) << "munmap(" << addr << ", " << num_bytes << ")";
13071306

1308-
auto unmapper = [this](const Mapping& mm, const MemoryRange& rem) {
1307+
auto unmapper = [this](Mapping m, MemoryRange rem) {
13091308
LOG(debug) << " unmapping (" << rem << ") ...";
13101309

1311-
Mapping m = std::move(mm);
13121310
remove_from_map(m.map);
13131311

13141312
LOG(debug) << " erased (" << m.map << ") ...";
@@ -1548,12 +1546,11 @@ void AddressSpace::ensure_replay_matches_single_recorded_mapping(Task* t, Memory
15481546
ASSERT(t, range.start() == floor_page_size(range.start()));
15491547
ASSERT(t, range.end() == ceil_page_size(range.end()));
15501548

1551-
auto fixer = [this, t, range](const Mapping& mm, const MemoryRange&) {
1552-
if (mm.map == range) {
1549+
auto fixer = [this, t, range](Mapping mapping, MemoryRange) {
1550+
if (mapping.map == range) {
15531551
// Existing single mapping covers entire range; nothing to do.
15541552
return;
15551553
}
1556-
Mapping mapping = std::move(mm);
15571554

15581555
// These should be null during replay
15591556
ASSERT(t, !mapping.mapped_file_stat);
@@ -2006,7 +2003,7 @@ void AddressSpace::maybe_update_breakpoints(Task* t, remote_ptr<uint8_t> addr,
20062003

20072004
void AddressSpace::for_each_in_range(
20082005
remote_ptr<void> addr, ssize_t num_bytes,
2009-
function<void(const Mapping& m, const MemoryRange& rem)> f, int how) {
2006+
function<void(Mapping m, MemoryRange rem)> f, int how) {
20102007
remote_ptr<void> region_start = floor_page_size(addr);
20112008
remote_ptr<void> last_unmapped_end = region_start;
20122009
remote_ptr<void> region_end = ceil_page_size(addr + num_bytes);

src/AddressSpace.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,11 +957,14 @@ class AddressSpace : public HasTaskSet {
957957
* Pass |ITERATE_CONTIGUOUS| to stop iterating when the last
958958
* contiguous mapping after |addr| within the region is seen.
959959
* Default is to iterate all mappings in the region.
960+
*
961+
* The callback takes parameters by value to avoid dangling
962+
* references if the memory map is modified inside the callback.
960963
*/
961964
enum { ITERATE_DEFAULT, ITERATE_CONTIGUOUS };
962965
void for_each_in_range(
963966
remote_ptr<void> addr, ssize_t num_bytes,
964-
std::function<void(const Mapping& m, const MemoryRange& rem)> f,
967+
std::function<void(Mapping m, MemoryRange rem)> f,
965968
int how = ITERATE_DEFAULT);
966969

967970
/**

0 commit comments

Comments
 (0)