- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
为了并发/并行GC,我感兴趣的是 mprotect 系统调用提供的内存顺序保证(即 mprotect 与多线程的行为或 mprotect 的内存模型)。我的问题是(假设没有编译器重新排序或有足够的编译器障碍)
如果线程 1 由于 mprotect 而触发地址段错误线程 2,我能确定一切都发生在线程 2 之前吗?可以在线程 1 的信号处理程序中观察到系统调用段错误?如果在信号中放置一个完整的内存屏障会怎样在线程 1 上执行加载之前处理程序?
如果线程 1 对设置为线程 2 的 PROT_NONE 并且没有触发段错误,这就足够了吗a 发生在两者之间的关系之前。或者换句话说,如果两个线程执行(*ga
以 0
开始,p
是页对齐地址,以只读方式开始)
// thread 1
*ga = 1;
*(volatile int*)p; // no segfault happens
// thread 2
mprotect(p, 4096, PROT_NONE); // Or replace 4096 by the real userspace-visible page size
a = *ga;
是否可以保证线程 2 上的 a
将为 1
? (假设没有在线程 1 上观察到段错误并且没有其他代码修改 *ga
)
我最感兴趣的是 Linux 行为,尤其是 x86(_64)、arm/aarch64 和 ppc 上的行为,但欢迎访问有关其他 archs/OS 的信息(对于 windows,将 mprotect 替换为 VirtualProtect 或其他名称... .).到目前为止,我在 x64 和 aarch64 Linux 上的测试表明没有违反这些规定,但我不确定我的测试是否具有决定性,或者该行为是否可以长期依赖。
一些搜索表明,mprotect
可能会在删除权限时对所有映射地址的线程发出 TLB shootdown,这可能会提供此处所述的保证(或者换句话说,提供此保证似乎是这种操作的目标)虽然我不清楚 future 内核代码的优化是否会打破这个保证。
引用 LKML post我一周前问过这个问题,但还没有回复......
编辑:澄清问题。我知道 tlb 击落应该提供我正在寻找的保证,但我想知道是否可以依赖这种行为。换句话说,内核发出此类请求的原因是什么,因为如果不是为了提供某种排序保证,则不需要它。
最佳答案
因此,我在此处发帖一天后在机械同情组中提出了这个问题,并得到了 Gil Tene 的答复。在他的允许下,这是我对他的回答的总结。完整线程可用here以防有任何我没有包含的内容不清楚。
对于操作系统的整体行为。
(as in "would be surprising for an OS to not meet):
A call to mprotect() is fully ordered with respect to loads and stores that happen before and after the call. This tends to be trivially achieved at the CPU and OS level because mprotect is a system call, which involves a trap, which in turn involves full ordering. [In strange no-ring-transition-implementations (e.g. in-kernel execution, etc.) the protect call would be presumably responsible for emulating this ordering assumption].
A call to mprotect will not return before the protection request semantically takes hold everywhere within the process. If the mprotect() call sets a protection that would cause a fault, any operation on any thread that happens after this mprotect() call is required to fault. Similarly, if the mprotect() call sets a protection that would prevent a fault, any operation on any thread that happens after this mprotect() call is required to NOT fault.
这实质上意味着其他线程对受影响页面的内存操作与调用mprotect
的线程同步。更具体地说,可以预期原始问题中提到的两种情况都得到保证。即
如果观察到由于 mprotect 调用导致受影响页面中一个线程的加载错误,则此错误发生在 mprotect() 调用之后,因此能够观察到 mprotect 之前发生的所有内存操作.
如果观察到受影响页面中一个线程上的加载没有错误地阻止 mprotect 调用,则加载发生在 mprotect 调用之前,并且 mprotect 调用和它之后的任何代码都在加载之后,并且将能够观察加载之前发生的任何内存操作。
还有人指出,传递性可能不起作用,即一个线程上的错误加载可能不会在另一个线程上的非错误加载之后。这可能(实际上)是由 tlb 刷新的非原子性引起的,导致不同的线程/cpus 在不同的时间观察访问权限的变化。
关于c - 多线程 mprotect 的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43722456/
我试图分配一个整数数组,然后使用mmap()将页面映射到两个不同的静态堆,然后使用mprotect()保护其中一个堆> 具有保护 PROT_NONE。 看来 mprotect() 保护的内容比我告诉它
我有一个简单的程序,我试图保护一 block 内存,然后将一个文件读入该内存,并在出现段错误时释放它。一开始我以为只有文件是 fifo 才有问题。但现在看来,即使是普通文件,它也会失败, 这是代码:
我正在使用 mprotect 将一些内存页设置为写保护。当尝试对该内存区域进行任何写入时,程序会收到一个 SIGSEGV 信号。从信号处理程序我知道在哪个内存地址尝试了写入,但我不知道如何找出哪条指令
我正在尝试使用 protect 修改 .text 段中的值以授予我写入权限: int pageSize = sysconf(_SC_PAGE_SIZE); int *toModify = (int
mprotect 系统调用保护页面边界内的内存区域: int mprotect(void *addr, size_t len, int prot); 在这里len应该是页面大小的倍数。 有没有办法只保
我在 stracing linux 内核中的一些常用命令,看到 mprotect() 被使用了很多次。我只是想知道,mprotect() 用来确定它为其设置保护值的内存地址在其自己的地址空间中的决定因
我不理解 mprotect 用法中的“对齐分配的内存”部分。 我指的是 http://linux.die.net/man/2/mprotect 上给出的代码示例 char *p; char c; /*
作为我正在进行的项目的一部分,我想分配一些内存,将操作码写入该内存,然后 call它。到那时,代码将跳转到我分配的内存中,运行操作码并使用操作码 ret 返回到之前的位置。 . 我的代码在 Linux
我想在数组“page”中使用 posix_memalign 分配 4096 字节,然后通过 mprotect() 使用 PROT_NONE 对其进行保护。分配似乎有效,但 protected() 返回
我正在尝试编写一个程序,在其中分配一 block 内存,然后有选择地更改该 block 的页面大小和页面对齐子集的保护。但是,当我尝试对 block 中大于 8 页的内存部分调用 mprotect 时
从这个页面:http://linux.die.net/man/2/mprotect 我了解除 PROT_EXEC 之外的标志。调用的效果是什么 mprotect(ptr, size, PROT_EXE
我正在编写一些通过系统调用调用 mprotect 的 ROP 漏洞利用代码,在调用 int 0x80 eax 后设置为 0x0 表示成功。将执行转移到目标地址仍然会导致 SIGSEGV。我希望有人能告
对于我的大学项目,我需要写保护进程的整个地址空间。我正在读取 /proc/self/maps 文件并解析映射。 因此对于格式为 08048000-0804c000 r-xp 00000000 08:0
如果我使用 PROT_NONE mprotect 段并且如果 SIGSEGV 由于写入而发生,该写入由 sigaction 和 sa_sigaction ,我们将能够使用siginfo_t的si_ad
我真的在寻找任何解决方案 - 我的目标是将小于系统页面大小的区域设置为 PROT_READ、PROT_WRITE、PROT_EXEC 或 PROT_NONE。这可能吗?似乎 mprotect 只是保护
我正在开发一个并行应用程序(C,pthread)。我跟踪了系统调用,因为在某些时候我的并行性能很差。我的跟踪显示我的程序多次调用 mprotect() ... 足以显着降低我的程序速度。 我确实分配了
为了并发/并行GC,我感兴趣的是 mprotect 系统调用提供的内存顺序保证(即 mprotect 与多线程的行为或 mprotect 的内存模型)。我的问题是(假设没有编译器重新排序或有足够的编译
因此,我正在尝试检查是否可以使用 mprotect 正确更改对映射分配内存的访问权限。这就是我写的: #include #include #include #include void main
我正在尝试学习一些堆栈溢出技术并在其中使用 shellcode。我能够成功地使用一些基本的 shellcode。然后我开始在程序集中使用 exeve 并调用 ls -l ,再次成功。现在我正在尝试使用
我想获取 mprotect 处理程序中的当前程序计数器 (PC) 值。从那里我想将 PC 的值增加“n”条指令,以便程序跳过一些指令。我想为 linux 内核版本 3.0.1 做所有这些。关于我可以获
我是一名优秀的程序员,十分优秀!