Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: avoid multiple calls to read same self.PC #1527

Merged
merged 2 commits into from
Sep 29, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions manticore/native/cpu/abstractcpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,19 +950,21 @@ def execute(self):
"""
Decode, and execute one instruction pointed by register PC
"""
if issymbolic(self.PC):
curpc = self.PC
if issymbolic(curpc):
raise ConcretizeRegister(self, "PC", policy="ALL")
if not self.memory.access_ok(self.PC, "x"):
raise InvalidMemoryAccess(self.PC, "x")
if not self.memory.access_ok(curpc, "x"):
raise InvalidMemoryAccess(curpc, "x")

self._publish("will_decode_instruction", self.PC)
self._publish("will_decode_instruction", curpc)
Copy link
Member

@disconnect3d disconnect3d Sep 20, 2019

Choose a reason for hiding this comment

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

The will_decode_instruction can also change the state, so we need to "invalidate" the cached value again

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a use case for this ?
Or is this a general principle when publishing an event ?

Copy link
Member

@disconnect3d disconnect3d Sep 20, 2019

Choose a reason for hiding this comment

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

I think we should not assume that ppl won't change state during any event.

Regarding use case: skipping a given instruction? or hooking it?

Also, loud thinking: what event is used when @hook on an address is used?

If that is not will_decode_instruction and is will_execute_instruction then maybe it should be the first one 🤔. Or at least someone might use that as an optimization.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For @hook, it is will_execute_instruction

After thinking, I agree that wa cannot assume change state during event will_decode_instruction So, I refresh PC value once.

Optimization is a bit less effective, but still good to have ;-)


insn = self.decode_instruction(self.PC)
insn = self.decode_instruction(curpc)
self._last_pc = self.PC

self._publish("will_execute_instruction", self.PC, insn)
self._publish("will_execute_instruction", self._last_pc, insn)

# FIXME (theo) why just return here?
# hook changed PC, so we trust that there is nothing more to do
if insn.address != self.PC:
return

Expand Down