如何限制()`而没有限制堆栈`malloc的收购内存? [英] How can I limit memory acquired with `malloc()` without also limiting stack?

查看:154
本文介绍了如何限制()`而没有限制堆栈`malloc的收购内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让学生code从分配猖獗和拖动我的测试机停了下来。我试过

I'm trying to keep student code from running wild with allocations and dragging my test machine to a halt. I've tried

setrlimit(RLIMIT_DATA, r);

其中,研究是一个结构持有的限制。但不幸的是,虽然这个限制停止 BRK SBRK 从分配,C库只是故障转移到 MMAP 并保持正常分配。

where r is a struct holding the limits. But unfortunately although this limit stops brk and sbrk from allocating, the C library just fails over to mmap and keeps right on allocating.

我也试过

setrlimit(RLIMIT_AS, r)

和这个停止进程在其轨道上,但这一补救措施过于严厉—它是不可能的过程,从 ENOMEM 错误的原因是因为没有堆栈空间恢复在code使得在遇到从返回), NULL 值的malloc(呼叫

and this stops the process in its tracks, but this remedy is too severe—it is impossible for the process to recover from the ENOMEM error because there's no stack space for the calls the code makes on encountering a NULL value returned from malloc().

我有超过二进制有限的控制,因此,如果有可能有一个系统调用的事,我倒是preFER这一点。但我需要封盖分配不破坏该进程的恢复能力的一些手段。有没有人有什么建议?

I have limited controls over the binaries, so if it's possible to do with a system call, I'd prefer that. But I need some means of capping allocation without destroying the process's ability to recover. Does anyone have suggestions?

更新:我发现一些所谓的 failmalloc ,但它是不是很复杂,虽然我可能导致故障有了它,我总是得到GDB不能诊断段错误。

UPDATE: I found something called failmalloc, but it is not very sophisticated, and although I can cause a failure with it, I always get a segfault that gdb cannot diagnose.

进一步更新:我发现了setrlimit(RLIMIT_AS,R)确实的似乎做我想要的工作的,至少在某些情况下,—以下段错误是被后来发生的由故障无关模块中引起的。除非有人想出了一些有趣的事情(或者一个理由继续的问题),我可能会删除的问题。

FURTHER UPDATE: I found that setrlimit(RLIMIT_AS, r) does seem to do the job I want, at least in some cases—the segfaults that were occurring afterward were caused by a fault in an unrelated module. Unless somebody comes up with something interesting (or a reason to keep the question), I will probably delete the question.

推荐答案

failmalloc ,您可以使用 LD_ preLOAD < SUP> *
环境变量和函数干预围绕打造包装的malloc(),并处任何限制存在。

Building on the idea used by failmalloc, you could use the LD_PRELOAD* environment variable and function interposition to build a wrapper around malloc() and impose any limitations there.

您将使用的需要一个指针动态加载到原来的的malloc()=htt​​p://www.opengroup.org/onlinepubs/009695399/功能/ dlsym.html相对=nofollow> 则dlsym() 。您不能直接调用原始的的malloc()从包装,因为这将是PTED为递归调用包装本身间$ P $。

You would need to dynamically load a pointer to the original malloc() using dlsym(). You cannot directly call the original malloc() from the wrapper because it will be interpreted as a recursive call to the wrapper itself.

#define _GNU_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <dlfcn.h>

void * malloc(size_t size)
{
   static void * (*func)(size_t) = NULL;
   void * ret;

   if (!func)
   {
      /* get reference to original (libc provided) malloc */
      func = (void *(*)(size_t)) dlsym(RTLD_NEXT, "malloc");
   }

   /* impose any necessary restrictions before calling malloc */
   ...

   /* call original malloc */
   ret = func(size);

   /* impose any necessary restrictions after calling malloc */
   ...

   return ret;
}

*请注意 LD_ preLOAD 必须指定完整路径插入库中,该库的设置是setuid程序禁用,以prevent安全问题。

* Note that LD_PRELOAD must specify the full path to the interposer library, and that library interposition is disabled for setuid programs in order to prevent security problems.

这是<一个href=\"http://stackoverflow.com/questions/998464/function-interposition-in-linux-without-dlsym/998489#998489\">alternative使用 则dlsym() 是使用的GNU链接 - 包装符号选项

这篇关于如何限制()`而没有限制堆栈`malloc的收购内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆