0

My device is Apple MacBook M1

I am writing a "malloc" function using brk and sbrk, however when I am advancing my unit test I found some strange things about sbrk when I need to use it to extend my heap.

The issue:

Here I will give a minimized example about it, and recreate the "abnormal" scenario

// 1st example
// if I just use sbrk for a large number like:
assert(sbrk(4096*1000000000) != (void *) -1); // sbrk return -1 if no more space 
// the assert holds
// second example
// if I apply a not big number like
assert(sbrk(40960032) != (void *) -1);
// the assert failed(or like 40960040 and so on)

It seems that the sbrk can give way more space than this(from the 1st example), however it just failed.

My attempts: 1.Reach to the manual, and the man sbrk says:

BUGS
     Setting the break may fail due to a temporary lack of swap space.  It is
     not possible to distinguish this from a failure caused by exceeding the
     maximum size of the data segment without consulting getrlimit.

so it may be a temporary lack of swap space, so I tried:

    printf("init %p\n", sbrk(0));
    sbrk(40960032);
    if(sbrk(1)!=(void*)-1){
        printf("now %p\n", sbrk(0));
    }

and it outputs

init 0x102648000
now 0x102648001

which shows the sbrk(40960032) also do not work

I also tried step sbrk(1) for 40960032 times and it will start to return -1 from a certain position, but i did not find the pattern

Can anyone give me some tips and I would be grateful

billz
  • 11
  • 1

0 Answers0