为什么系统调用返回 EFAULT 而不是发送段错误? [英] Why do system calls return EFAULT instead of sending a segfault?

查看:81
本文介绍了为什么系统调用返回 EFAULT 而不是发送段错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

明确地说,这是一个设计而不是一个实现问题

To be clear, this is a design rather than an implementation question

我想知道为什么 POSIX 会出现这种行为的原因.POSIX 系统调用在给定无效内存位置时返回 EFAULT,而不是使用户空间程序崩溃(通过发送 sigsegv),这使得它们的行为与用户空间函数不一致.

I want to know the rationale behind why POSIX behaves this way. POSIX system calls when given an invalid memory location return EFAULT rather than crashing the userspace program (by sending a sigsegv), which makes their behavior inconsistent with userspace functions.

为什么?这不只是隐藏内存错误吗?这是历史错误还是有充分的理由?

Why? Doesn't this just hide memory bugs? Is it a historical mistake or is there a good reason for it?

推荐答案

因为系统调用是由内核执行的,而不是由用户程序执行---当系统调用发生时,用户进程暂停并等待内核执行结束.

Because system calls are executed by the kernel, not by the user program --- when the system call occurs, the user process halts and waits for the kernel to finish.

当然,内核本身不允许出现段错误,因此它必须手动检查用户进程提供的所有地址区域.如果其中一项检查失败,系统调用将失败并显示 EFAULT.所以在这种情况下,段错误实际上并没有发生——内核显式检查以确保所有地址都有效避免了它.因此,不发送信号是有道理的.

The kernel itself, of course, isn't allowed to seg fault, so it has to manually check all the address areas the user process gives it. If one of these checks fails, the system call fails with EFAULT. So in this situation a segmentation fault hasn't actually happening --- it's been avoided by the kernel explicitly checking to make sure all the addresses are valid. Hence it makes sense that no signal is sent.

此外,如果发送了信号,内核将无法将有意义的程序计数器附加到该信号上,当系统调用执行时,用户进程实际上并未执行跑步.这意味着用户进程将无法产生像样的诊断信息、重新启动失败的指令等.

In addition, if a signal were sent, there'd be no way the kernel could attach a meaningful program counter to the signal, the user process isn't actually executing when the system call is running. This means there'd be no way for the user process to produce decent diagnostics, restart the failed instruction, etc.

总结:主要是历史,但推理有实际逻辑.与 EINTR 一样,这并没有减少处理的烦躁.

To summarise: mostly historical, but there is actual logic to the reasoning. Like EINTR, this doesn't make it any less irritating to deal with.

这篇关于为什么系统调用返回 EFAULT 而不是发送段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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