lkml.org 
[lkml]   [2007]   [Oct]   [25]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
Subjectx86: randomize brk() and RLIMIT_DATA
Would be neat if randomized brk and setrlimit(RLIMIT_DATA, ...)
worked in a predictable way:

$ gcc brk.c -fPIC -pie -m64;./a.out;./a.out;./a.out
sbrk=0x7f721b815000 main=0x7f721af04860
sbrk succeeded (brk=0x7f721b909240)
sbrk=0x7fc3d77e2000 main=0x7fc3d66fa860
sbrk failed: Cannot allocate memory (brk=0x7fc3d77e2000)
sbrk=0x7f05b0615000 main=0x7f05af76b860
sbrk failed: Cannot allocate memory (brk=0x7f05b0615000)

--
Do what you love because life is too short for anything else.

#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <errno.h>
#include <string.h>

int main(void)
{
void *p;
const struct rlimit rlim_data = {
.rlim_cur = 10 << 20,
.rlim_max = RLIM_INFINITY
};
int saved_errno;

p = sbrk(0);
fprintf(stderr, "sbrk=%p main=%p\n", p, &main);
if (setrlimit(RLIMIT_DATA, &rlim_data) == -1) {
fprintf(stderr, "setrlimit failed: %s\n", strerror(errno));
return 1;
}
if (sbrk(1 << 20) == (void*)-1) {
saved_errno = errno;
fprintf(stderr, "sbrk failed: %s (brk=%p)\n",
strerror(saved_errno), sbrk(0));
return 1;
} else {
fprintf(stderr, "sbrk succeeded (brk=%p)\n", sbrk(0));
return 0;
}
}

\
 
 \ /
  Last update: 2007-10-25 15:43    [W:0.069 / U:0.440 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site