Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make get_or_set_index immune to memory address reuse #5

Open
wants to merge 1 commit into
base: for_pull_requests
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions CDS_test/safe_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,19 +350,19 @@ namespace sf {

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

if (index_op == unregister_thread_op) { // unregister thread
if (shared_locks_array[set_index].value == 1) // if isn't shared_lock now
thread_local_index_hashmap.erase(this);
thread_local_index_hashmap.erase(&shared_locks_array);
else
return -1;
}
else if (index_op == register_thread_op) { // register thread
thread_local_index_hashmap.emplace(this, unregister_t(set_index, shared_locks_array_ptr));
thread_local_index_hashmap.emplace(&shared_locks_array, unregister_t(set_index, shared_locks_array_ptr));

// remove info about deleted contfree-mutexes
for (auto it = thread_local_index_hashmap.begin(), ite = thread_local_index_hashmap.end(); it != ite;) {
Expand Down
8 changes: 4 additions & 4 deletions Real_app_bench/safe_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,19 +350,19 @@ namespace sf {

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

if (index_op == unregister_thread_op) { // unregister thread
if (shared_locks_array[set_index].value == 1) // if isn't shared_lock now
thread_local_index_hashmap.erase(this);
thread_local_index_hashmap.erase(&shared_locks_array);
else
return -1;
}
else if (index_op == register_thread_op) { // register thread
thread_local_index_hashmap.emplace(this, unregister_t(set_index, shared_locks_array_ptr));
thread_local_index_hashmap.emplace(&shared_locks_array, unregister_t(set_index, shared_locks_array_ptr));

// remove info about deleted contfree-mutexes
for (auto it = thread_local_index_hashmap.begin(), ite = thread_local_index_hashmap.end(); it != ite;) {
Expand Down
8 changes: 4 additions & 4 deletions bench_contfree/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,19 @@ class contention_free_shared_mutex {

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

if (index_op == unregister_thread_op) { // unregister thread
if (shared_locks_array[set_index].value == 1) // if isn't shared_lock now
thread_local_index_hashmap.erase(this);
thread_local_index_hashmap.erase(&shared_locks_array);
else
return -1;
}
else if (index_op == register_thread_op) { // register thread
thread_local_index_hashmap.emplace(this, unregister_t(set_index, shared_locks_array_ptr));
thread_local_index_hashmap.emplace(&shared_locks_array, unregister_t(set_index, shared_locks_array_ptr));

// remove info about deleted contfree-mutexes
for (auto it = thread_local_index_hashmap.begin(), ite = thread_local_index_hashmap.end(); it != ite;) {
Expand Down
8 changes: 4 additions & 4 deletions benchmark/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,19 +331,19 @@ class contention_free_shared_mutex {

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

if (index_op == unregister_thread_op) { // unregister thread
if (shared_locks_array[set_index].value == 1) // if isn't shared_lock now
thread_local_index_hashmap.erase(this);
thread_local_index_hashmap.erase(&shared_locks_array);
else
return -1;
}
else if (index_op == register_thread_op) { // register thread
thread_local_index_hashmap.emplace(this, unregister_t(set_index, shared_locks_array_ptr));
thread_local_index_hashmap.emplace(&shared_locks_array, unregister_t(set_index, shared_locks_array_ptr));

// remove info about deleted contfree-mutexes
for (auto it = thread_local_index_hashmap.begin(), ite = thread_local_index_hashmap.end(); it != ite;) {
Expand Down
8 changes: 4 additions & 4 deletions contfree_shared_mutex/safe_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,19 @@ namespace sf {

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

if (index_op == unregister_thread_op) { // unregister thread
if (shared_locks_array[set_index].value == 1) // if isn't shared_lock now
thread_local_index_hashmap.erase(this);
thread_local_index_hashmap.erase(&shared_locks_array);
else
return -1;
}
else if (index_op == register_thread_op) { // register thread
thread_local_index_hashmap.emplace(this, unregister_t(set_index, shared_locks_array_ptr));
thread_local_index_hashmap.emplace(&shared_locks_array, unregister_t(set_index, shared_locks_array_ptr));

// remove info about deleted contfree-mutexes
for (auto it = thread_local_index_hashmap.begin(), ite = thread_local_index_hashmap.end(); it != ite;) {
Expand Down
8 changes: 4 additions & 4 deletions safe_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,19 +350,19 @@ namespace sf {

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

if (index_op == unregister_thread_op) { // unregister thread
if (shared_locks_array[set_index].value == 1) // if isn't shared_lock now
thread_local_index_hashmap.erase(this);
thread_local_index_hashmap.erase(&shared_locks_array);
else
return -1;
}
else if (index_op == register_thread_op) { // register thread
thread_local_index_hashmap.emplace(this, unregister_t(set_index, shared_locks_array_ptr));
thread_local_index_hashmap.emplace(&shared_locks_array, unregister_t(set_index, shared_locks_array_ptr));

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