Skip to content

Commit 793b4c6

Browse files
facontidavideaysha-afrah26claude
committed
fix use-after-free of a blackboard entry held by getAnyLocked
AnyPtrLocked holds raw pointers into a Blackboard::Entry and can not own it without changing its layout (ABI). Attach a custom deleter to every Entry at its two allocation sites: an entry whose last shared_ptr is dropped while its mutex is still locked is parked, and destroyed by a later retirement that finds it unlocked. Being attached at allocation time, the deleter covers unset(), clear(), cloneInto() and ~Blackboard with no change to them, including the unset() already inlined in binaries built against the 4.10 headers. No header change. Alternative to #1181; the AnyPtrLocked* regression tests come from that PR. Co-Authored-By: Aysha Afrah Ziya <aysha26@digiscrypt.com> Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 8843447 commit 793b4c6

2 files changed

Lines changed: 226 additions & 2 deletions

File tree

‎src/blackboard.cpp‎

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
#include "behaviortree_cpp/json_export.h"
44

5+
#include <algorithm>
6+
#include <mutex>
57
#include <tuple>
68
#include <unordered_set>
9+
#include <vector>
710

811
namespace BT
912
{
@@ -14,6 +17,67 @@ bool IsPrivateKey(StringView str)
1417
{
1518
return str.size() >= 1 && str.data()[0] == '_';
1619
}
20+
21+
// Blackboard::getAnyLocked() returns an AnyPtrLocked: raw pointers to the value
22+
// and to the (locked) entry_mutex of an Entry. It does not own the Entry and its
23+
// layout can not change (ABI), so the only sign that the holder is done is the
24+
// release of the mutex. Therefore an Entry whose last shared_ptr is dropped
25+
// (unset(), clear(), cloneInto(), ~Blackboard) while its mutex is still locked
26+
// is parked here, and destroyed later by the first RetireEntry() that finds it
27+
// unlocked.
28+
struct RetiredEntries
29+
{
30+
std::mutex mutex;
31+
std::vector<Blackboard::Entry*> entries;
32+
};
33+
34+
RetiredEntries& GetRetiredEntries()
35+
{
36+
// Never destroyed: a Blackboard with static storage duration may be
37+
// destroyed after this object during program exit.
38+
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
39+
static auto* const instance = new RetiredEntries();
40+
return *instance;
41+
}
42+
43+
// Deleter of every shared_ptr<Entry>. Being attached at allocation time, it
44+
// covers all the removal paths, including those inlined in the user's binary.
45+
void RetireEntry(Blackboard::Entry* retired)
46+
{
47+
std::vector<Blackboard::Entry*> unlocked;
48+
{
49+
auto& parked = GetRetiredEntries();
50+
const std::scoped_lock lock(parked.mutex);
51+
parked.entries.push_back(retired);
52+
// No shared_ptr is left, so nobody can lock these entries anymore: once
53+
// try_lock() succeeds, the entry is unreachable.
54+
// Note: the calling thread may be the one holding the AnyPtrLocked. A
55+
// try_lock() on a std::mutex owned by the caller is formally undefined, but
56+
// it simply fails on all the supported platforms (pthread: EBUSY).
57+
const auto it = std::partition(parked.entries.begin(), parked.entries.end(),
58+
[](Blackboard::Entry* entry) {
59+
if(entry->entry_mutex.try_lock())
60+
{
61+
entry->entry_mutex.unlock();
62+
return false;
63+
}
64+
return true;
65+
});
66+
unlocked.assign(it, parked.entries.end());
67+
parked.entries.erase(it, parked.entries.end());
68+
}
69+
// Destroyed outside the lock: the destructor of a stored value may remove
70+
// other entries, i.e. call this function again.
71+
for(auto* entry : unlocked)
72+
{
73+
delete entry; // NOLINT(cppcoreguidelines-owning-memory)
74+
}
75+
}
76+
77+
std::shared_ptr<Blackboard::Entry> MakeEntry(const TypeInfo& info)
78+
{
79+
return { new Blackboard::Entry(info), &RetireEntry };
80+
}
1781
} // namespace
1882

1983
void Blackboard::enableAutoRemapping(bool remapping)
@@ -248,7 +312,7 @@ void Blackboard::cloneInto(Blackboard& dst) const
248312
{
249313
// create new entry from src
250314
std::scoped_lock src_lock(task.src->entry_mutex);
251-
auto new_entry = std::make_shared<Entry>(task.src->info);
315+
auto new_entry = MakeEntry(task.src->info);
252316
new_entry->value = task.src->value;
253317
new_entry->string_converter = task.src->string_converter;
254318
new_entries.emplace_back(task.key, std::move(new_entry));
@@ -336,7 +400,7 @@ std::shared_ptr<Blackboard::Entry> Blackboard::createEntryImpl(const std::string
336400
}
337401
// not remapped, not found. Create locally.
338402

339-
auto entry = std::make_shared<Entry>(info);
403+
auto entry = MakeEntry(info);
340404
// even if empty, let's assign to it a default type
341405
entry->value = Any(info.type());
342406
storage_.insert({ key, entry });

‎tests/gtest_blackboard.cpp‎

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#include "behaviortree_cpp/blackboard.h"
1414
#include "behaviortree_cpp/bt_factory.h"
1515

16+
#include <atomic>
17+
#include <memory>
18+
#include <thread>
19+
1620
#include <gtest/gtest.h>
1721

1822
#include "../sample_nodes/dummy_nodes.h"
@@ -303,6 +307,162 @@ TEST(BlackboardTest, AnyPtrLocked)
303307
}
304308
#endif
305309

310+
// An entry removed from the blackboard must stay alive as long as an
311+
// AnyPtrLocked refers to it, and removing it must never block, not even
312+
// from the thread holding the lock.
313+
314+
TEST(BlackboardTest, AnyPtrLockedSurvivesUnset)
315+
{
316+
auto blackboard = Blackboard::create();
317+
blackboard->set("value", 42);
318+
319+
auto locked = blackboard->getAnyLocked("value");
320+
ASSERT_TRUE(bool(locked));
321+
322+
blackboard->unset("value");
323+
ASSERT_TRUE(blackboard->getKeys().empty());
324+
325+
// the entry must stay alive as long as we hold the lock
326+
ASSERT_EQ(locked.get()->cast<int>(), 42);
327+
328+
locked = {};
329+
ASSERT_FALSE(bool(blackboard->getAnyLocked("value")));
330+
}
331+
332+
TEST(BlackboardTest, AnyPtrLockedSurvivesClear)
333+
{
334+
auto blackboard = Blackboard::create();
335+
blackboard->set("value", 42);
336+
337+
auto locked = blackboard->getAnyLocked("value");
338+
ASSERT_TRUE(bool(locked));
339+
340+
blackboard->clear();
341+
ASSERT_TRUE(blackboard->getKeys().empty());
342+
ASSERT_EQ(locked.get()->cast<int>(), 42);
343+
}
344+
345+
TEST(BlackboardTest, AnyPtrLockedSurvivesCloneInto)
346+
{
347+
auto src = Blackboard::create();
348+
auto dst = Blackboard::create();
349+
dst->set("stale", 42);
350+
351+
auto locked = dst->getAnyLocked("stale");
352+
ASSERT_TRUE(bool(locked));
353+
354+
// "stale" doesn't exist in src, so cloneInto() removes it from dst
355+
src->cloneInto(*dst);
356+
ASSERT_TRUE(dst->getKeys().empty());
357+
ASSERT_EQ(locked.get()->cast<int>(), 42);
358+
}
359+
360+
TEST(BlackboardTest, AnyPtrLockedSurvivesBlackboardDestruction)
361+
{
362+
auto blackboard = Blackboard::create();
363+
blackboard->set("value", 42);
364+
365+
auto locked = blackboard->getAnyLocked("value");
366+
ASSERT_TRUE(bool(locked));
367+
368+
blackboard.reset();
369+
ASSERT_EQ(locked.get()->cast<int>(), 42);
370+
}
371+
372+
TEST(BlackboardTest, AnyPtrLockedCrossUnsetDoesNotDeadlock)
373+
{
374+
auto blackboard = Blackboard::create();
375+
blackboard->set("A", 1);
376+
blackboard->set("B", 2);
377+
378+
// Each thread holds a lock on one entry and removes the other one, while
379+
// the other thread does the opposite.
380+
std::atomic<int> ready = 0;
381+
int values[2] = { 0, 0 };
382+
auto hold_and_unset = [&](const char* held, const char* removed, int& out) {
383+
auto locked = blackboard->getAnyLocked(held);
384+
ready++;
385+
while(ready < 2)
386+
{
387+
std::this_thread::yield();
388+
}
389+
blackboard->unset(removed);
390+
out = locked ? locked.get()->cast<int>() : 0;
391+
};
392+
393+
std::thread t1(hold_and_unset, "A", "B", std::ref(values[0]));
394+
std::thread t2(hold_and_unset, "B", "A", std::ref(values[1]));
395+
t1.join();
396+
t2.join();
397+
398+
ASSERT_EQ(values[0], 1);
399+
ASSERT_EQ(values[1], 2);
400+
ASSERT_TRUE(blackboard->getKeys().empty());
401+
}
402+
403+
TEST(BlackboardTest, AnyPtrLockedDeferredEntryIsDestroyed)
404+
{
405+
auto blackboard = Blackboard::create();
406+
auto value = std::make_shared<int>(42);
407+
std::weak_ptr<int> weak_value = value;
408+
blackboard->set("value", value);
409+
value.reset();
410+
411+
{
412+
auto locked = blackboard->getAnyLocked("value");
413+
ASSERT_TRUE(bool(locked));
414+
blackboard->unset("value");
415+
// still alive, since we hold the lock
416+
ASSERT_FALSE(weak_value.expired());
417+
}
418+
// the lock was released: the entry is destroyed by the next removal
419+
blackboard->set("other", 1);
420+
blackboard->unset("other");
421+
ASSERT_TRUE(weak_value.expired());
422+
}
423+
424+
// Stress test, meaningful with ASan/TSan: readers hold an AnyPtrLocked while
425+
// another thread keeps removing and re-creating the same entry.
426+
TEST(BlackboardTest, AnyPtrLockedConcurrentUnsetStress)
427+
{
428+
auto blackboard = Blackboard::create();
429+
std::atomic_bool stop = false;
430+
std::atomic_int reads = 0;
431+
432+
auto reader = [&]() {
433+
while(!stop)
434+
{
435+
if(auto locked = blackboard->getAnyLocked("value"))
436+
{
437+
// set() inserts the entry before assigning the value: it may be empty.
438+
// Otherwise, read it while the writer may unset the key.
439+
if(!locked->empty())
440+
{
441+
ASSERT_EQ(locked->cast<int>(), 42);
442+
reads++;
443+
}
444+
}
445+
}
446+
};
447+
std::thread reader_a(reader);
448+
std::thread reader_b(reader);
449+
450+
for(int i = 0; i < 5000; i++)
451+
{
452+
blackboard->set("value", 42);
453+
blackboard->unset("value");
454+
}
455+
// make sure that the readers had the chance to run
456+
blackboard->set("value", 42);
457+
while(reads == 0)
458+
{
459+
std::this_thread::yield();
460+
}
461+
stop = true;
462+
reader_a.join();
463+
reader_b.join();
464+
}
465+
306466
TEST(BlackboardTest, SetStringView)
307467
{
308468
auto bb = Blackboard::create();

0 commit comments

Comments
 (0)