From dca893cc2e9ccb06f0bdd1fb880ef3b7c42a07d3 Mon Sep 17 00:00:00 2001 From: "Takacs, Philipp" Date: Wed, 17 Dec 2025 16:52:50 +0100 Subject: [PATCH 1/4] add uc_hook_set_user_data 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. --- include/unicorn/unicorn.h | 15 +++++++++++++++ uc.c | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/unicorn/unicorn.h b/include/unicorn/unicorn.h index 98a21a3418..226326743d 100644 --- a/include/unicorn/unicorn.h +++ b/include/unicorn/unicorn.h @@ -1149,6 +1149,21 @@ uc_err uc_hook_add(uc_engine *uc, uc_hook *hh, int type, void *callback, UNICORN_EXPORT uc_err uc_hook_del(uc_engine *uc, uc_hook hh); +/* + change the user data from a hook callback. + This change the user-defined data for a given hook. + NOTE: It's undefinde behavior when called on a hook which was not initialized + by uc_hook_add or deleted by uc_hook_delete + @hh: handle returned by uc_hook_add() + @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 + hook +*/ +UNICORN_EXPORT +uc_err uc_hook_set_user_data(uc_hook hh, void *user_data); + /* Variables to control which state should be stored in the context. Defaults to UC_CTL_CONTEXT_CPU. The options are used in a bitfield diff --git a/uc.c b/uc.c index 1de8a5b98f..0acbd08126 100644 --- a/uc.c +++ b/uc.c @@ -2060,6 +2060,17 @@ uc_err uc_hook_del(uc_engine *uc, uc_hook hh) return UC_ERR_OK; } +UNICORN_EXPORT +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) { + return UC_ERR_ARG; + } + hook->user_data = user_data; + return UC_ERR_OK; +} + // TCG helper // 2 arguments are enough for most opcodes. Load/Store needs 3 arguments but we // have memory hooks already. We may exceed the maximum arguments of a tcg From 316d92a06ce9b7029971148332ce3acbb5b59cbf Mon Sep 17 00:00:00 2001 From: "Takacs, Philipp" Date: Tue, 17 Feb 2026 10:37:29 +0100 Subject: [PATCH 2/4] hooks allow to change the user data of block and code hooks 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. --- uc.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/uc.c b/uc.c index 0acbd08126..940aee21a0 100644 --- a/uc.c +++ b/uc.c @@ -2061,11 +2061,18 @@ uc_err uc_hook_del(uc_engine *uc, uc_hook hh) } UNICORN_EXPORT -uc_err uc_hook_set_user_data(uc_hook hh, void *user_data) +uc_err uc_hook_set_user_data(uc_engine *uc, uc_hook hh, void *user_data) { struct hook *hook = (struct hook *)hh; if (hook->type == UC_HOOK_BLOCK || hook->type == UC_HOOK_CODE) { - return UC_ERR_ARG; + if (uc->nested_level) { + return UC_ERR_ARG; + } + if (hook->end < hook->begin) { + uc->tb_flush(uc); + } else { + uc->uc_invalidate_tb(uc, hook->begin, hook->end - hook->begin); + } } hook->user_data = user_data; return UC_ERR_OK; From dce4d80b58c9fb0cf8e722c0261017713ee8cccc Mon Sep 17 00:00:00 2001 From: "Takacs, Philipp" Date: Tue, 17 Feb 2026 10:47:19 +0100 Subject: [PATCH 3/4] fixup! hooks allow to change the user data of block and code hooks --- include/unicorn/unicorn.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/unicorn/unicorn.h b/include/unicorn/unicorn.h index 226326743d..fc60423092 100644 --- a/include/unicorn/unicorn.h +++ b/include/unicorn/unicorn.h @@ -1154,6 +1154,7 @@ uc_err uc_hook_del(uc_engine *uc, uc_hook hh); This change the user-defined data for a given hook. NOTE: It's undefinde behavior when called on a hook which was not initialized by uc_hook_add or deleted by uc_hook_delete + @uc: handle returned by uc_open() @hh: handle returned by uc_hook_add() @user_data: user-defined data. This will be passed to callback function in its last argument @user_data @@ -1162,7 +1163,7 @@ uc_err uc_hook_del(uc_engine *uc, uc_hook hh); hook */ UNICORN_EXPORT -uc_err uc_hook_set_user_data(uc_hook hh, void *user_data); +uc_err uc_hook_set_user_data(uc_engine *uc, uc_hook hh, void *user_data); /* Variables to control which state should be stored in the context. From b6069afcf3de837f519dd079fd97da1c431fab1e Mon Sep 17 00:00:00 2001 From: "Takacs, Philipp" Date: Tue, 17 Feb 2026 13:14:27 +0100 Subject: [PATCH 4/4] fixup! fixup! hooks allow to change the user data of block and code hooks --- include/unicorn/unicorn.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/unicorn/unicorn.h b/include/unicorn/unicorn.h index fc60423092..38c1d30f89 100644 --- a/include/unicorn/unicorn.h +++ b/include/unicorn/unicorn.h @@ -1159,8 +1159,8 @@ uc_err uc_hook_del(uc_engine *uc, uc_hook hh); @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 - hook + @return UC_ERR_OK on success, or UC_ERR_ARG when the hook was block or code + hook and emulation is runnings */ UNICORN_EXPORT uc_err uc_hook_set_user_data(uc_engine *uc, uc_hook hh, void *user_data);