Reverse Engineering for Beginners

(avery) #1

CHAPTER 92. OPENMP CHAPTER 92. OPENMP


Listing 92.4: GCC 4.8.1
mov edi, OFFSET FLAT:main._omp_fn.0
call GOMP_parallel_start
mov edi, 0
call main._omp_fn.0
call GOMP_parallel_end

Unlike MSVC’s implementation, what GCC code does is to start 3 threads, and run the fourth in the current thread. So there
are 4 threads instead of the 5 in MSVC.


Here is themain._omp_fn.0function:


Listing 92.5: GCC 4.8.1

main._omp_fn.0:
push rbp
mov rbp, rsp
push rbx
sub rsp, 40
mov QWORD PTR [rbp-40], rdi
call omp_get_num_threads
mov ebx, eax
call omp_get_thread_num
mov esi, eax
mov eax, 2147483647 ; 0x7FFFFFFF
cdq
idiv ebx
mov ecx, eax
mov eax, 2147483647 ; 0x7FFFFFFF
cdq
idiv ebx
mov eax, edx
cmp esi, eax
jl .L15
.L18:
imul esi, ecx
mov edx, esi
add eax, edx
lea ebx, [rax+rcx]
cmp eax, ebx
jge .L14
mov DWORD PTR [rbp-20], eax
.L17:
mov eax, DWORD PTR [rbp-20]
mov edi, eax
call check_nonce
add DWORD PTR [rbp-20], 1
cmp DWORD PTR [rbp-20], ebx
jl .L17
jmp .L14
.L15:
mov eax, 0
add ecx, 1
jmp .L18
.L14:
add rsp, 40
pop rbx
pop rbp
ret


Here we see the division clearly: by callingomp_get_num_threads()andomp_get_thread_num() we get the
number of threads running, and also the current thread’s number, and then determine the loop’s interval. Then we run
check_nonce().


GCC also inserted theLOCK ADD instruction right in the code, unlike MSVC, which generated a call to a separate DLL
function:


Listing 92.6: GCC 4.8.1
lock add DWORD PTR checked[rip], 1
call GOMP_critical_start
Free download pdf