-
-
Notifications
You must be signed in to change notification settings - Fork 55
Global variable chapter added #162
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
Farhan-25
wants to merge
3
commits into
rizinorg:master
Choose a base branch
from
Farhan-25:global_var
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,3 +16,5 @@ index.tex | |
| /site_libs/ | ||
| /index_files/ | ||
| /docs/* | ||
|
|
||
| **/*.quarto_ipynb | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| # Global Variables(`avg` Commands) | ||
|
|
||
| Global variables are declared outside of any function or block and are accessible throughout the entire program. They persist for the full duration of execution. | ||
|
|
||
| In reverse engineering, identifying global variables: | ||
|
|
||
| - Improves code readability | ||
| - Makes cross-references easier to follow | ||
| - Helps with data-flow and structure analysis | ||
|
|
||
| **Important:** | ||
| > In Rizin, global variables are identified during analysis. | ||
| > Run `aa` or `aaa` before using `avg` commands, otherwise no globals will appear. | ||
|
|
||
| The help command to list all `avg` commands is: `avg?` | ||
| ``` | ||
| avgl[jqt] [<var_name>] # show/list global variables | ||
| avga <var_name> <type> # add global variable manually | ||
| avgd <addr> # delete the global variable at the addr | ||
| avgm <name> # delete global variable with name | ||
| avgn <old_var_name> <new_var_name> # rename the global variable | ||
| avgp <name> # print the global variable value | ||
| avgt <var_name> <type> # change the global variable type | ||
| avgx[jq] <name> # print all xrefs to the global variable | ||
| ``` | ||
|
|
||
| ``` | ||
| avgl [<var_name>] # show/list global variables | ||
| avglj [<var_name>] # show/list global variables (JSON mode) | ||
| avglq [<var_name>] # show/list global variables (quiet mode) | ||
| avglt [<var_name>] # show/list global variables (table mode) | ||
| ``` | ||
| There are 3 modes of display. Which helps in a better analysis and view the variables. | ||
|
|
||
| **Adding Global Variables** | ||
|
|
||
| `avga <var_name> <type>` This command lets you manually add global variables with their name and type. | ||
|
|
||
| You use then when a global variables is missed during the analysis. Here `<type>` means the datatype of the variable. Example: `int`, `char`, `long`. | ||
|
|
||
| This manually defines a global variable when Rizin’s automatic analysis did not detect it. | ||
|
|
||
| **Printing Global Variables** | ||
|
|
||
| `avgp <name>` reads memory at the address of the specified global variable and displays its current value. | ||
| >In debug mode → shows the value at the current breakpoint. | ||
|
|
||
| >Without debugging → shows the value stored in the binary. | ||
|
|
||
|
|
||
| **Changing Variable Type** | ||
|
|
||
| `avgt <var_name> <type>` This command lets you change the data-type of the variable. | ||
|
|
||
| Note: The correct data-type must be used which helps in improving output, cross-reference accuracy and structure recovery. | ||
|
|
||
| There are two modes of display quiet and JSON. | ||
|
|
||
| ### Example: | ||
|
|
||
| ``` | ||
| # Ran 'avglt' to check for existing global variables(none were present). | ||
| [0x00001040]> avglt | ||
| name type size address decl_file decl_line decl_col | ||
| ―――――――――――――――――――――――――――――――――――――――――――――――――――― | ||
| [0x00001040]> avga test_variable int | ||
| [0x00001040]> avglt | ||
| name type size address decl_file decl_line decl_col | ||
| ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― | ||
| test_variable int 0x4 0x1040 - -1 -1 | ||
| [0x00001040]> avgp test_variable | ||
| int : 0x00001040 = 3644689736 | ||
| [0x00001040]> avgt test_variable char | ||
| [0x00001040]> avgp test_variable | ||
| char : 0x00001040 = 'H' | ||
| [0x00001040]> avgn test_variable renamed_variable | ||
| [0x00001040]> avglt | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Example is good. But I would suggest to move the description into here. It is easier to follow this way. |
||
| name type size address decl_file decl_line decl_col | ||
| ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― | ||
| renamed_variable char 0x1 0x1040 - -1 -1 | ||
| [0x00001040]> avgx renamed_variable | ||
| [0x00001040]> avgm renamed_variable | ||
| [0x00001040]> avglt | ||
| name type size address decl_file decl_line decl_col | ||
| ―――――――――――――――――――――――――――――――――――――――――――――――――――― | ||
| ``` | ||
|
|
||
| ### What Was Done in This Example | ||
|
|
||
| - Ran `avglt` to check for existing global variables (none were present). | ||
| - Used `avga test_variable int` to manually create a new global variable of type `int`. | ||
| - Ran `avglt` again to confirm the variable was successfully added. | ||
| - Used `avgp test_variable` to print the value at that memory location as an `int`. | ||
| - Changed the variable type with `avgt test_variable char`. | ||
| - Printed the value again using `avgp test_variable`, now interpreted as a `char` (`'H'`), showing how type affects data interpretation. | ||
| - Renamed the variable using `avgn test_variable renamed_variable`. | ||
| - Ran `avglt` to verify the variable name and updated type were reflected. | ||
| - Used `avgx renamed_variable` to check for cross-references (none found since it was manually added). | ||
| - Deleted the variable using `avgm renamed_variable`. | ||
| - Ran `avglt` one final time to confirm the global variable list was empty again. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you tried this btw?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I tested it using a small C program with a global variable. The behavior works as expected, but my wording was inaccurate I should have said that in debug mode the value is read from process memory, rather than writing that it corresponds to a specific breakpoint location.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you open a PR in Rizin which adds this test please?
Because we don't have one and this is a pretty essential thing to check.
the binary with source code should be added in rizin-testbins repo.
Would really appreciate it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure i can do that,
As you can see while debugging the value of
test_globalwas stored as -1 in the process memory. Hence avgp prints that value instead of test_global.@Rot127 please check once is this correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it is not.
Before the first
printfcall it should print 1337 (and it does for me).Then it sets it to 1.
Before the second
printfcall it should print 1. But for me it still prints 1337.