我正在尝试在 C 程序中使用 brk() 函数。我的目标是通过检查当前程序中断(pb)来直接使用它(作为更大测试的一部分)
void *current_break = sbrk(0);
执行 malloc(用于测试,如果分配的空间足够大,malloc 有时应该执行 brk)
void* mallow_return = malloc(1);
而不是通过使用当前地址+增量直接执行 brk() (并检查这是否增加了 pb):
int increase = 0x01;
void * newbreak = current_break + increase;
int return_value = brk(&newbreak);
我的问题是,无论是使用大型 malloc (malloc(5000;)) 还是使用(对齐或未对齐) brk() 调用 pb 都不会更改。检查 errno 时,我得到一个
无法分配内存!
error (as given by
strerror(errno)
谁能明白为什么我无论如何都无法增加程序中断?
感谢您的任何提示!
(系统为:Debian 10(buster),内核为 4.19)
编辑:根据要求,这是主要功能,包括:
#include <unistd.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
void main(int argc, char **argv)
{
printf("[+] Get current program break\n");
void *current_break = sbrk(0);
printf("\t[+] Current break point=\t%#x\n", current_break);
printf("[+] Malloc should call brk as well\n");
void* mallow_return = malloc(1);
printf("[+] Check if malloc changes PB\n");
void *after_break = sbrk(0);
printf("\t[+] After malloc pbreak=\t%#x\n", after_break);
int increase = 0x01;
printf("\t[+] Increasing p-break direclyby %d\n", increase);
void * newbreak = current_break + increase;
printf("\t[+] Setting break point to=\t%#x\n", newbreak);
int return_value = brk(&newbreak);
//check if error was thrown
int errornumber = errno;
if (errornumber != 0)
{
printf("\t[+] Error: %s!\n", strerror(errornumber));
return -1;
}
//check if pb was set now
printf("\t[?] New program break value?\t%#x\n", sbrk(0));
printf("[?] Return value of brk: %d\n", return_value);
return;
}