{"action":"created","issue":{"url":"https://api.github.com/repos/unicorn-engine/unicorn/issues/346","labels_url":"https://api.github.com/repos/unicorn-engine/unicorn/issues/346/labels{/name}","comments_url":"https://api.github.com/repos/unicorn-engine/unicorn/issues/346/comments","events_url":"https://api.github.com/repos/unicorn-engine/unicorn/issues/346/events","html_url":"https://github.com/unicorn-engine/unicorn/issues/346","id":123995044,"number":346,"title":"Need some help with implementing the post-instruction hook.","user":{"login":"farmdve","id":7746376,"avatar_url":"https://avatars.githubusercontent.com/u/7746376?v=3","gravatar_id":"","url":"https://api.github.com/users/farmdve","html_url":"https://github.com/farmdve","followers_url":"https://api.github.com/users/farmdve/followers","following_url":"https://api.github.com/users/farmdve/following{/other_user}","gists_url":"https://api.github.com/users/farmdve/gists{/gist_id}","starred_url":"https://api.github.com/users/farmdve/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/farmdve/subscriptions","organizations_url":"https://api.github.com/users/farmdve/orgs","repos_url":"https://api.github.com/users/farmdve/repos","events_url":"https://api.github.com/users/farmdve/events{/privacy}","received_events_url":"https://api.github.com/users/farmdve/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/unicorn-engine/unicorn/labels/enhancement","name":"enhancement","color":"84b6eb"},{"url":"https://api.github.com/repos/unicorn-engine/unicorn/labels/help%20wanted","name":"help wanted","color":"159818"},{"url":"https://api.github.com/repos/unicorn-engine/unicorn/labels/TODO","name":"TODO","color":"fbca04"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":37,"created_at":"2015-12-27T20:22:41Z","updated_at":"2016-01-17T17:03:59Z","closed_at":null,"body":"Before I start I'd like to just mention that maybe we need an IRC channel to discuss things. e.g #unicorn-engine on freenode.\r\n\r\nSo, as you all of you may be aware, currently we do a pre-instruction hook. Which means our hook is called before the instruction is executed, thus it makes sense to have a post-instruction hook. The pre hook would setup some stuff, the post hook would change them back(if need be).\r\n\r\nThis past week is what I've been trying to do, but it turned out to be much more difficult than I thought.\r\n\r\nThe problem mostly stems from the way some instructions are jitted. Some are jitted to exit the translation block and enter it again. At first I placed the post instruction hook after the instruction was jitted, e.g at the end of disas_insn, this worked for a bit, until I encountered instructions like REP STOS which is jitted in a way that it checks condition first, does whatever operation it must and re-enters the translation block to check the condition again, thereby bypassing my hook. I kept this hook, but added the same code to the `gen_op_jmp_v` which again worked magnificently, except now instructions like ret were not being hooked. So back at square one. I decided that I'd add the post-instruction hook in the same place as the pre-instruction hook.\r\n\r\n```c\r\n    if (env->uc->hook_insn) {\r\n        trace = hook_find(env->uc, UC_HOOK_CODE, pc_start);\r\n        if (trace) {\r\n            if (s->last_cc_op != s->cc_op) {\r\n                sync_eflags(s, tcg_ctx);\r\n                s->last_cc_op = s->cc_op;\r\n                changed_cc_op = true;\r\n            }\r\n            // generate code to call callback\r\n            gen_uc_tracecode(tcg_ctx, 0xf1f1f1f1, trace->callback, env->uc, pc_start, trace->user_data);\r\n            // the callback might want to stop emulation immediately\r\n            check_exit_request(tcg_ctx);\r\n        }\r\n    }\r\n```\r\n\r\nAdding here would be more or less the same, it would be called before the next instruction is executed. But unfortunately it is also going to be plagued by other problems. The first is the early check code \"// early check to see if the address of this block is the until address\", our post instruction code never gets jitted if this is the last address.\r\nThe other problem is that instructions like REP STOSD/MOVS would re-enter the TB and execute our hook once again(in addition to the pre-instruction hook for the current instruction).\r\n\r\nSuggestions?"},"comment":{"url":"https://api.github.com/repos/unicorn-engine/unicorn/issues/comments/172353615","html_url":"https://github.com/unicorn-engine/unicorn/issues/346#issuecomment-172353615","issue_url":"https://api.github.com/repos/unicorn-engine/unicorn/issues/346","id":172353615,"user":{"login":"farmdve","id":7746376,"avatar_url":"https://avatars.githubusercontent.com/u/7746376?v=3","gravatar_id":"","url":"https://api.github.com/users/farmdve","html_url":"https://github.com/farmdve","followers_url":"https://api.github.com/users/farmdve/followers","following_url":"https://api.github.com/users/farmdve/following{/other_user}","gists_url":"https://api.github.com/users/farmdve/gists{/gist_id}","starred_url":"https://api.github.com/users/farmdve/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/farmdve/subscriptions","organizations_url":"https://api.github.com/users/farmdve/orgs","repos_url":"https://api.github.com/users/farmdve/repos","events_url":"https://api.github.com/users/farmdve/events{/privacy}","received_events_url":"https://api.github.com/users/farmdve/received_events","type":"User","site_admin":false},"created_at":"2016-01-17T17:03:59Z","updated_at":"2016-01-17T17:03:59Z","body":"@aquynh \r\n\r\nIf the hook API is going to be revamped, I will wait as I will need to change the code either way. Plus right now I am also busy with some life-matters and I can't do anything."}}