【问题标题】:core file size limit has non-deterministic effects on processes核心文件大小限制对进程具有不确定性影响
【发布时间】:2015-08-01 19:12:59
【问题描述】:

我正在运行自定义 2.6.27 内核,我刚刚注意到在段错误期间生成的核心文件大于为进程设置的硬核文件大小限制。

更奇怪的是,核心文件只是有时被截断(但没有达到 ulimit 设置的限制)。

例如,这是我将在下面尝试崩溃的程序:

int main(int argc, char **argv)
{
    // Get the hard and soft limit from command line
    struct rlimit new = {atoi(argv[1]), atoi(argv[1])};

    // Create some memory so as to beef up the core file size
    void *p = malloc(10 * 1024 * 1024);

    if (!p)
        return 1;

    if (setrlimit(RLIMIT_CORE, &new)) // Set the hard and soft limit
        return 2;                     // for core files produced by this
                                      // process

    while (1);

    free(p);
    return 0;
}

这是执行:

Linux# ./a.out 1446462 &    ## Set hard and soft limit to ~1.4 MB
[1] 14802
Linux# ./a.out 1446462 &
[2] 14803
Linux# ./a.out 1446462 &
[3] 14804
Linux# ./a.out 1446462 &
[4] 14807

Linux# cat /proc/14802/limits | grep core
Max core file size        1446462              1446462              bytes

Linux# killall -QUIT a.out

Linux# ls -l
total 15708
-rwxr-xr-x 1 root root     4624 Aug  1 18:28 a.out
-rw------- 1 root root 12013568 Aug  1 18:39 core.14802         <=== truncated core
-rw------- 1 root root 12017664 Aug  1 18:39 core.14803
-rw------- 1 root root 12013568 Aug  1 18:39 core.14804         <=== truncated core
-rw------- 1 root root 12017664 Aug  1 18:39 core.14807
[1]   Quit                    (core dumped) ./a.out 1446462
[2]   Quit                    (core dumped) ./a.out 1446462
[3]   Quit                    (core dumped) ./a.out 1446462
[4]   Quit                    (core dumped) ./a.out 1446462

所以这里发生了很多事情。我将每个进程的硬限制设置为大约 1.4 MB。

  1. 生成的核心文件远远超出了此设置的限制。为什么?
  2. 生成的 4 个核心文件中的 2 个被截断,但正好是 4096 字节。这是怎么回事?

我知道核心文件包含分配的完整堆栈和堆内存等内容。对于这样一个简单的程序(最多给或取几个字节),这不应该是相当恒定的,从而在多个实例之间产生一致的核心吗?

编辑:

1du的请求输出

Linux# du core.*
1428    core.14802
1428    core.14803
1428    core.14804
1428    core.14807

Linux# du -b core.*
12013568    core.14802
12017664    core.14803
12013568    core.14804
12017664    core.14807

2malloc() 之后添加memset() 肯定会占据主导地位,因为核心文件现在都被截断为1449984(仍然超过限制的3522 字节)。

那么为什么以前的核心这么大,它们包含什么?不管是什么,它都不受流程的限制。

3 新程序也显示了一些有趣的行为:

Linux# ./a.out 12017664 &
[1] 26586
Linux# ./a.out 12017664 &
[2] 26589
Linux# ./a.out 12017664 &
[3] 26590
Linux# ./a.out 12017663 &        ## 1 byte smaller
[4] 26653
Linux# ./a.out 12017663 &        ## 1 byte smaller
[5] 26666
Linux# ./a.out 12017663 &        ## 1 byte smaller
[6] 26667

Linux# killall -QUIT a.out

Linux# ls -l
total ..
-rwxr-xr-x 1 root root     4742 Aug  1 19:47 a.out
-rw------- 1 root root 12017664 Aug  1 19:47 core.26586
-rw------- 1 root root 12017664 Aug  1 19:47 core.26589
-rw------- 1 root root 12017664 Aug  1 19:47 core.26590
-rw------- 1 root root  1994752 Aug  1 19:47 core.26653           <== ???
-rw------- 1 root root  9875456 Aug  1 19:47 core.26666           <== ???
-rw------- 1 root root  9707520 Aug  1 19:47 core.26667           <== ???
[1]   Quit                    (core dumped) ./a.out 12017664
[2]   Quit                    (core dumped) ./a.out 12017664
[3]   Quit                    (core dumped) ./a.out 12017664
[4]   Quit                    (core dumped) ./a.out 12017663
[5]   Quit                    (core dumped) ./a.out 12017663
[6]   Quit                    (core dumped) ./a.out 12017663

【问题讨论】:

  • 首先,du 显示核心文件使用的实际字节数是多少?另外,memset() 内存。简单地执行malloc() 不会导致实际内存被映射到进程中。
  • 4096 是虚拟内存页面大小。创建核心转储后,分配给进程的所有 vm 页面都将转储到核心文件中。较小的文件为 2933 页,较大的文件为 2934 页。所以这是相当一致的。
  • @AndrewHenle 添加memset() 确实使事情更加一致,但并非完全一致。我已经用这个和du 结果更新了我的问题。
  • @user3386109 是有道理的。但是 gdb 抱怨这两个被截断了,但另外两个完全没问题。因此,对于所有 4 个内核,它都需要 12017664 字节。
  • 程序变得如此之大,因为它们没有分配物理内存,只是分配虚拟内存。当 GLIBC 的 malloc() 被赋予大量内存来分配它时,请求在内核的另一个虚拟内存页面中执行此操作。出于某种原因,核心文件正在写入所有内存,包括从未物理存在的字节(这很奇怪,因为大多数文件系统的 Linux 中的文件都可以离开file holes)。

标签: c linux coredump


【解决方案1】:

核心转储的实现可以在fs/binfmt_elf.c中找到。我会按照3.12及以上的代码(改成commit 9b56d5438),但逻辑非常相似。

代码最初决定在vma_dump_size 中转储多少 VMA(虚拟内存区域)。对于像brk 堆这样的匿名VMA,它返回VMA 的完整大小。在此步骤中,不涉及核心限制。

写入核心转储的第一阶段然后为每个 VMA 写入一个 PT_LOAD 标头。这基本上是一个指针,指示在 ELF 文件的其余部分中的何处可以找到数据。实际数据是通过for循环写入的,实际上是第二阶段。

在第二阶段,elf_core_dump 反复调用get_dump_page 为必须转储的程序地址空间的每一页获取一个struct page 指针。 get_dump_pagemm/gup.c 中的一个常用实用函数。 get_dump_page 的评论很有帮助:

 * Returns NULL on any kind of failure - a hole must then be inserted into
 * the corefile, to preserve alignment with its headers; and also returns
 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
 * allowing a hole to be left in the corefile to save diskspace.

事实上,如果get_dump_page 返回NULLelf_core_dump 会调用fs/coredump.c 中的函数(内核中的dump_seek,3.12+ 中的dump_skip)。此函数调用 lseek 以在转储中留下一个洞(实际上,因为这是它直接在 struct file 指针上调用 file-&gt;f_op-&gt;llseek 的内核)。 主要区别在于 dump_seek 确实不遵守 ulimit,而较新的 dump_skip 确实如此。

至于为什么第二个程序有奇怪的行为,可能是因为 ASLR(地址空间随机化)。截断哪个 VMA 取决于 VMA 的相对顺序,它是随机的。您可以尝试禁用它

echo 0 | sudo tee /proc/sys/kernel/randomize_va_space

看看你的结果是否更均匀。要重新启用 ASLR,请使用

echo 2 | sudo tee /proc/sys/kernel/randomize_va_space

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 2011-12-30
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    相关资源
    最近更新 更多