SQL Server, A bit of reverse engineering inside the parser: the Parser and the GetChar procedure. (attention contains news published for the first time)
Hello friends,
Welcome to another post about the Parser. We started talking about it in this posts: SQL Server is a compiler! & Where T-SQL tokens are stored?
What did we say? Briefly we say that every batch
you send to the engine is forst analized and then traduced into a series of more simple
instructions called the input tree.
The CParser::GetChar procedure
1) As for every asm routine written in C (as the entire SQL Server is) we can find at the start of the function the instruction used to save the rbx register: This istruction is the "push rbx"
Then you will find the instruction "sub rsp, 20h" that allocates 0x20 bytes of stack space.
At the end of the function we will find the "inverse" opcodes: "add rsp,20h" , "pop rbx" and "ret".
I hightlight these areas in yellow.
2) A single Parameter is passed to the function through the register RCX
3) The result is stored into the EAX register
00007fff`633235f0 53 push rbx
00007fff`633235f1 4883ec20 sub rsp,20h
00007fff`633235f5 8b9164050000 mov edx,dword ptr [rcx+564h]
00007fff`633235fb 488bd9 mov rbx,rcx
00007fff`633235fe 8b8968050000 mov ecx,dword ptr [rcx+568h]
00007fff`63323604 3bd1 cmp edx,ecx
00007fff`63323606 7d44 jge sqllang!CParser::GetChar+0x19 (00007fff`6332364c)
00007fff`63323608 81faa00f0000 cmp edx,0FA0h
00007fff`6332360e 0f8310740c00 jae sqllang!CParser::GetChar+0x44 (00007fff`633eaa24)
00007fff`63323614 48638b64050000 movsxd rcx,dword ptr [rbx+564h]
00007fff`6332361b 0fb7944b38060000 movzx edx,word ptr [rbx+rcx*2+638h]
00007fff`63323623 8d4101 lea eax,[rcx+1]
00007fff`63323626 83835805000002 add dword ptr [rbx+558h],2
00007fff`6332362d 898364050000 mov dword ptr [rbx+564h],eax
00007fff`63323633 6683fa0a cmp dx,0Ah
00007fff`63323637 0f849dbd0000 je sqllang!CParser::GetChar+0xc4 (00007fff`6332f3da)
00007fff`6332363d 0fb7c2 movzx eax,dx
00007fff`63323640 898360050000 mov dword ptr [rbx+560h],eax
00007fff`63323646 4883c420 add rsp,20h
00007fff`6332364a 5b pop rbx
00007fff`6332364b c3 ret
00007fff`6332364c 83835805000002 add dword ptr [rbx+558h],2
00007fff`63323653 8d4201 lea eax,[rdx+1]
00007fff`63323656 898364050000 mov dword ptr [rbx+564h],eax
00007fff`6332365c 83c8ff or eax,0FFFFFFFFh
00007fff`6332365f c78360050000ffffffff mov dword ptr [rbx+560h],0FFFFFFFFh
00007fff`63323669 4883c420 add rsp,20h
00007fff`6332366d 5b pop rbx
00007fff`6332366e c3 ret
00007fff`6332366f 90 nop
But how does it works?
00007fff`63b535fb 488bd9 mov rbx,rcx
00007fff`63b535fe 8b8968050000 mov ecx,dword ptr [rcx+568h]
00007fff`63b53604 3bd1 cmp edx,ecx
00007fff`63323608 81faa00f0000 cmp edx,0FA0h
00007fff`6332360e 0f8310740c00 jae sqllang!CParser::GetChar+0x44 (00007fff`633eaa24)
00007fff`6332361b 0fb7944b38060000 movzx edx,word ptr [rbx+rcx*2+638h]
00007fff`63323626 83835805000002 add dword ptr [rbx+558h],2
00007fff`6332362d 898364050000 mov dword ptr [rbx+564h],eax
00007fff`63323640 898360050000 mov dword ptr [rbx+560h],eax
00007fff`6332366d 5b pop rbx
00007fff`6332366e c3 ret
00007fff`6332366f 90 nop
Luca
Comments
Post a Comment