使用新的位置时Sigbus

SIGBUS When Using Placement new?

本文关键字:Sigbus 位置      更新时间:2023-10-16

我有以下代码(更新(:

#include <iostream>
#include <cassert>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <new>
struct S {
uint32_t a;
uint32_t b;
};
int main() {
    const auto fd = open("/tmp/abc.txt", O_RDWR | O_CREAT, S_IRWXU);
    assert(fd > 0);
    void* ptr = mmap(nullptr, sizeof(S), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    assert(MAP_FAILED != ptr);
    std::cout << "address: " << ptr << std::endl;
    auto tptr = new (ptr) S();
    tptr->a = 99;
    const auto rc = msync(&ptr, sizeof(S), MS_ASYNC);
    std::cout << "msync returned " << rc << std::endl;
    if (rc != 0) {
        std::cout << "error code = " << errno << ": " << strerror(errno) << std::endl;
    }
}

当我在GDB中运行它时,我得到了:

address: 0x7ffff7ff8000
Program received signal SIGBUS, Bus error.
0x0000000000400adb in main () at msync.cpp:20
20      auto tptr = new (ptr) S();

我看到有人提到了内存对准问题,我检查了0x7fffff7ff8000可以由2、8、16、32和64排除。还是其他?

预先感谢。

似乎您正在尝试 create 在此处写入文件并将其写入其中,因此最初它的大小为零,并且占据零存储页面。但是mmap不能在文件的末尾写入,有效地为您分配了内存:首先,它会知道要添加多少个字节?您需要确保/tmp/abc.txt包含一些字符,这些字符随后通过位置new 覆盖。它不能附加

在我将8个随机字节写入/tmp/abc.txt中后,运行您的程序成功并通过

覆盖这些字节
63 00 00 00 00 00 00 00

正如我X86-64上预期的那样。然后,该程序报告您可能打算生成的msync错误,并优雅地退出。