Funktionen für Stack Overflow und Heap Overflow

OP #2389379
Lesenswert?

Hier sind zwei kleine Funktionen für Stack Overflow und Heap Overflow.
Damit kann man gut testen ob die Maßnahmen dagegen wirken und was 
passiert wenn nicht. Damit sollte so ziemlich jeder Mikrocontroller 
hängen bleiben.


// Heap overflow function
int
heap_overflow(void)
{
  int i, *ptr_j;
  for(;;)
  {
    ptr_j = (int *)malloc (sizeof(int));
  i ^= *ptr_j; // use the memory to avoid to get optimized away
  }
  return i;
}


// Stack overflow function via ackermann function, to avoid getting
// optimized away.
// ackermann(4,2) makes about 10 to the power of 19729 recursions.
#define stack_overflow() ackermann( 4, 2)

// ackermann function, http://en.wikipedia.org/wiki/Ackermann_function.
// This is one of the simplest and earliest-discovered examples of a 
total
// computable function that is not primitive recursive.
long int
ackermann (const long int m, const long int n)
{
  if (0 == m)
    return (n + 1);
  if (0 == n)
    return (ackermann (m - 1, 1));
  else
    return (ackermann (m - 1, ackermann (m, n - 1)));
}                               // ackermann

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren