Skip to content

add uc_hook_set_user_data#2274

Merged
wtdcode merged 4 commits intounicorn-engine:devfrom
PhilippTakacs:hook_set_user_data
Feb 17, 2026
Merged

add uc_hook_set_user_data#2274
wtdcode merged 4 commits 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
Member

Choose a reason for hiding this comment

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

For a bit more context, not enabling inlining for more than 1 hooks was due to a bad hack in tcg_optimize leading to segfault at that time. Since I removed the hack recently, we could extend inlining to any number of hooks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I somehow misinterpreted what this code is doing, thanks for explaining.

Copy link
Member

Choose a reason for hiding this comment

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

Also I would also recommend not allowing changing data during emulation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I guess this can be merged now?

Copy link
Member

Choose a reason for hiding this comment

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

yeah, look good now except the small typo and documents above.

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.
@PhilippTakacs
Copy link
Contributor Author

@wtdcode can you merge this PR or is there something missing/blocking?

Now it is possible to change the user data of block and code hooks. The
corresponding translation blocks are cleared. It is only allowed to be
done while the emulation is stopped.
@user_data: user-defined data. This will be passed to callback function in its
last argument @user_data

@return UC_ERR_OK on success, or UC_ERR_ARGS when the hook was block or code
Copy link
Member

Choose a reason for hiding this comment

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

The documents are not updated here (return error during emulation) and UC_ERR_ARGS seems a typo.

@wtdcode wtdcode merged commit 99a99bb into unicorn-engine:dev Feb 17, 2026
37 of 38 checks passed
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