Skip to content

add uc_hook_set_user_data#2274

Open
PhilippTakacs wants to merge 1 commit intounicorn-engine:devfrom
PhilippTakacs:hook_set_user_data
Open

add uc_hook_set_user_data#2274
PhilippTakacs wants to merge 1 commit intounicorn-engine:devfrom
PhilippTakacs:hook_set_user_data

Conversation

@PhilippTakacs
Copy link
Contributor

This allows to change the user_data for hooks. This way it's simpler to adopt the hooks for current needs. I.e. change the page table entries for the tlb hook.

@wtdcode
Copy link
Member

wtdcode commented Jan 16, 2026

LGTM.

@PhilippTakacs
Copy link
Contributor Author

LGTM.

Shall I rebase this to current dev to rerun the CI or is there anything else blocking the merge?

uc_err uc_hook_set_user_data(uc_hook hh, void *user_data)
{
struct hook *hook = (struct hook *)hh;
if (hook->type == UC_HOOK_BLOCK || hook->type == UC_HOOK_CODE) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait why do we exclude these two types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the callbacks are directly called by the tb with the user_data pointer written to the tb. It would require to rebuild the tb to allow change the user_data.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's correct. Can we inline the pointer to the hook struct in the tb instead? In that case, the modification to uc_hook would take effect as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No because there is no wrapper function in unicorn for these hooks. The cb function is directly called. I could write a wrapper function which just use the hook_struct. This might affect performance which I would like to avoid in the block case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh okay, I see. I forgot the inline optimization.

A wrapper would hurt performance quite a lot. Spare me some time to think about if we have better ways.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What could be done is only allow to change the user_data of code/block hooks while unicorn isn't running and then clear the affected tb cache. This would only affect performance when you change the user data.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A I missed that I have designed the API without an uc_engine, so I can't test if the emulation is running. So I would say it's ok to just disallow changing user data for these hooks. What do you think?

This allows to change the user_data for hooks. This way it's simpler to
adopt the hooks for current needs. I.e. change the page table entries
for the tlb hook.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants