Skip to content

Commit 53879f8

Browse files
committed
Memory
1 parent a3dd512 commit 53879f8

1 file changed

Lines changed: 64 additions & 3 deletions

File tree

‎A_Gentle_Introduction_to_REV/README.md‎

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,24 @@ When reversing programs, static linking causes many problems to us reversers. Go
122122

123123
Data and a program's code both need to be stored on the system to allow the processor to perform operations. Computers use a multi-layer system of caches and cold memory to improve performance, with the fastest memory type being CPU registers.
124124

125+
126+
## Registers
127+
125128
Registers exist inside the processor and are used for high-performance storage and operations. The size of the register depends, as previously mentioned, on the architecture of the CPU, with most modern registers holding 64 bits of data.
126129

127130
Different registers are used for different reasons and knowing the purpose of each one, is required, when analyzing a binary file.
128131

129132

130133

131-
## General Purpose Registers
134+
### General Purpose Registers
132135

133136
General Purpose Registers (GPRs) hold data that the function uses to complete its operations. When initializing a variable using a given value or operating upon values, general purpose registers hold those values.
134137

135138
They can hold both data and addresses, which means that they can serve as pointers to other locations in memory too. Since the processor does not differentiate between pointers and data, it is up to the developer and the reverser to decide which type it is.
136139

137140
We can break GPRs (not exclusively, but mainly) into sub-registers for more fine-grained control over the data. The 64-bit RAX register for example, consists of the EAX register, which holds the lower 32-bits of the RAX register, the AX register, which holds the lower 16-bits and AL the 8-bits, whereas AH, the following 8-bits, meaning bits 8-15.
138141

139-
## Pointer Registers
142+
### Pointer Registers
140143

141144
The Instruction Pointer (RIP) is the main pointer register in the processor and it determines what instruction the processor will execute next. The processor looks at the pointer that it holds, and fetches, decodes and executes the instruction that it points to. These three steps make up the "Instruction Cycle" of the computer.
142145

@@ -152,7 +155,7 @@ RSP and RBP are NOT Pointer Registers, they are GPRs, but it aids in understandi
152155
---
153156

154157

155-
## Flags Register
158+
### Flags Register
156159

157160
Sometimes the processor needs to send a signal to itself in the future, to prevent unwanted behaviors, or increase performance. For example, when adding two numbers that result in a number that is greater than the maximum value that the architecture is able to represent, the processor will activate the Carry Flag, that signifies that the resulting value is not going to be the same as the expected one.
158161

@@ -168,6 +171,64 @@ There are 22 bits worth of flags in the RFLAGS register, but the main ones are:
168171
| Sign Flag (SF) | Indicate that result of calculation is negative |
169172

170173
Each of these flags occupy just a single bit in the RFLAGS register, and there are many more flags that have a unique task. More information regarding flags and registers can be found [here](https://wiki.osdev.org/CPU_Registers_x86-64).
174+
175+
176+
177+
## Stack
178+
179+
The stack is a structure inside Random Access Memory (RAM) that is used by the program to store local variables for a limited amount of time. As previously mentioned, there can be multiple sub-stacks that are described by RBP (Base) and RSP (Top).
180+
181+
Imagining the stack as a stack data-structure is not entirely correct. PUSHing (adding) and POPing (removing) elements from the stack is one of the most used features of it. Besides that, we are also able to specify locations on it directly. An addressable location on the stack is able to store as many bits as the architecture of the processor. x86-64 is a 64-bit architecture, so each location on the stack is able to hold 64-bits worth of data. This means that it is ideal for static size, and small data structures, with dynamic size, and larger data structure moving to the heap.
182+
183+
The stack is used when the amount of data that we need to store is more than the registers are able to provide. This process is called stack-spilling and introduces minor slow-downs, since registers are faster than RAM, but is quite often unavoidable.
184+
185+
---
186+
**Example of direct indexing on the stack**
187+
188+
Consider this scenario, you push a value onto the stack and after it push 4 more values, moving the RSP away from it. When you want to access the value that you pushed onto the stack, you have to pop off each of the 4 following element to reach it. This will of course overwrite those elements, and maintaining them would be a big hassle. Instead of POPing 4 times, we can directly specify the location manually.
189+
190+
```asm
191+
PUSH 1
192+
PUSH 2
193+
PUSH 3
194+
PUSH 4
195+
PUSH 5
196+
197+
MOV RAX, [RSP + 32] ; Load 1 into RAX, offset is 4 x 8 = 32
198+
```
199+
---
200+
201+
202+
## Heap
203+
204+
Heap memory is also a memory structure inside RAM, but it's implementation and usage differs slightly, when compared to the stack. Heap memory does not allow for PUSHing and POPing, but only direct indexing. When adding data to the heap, the computer needs to first check if there is enough space to hold the data. If that is not the case, memory is allocated, which is an expensive operation. Access and write times are the same between the stack and the heap, but the allocation time overhead makes it less appealing in cases where high-performance is important.
205+
206+
207+
# Source Code
208+
209+
Being proficient in forward engineering is required for reverse engineering. Many of the standard programming patterns have an equivalent disassembled version, and this aids us in the process of understanding a binary file.
210+
211+
## Conditionals
212+
213+
214+
215+
216+
217+
218+
219+
220+
221+
222+
223+
224+
225+
226+
227+
228+
229+
230+
231+
171232

172233

173234

0 commit comments

Comments
 (0)