Forum: Projekte & Code Funktionen für Stack Overflow und Heap Overflow


von Erwin M. (nobodyy)


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

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.