Skip to content

Commit 96ed683

Browse files
committed
Make get_or_set_index immune to memory address reuse
1 parent eb9391c commit 96ed683

File tree

6 files changed

+24
-24
lines changed

6 files changed

+24
-24
lines changed

CDS_test/safe_ptr.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,19 +350,19 @@ namespace sf {
350350

351351
int get_or_set_index(index_op_t index_op = get_index_op, int set_index = -1) {
352352
thread_local static std::unordered_map<void *, unregister_t> thread_local_index_hashmap;
353-
// get thread index - in any cases
354-
auto it = thread_local_index_hashmap.find(this);
353+
// get thread index - in any cases. Use &shared_locks_array as key ("this" may get recycled if contfree-mtx under "it" was deleted)
354+
auto it = thread_local_index_hashmap.find(&shared_locks_array);
355355
if (it != thread_local_index_hashmap.cend())
356356
set_index = it->second.thread_index;
357357

358358
if (index_op == unregister_thread_op) { // unregister thread
359359
if (shared_locks_array[set_index].value == 1) // if isn't shared_lock now
360-
thread_local_index_hashmap.erase(this);
360+
thread_local_index_hashmap.erase(&shared_locks_array);
361361
else
362362
return -1;
363363
}
364364
else if (index_op == register_thread_op) { // register thread
365-
thread_local_index_hashmap.emplace(this, unregister_t(set_index, shared_locks_array_ptr));
365+
thread_local_index_hashmap.emplace(&shared_locks_array, unregister_t(set_index, shared_locks_array_ptr));
366366

367367
// remove info about deleted contfree-mutexes
368368
for (auto it = thread_local_index_hashmap.begin(), ite = thread_local_index_hashmap.end(); it != ite;) {

Real_app_bench/safe_ptr.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,19 +350,19 @@ namespace sf {
350350

351351
int get_or_set_index(index_op_t index_op = get_index_op, int set_index = -1) {
352352
thread_local static std::unordered_map<void *, unregister_t> thread_local_index_hashmap;
353-
// get thread index - in any cases
354-
auto it = thread_local_index_hashmap.find(this);
353+
// get thread index - in any cases. Use &shared_locks_array as key ("this" may get recycled if contfree-mtx under "it" was deleted)
354+
auto it = thread_local_index_hashmap.find(&shared_locks_array);
355355
if (it != thread_local_index_hashmap.cend())
356356
set_index = it->second.thread_index;
357357

358358
if (index_op == unregister_thread_op) { // unregister thread
359359
if (shared_locks_array[set_index].value == 1) // if isn't shared_lock now
360-
thread_local_index_hashmap.erase(this);
360+
thread_local_index_hashmap.erase(&shared_locks_array);
361361
else
362362
return -1;
363363
}
364364
else if (index_op == register_thread_op) { // register thread
365-
thread_local_index_hashmap.emplace(this, unregister_t(set_index, shared_locks_array_ptr));
365+
thread_local_index_hashmap.emplace(&shared_locks_array, unregister_t(set_index, shared_locks_array_ptr));
366366

367367
// remove info about deleted contfree-mutexes
368368
for (auto it = thread_local_index_hashmap.begin(), ite = thread_local_index_hashmap.end(); it != ite;) {

bench_contfree/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,19 +235,19 @@ class contention_free_shared_mutex {
235235

236236
int get_or_set_index(index_op_t index_op = get_index_op, int set_index = -1) {
237237
thread_local static std::unordered_map<void *, unregister_t> thread_local_index_hashmap;
238-
// get thread index - in any cases
239-
auto it = thread_local_index_hashmap.find(this);
238+
// get thread index - in any cases. Use &shared_locks_array as key ("this" may get recycled if contfree-mtx under "it" was deleted)
239+
auto it = thread_local_index_hashmap.find(&shared_locks_array);
240240
if (it != thread_local_index_hashmap.cend())
241241
set_index = it->second.thread_index;
242242

243243
if (index_op == unregister_thread_op) { // unregister thread
244244
if (shared_locks_array[set_index].value == 1) // if isn't shared_lock now
245-
thread_local_index_hashmap.erase(this);
245+
thread_local_index_hashmap.erase(&shared_locks_array);
246246
else
247247
return -1;
248248
}
249249
else if (index_op == register_thread_op) { // register thread
250-
thread_local_index_hashmap.emplace(this, unregister_t(set_index, shared_locks_array_ptr));
250+
thread_local_index_hashmap.emplace(&shared_locks_array, unregister_t(set_index, shared_locks_array_ptr));
251251

252252
// remove info about deleted contfree-mutexes
253253
for (auto it = thread_local_index_hashmap.begin(), ite = thread_local_index_hashmap.end(); it != ite;) {

benchmark/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,19 +331,19 @@ class contention_free_shared_mutex {
331331

332332
int get_or_set_index(index_op_t index_op = get_index_op, int set_index = -1) {
333333
thread_local static std::unordered_map<void *, unregister_t> thread_local_index_hashmap;
334-
// get thread index - in any cases
335-
auto it = thread_local_index_hashmap.find(this);
334+
// get thread index - in any cases. Use &shared_locks_array as key ("this" may get recycled if contfree-mtx under "it" was deleted)
335+
auto it = thread_local_index_hashmap.find(&shared_locks_array);
336336
if (it != thread_local_index_hashmap.cend())
337337
set_index = it->second.thread_index;
338338

339339
if (index_op == unregister_thread_op) { // unregister thread
340340
if (shared_locks_array[set_index].value == 1) // if isn't shared_lock now
341-
thread_local_index_hashmap.erase(this);
341+
thread_local_index_hashmap.erase(&shared_locks_array);
342342
else
343343
return -1;
344344
}
345345
else if (index_op == register_thread_op) { // register thread
346-
thread_local_index_hashmap.emplace(this, unregister_t(set_index, shared_locks_array_ptr));
346+
thread_local_index_hashmap.emplace(&shared_locks_array, unregister_t(set_index, shared_locks_array_ptr));
347347

348348
// remove info about deleted contfree-mutexes
349349
for (auto it = thread_local_index_hashmap.begin(), ite = thread_local_index_hashmap.end(); it != ite;) {

contfree_shared_mutex/safe_ptr.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,19 @@ namespace sf {
9393

9494
int get_or_set_index(index_op_t index_op = get_index_op, int set_index = -1) {
9595
thread_local static std::unordered_map<void *, unregister_t> thread_local_index_hashmap;
96-
// get thread index - in any cases
97-
auto it = thread_local_index_hashmap.find(this);
96+
// get thread index - in any cases. Use &shared_locks_array as key ("this" may get recycled if contfree-mtx under "it" was deleted)
97+
auto it = thread_local_index_hashmap.find(&shared_locks_array);
9898
if (it != thread_local_index_hashmap.cend())
9999
set_index = it->second.thread_index;
100100

101101
if (index_op == unregister_thread_op) { // unregister thread
102102
if (shared_locks_array[set_index].value == 1) // if isn't shared_lock now
103-
thread_local_index_hashmap.erase(this);
103+
thread_local_index_hashmap.erase(&shared_locks_array);
104104
else
105105
return -1;
106106
}
107107
else if (index_op == register_thread_op) { // register thread
108-
thread_local_index_hashmap.emplace(this, unregister_t(set_index, shared_locks_array_ptr));
108+
thread_local_index_hashmap.emplace(&shared_locks_array, unregister_t(set_index, shared_locks_array_ptr));
109109

110110
// remove info about deleted contfree-mutexes
111111
for (auto it = thread_local_index_hashmap.begin(), ite = thread_local_index_hashmap.end(); it != ite;) {

safe_ptr.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,19 +350,19 @@ namespace sf {
350350

351351
int get_or_set_index(index_op_t index_op = get_index_op, int set_index = -1) {
352352
thread_local static std::unordered_map<void *, unregister_t> thread_local_index_hashmap;
353-
// get thread index - in any cases
354-
auto it = thread_local_index_hashmap.find(this);
353+
// get thread index - in any cases. Use &shared_locks_array as key ("this" may get recycled if contfree-mtx under "it" was deleted)
354+
auto it = thread_local_index_hashmap.find(&shared_locks_array);
355355
if (it != thread_local_index_hashmap.cend())
356356
set_index = it->second.thread_index;
357357

358358
if (index_op == unregister_thread_op) { // unregister thread
359359
if (shared_locks_array[set_index].value == 1) // if isn't shared_lock now
360-
thread_local_index_hashmap.erase(this);
360+
thread_local_index_hashmap.erase(&shared_locks_array);
361361
else
362362
return -1;
363363
}
364364
else if (index_op == register_thread_op) { // register thread
365-
thread_local_index_hashmap.emplace(this, unregister_t(set_index, shared_locks_array_ptr));
365+
thread_local_index_hashmap.emplace(&shared_locks_array, unregister_t(set_index, shared_locks_array_ptr));
366366

367367
// remove info about deleted contfree-mutexes
368368
for (auto it = thread_local_index_hashmap.begin(), ite = thread_local_index_hashmap.end(); it != ite;) {

0 commit comments

Comments
 (0)