You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@stdcxx.apache.org by Farid Zaripov <Fa...@kyiv.vdiweb.com> on 2006/07/07 16:13:23 UTC

Environment for the test of the string functions memory overrun with example

tests/include/rw_test_overrun.h
tests/src/rw_test_overrun.cpp
tests/strings/rwmatch_test.cpp

   rw_test_overrun.h contains declaration of the class test_string and 
#defines used for testing for the memory overrun.
   rw_test_overrun.cpp contains definition of the class test_string and 
static utility functions.

   rwmatch_test.cpp - example of using test_string in tests.

   Main idea of the test is create string buffer, protected on the end, 
and pass this buffer to the function which is tested to the correctness 
memory addressing. Also test_string class contains method protect which 
can used for check that the any function fun (const char * str) really 
doen't modify the content of the string (i.e fun accesses the str only 
for read; detected even non-modifying writes like str[0] = str[0]).

Farid.

Re: Environment for the test of the string functions memory overrun with example

Posted by Martin Sebor <se...@roguewave.com>.
Farid Zaripov wrote:
> tests/include/rw_test_overrun.h
> tests/src/rw_test_overrun.cpp
> tests/strings/rwmatch_test.cpp
> 
>   rw_test_overrun.h contains declaration of the class test_string and 
> #defines used for testing for the memory overrun.
>   rw_test_overrun.cpp contains definition of the class test_string and 
> static utility functions.

Cool! I think this could be useful as a general memory allocator
for the test suite, and might even take place of (or at least
replace the implementation of) the replacement operator new (in
new.cpp) that we've been having problems with on some platforms
(AIX and Windows).

To make it possible I think a more generic and C-ish interface
might be preferable. I.e., instead of a class, how about just
a pair of malloc and free-like functions, say

     enum {
         RW_PROT_READ  = 1 << 0,
         RW_PROT_WRITE = 1 << 1,
         RW_PROT_RDWR  = RW_PROT_READ | RW_PROT_WRITE,
         RW_PROT_EXEC  = 1 << 2,
     };
     void* rw_alloc(size_t nbytes, int flags = -1);

and

     void rw_free(void *addr);

with flags specifying the kind of memory to use (-1 for ordinary,
otherwise protected/guarded) and the protection (read, write, and
execute).

This way we could use the interface to allocate any type of storage,
not just char*. We could also use it as the general allocator for
the driver (i.e., instead of malloc() or operator new) and debug
any driver issues. We would probably want to be able to control
this facility using some environment variable in addition to
the function argument so that by simply setting the environment
we could enable strict checking without having to recompile any
files.

Btw., if you haven't yet, you might want to take a look at the code
in src/memattr.cpp and tests/src/new.cpp. You might it helpful in
working on this (e.g., rw_free() shouldn't crash even when passed
an invalid pointer or a valid pointer to a corrupt chunk of memory).

Martin