Skip to content

Commit 08153b5

Browse files
authored
i#7685 DrPoints: BB ID bug fix (#7755)
The key to `bb_id_table` is not just an integer that we can store into a `void*`. It's a struct that needs to be allocated when adding a new entry into the table. Previously we allocated the key on the stack, hence losing it after the BB instrumentation function. We now allocate it on the heap with `dr_global_alloc()`. We also configure the hashtable setting a function to free its keys when deleted. Issue #7685
1 parent 0e382b0 commit 08153b5

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

clients/drpoints/drpoints.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ free_bbv(void *entry)
198198
dr_global_free(vector, sizeof(*vector));
199199
}
200200

201+
static void
202+
free_bb_id(void *key)
203+
{
204+
modidx_offset_t *bb_id_key = static_cast<modidx_offset_t *>(key);
205+
dr_global_free(bb_id_key, sizeof(*bb_id_key));
206+
}
207+
201208
static void
202209
free_count(void *entry)
203210
{
@@ -319,7 +326,13 @@ event_app_instruction(void *drcontext, void *tag, instrlist_t *bb, instr_t *inst
319326
uint64_t bb_id = reinterpret_cast<uint64_t>(bb_id_ptr);
320327
if (bb_id_ptr == nullptr) {
321328
bb_id = unique_bb_count;
322-
hashtable_add(&bb_id_table, &bb_id_key, reinterpret_cast<void *>(bb_id));
329+
// Only allocate the key when adding to the table. Lookup key can stay on the
330+
// stack.
331+
modidx_offset_t *bb_id_key_to_add =
332+
static_cast<modidx_offset_t *>(dr_global_alloc(sizeof(*bb_id_key_to_add)));
333+
bb_id_key_to_add->modidx = modidx;
334+
bb_id_key_to_add->offset = offset;
335+
hashtable_add(&bb_id_table, bb_id_key_to_add, reinterpret_cast<void *>(bb_id));
323336
++unique_bb_count;
324337
}
325338

@@ -572,6 +585,14 @@ dr_client_main(client_id_t id, int argc, const char *argv[])
572585
hashtable_init_ex(&dynamorio::drpoints::bb_id_table, HASH_BITS_BB_ID, HASH_INTPTR,
573586
/*str_dup=*/false, /*synch=*/false, nullptr,
574587
dynamorio::drpoints::bb_id_hash, dynamorio::drpoints::bb_id_cmp);
588+
// We need to configure the hashtable to add free_key_func, as keys are allocated with
589+
// dr_global_alloc(). We leave the other config parameters with default values.
590+
hashtable_config_t bb_id_table_config;
591+
bb_id_table_config.size = sizeof(bb_id_table_config);
592+
bb_id_table_config.resizable = true;
593+
bb_id_table_config.resize_threshold = 75;
594+
bb_id_table_config.free_key_func = dynamorio::drpoints::free_bb_id;
595+
hashtable_configure(&dynamorio::drpoints::bb_id_table, &bb_id_table_config);
575596
drvector_init(&dynamorio::drpoints::bbvs, 0, /*synch=*/false,
576597
dynamorio::drpoints::free_bbv);
577598

0 commit comments

Comments
 (0)