Monday, April 26, 2010

General program bugs

  • Buffer overflow

  • Use After free:
Volatile stack
For example, return a pointer on a stack.
  • Memory leak:
mtrace

  • Double free

  • Free unallocated memory
char* string="abcdef";
free string;

  • Heap overflow
p=malloc(strnlen(mystr,MAX_LENGTH)
strncpy(p,mystr,MAX_LENGTH)

use mcheck and mprobe MALLOC_CHECK_

  • Race condition
static int count=0;

count++;
(1) Read into register
(2) increase to one
(3) write back the new value into memory

  • Deadlock
PTHREAD_MUTEX_ERROR_CHECK

  • Compiler optimization
volatile: slow, only option for hardware parameters

memory barrier
asm volatile ("" : : : "memory")
write back all the data from register back to the memory. 

  • CPU optimization
hardware memory barrier

  • Signal Handler
cannot be blocked using locks

  • Tips for troubleshooting
Write to system log /var/log/messages
(bash) logger -p err "test"
syslog()

change syslog level by modify the /etc/syslog.conf


disable assert() using NDEBUG

Function to print backtrace
#include
int backtrace(int **buffer, int size)

POSIX threads trace toolkit

use ar to create static library and ln to create a dynamic library

No comments: