简体   繁体   English

在MSP430上,当我取消引用空指针时会发生什么?

[英]On MSP430, what will happen when I dereference a null pointer?

I know dereferencing a null pointer is undefined - but I would like to know what happens on a specific target - an MSP430. 我知道取消引用空指针是未定义的 - 但我想知道在特定目标上发生了什么 - 一个MSP430。

I don't have a board to load this on in front of me to test this out right now. 我现在没有一块板可以在我面前加载它来测试它。

What would happen if I did this (or similar)? 如果我这样做(或者类似的话)会发生什么?

int * foo = NULL;
(*foo)++; //Crash?

Location 0x0 is in the SFR range and is reserved. 位置0x0在SFR范围内并保留。

Would it generate a PUC/POR? 它会产生PUC / POR吗? Or would it silently "work"? 还是会默默地“工作”?

The assembly generated is 生成的程序集是

;int * foo = NULL;
clr.w   R15
;(*foo)++;
inc.w   R15

So location 0x0 is literally being incremented by 1. 因此,位置0x0实际上是递增1。

When I run this in the simulator I see the value at address 0x0 go from 0 to 1. I get no warnings in the debug log and the program exits normally. 当我在模拟器中运行它时,我看到地址0x0的值从0变为1.我在调试日志中没有警告,程序正常退出。

I am using the IAR EW430 compiler/assembler/simulator. 我正在使用IAR EW430编译器/汇编器/模拟器。

Not only writing and reading the address 0x0 will not cause a crash or a reboot, it actually a completely legal operation that is often used by MSP430 applications. 不仅写入和读取地址0x0不会导致崩溃或重新启动,它实际上是MSP430应用程序经常使用的完全合法的操作。

The initial portion or MSP430 memory map is reserved for I/O ports and control registers: http://en.wikipedia.org/wiki/TI_MSP430#MSP430_address_space 初始部分或MSP430存储器映射保留用于I / O端口和控制寄存器: http//en.wikipedia.org/wiki/TI_MSP430#MSP430_address_space

In particular, the control registers at 0x0 and subsequent addresses are: 特别是,0x0和后续地址的控制寄存器是:

 #define IE1_                  0x0000    /* Interrupt Enable 1 */
 #define IE2_                  0x0001    /* Interrupt Enable 2 */
 #define IFG1_                 0x0002    /* Interrupt Flag 1 */
 #define IFG2_                 0x0003    /* Interrupt Flag 2 */

So for example writing zero to that memory address by dereferencing a uint8_t * or uint16_t * pointer is going to disable interrupts. 因此,例如通过解除引用uint8_t *uint16_t *指针将零写入该内存地址将禁用中断。 Writing zero by dereferencing an uint32_t * it is also going to clear the flags. 通过解除引用uint32_t *来写零,它也将清除标志。 Incrementing the value of these registers does not make a lot of sense, but should be completely legal. 增加这些寄存器的值并没有多大意义,但应该是完全合法的。

At least this is the case on msp430 Series 1, Series 2 and Series 4. By checking the header files I was not able to find anything mapped to 0x0 on Series 5 (the interrupt control registers are mapped to region starting from 0x0100). 至少在msp430系列1,系列2和系列4上就是这种情况。通过检查头文件,我无法在系列5上找到映射到0x0的任何内容(中断控制寄存器映射到从0x0100开始的区域)。

So if you want to catch places in code where the NULL pointer is dereferenced, you're completely on your own. 因此,如果你想在代码中捕获NULL指针被解除引用的位置,那么你完全靠自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM