Changeset 27269 in vbox for trunk/src/VBox/Runtime
- Timestamp:
- Mar 11, 2010 10:15:46 AM (15 years ago)
- Location:
- trunk/src/VBox/Runtime/testcase
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/testcase/Makefile.kmk
r26824 r27269 5 5 6 6 # 7 # Copyright (C) 2006-20 09Sun Microsystems, Inc.7 # Copyright (C) 2006-2010 Sun Microsystems, Inc. 8 8 # 9 9 # This file is part of VirtualBox Open Source Edition (OSE), as … … 80 80 tstLog \ 81 81 tstMemAutoPtr \ 82 tstRTMemEf \ 82 83 tstRTMemCache \ 83 84 tstRTMemPool \ … … 322 323 tstMemAutoPtr_SOURCES = tstMemAutoPtr.cpp 323 324 325 tstRTMemEf_TEMPLATE = VBOXR3TSTEXE 326 tstRTMemEf_SOURCES = tstRTMemEf.cpp 327 324 328 tstRTMemCache_TEMPLATE = VBOXR3TSTEXE 325 329 tstRTMemCache_SOURCES = tstRTMemCache.cpp -
trunk/src/VBox/Runtime/testcase/tstRTMemEf.cpp
r27244 r27269 1 1 /* $Id$ */ 2 2 /** @file 3 * IPRT - Testcase the RTMemAutoPtr template.3 * IPRT - Testcase for the RTMemEf* functions. 4 4 */ 5 5 6 6 /* 7 * Copyright (C) 20 08Sun Microsystems, Inc.7 * Copyright (C) 2010 Sun Microsystems, Inc. 8 8 * 9 9 * This file is part of VirtualBox Open Source Edition (OSE), as … … 36 36 #include <iprt/initterm.h> 37 37 #include <iprt/string.h> 38 #include <iprt/rand.h>39 40 41 /*******************************************************************************42 * Structures and Typedefs *43 *******************************************************************************/44 typedef struct TSTMEMAUTOPTRSTRUCT45 {46 uint32_t a;47 uint32_t b;48 uint32_t c;49 } TSTMEMAUTOPTRSTRUCT;50 38 51 39 … … 53 41 * Global Variables * 54 42 *******************************************************************************/ 55 #ifndef TST_MEM_AUTO_PTR_ONLY_DISAS56 43 static unsigned g_cErrors = 0; 57 static unsigned g_cFrees;58 #endif59 44 60 45 61 /* 62 * Feel free to inspect with gdb / objdump / whatever / g++ -fverbose-asm in 63 * a release build and compare with tstMemAutoPtrDisas1PureC. 64 */ 65 extern "C" int tstMemAutoPtrDisas1(void **ppv) 46 static int tstMemAllocEfAccess() 66 47 { 67 RTMemAutoPtr<TSTMEMAUTOPTRSTRUCT> Handle(1); 68 if (!Handle) 69 { 70 Handle->a = RTRandU32(); 71 if (Handle->a < UINT32_MAX / 2) 72 { 73 *ppv = Handle.release(); 74 return VINF_SUCCESS; 75 } 76 } 77 return VERR_TRY_AGAIN; 48 int32_t v; 49 50 /* Trivial alloc fence test - allocate a single word and access both 51 * the word after the allocated block and the word before. One of them 52 * will crash no matter whether the fence is at the bottom or on top. */ 53 int32_t *p = (int32_t *)RTMemEfAlloc(sizeof(int32_t)); 54 RTPrintf("tstRTMemAllocEfAccess: allocated int32_t at %#p\n"); 55 RTPrintf("tstRTMemAllocEfAccess: triggering buffer overrun...\n"); 56 v = *(p + 1); 57 RTPrintf("tstRTMemAllocEfAccess: triggering buffer underrun...\n"); 58 v = *(p - 1); 59 60 /* Reaching this is a severe error. */ 61 return VERR_GENERAL_FAILURE; 78 62 } 79 80 /*81 * For comparing to tstMemAutoPtrDisas1.82 */83 extern "C" int tstMemAutoPtrDisas1PureC(void **ppv)84 {85 TSTMEMAUTOPTRSTRUCT *pHandle = (TSTMEMAUTOPTRSTRUCT *)RTMemRealloc(NULL, sizeof(*pHandle));86 if (pHandle)87 {88 pHandle->a = RTRandU32();89 if (pHandle->a < UINT32_MAX / 2)90 {91 *ppv = pHandle;92 return VINF_SUCCESS;93 }94 RTMemFree(pHandle);95 }96 return VERR_TRY_AGAIN;97 }98 99 100 #ifndef TST_MEM_AUTO_PTR_ONLY_DISAS101 102 template <class T>103 void tstMemAutoPtrDestructorCounter(T *aMem)104 {105 if (aMem == NULL)106 {107 RTPrintf("tstMemAutoPtr(%d): Destructor called with NILL handle!\n");108 g_cErrors++;109 }110 else if (!VALID_PTR(aMem))111 {112 RTPrintf("tstMemAutoPtr(%d): Destructor called with a bad handle %p\n", aMem);113 g_cErrors++;114 }115 RTMemEfFree(aMem);116 g_cFrees++;117 }118 119 120 void *tstMemAutoPtrAllocatorNoZero(void *pvOld, size_t cbNew)121 {122 void *pvNew = RTMemRealloc(pvOld, cbNew);123 if (pvNew)124 memset(pvNew, 0xfe, cbNew);125 return pvNew;126 }127 128 63 129 64 int main() 130 65 { 131 66 RTR3Init(); 132 RTPrintf("tst MemAutoPtr: TESTING...\n");67 RTPrintf("tstRTMemEf: TESTING...\n"); 133 68 134 69 #define CHECK_EXPR(expr) \ 135 do { bool const f = !!(expr); if (!f) { RTPrintf("tst MemAutoPtr(%d): %s!\n", __LINE__, #expr); g_cErrors++; } } while (0)70 do { bool const f = !!(expr); if (!f) { RTPrintf("tstRTMemEf(%d): %s!\n", __LINE__, #expr); g_cErrors++; } } while (0) 136 71 137 72 /* … … 139 74 */ 140 75 { 141 RTMemAutoPtr<char> NilObj; 142 CHECK_EXPR(!NilObj); 143 CHECK_EXPR(NilObj.get() == NULL); 144 CHECK_EXPR(NilObj.release() == NULL); 145 NilObj.reset(); 146 } 147 148 { 149 RTMemAutoPtr<char> Alloc(10); 150 CHECK_EXPR(Alloc.get() != NULL); 151 char *pch = Alloc.release(); 152 CHECK_EXPR(pch != NULL); 153 CHECK_EXPR(Alloc.get() == NULL); 154 155 RTMemAutoPtr<char> Manage(pch); 156 CHECK_EXPR(Manage.get() == pch); 157 CHECK_EXPR(&Manage[0] == pch); 158 CHECK_EXPR(&Manage[1] == &pch[1]); 159 CHECK_EXPR(&Manage[9] == &pch[9]); 160 } 161 162 /* 163 * Use the electric fence memory API to check alternative template 164 * arguments and also check some subscript / reference limit thing. 165 */ 166 { 167 RTMemAutoPtr<char, RTMemEfAutoFree<char>, RTMemEfRealloc> Electric(10); 168 CHECK_EXPR(Electric.get() != NULL); 169 Electric[0] = '0'; 170 CHECK_EXPR(Electric[0] == '0'); 171 CHECK_EXPR(*Electric == '0'); 172 //CHECK_EXPR(Electric == '0'); 173 Electric[9] = '1'; 174 CHECK_EXPR(Electric[9] == '1'); 175 /* Electric[10] = '2'; - this will crash (of course) */ 176 } 177 178 /* 179 * Check that memory is actually free when it should be and isn't when it shouldn't. 180 * Use the electric heap to get some extra checks. 181 */ 182 g_cFrees = 0; 183 { 184 RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> FreeIt(128); 185 FreeIt[127] = '0'; 186 } 187 CHECK_EXPR(g_cFrees == 1); 188 189 g_cFrees = 0; 190 { 191 RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> FreeIt2(128); 192 FreeIt2[127] = '1'; 193 FreeIt2.reset(); 194 FreeIt2.alloc(128); 195 FreeIt2[127] = '2'; 196 FreeIt2.reset(FreeIt2.get()); /* this one is weird, but it's how things works... */ 197 } 198 CHECK_EXPR(g_cFrees == 2); 199 200 g_cFrees = 0; 201 { 202 RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> DontFreeIt(256); 203 DontFreeIt[255] = '0'; 204 RTMemEfFree(DontFreeIt.release()); 205 } 206 CHECK_EXPR(g_cFrees == 0); 207 208 g_cFrees = 0; 209 { 210 RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> FreeIt3(128); 211 FreeIt3[127] = '0'; 212 CHECK_EXPR(FreeIt3.realloc(128)); 213 FreeIt3[127] = '0'; 214 CHECK_EXPR(FreeIt3.realloc(256)); 215 FreeIt3[255] = '0'; 216 CHECK_EXPR(FreeIt3.realloc(64)); 217 FreeIt3[63] = '0'; 218 CHECK_EXPR(FreeIt3.realloc(32)); 219 FreeIt3[31] = '0'; 220 } 221 CHECK_EXPR(g_cFrees == 1); 222 223 g_cFrees = 0; 224 { 225 RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> FreeIt4; 226 CHECK_EXPR(FreeIt4.alloc(123)); 227 CHECK_EXPR(FreeIt4.realloc(543)); 228 FreeIt4 = (char *)NULL; 229 CHECK_EXPR(FreeIt4.get() == NULL); 230 } 231 CHECK_EXPR(g_cFrees == 1); 232 233 /* 234 * Check the ->, [] and * (unary) operators with some useful struct. 235 */ 236 { 237 RTMemAutoPtr<TSTMEMAUTOPTRSTRUCT> Struct1(1); 238 Struct1->a = 0x11223344; 239 Struct1->b = 0x55667788; 240 Struct1->c = 0x99aabbcc; 241 CHECK_EXPR(Struct1->a == 0x11223344); 242 CHECK_EXPR(Struct1->b == 0x55667788); 243 CHECK_EXPR(Struct1->c == 0x99aabbcc); 244 245 Struct1[0].a = 0x11223344; 246 Struct1[0].b = 0x55667788; 247 Struct1[0].c = 0x99aabbcc; 248 CHECK_EXPR(Struct1[0].a == 0x11223344); 249 CHECK_EXPR(Struct1[0].b == 0x55667788); 250 CHECK_EXPR(Struct1[0].c == 0x99aabbcc); 251 252 (*Struct1).a = 0x11223344; 253 (*Struct1).b = 0x55667788; 254 (*Struct1).c = 0x99aabbcc; 255 CHECK_EXPR((*Struct1).a == 0x11223344); 256 CHECK_EXPR((*Struct1).b == 0x55667788); 257 CHECK_EXPR((*Struct1).c == 0x99aabbcc); 258 259 /* since at it... */ 260 Struct1.get()->a = 0x11223344; 261 Struct1.get()->b = 0x55667788; 262 Struct1.get()->c = 0x99aabbcc; 263 CHECK_EXPR(Struct1.get()->a == 0x11223344); 264 CHECK_EXPR(Struct1.get()->b == 0x55667788); 265 CHECK_EXPR(Struct1.get()->c == 0x99aabbcc); 266 } 267 268 /* 269 * Check the zeroing of memory. 270 */ 271 { 272 RTMemAutoPtr<uint64_t, RTMemAutoDestructor<uint64_t>, tstMemAutoPtrAllocatorNoZero> Zeroed1(1, true); 273 CHECK_EXPR(*Zeroed1 == 0); 274 } 275 276 { 277 RTMemAutoPtr<uint64_t, RTMemAutoDestructor<uint64_t>, tstMemAutoPtrAllocatorNoZero> Zeroed2; 278 Zeroed2.alloc(5, true); 279 CHECK_EXPR(Zeroed2[0] == 0); 280 CHECK_EXPR(Zeroed2[1] == 0); 281 CHECK_EXPR(Zeroed2[2] == 0); 282 CHECK_EXPR(Zeroed2[3] == 0); 283 CHECK_EXPR(Zeroed2[4] == 0); 76 CHECK_EXPR(tstMemAllocEfAccess()); 284 77 } 285 78 … … 293 86 return !!g_cErrors; 294 87 } 295 #endif /* TST_MEM_AUTO_PTR_ONLY_DISAS */
Note:
See TracChangeset
for help on using the changeset viewer.