Skip to content

Commit 3ee350d

Browse files
Simnclaude
andcommitted
Don't expose stale values through recycled ThreadLocal slots
When a ThreadLocal is finalised its slot is reused by the next ThreadLocal, but threads still held the values stored for the old one, so a new ThreadLocal could return a value of an unrelated type. Slots now carry a generation that is bumped on reuse, and threads store the generation along with each value. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 504403d commit 3ee350d

4 files changed

Lines changed: 28 additions & 10 deletions

File tree

‎include/hx/thread/ThreadLocal.hpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ namespace hx
1515
struct Impl;
1616

1717
Impl* impl;
18+
// Slots are recycled, values a thread stored for an older generation are not visible.
19+
const int generation;
1820

1921
public:
2022
ThreadLocal_obj();

‎src/hx/thread/ThreadImpl.cpp‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,15 @@ hx::thread::ThreadImpl_obj::ThreadImpl_obj(const int _id)
127127
: id(_id)
128128
, native(new Native())
129129
, scratch(std::numeric_limits<uint16_t>::max(), std::numeric_limits<uint16_t>::max())
130-
, slots(0, 0) { }
130+
, slots(0, 0)
131+
, slotGenerations(0, 0) { }
131132

132133
hx::thread::ThreadImpl_obj::ThreadImpl_obj(const int _id, Thread_obj::CreateFunction _job, CountingSemaphore _semaphore)
133134
: id(_id)
134135
, native(new Native(_job, this, _semaphore))
135136
, scratch(std::numeric_limits<uint16_t>::max(), std::numeric_limits<uint16_t>::max())
136137
, slots(0, 0)
138+
, slotGenerations(0, 0)
137139
{
138140
}
139141

@@ -150,20 +152,27 @@ hx::thread::ThreadImpl_obj::Native::Native(Thread_obj::CreateFunction _job, Thre
150152
#endif
151153
}
152154

153-
Dynamic hx::thread::ThreadImpl_obj::getSlot(const int id)
155+
Dynamic hx::thread::ThreadImpl_obj::getSlot(const int id, const int generation)
154156
{
155-
return slots[id];
157+
if (slotGenerations->__get(id) != generation)
158+
{
159+
return null();
160+
}
161+
162+
return slots->__get(id);
156163
}
157164

158-
void hx::thread::ThreadImpl_obj::setSlot(const int id, const Dynamic& obj)
165+
void hx::thread::ThreadImpl_obj::setSlot(const int id, const Dynamic& obj, const int generation)
159166
{
160167
slots->__SetItem(id, obj);
168+
slotGenerations->__SetItem(id, generation);
161169
}
162170

163171
void hx::thread::ThreadImpl_obj::__Mark(HX_MARK_PARAMS)
164172
{
165173
HX_MARK_MEMBER(scratch);
166174
HX_MARK_MEMBER(slots);
175+
HX_MARK_MEMBER(slotGenerations);
167176
}
168177

169178
#ifdef HXCPP_VISIT_ALLOCS
@@ -172,6 +181,7 @@ void hx::thread::ThreadImpl_obj::__Visit(HX_VISIT_PARAMS)
172181
{
173182
HX_VISIT_MEMBER(scratch);
174183
HX_VISIT_MEMBER(slots);
184+
HX_VISIT_MEMBER(slotGenerations);
175185
}
176186

177187
void hx::thread::ThreadImpl_obj::finalise(hx::Object* obj)

‎src/hx/thread/ThreadImpl.hpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ namespace hx
2020
int cursor;
2121
Array<uint8_t> scratch;
2222
Array<Dynamic> slots;
23+
// The generation of the ThreadLocal each slot value was stored for
24+
Array<int> slotGenerations;
2325

2426
public:
2527
friend class Scratch;
@@ -42,8 +44,8 @@ namespace hx
4244
String getName() HXCPP_OVERRIDE;
4345
void setName(const String& name) HXCPP_OVERRIDE;
4446

45-
Dynamic getSlot(const int id);
46-
void setSlot(const int id, const Dynamic& obj);
47+
Dynamic getSlot(const int id, const int generation = 0);
48+
void setSlot(const int id, const Dynamic& obj, const int generation = 0);
4749

4850
String toString() HXCPP_OVERRIDE;
4951

‎src/hx/thread/ThreadLocal.cpp‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ struct hx::thread::ThreadLocal_obj::Impl
1414
static int counter;
1515

1616
const int slot;
17+
int generation;
1718

18-
Impl(const int _slot) : slot(_slot) {}
19+
Impl(const int _slot) : slot(_slot), generation(0) {}
1920

2021
static Impl* get()
2122
{
@@ -30,6 +31,9 @@ struct hx::thread::ThreadLocal_obj::Impl
3031

3132
slots.pop_front();
3233

34+
// Threads might still hold values from the previous owner of this slot.
35+
recycled->generation++;
36+
3337
return recycled;
3438
}
3539

@@ -47,7 +51,7 @@ std::list<hx::thread::ThreadLocal_obj::Impl*> hx::thread::ThreadLocal_obj::Impl:
4751

4852
int hx::thread::ThreadLocal_obj::Impl::counter = 0;
4953

50-
hx::thread::ThreadLocal_obj::ThreadLocal_obj() : impl(Impl::get())
54+
hx::thread::ThreadLocal_obj::ThreadLocal_obj() : impl(Impl::get()), generation(impl->generation)
5155
{
5256
hx::GCSetFinalizer(this, Impl::finalise);
5357
}
@@ -56,12 +60,12 @@ Dynamic hx::thread::ThreadLocal_obj::get()
5660
{
5761
auto current = reinterpret_cast<hx::thread::ThreadImpl_obj*>(Thread_obj::current().GetPtr());
5862

59-
return current->getSlot(impl->slot);
63+
return current->getSlot(impl->slot, generation);
6064
}
6165

6266
void hx::thread::ThreadLocal_obj::set(Dynamic obj)
6367
{
6468
auto current = reinterpret_cast<hx::thread::ThreadImpl_obj*>(Thread_obj::current().GetPtr());
6569

66-
current->setSlot(impl->slot, obj);
70+
current->setSlot(impl->slot, obj, generation);
6771
}

0 commit comments

Comments
 (0)