diff options
Diffstat (limited to 'libvtv/testsuite/libvtv.mempool.cc')
-rw-r--r-- | libvtv/testsuite/libvtv.mempool.cc/mempool.exp | 68 | ||||
-rw-r--r-- | libvtv/testsuite/libvtv.mempool.cc/mempool_negative.cc | 193 | ||||
-rw-r--r-- | libvtv/testsuite/libvtv.mempool.cc/mempool_positive.cc | 199 |
3 files changed, 460 insertions, 0 deletions
diff --git a/libvtv/testsuite/libvtv.mempool.cc/mempool.exp b/libvtv/testsuite/libvtv.mempool.cc/mempool.exp new file mode 100644 index 00000000000..9565eae711a --- /dev/null +++ b/libvtv/testsuite/libvtv.mempool.cc/mempool.exp @@ -0,0 +1,68 @@ +load_lib libvtv-dg.exp +load_gcc_lib gcc-dg.exp + +global VTV_FLAGS +set VTV_FLAGS [list {-O0} {-O2}] + +libvtv_init c++ + +set shlib_ext [get_shlib_extension] +set lang_link_flags "-shared-libgcc -lstdc++" +set lang_test_file_found 0 +set lang_library_path "../libstdc++-v3/src/.libs" + +dg-init + +set blddir [lookfor_file [get_multilibs] libvtv] + +# Find the correct libstdc++ library to use. + +if { $blddir != "" } { + # Look for a static libstdc++ first. + if [file exists "${blddir}/${lang_library_path}/libstdc++.a"] { + set lang_test_file "${lang_library_path}/libstdc++.a" + set lang_test_file_found 1 + # We may have a shared only build, so look for a shared libstdc++. + } elseif [file exists "${blddir}/${lang_library_path}/libstdc++.${shlib_ext}"] { + set lang_test_file "${lang_library_path}/libstdc++.${shlib_ext}" + set lang_test_file_found 1 + } else { + puts "looking for ${blddir}/${lang_library_path}/libstdc++.${shlib_ext}" + puts "No libstdc++ library found, will not execute c++ tests" + } +} elseif { [info exists GXX_UNDER_TEST] } { + set lang_test_file_found 1 + # Needs to exist for libvtv.exp. + set lang_test_file "" +} else { + puts "GXX_UNDER_TEST not defined, will not execute c++ tests" +} + + +global srcdir + +if { $lang_test_file_found } { + # Set the path for finding libstdc++. + if { $blddir != "" } { + set ld_library_path "$always_ld_library_path:${blddir}/${lang_library_path}" + } else { + set ld_library_path "$always_ld_library_path" + } + append ld_library_path [gcc-set-multilib-library-path $GCC_UNDER_TEST] + set_ld_library_path_env_vars + + # Make sure we can find the libstdc++ header files. + set flags_file "${blddir}/../libstdc++-v3/scripts/testsuite_flags" + if { [file exists $flags_file] } { + set libstdcxx_includes [exec sh $flags_file --build-includes] + } else { + set libstdcxx_includes "" + } + + # Run the tests with -fvtable-verify=std + foreach flags $VTV_FLAGS { + foreach srcfile [lsort [glob -nocomplain ${srcdir}/libvtv.mempool.cc/*.cc]] { + dg-runtest $srcfile "$flags -fvtable-verify=std" $libstdcxx_includes + } + } +} diff --git a/libvtv/testsuite/libvtv.mempool.cc/mempool_negative.cc b/libvtv/testsuite/libvtv.mempool.cc/mempool_negative.cc new file mode 100644 index 00000000000..2d5e1d0b531 --- /dev/null +++ b/libvtv/testsuite/libvtv.mempool.cc/mempool_negative.cc @@ -0,0 +1,193 @@ +#include <string.h> +#include <assert.h> +#include <signal.h> +#include <stdio.h> +#include <setjmp.h> + +#include "vtv_malloc.h" +#include "../../../include/vtv-change-permission.h" + +volatile static int signal_count = 0; + +sigjmp_buf before_segv; + +unsigned int vtv_debug = 0; + +static void +handler(int sig, siginfo_t *si, void *unused) +{ + signal_count++; + /* You are not supposed to longjmp out of a signal handler but it seems + to work for this test case and it simplifies it */ + siglongjmp(before_segv, 1); +} + +/* Try to modify the memory pointed by "s" but dont actually change the values. + Assumes and verifies the memory to be modified is mprotected */ +void mempoke(void * s, size_t n) +{ + volatile char * p = (char *)s; + int ret; + + signal_count = 0; + ret = sigsetjmp(before_segv, 1); + if (ret == 0) + p[0] = p[0]; + + assert(ret == 1 && signal_count == 1); + + ret = sigsetjmp(before_segv, 1); + if (ret == 0) + p[n - 1] = p[n - 1]; + + assert(ret == 1 && signal_count == 2); +} + +int main() +{ + char * ptr; + int size; + + /* Set up handler for SIGSEGV. */ + struct sigaction sa; + sa.sa_flags = SA_SIGINFO; + sigemptyset(&sa.sa_mask); + sa.sa_sigaction = handler; + if (sigaction(SIGSEGV, &sa, NULL) == -1) + assert(0); + + /* Make the 'bookkeeping' vars read-write. */ + __VLTChangePermission (__VLTP_READ_WRITE); + __vtv_malloc_init(); + + size = 10; + + /* Verify not writable after unprotect */ + __vtv_malloc_unprotect(); + ptr = (char *)__vtv_malloc(size); + memset(ptr, 'a', size); + __vtv_malloc_protect(); + mempoke(ptr, size); + __vtv_free(ptr); + + /* verify not-writable after protect, unprotect */ + __vtv_malloc_unprotect(); + ptr = (char *)__vtv_malloc(size); + memset(ptr, 'a', size); + __vtv_malloc_protect(); + __vtv_malloc_unprotect(); + memset(ptr, 'a', size); + assert(ptr[size - 1] == 'a'); + __vtv_malloc_protect(); + assert(ptr[size - 1] == 'a'); + mempoke(ptr,size); + __vtv_free(ptr); + + /* Allocate a bunch of small objects. + Make sure the alignment is correct. + Verify data has not been corrupted. + Make sure the data cannot modified */ + { + int s; + for (s = 3; s < 28; s += 3) + { + size = s; + { + int i; + #define ITERS 1000 + char * ptrs[ITERS]; + + __vtv_malloc_unprotect(); + for (i = 0; i < ITERS; i++) + { + ptr = (char *)__vtv_malloc(size); + assert(((long)ptr & VTV_ALIGNMENT_MASK) == 0); + memset(ptr, (i & 127), size); + assert(ptr[size - 1] == (i & 127)); + ptrs[i] = ptr; + } + __vtv_malloc_protect(); + + for (i = 0; i < ITERS; i++) + mempoke(ptrs[i], size); + + __vtv_malloc_unprotect(); + for (i = 0; i < ITERS; i++) + __vtv_free(ptrs[i]); + __vtv_malloc_protect(); + } + } + } + + /* Allocate a bunch of medium size objects. + Make sure the alignment is correct. + Verify data has not been corrupted. + Try to modify the data to verify everything gets unprotected */ + { + int s; + for (s = 501; s < 2500; s += 91) + { + size = s; + { + int i; + #define ITERS2 100 + char * ptrs[ITERS2]; + + __vtv_malloc_unprotect(); + for (i = 0; i < ITERS2; i++) + { + + ptr = (char *)__vtv_malloc(size); + assert(((long)ptr & VTV_ALIGNMENT_MASK) == 0); + memset(ptr, i & 127, size); + assert(ptr[size - 1] == i & 127); + ptrs[i] = ptr; + } + __vtv_malloc_protect(); + + for (i = 0; i < ITERS2; i++) + mempoke(ptrs[i], size); + + __vtv_malloc_unprotect(); + for (i = 0; i < ITERS2; i++) + __vtv_free(ptrs[i]); + __vtv_malloc_protect(); + } + } + } + + /* Allocate a bunch of medium size objects. Make sure the alignment is correct */ + { + int s; + for (s = 3001; s < 15000; s += 307) + { + size = s; + { + int i; + #define ITERS3 50 + char * ptrs[ITERS3]; + + __vtv_malloc_unprotect(); + for (i = 0; i < ITERS3; i++) + { + ptr = (char *)__vtv_malloc(size); + assert(((long)ptr & VTV_ALIGNMENT_MASK) == 0); + memset(ptr, i & 127, size); + assert(ptr[size - 1] == i & 127); + ptrs[i] = ptr; + } + __vtv_malloc_protect(); + + for (i = 0; i < ITERS3; i++) + mempoke(ptrs[i], size); + + __vtv_malloc_unprotect(); + for (i = 0; i < ITERS3; i++) + __vtv_free(ptrs[i]); + __vtv_malloc_protect(); + } + } + } + + return 0; +} diff --git a/libvtv/testsuite/libvtv.mempool.cc/mempool_positive.cc b/libvtv/testsuite/libvtv.mempool.cc/mempool_positive.cc new file mode 100644 index 00000000000..5e60df9b686 --- /dev/null +++ b/libvtv/testsuite/libvtv.mempool.cc/mempool_positive.cc @@ -0,0 +1,199 @@ +#include <string.h> +#include <assert.h> +#include <signal.h> +#include <stdio.h> + +#include "vtv_malloc.h" +#include "../../../include/vtv-change-permission.h" + +unsigned int vtv_debug = 0; + +static void +handler(int sig, siginfo_t *si, void *unused) +{ + printf("Got SIGSEGV at address: 0x%lx\n", + (long) si->si_addr); + exit(1); +} + +int memchk(const void * s, int c, size_t n) +{ + const char * p = (const char *)s; + for (; p < ((char *)s + n); p++) + if (*p != c) + return 1; + return 0; +} + +int main() +{ + char * ptr; + int size; + + /* Set up handler for SIGSEGV. In this test case, we should never hit any SIGSEGV */ + struct sigaction sa; + sa.sa_flags = SA_SIGINFO; + sigemptyset(&sa.sa_mask); + sa.sa_sigaction = handler; + if (sigaction(SIGSEGV, &sa, NULL) == -1) + assert(0); + + /* Make the 'bookkeeping' vars read-write. */ + __VLTChangePermission (__VLTP_READ_WRITE); + __vtv_malloc_init(); + + size = 10; + + /* Verify simple allocation and deallocation */ + __vtv_malloc_unprotect(); + ptr = (char *)__vtv_malloc(size); + __vtv_malloc_protect(); + __vtv_free(ptr); + + /* Verify writable after unprotect */ + __vtv_malloc_unprotect(); + ptr = (char *)__vtv_malloc(size); + memset(ptr, 'a', size); + __vtv_malloc_protect(); + __vtv_free(ptr); + + /* verify readable after protect */ + __vtv_malloc_unprotect(); + ptr = (char *)__vtv_malloc(size); + memset(ptr, 'a', size); + __vtv_malloc_protect(); + assert(ptr[size - 1] == 'a'); + __vtv_free(ptr); + + /* verify writable after protect, unprotect */ + __vtv_malloc_unprotect(); + ptr = (char *)__vtv_malloc(size); + memset(ptr, 'a', size); + __vtv_malloc_protect(); + __vtv_malloc_unprotect(); + memset(ptr, 'a', size); + assert(ptr[size - 1] == 'a'); + __vtv_malloc_protect(); + assert(ptr[size - 1] == 'a'); + __vtv_free(ptr); + + /* Allocate a bunch of small objects. + Make sure the alignment is correct. + Verify data has not been corrupted. + Try to modify the data to verify everything gets unprotected */ + { + int s; + for (s = 3; s < 28; s += 3) + { + size = s; + { + int i; + #define ITERS 1000 + char * ptrs[ITERS]; + + __vtv_malloc_unprotect(); + for (i = 0; i < ITERS; i++) + { + ptr = (char *)__vtv_malloc(size); + assert(((long)ptr & VTV_ALIGNMENT_MASK) == 0); + memset(ptr, (i & 127), size); + assert(ptr[size - 1] == (i & 127)); + ptrs[i] = ptr; + } + __vtv_malloc_protect(); + + __vtv_malloc_unprotect(); + for (i = 0; i < ITERS; i++) + { + if (memchk(ptrs[i], i & 127, size) != 0) + assert(0); + memset(ptrs[i], (i + 1) & 127, size); + if (memchk(ptrs[i], (i + 1) & 127, size) != 0) + assert(0); + __vtv_free(ptrs[i]); + } + __vtv_malloc_protect(); + } + } + } + + /* Allocate a bunch of medium size objects. + Make sure the alignment is correct. + Verify data has not been corrupted. + Try to modify the data to verify everything gets unprotected */ + { + int s; + for (s = 501; s < 2500; s += 91) + { + size = s; + { + int i; + #define ITERS2 100 + char * ptrs[ITERS2]; + + __vtv_malloc_unprotect(); + for (i = 0; i < ITERS2; i++) + { + + ptr = (char *)__vtv_malloc(size); + assert(((long)ptr & VTV_ALIGNMENT_MASK) == 0); + memset(ptr, i & 127, size); + assert(ptr[size - 1] == i & 127); + ptrs[i] = ptr; + } + __vtv_malloc_protect(); + + __vtv_malloc_unprotect(); + for (i = 0; i < ITERS2; i++) + { + if (memchk(ptrs[i], i & 127, size) != 0) + assert(0); + memset(ptrs[i], (i + 1) & 127, size); + if (memchk(ptrs[i], (i + 1) & 127, size) != 0) + assert(0); + __vtv_free(ptrs[i]); + } + __vtv_malloc_protect(); + } + } + } + + /* Allocate a bunch of medium size objects. Make sure the alignment is correct */ + { + int s; + for (s = 3001; s < 15000; s += 307) + { + size = s; + { + int i; + #define ITERS3 50 + char * ptrs[ITERS3]; + + __vtv_malloc_unprotect(); + for (i = 0; i < ITERS3; i++) + { + ptr = (char *)__vtv_malloc(size); + assert(((long)ptr & VTV_ALIGNMENT_MASK) == 0); + memset(ptr, i & 127, size); + assert(ptr[size - 1] == i & 127); + ptrs[i] = ptr; + } + __vtv_malloc_protect(); + + __vtv_malloc_unprotect(); + for (i = 0; i < ITERS3; i++) + { + if (memchk(ptrs[i], i & 127, size) != 0) + assert(0); + memset(ptrs[i], (i + 1) & 127, size); + if (memchk(ptrs[i], (i + 1) & 127, size) != 0) + assert(0); + __vtv_free(ptrs[i]); + } + __vtv_malloc_protect(); + } + } + } + + return 0; +} |