{"action":"created","issue":{"url":"https://api.github.com/repos/ekse/unicorn-rs/issues/13","repository_url":"https://api.github.com/repos/ekse/unicorn-rs","labels_url":"https://api.github.com/repos/ekse/unicorn-rs/issues/13/labels{/name}","comments_url":"https://api.github.com/repos/ekse/unicorn-rs/issues/13/comments","events_url":"https://api.github.com/repos/ekse/unicorn-rs/issues/13/events","html_url":"https://github.com/ekse/unicorn-rs/issues/13","id":196248203,"number":13,"title":"Accessing the CPU in hooks","user":{"login":"oblivia-simplex","id":8534011,"avatar_url":"https://avatars.githubusercontent.com/u/8534011?v=3","gravatar_id":"","url":"https://api.github.com/users/oblivia-simplex","html_url":"https://github.com/oblivia-simplex","followers_url":"https://api.github.com/users/oblivia-simplex/followers","following_url":"https://api.github.com/users/oblivia-simplex/following{/other_user}","gists_url":"https://api.github.com/users/oblivia-simplex/gists{/gist_id}","starred_url":"https://api.github.com/users/oblivia-simplex/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/oblivia-simplex/subscriptions","organizations_url":"https://api.github.com/users/oblivia-simplex/orgs","repos_url":"https://api.github.com/users/oblivia-simplex/repos","events_url":"https://api.github.com/users/oblivia-simplex/events{/privacy}","received_events_url":"https://api.github.com/users/oblivia-simplex/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-12-17T23:14:48Z","updated_at":"2016-12-21T20:41:51Z","closed_at":"2016-12-18T00:30:12Z","body":"disclaimer: I'm a rust noob. I may be missing something essential, here.\r\n\r\nIs there an intended way to access the Unicorn engine's CPU context from a code hook? The following (which more or less follows the pattern in the test routines) compiles without complaint, but then causes the emulation to hang for several seconds, until it crashes with an exit code of 1. \r\n```\r\n 53     if _DEBUG {                                                                                                                              \r\n 54       let callback_c =                                                                                                                       \r\n 55         move |u: &unicorn::Unicorn, addr: u64, _: u32| {                                                                                     \r\n 56           println!(\"[{:08x}] SP: {:08x}\", addr, u.reg_read(RegisterARM::SP.to_i32()).expect(\"Error reading SP\"));                            \r\n 57         };                                                                                                                                   \r\n 58       // add some hooks if in debugging mode                                                                                                 \r\n 59       uc.add_code_hook(CodeHookType::CODE,                                                                                                   \r\n 60                        BASE_ADDR,                                                                                                            \r\n 61                        BASE_ADDR+(MEM_SIZE as u64),                                                                                          \r\n 62                        callback_c)                                                                                                           \r\n 63         .expect(\"Error adding code hook\");                                                                                                   \r\n 64     }                                                                                                                                        \r\n 65                                                                                                                                              \r\n 66     uc                                                                                                                                       \r\n 67   }         \r\n```\r\nedit: what I'm trying to do here is access the internal unicorn handle's reg_read() method, in lieu of the usual CPU reg_read wrapper (which takes an enum arg instead of an i32), since the Unicorn handle is all that the callback signature seems to give us to work with. This same hook works fine if I leave out the u.reg_read bit, and just print the addr value.\r\n\r\n**update**: No worries. Turned out to be a scoping/lifetime issue after all, which, for reasons still unclear to me, the compiler wasn't picking up on. I moved the hook-adding routine to a separate function -- outside of the function that creates and initializes the engine -- and then called that function from the function that calls the first, initializing function, and **now everything appears to be working fine**. This _partially_ makes sense to me, given what I grasp of rust's memory management system, and the rest I suppose will become clearer as I become less noobish. Closing the issue. Thanks for letting me rubber-duck debug here. "},"comment":{"url":"https://api.github.com/repos/ekse/unicorn-rs/issues/comments/268633922","html_url":"https://github.com/ekse/unicorn-rs/issues/13#issuecomment-268633922","issue_url":"https://api.github.com/repos/ekse/unicorn-rs/issues/13","id":268633922,"user":{"login":"isra17","id":1420974,"avatar_url":"https://avatars.githubusercontent.com/u/1420974?v=3","gravatar_id":"","url":"https://api.github.com/users/isra17","html_url":"https://github.com/isra17","followers_url":"https://api.github.com/users/isra17/followers","following_url":"https://api.github.com/users/isra17/following{/other_user}","gists_url":"https://api.github.com/users/isra17/gists{/gist_id}","starred_url":"https://api.github.com/users/isra17/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/isra17/subscriptions","organizations_url":"https://api.github.com/users/isra17/orgs","repos_url":"https://api.github.com/users/isra17/repos","events_url":"https://api.github.com/users/isra17/events{/privacy}","received_events_url":"https://api.github.com/users/isra17/received_events","type":"User","site_admin":false},"created_at":"2016-12-21T20:41:51Z","updated_at":"2016-12-21T20:41:51Z","body":"This look like a guenine issue, either the compiler should pick up the lifetime issue or it should not crash. Are you able to get a simple test case that reliably reproduce the issue?\r\nThanks!"}}
{"actor":{"display_login":"isra17"}}