Intel® Inspector User Guide for Linux* OS

ID 767796
Date 10/31/2024
Public
Document Table of Contents

Invalid Memory Access

Occurs when a read or write instruction references memory that is logically or physically invalid.

Problem type: Invalid memory access (Read)

Problem type: Invalid memory access (Write)

ID

Code Location

Description

1

Allocation site

If present, represents the location and associated call stack from which the memory block containing the offending address was allocated.

2

Deallocation site

If present, represents the location and associated call stack from which the memory block containing the offending address was deallocated.

The deallocation makes the access to the offending memory address logically invalid.

3

Read or Write

Represents the instruction and associated call stack responsible for the invalid access.

If no allocation or deallocation is associated with this problem, the memory address might be one of the following:

  • Logically invalid stack space (below the current stack pointer value)

  • Memory that is physically not allocated to the process

  • Memory that has been deallocated

NOTE:

The offset, if shown in the Code Locations pane, represents the byte offset into the allocated buffer where the Invalid memory access occurred.

C Examples

Heap example:

char *pStr = (char*) malloc(20);
free(pStr);
strcpy(pStr, "my string"); // Invalid write to deallocated memory

Stack example (set Analyze stack accesses to Yes when you configure the analysis):

void stackUnderrun()
{
   char array[10];
   strcpy(array, "my string");
   int len = strlen(array) - 1;
   while (array[len] != 'Z') // Will read memory outside of array
      len--;
}

Fortran Example

integer, allocatable :: i, b(:)
    
allocate( b(100))
    
! Body of Console1    
do i = 1, 100
    b(i) = i
end do
    
deallocate(b)
! Invalid write to deallocated memory    
b(1) = 1

Possible Correction Strategies

If

Do This

An invalid pointer dereference (corrupt or invalid value) occurs.

This is usually a logic error in a sequential algorithm.

For example, the pointer may increment past the end or decrement before the beginning of dynamically allocated memory. This pointer address value can be the address of memory that is marked invalid to access.

When dereferencing a pointer, ensure:

  • The pointer refers to a valid object.

  • Pointer arithmetic stays within the bounds of the dynamically allocated memory.

A stale reference to memory occurs.

You can easily recognize this situation if allocation and deallocation information is present.

For example, if a thread holds a pointer to the heap, and another thread releases the memory back to the heap, then all future accesses to this memory are invalid.

You need a clear concept of object ownership to ensure one thread does not mistakenly release an object back to the heap while another thread considers the pointer valid. Consider reference counting.

The Intel Inspector detects an invalid memory access when enhanced dangling pointer check is disabled.

Run another memory error analysis with enhanced dangling pointer check enabled to potentially report allocation and deallocation information.