5

So, as a challenge, and for performance, I'm writing a simple server in assembly. The only way I know of is via system calls. (through int 0x80) Obviously, I'm going to need more memory than allocated at assemble, or at load, so I read up and decided I wanted to use sbrk(), mainly because I don't understand mmap() :p

At any rate, Linux provides no interrupt for sbrk(), only brk().

So... how do I find the current program break to use brk()? I thought about using getrlimit(), but I don't know how to get a resource (the process id I'd guess) to pass to getrlimit(). Or should I find some other way to implement sbrk()?

Jon Weldon
  • 115
  • 2
  • 8
  • 1
    Welcome to Stack Overflow! "The only way I know of is via system calls.". There are also library calls, like `sbrk`. Or do you mean "in assembly without libc"? – Robᵩ Apr 25 '12 at 19:16
  • 1
    I wasn't sure how to make library calls. (and still don't really know how) do you think library calls are faster than system calls? – Jon Weldon Apr 25 '12 at 19:44
  • `mmap` is simple. It's not good for small allocations though. – doug65536 Oct 26 '17 at 07:37

2 Answers2

4

The sbrk function can be implemented by getting the current value and subtracting the desired amount manually. Some systems allow you to get the current value with brk(0), others keep track of it in a variable [which is initialized with the address of _end, which is set up by the linker to point to the initial break value].

This is a very platform-specific thing, so YMMV.

EDIT: On linux:

However, the actual Linux system call returns the new program break on success. On failure, the system call returns the current break. The glibc wrapper function does some work (i.e., checks whether the new break is less than addr) to provide the 0 and -1 return values described above.

So from assembly, you can call it with an absurd value like 0 or -1 to get the current value.

Be aware that you cannot "free" memory allocated via brk - you may want to just link in a malloc function written in C. Calling C functions from assembly isn't hard.

Random832
  • 37,415
  • 3
  • 44
  • 63
  • Or OP could make the `mmap` syscall with `MAP_ANONYMOUS|MAP_PRIVATE` to get memory, and `munmap` to free it. This is more expensive than managing your memory yourself in userspace, but would be a lot easier to do from assembly... – R.. GitHub STOP HELPING ICE Apr 27 '12 at 04:23
  • 2
    "Be aware that you cannot "free" memory allocated via brk". But `man brk` says "Increasing the program break has the effect of allocating memory to the process; decreasing the break deallocates memory." So how is it that you cannot "free" memory allocated via brk? – automaton Jul 28 '15 at 08:00
  • @automaton It's not safe to do so because some other function you've called after allocating that memory may have allocated more memory "on top of" it. You'd have to write a whole memory management system. – Random832 Jul 29 '15 at 21:44
0

Source:

#include <unistd.h>
#define SOME_NUMBER  8
int main() {
  void *ptr = sbrk(8);
  return 0;
}

Compile using with Assembly Output option

gcc -S -o test.S test.c

Then look at the ASM code

_main:
Leh_func_begin1:
    pushq   %rbp
Ltmp0:
    movq    %rsp, %rbp
Ltmp1:
    subq    $16, %rsp
Ltmp2:
    movl    $8, %eax
    movl    %eax, %edi
    callq   _sbrk
    movq    %rax, -16(%rbp)
    movl    $0, -8(%rbp)
    movl    -8(%rbp), %eax
    movl    %eax, -4(%rbp)
    movl    -4(%rbp), %eax
    addq    $16, %rsp
    popq    %rbp
    ret
Leh_func_end1:

There is no system call for it but you should be able to still make the call

lukecampbell
  • 14,728
  • 4
  • 34
  • 32
  • I linked the binary static on fedora16/x86, and objdumped. I saw that __sbrk calls __brk which calls the brk syscall (syscall 45) – Marco van de Voort Apr 25 '12 at 19:36
  • Perhaps, but what you have posted still depends on an external implementation of _sbrk being linked in – Chris Stratton Apr 25 '12 at 19:37
  • Yes, as I mentioned there is no system call for sbrk (at least on my machine and I am reasonably certain it's excluded from the Unix standard as a system call) so to call sbrk you would need to have it linked in `libc` should have it implemented. – lukecampbell Apr 25 '12 at 20:00