0

I'm trying to increase the heap size by 100 by changing the brk and I don't know why my code doesn't work.
Here is the part of the code that tries do it:

movq $0, %rdi
movq $12, %rax
syscall
movq %rax, InicialHeap

movq InicialHeap, %rsi
mov $str, %rdi
call printf

movq $100, %rdi
movq $12, %rax
syscall

movq %rax, %rsi
mov $str, %rdi
call printf

movq InicialHeap, %rdi
movq $12, %rax
syscall

movq InicialHeap, %rsi
mov $str, %rdi
call printf

movq $60, %rax
syscall

The program should print something like:
x (print InicialHeap)
x + 100 (print InicialHeap + 100)
x (print InicialHeap)
But it only prints 3 times the same result "x".
What do I have to do to increase my heap size?

  • Why would it add 100 in the second call, but not add the value stored in `InicialHeap` in the third call? – Ross Ridge Jun 19 '16 at 06:18
  • 1
    Can you rerun your program with strace and post results? For example, `strace ./a.out`. Strace will print all syscalls, decode their parameters and show and decode return values. What is your OS? In linux brk is a bit strange: http://linux.die.net/man/2/brk. – osgx Jun 19 '16 at 06:19
  • Although not an answer to this question, some example 32-bit code (not 64-bit) using NASM (Not GNU assembler) can be found in this SO answer: http://stackoverflow.com/a/33903235/3857942 . The code does use the `brk` 32-bit syscall but the idea in that code should be adaptable to use the 64-bit syscalls in your assembler with minimal effort. – Michael Petch Jun 19 '16 at 07:13
  • What operating system are you programming for? – fuz Jun 19 '16 at 10:03

1 Answers1

0

As the NOTES section of the man page for int brk(void *addr); describes, the system call (__NR_brk = 12) actually implements brk(), not sbrk, but returns the current break rather than an integer.

As @osgx comments, try running your program under strace to see what return values you're getting. e.g. from strace /bin/true, you can see that it's normal for the dynamic linker to start off by using brk(0) to find out the current break:

brk(0)                                  = 0x24c6000

From there, you should save the return value, and make your next call to brk() with an offset from that.


Your current code clearly can't work:

movq $100, %rdi
movq $12, %rax
syscall             ;;  brk((void*)100)
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847