Skip to main content
ckw089
Associate III
October 29, 2020
Question

Trap NULL pointer dereference

  • October 29, 2020
  • 4 replies
  • 1929 views

Hi,

We are using STM32H750 for the new product development. The IDE is IAR Embedded Workbench and the RTOS is uC/OS-II.

We found that it is OK to dereference NULL pointer on STM32H750.

Any way to trap NULL pointer dereference to generate an access violation error ?

rgds,

kc Wong

This topic has been closed for replies.

4 replies

TDK
Super User
October 29, 2020

Edit: Oh I misunderstood.

"If you feel a post has answered your question, please click ""Accept as Solution""."
ckw089
ckw089Author
Associate III
October 29, 2020

The address zero is mapped to the ITCM ram.

define symbol __ICFEDIT_region_ITCMR_start__      = 0x00000000;

define symbol __ICFEDIT_region_ITCMR_end__       = 0x0000FFFF;

define symbol __ICFEDIT_region_DTCMR_start__      = 0x20000000;

define symbol __ICFEDIT_region_DTCMR_end__       = 0x2001FFFF;

place in ITCMR_region      { section .textrw };

place in DTCMR_region      { section .dtcm_ram };

And the below code will just execute fine.

 int* p = 0;

 int x = *p;

  

 x = 0x12345678;

 *p = x;

Piranha
Principal III
October 31, 2020

Should be able to do it by configuring at least a small memory block at the beginning of ITCM with "no access" in MPU.

ckw089
ckw089Author
Associate III
November 1, 2020

Thanks Piranha, you are right.

In order to catch NULL pointer dereference defects, we shall configure the MPU to no access (or read-only) for the whole ITCMR_region (or the first 1024 bytes).

 MPU_InitStruct.Enable = MPU_REGION_ENABLE;

 MPU_InitStruct.BaseAddress = 0x00000000;

 MPU_InitStruct.Size = MPU_REGION_SIZE_64KB;

  MPU_InitStruct.AccessPermission = MPU_REGION_NO_ACCESS;

  MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;

 MPU_InitStruct.IsCacheable = MPU_ACCESS_CACHEABLE;

 MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;

 MPU_InitStruct.Number = MPU_REGION_NUMBER1;

 MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;

 MPU_InitStruct.SubRegionDisable = 0x00;

 MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;

 HAL_MPU_ConfigRegion(&MPU_InitStruct);