Skip to content

Add stack variable support #81

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

withzombies
Copy link

This adds a few functions for manipulating IDA stack variables. These are mostly relevant when no decompiler is available, but it will make decompilation better as well (I wish they'd finally unify their type systems!)

I also added a helper function for resolving types by name which handles many of the standard types (it assumes sizeof(int) == 32, sizeof(long) == 32 though).

Copy link
Owner

@mrexodia mrexodia left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution! I added some comments to adjust.

@idaread
def get_stack_frame_variables(
function_address: Annotated[str, "Address of the disassembled function to retrieve the stack frame variables"]
) -> list[dict]:
Copy link
Owner

Choose a reason for hiding this comment

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

Please return a TypedDict with the appropriate dict members.

@@ -501,6 +502,73 @@ def create_demangled_to_ea_map():
if demangled:
DEMANGLED_TO_EA[demangled] = ea


def get_type_by_name(type_name: str) -> ida_typeinf.tinfo_t:
Copy link
Owner

Choose a reason for hiding this comment

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

Isn't there a way to directly go from name -> tinfo_t? I vaguely remember you can use one of the constructors for this.

size = udm.size // 8
type = str(udm.type)

members += [{'name': name, 'offset': hex(offset), 'size': hex(size), 'type': type}]
Copy link
Owner

Choose a reason for hiding this comment

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

Would prefer formatting this with a key per line. Also use " instead of '`' to match the existing codebase.


@jsonrpc
@idaread
def get_defined_structures() -> list[dict]:
Copy link
Owner

Choose a reason for hiding this comment

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

TypedDict for structure definition.

raise IDAError(f"{old_name} is an argument member. Will not change the name.")

sval = ida_frame.soff_to_fpoff(func, offset)
return ida_frame.define_stkvar(func, new_name, sval, udm.type)
Copy link
Owner

Choose a reason for hiding this comment

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

I think it will be better to return None and then raise IDAError("failed to define stack variable") to get more actionable error messages.

offset: Annotated[str, "Offset of the stack frame variable"],
variable_name: Annotated[str, "Name of the stack variable"],
type_name: Annotated[str, "Type of the stack variable"]
) -> bool:
Copy link
Owner

Choose a reason for hiding this comment

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

Same error comment as above.

function_address: Annotated[str, "Address of the disassembled function to set the stack frame variables"],
variable_name: Annotated[str, "Name of the stack variable"],
type_name: Annotated[str, "Type of the stack variable"]
) -> bool:
Copy link
Owner

Choose a reason for hiding this comment

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

Same error comment.

def delete_stack_frame_variable(
function_address: Annotated[str, "Address of the function to set the stack frame variables"],
variable_name: Annotated[str, "Name of the stack variable"]
) -> bool:
Copy link
Owner

Choose a reason for hiding this comment

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

Same error comment.

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.

3 participants