Virtual Register رجیستر های مجازی
تا اینجا فهمیدیم Dispatcher تصمیم میگیره کدوم Handler اجرا بشه و Handler هم کار اصلی رو انجام میده
Handler
ها داده ها رو کجا نگه میدارن؟
روی رجیستر های واقعی CPU مثل RAX و RBX؟ معمولا نه
بیشتر ماشینهای مجازی از چیزی به اسم
Virtual Register استفاده میکنن
Virtual Register
یه فضای حافظه ست که نقش رجیستر های CPU رو بازی میکنه
مثلا فرض کنید ماشین مجازی 8 تا رجیستر داره:
V0 V1 V2 V3 V4 V5 V6 V7
وقتی Opcode زیر اجرا میشه:
LOAD V0, 10
مقدار 10 داخل V0 قرار میگیره
بعد Opcode بعدی:
LOAD V1, 20
عدد 20 داخل V1 ذخیره میشه
حالا Opcode بعدی:
ADD V0, V1
ماشین مجازی مقدار V0 و V1 رو جمع میکنه
نتیجه دوباره داخل V0 ذخیره میشه
اگر بعدش این Opcode اجرا بشه:
PRINT V0
عدد 30 چاپ میشه
اینجا هیچکدوم از رجیسترهای واقعی CPU مستقیما دیده نمیشن
ممکنه داخل Handlerه ا فقط چند دستور مثل این ببینید:
mov rax, [rdi+18h]
mov rcx, [rdi+20h]
add rax, rcx
mov [rdi+18h], rax
در نگاه اول انگار برنامه فقط داره با حافظه کار میکنه
ولی در واقع:
[rdi+18h] = V0 [rdi+20h] = V1
یعنی این آدرس های حافظه همون رجیستر های مجازی هستن به همین دلیل یکی از اولین کارهای تحلیلگر اینه که محل نگهداری Virtual Register ها رو پیدا کنه
وقتی بفهمید هر Offset مربوط به کدوم Virtual Register هست خوندن Handler ها چند برابر راحت تر میشه
خیلی وقت ها حتی اسمگذاری هم میکنن:
[rdi+18h] → V0 [rdi+20h] → V1 [rdi+28h] → V2
از این لحظه به بعد به جای آدرس های حافظه ذهن تحلیلگر با رجیسترهای مجازی کار میکنه این کار باعث میشه منطق ماشین مجازی کم کم شبیه یک CPU معمولی به نظر برسه
تمرین:
فرض کنید یک ماشین مجازی چهار رجیستر داره:
V0 V1 V2 V3
و Opcode های زیر اجرا میشن:
LOAD V0, 15
LOAD V1, 5
SUB V0, V1
PRINT V0
بدون اینکه کدی بنویسید مرحله به مرحله مشخص کنید بعد از اجرای هر Opcode مقدار هر Virtual Register چقدر میشه
Virtual Register So far we have understood that Dispatcher decides which Handler to execute and Handler does the main work
Where do Handlers store data?
On real CPU registers like RAX and RBX? Usually not
Most virtual machines use something called Virtual Register
Virtual Register
is a memory space that acts as CPU registers
For example, suppose the virtual machine has 8 registers:
V0 V1 V2 V3 V4 V5 V6 V7
When the following Opcode is executed:
LOAD V0, 10
10 is placed into V0
Then the next Opcode:
LOAD V1, 20
20 is stored into V1
Now the next Opcode:
ADD V0, V1
The virtual machine adds V0 and V1
The result is stored back into V0
If this Opcode is then executed:
PRINT V0
30 is printed
Here none of the actual CPU registers are directly visible
You may only see a few instructions inside the Handlers like this:
mov rax, [rdi+18h]
mov rcx, [rdi+20h]
add rax, rcx
mov [rdi+18h], rax
At first glance, it seems like the program is only working with memory
But in fact:
[rdi+18h] = V0 [rdi+20h] = V1
That is, these memory addresses are the same virtual registers, which is why one of the first tasks of the analyst is to find the location of the Virtual Registers.
When you understand which Virtual Register each Offset belongs to, reading the Handlers becomes much easier.
Many times they even give them names:
[rdi+18h] → V0 [rdi+20h] → V1 [rdi+28h] → V2
From this moment on, instead of memory addresses, the analyst's mind works with virtual registers. This makes the logic of the virtual machine look a little like a regular CPU.
Exercise:
Suppose a virtual machine It has four registers:
V0 V1 V2 V3
And the following Opcodes are executed:
LOAD V0, 15
LOAD V1, 5
SUB V0, V1
PRINT V0
Without writing any code, determine step by step what the value of each Virtual Register will be after executing each Opcode.
@reverseengine