如何准确触发访问冲突异常 [英] How exactly an access violation exception is triggered

查看:53
本文介绍了如何准确触发访问冲突异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,错误可能导致违反内存访问异常.

Sometimes bugs can cause memory access violation exception.

如何精确触发此异常?幕后工作机制是什么?

How exactly this exception is triggered? What mechanism works behind the scenes?

是否需要来自CPU(从哪个CPU开始?)/从操作系统(从哪个版本开始?)/从编译器(从哪个版本开始?)的支持

Does it need support from the CPU (starting at what CPU?) / from the OS (starting at what version?) / from the compiler (starting at what version?)

我想更好地理解一种具体情况:

One specific scenario I want to understand better:

以下代码可能会导致访问冲突异常.

The following code may cause an access violation exception.

TCHAR* czXXX= _T("ABCDEFG");
czXXX[0]= 'A';

我猜czXXX指向一个只读存储块,但是究竟会发生什么呢?

I guess czXXX points to a read-only memory block, but what exactly happens?

推荐答案

内存访问冲突是一个大话题:)

Memory access violations are a large topic :)

计算机系统中的信息保护(始于1973年:)采用 segments 的机制,其中为进程分配了 base bound ;任何尝试访问超出 base:base + bound 范围的内存都意味着该程序做了一些愚蠢的事情,应该被杀死.

The Protection of Information in Computer Systems (from 1973 :) lays out of a mechanism of segments, where processes are allocated a base and a bound; any attempt to access memory outside the range base:base+bound meant the program had done something silly and should be killed.

80x86系列处理器实现了基本的网段支持,并且 GEMSOS 安全性内核是基于此机制的A1认证的操作系统内核.

The 80x86 line of processors implement basic segment support, and the GEMSOS security kernel is an A1-certified operating system kernel based on this mechanism.

但是细分市场不是很动态,几乎所有现代操作系统都是分页系统,内存中的页面不可用.这取决于具有 MMU 的CPU,该内存管理单元检查所有内存访问以获取正确的特权.以及是否存在正确的内存映射.当某个进程尝试访问当前未映射到RAM的内存时,MMU会向CPU发出信号,表明发生了故障,并且CPU暂停了该进程以从磁盘加载请求的内存页.(或者,如果不应为该进程映射内存,则说它尝试访问 0x0 或某些尚未使用 mmap 或类似内存映射的随机内存位置分配原语,它会杀死进程.)

But segments aren't very dynamic, and almost all modern operating systems are paging systems, that page in memory when it isn't available. This relies on the CPU having an MMU, memory management unit, that checks all memory accesses for correct privileges and presence/absence of the correct memory mapping. When a process tries to access memory that isn't currently mapped into RAM, the MMU signals the CPU that a fault has occurred, and the CPU suspends the process to load the requested memory page from disk. (Or, if the memory should not be mapped for the process, say it tries to access 0x0 or some random memory location that hasn't been mapped with mmap or similar memory allocating primitives, it kills the process.)

英特尔的80386是第一款支持分页的英特尔芯片,这就是Windows 3.1的"386增强模式"比286模式好得多的原因.

Intel's 80386 was the first Intel chip to support paging, which is why Windows 3.1's "386 Enchanced Mode" was so much better than the 286 mode.

编译器并没有真正参与其中,但是CPU,MMU和操作系统内核必须都可以协同工作.

Compilers aren't really involved, but the CPU, MMU, and operating system kernel must all work together.

这篇关于如何准确触发访问冲突异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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