brk

用brk实现sbrk,关于brk的返回值

好久不见. 提交于 2020-03-01 03:58:32
首先我们已经知道linux下,malloc最后调用的是sbrk函数,而sbrk是对brk的简单封装。 用sbrk模仿malloc很简单,sbrk(0)得到当前breakpoint,再调用sbrk(size)即可。(PS:breakpoint表示堆结束地址) 一直以来让我困惑的是,怎么用brk去实现sbrk,换句话说,就是只有brk系统调用,如何能得知当前的breakpoint...难道就没有人想过这个问题嘛?搜索了各种关键字,来来回回都围绕着sbrk讲,算了,自己动手,丰衣足食,咱求人不如求己,还是自己分析分析好了, glibc中brk的wrapper如下: #include <unistd.h> int brk(void *addr); man手册中对此函数的描述: brk() sets the end of the data segment to the value specified by addr, when that value is reasonable, the system has enough memory, and the process does not exceed its maximum data size (see setrlimit(2)). RETURN VALUE On success, brk() returns zero. On error,

What's unsafe/legacy about brk/sbrk?

删除回忆录丶 提交于 2019-12-12 10:44:11
问题 I've heard in a lot of places (musl mailing list, macOS forums, etc.) that brk() and sbrk() are unsafe. Many of these places either don't give explanations at all, or give very vague explanations. For example, this link states that "these functions are fundamentally broken", and goes on to say that the malloc and sbrk subsystems are utterly broken, that they ruin the heap, et al. My question is: Why is this so? If malloc is used in such a way that it allocates a block of memory with sbrk

Assembly x86 brk() call use

喜欢而已 提交于 2019-11-29 07:48:17
i am trying to dynamically allocate memory into the heap and then assign values in those memory addresses. I understand how to allocate the memory but how would i assign for example the value in a register to that first dynamic memory address? This is what i have so far:` push rbp mov rbp, rsp ;initialize an empy stack to create activation records for the rest of the subroutines mov rax, 0x2d ;linux system call for brk() mov rbx, 0x0 ;to get the adress of the first adress we are allocating we must have 0 in rbx int 0x80 ;calls the linux operating system kernel for assistance mov [brk

How are sbrk/brk implemented in Linux?

前提是你 提交于 2019-11-28 04:44:52
I was thinking about how the Linux kernel implements system calls and I was wondering if someone could give me a high level view of how sbrk/brk work? I've reviewed the kernel code, but there is just so much of it and I don't understand it. I was hoping for a summary from someone? In a very high level view, the Linux kernel tracks the memory visible to a process as several "memory areas" ( struct vm_area_struct ). There is also a structure which represents (again in a very high level view) a process' whole address space ( struct mm_struct ). Each process (except some kernel threads) has

Assembly x86 brk() call use

亡梦爱人 提交于 2019-11-28 01:28:01
问题 i am trying to dynamically allocate memory into the heap and then assign values in those memory addresses. I understand how to allocate the memory but how would i assign for example the value in a register to that first dynamic memory address? This is what i have so far:` push rbp mov rbp, rsp ;initialize an empy stack to create activation records for the rest of the subroutines mov rax, 0x2d ;linux system call for brk() mov rbx, 0x0 ;to get the adress of the first adress we are allocating we