0

Hey guys for my second year Computer Science project we were tasked with creating a garbage collector in 32-bit nasm assembly. I managed to get everything running fine except one thing. We are suppose to mimick the gclib sbrk function using the system call to brk.

Here is my working version:

.if2:
 mov eax, 0
 push eax
 call abrk
 add esp, 4
 mov ecx, eax
 mov edx, num_units
 shl edx, 3
 push edx
 call sbrk
 add esp, 4
 cmp eax, -1
 je .if2body
 mov vp, ecx
 jmp .cont1
.if2body:
mov eax, 0x00      
jmp    .return

now to get rid of the sbrk i did the following which causes a segfault in a completely different function:

.if2:
 mov eax, 0
 push eax
 call abrk
 add esp, 4
 mov ecx, eax
 mov edx, num_units
 shl edx, 3
 add eax, edx
 push eax
 call abrk
 add esp, 4
 cmp eax, ecx
 je .if2body
 mov vp, ecx
 jmp .cont1
.if2body:
mov eax, 0x00  
jmp    .return

Here is the abrk function:

%define SYS_brk 0x2d
%define addr [ebp+8]
abrk:
 push ebp
 mov ebp, esp
 push ebx
 mov eax, SYS_brk
 mov ebx, addr
 int 0x80
.end:
 pop ebx
 mov esp, ebp
 pop ebp
 ret

I have no idea what could be wrong, I've asked my lecturer and he could not find a fault either.

Also the version that does not work worked on my universities lab pcs but not on mine could it be caused by difference in ubuntu versions? Thanks for the help.

Zhunaid
  • 1
  • 2
  • Have you looked at `strace` output to see what your code (or glibc) is passing to the actual syscall? How did the crash in other code happen? (i.e. what was that code doing, and what memory was it trying to access when it faulted?) – Peter Cordes Oct 24 '16 at 09:35
  • Re: different behaviour on different computers: Are they all running 64-bit kernels? Are you sure you actually built 32-bit binaries on all of them, and didn't accidentally build a 64-bit binary that truncates RSP to 32 bits? – Peter Cordes Oct 24 '16 at 09:38
  • Have you considered commenting your code? – David Hoelzer Oct 24 '16 at 11:14
  • Note that the brk system call doesn't work like the brk library function on Linux. But how can we know that you use Linux if you don't tag it? – fuz Oct 24 '16 at 11:50
  • I've narrowed it down further. Both sbrk and brk do correctly compute the new system break but the system call to brk for some reason does not actually increase the system break for some reason because the segfault occurs when we try to access memory outside of the system break. I've mad sure that it is in 32-bit by running it on a 32-bit virtual ubuntu. – Zhunaid Oct 24 '16 at 11:51

0 Answers0