LINUX.ORG.RU

how to track memory access errors at runtime?


0

0

I have an application (C++) where I'd like to add the facility to track memory access errors (like access to already freed memory, read or write beyond the bounds of a memory allocation etc.) and handle these errors at runtime (something like try{}catch() blocks).

Are there any ready libraries to handle this?

anonymous

I have looked at ElectricFence but it is useful only when one debugs his program. I need something I could link with my app and track all error accesses at runtime in manner as with try{}catch(){}.

anonymous
()

You cannot generally do this without severe performance penalties, and I doubt it is even generally possible (i.e. with code like

char *i = "foo";

for(char* j =i; *j != "a"; ++j) { /* try to do smth */ }

and other C-legacy constructs).

You may approach you goal by banning raw pointers, C-string and arrays; and using smart pointers (that are designed to throw an exception when improperly dereferenced) and containers everywhere. It will cost you both in performance and in development complexity, I think.

In practice, I believe, writing a comprehensive test-suite (80%+ test coverage) and running it in valgrind or ElectricFence will detect most memory-management errors (save possibly the ones caused by race condition in multithreaded environment).

Oh, one last thing: this is a Russian language forum. Sure you can read Russian - you posted here, after all; why can't you use untransliteration service or an "online keyboard" (i.e. the one at http://www.yandex.ru/ or a better one somewhere)?

anonymous
()
Ответ на: комментарий от anonymous

Ok, then I'll stick with ElectricFence. Thanks.

> Oh, one last thing: this is a Russian language forum. Sure you can read Russian - you posted here, after all; why can't you use untransliteration service or an "online keyboard" (i.e. the one at http://www.yandex.ru/ or a better one somewhere)?

lenivo shelkat po ekrannoi klaviature :)

anonymous
()
Ответ на: комментарий от anonymous

It could help:

#include <stdio.h>
#include <efence.h>
#include <signal.h>

void catch_me(int sig)
{
  puts ("Exception");
}

int main()
{
  char *bad = "test";
  signal (SIGILL, catch_me);
  signal (SIGSEGV, catch_me);
  free (bad);
  bad[12] = 23;
  return 0;
}

kmeaw ★★★
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.