| 1 |
/* |
| 2 |
* Copyright (c) 2011 by Hewlett-Packard Company. All rights reserved. |
| 3 |
* |
| 4 |
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 5 |
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 6 |
* |
| 7 |
* Permission is hereby granted to use or copy this program |
| 8 |
* for any purpose, provided the above notices are retained on all copies. |
| 9 |
* Permission to modify the code and to distribute modified code is granted, |
| 10 |
* provided the above notices are retained, and a notice that the code was |
| 11 |
* modified is included with the above copyright notice. |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
/* Test that objects reachable from an object allocated with */ |
| 16 |
/* GC_malloc_with_finalizer is not reclaimable before the finalizer */ |
| 17 |
/* is called. */ |
| 18 |
|
| 19 |
#include <stdlib.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
/* For GC_[P]THREADS */ |
| 25 |
# include "config.h" |
| 26 |
#endif |
| 27 |
|
| 28 |
#undef GC_NO_THREAD_REDIRECTS |
| 29 |
#include "gc_disclaim.h" |
| 30 |
|
| 31 |
#define NOT_GCBUILD |
| 32 |
#include "private/gc_priv.h" |
| 33 |
/* Redefine the standard rand() with a trivial (yet sufficient for */ |
| 34 |
/* the test purpose) implementation to avoid crashes inside rand() */ |
| 35 |
/* on some hosts (e.g. FreeBSD 13.0) when used concurrently. */ |
| 36 |
/* The standard specifies rand() as not a thread-safe API function. */ |
| 37 |
/* On other hosts (e.g. OpenBSD 7.3), use of the standard rand() */ |
| 38 |
/* causes "rand() may return deterministic values" warning. */ |
| 39 |
#undef rand |
| 40 |
static GC_RAND_STATE_T seed; /* concurrent update does not hurt the test */ |
| 41 |
#define rand() GC_RAND_NEXT(&seed) |
| 42 |
|
| 43 |
#define my_assert(e) \ |
| 44 |
if (!(e)) { \ |
| 45 |
fflush(stdout); \ |
| 46 |
fprintf(stderr, "Assertion failure, line %d: " #e "\n", __LINE__); \ |
| 47 |
exit(-1); \ |
| 48 |
} |
| 49 |
|
| 50 |
int memeq(void *s, int c, size_t len) |
| 51 |
{ |
| 52 |
while (len--) { |
| 53 |
if (*(char *)s != c) |
| 54 |
return 0; |
| 55 |
s = (char *)s + 1; |
| 56 |
} |
| 57 |
return 1; |
| 58 |
} |
| 59 |
|
| 60 |
void GC_CALLBACK misc_sizes_dct(void *obj, void *cd) |
| 61 |
{ |
| 62 |
unsigned log_size = *(unsigned char *)obj; |
| 63 |
size_t size; |
| 64 |
|
| 65 |
my_assert(log_size < sizeof(size_t) * 8); |
| 66 |
my_assert(cd == NULL); |
| 67 |
size = (size_t)1 << log_size; |
| 68 |
my_assert(memeq((char *)obj + 1, 0x56, size - 1)); |
| 69 |
} |
| 70 |
|
| 71 |
void test_misc_sizes(void) |
| 72 |
{ |
| 73 |
static const struct GC_finalizer_closure fc = { misc_sizes_dct, NULL }; |
| 74 |
int i; |
| 75 |
for (i = 1; i <= 20; ++i) { /* Up to 1 MiB. */ |
| 76 |
void *p = GC_finalized_malloc((size_t)1 << i, &fc); |
| 77 |
if (p == NULL) { |
| 78 |
fprintf(stderr, "Out of memory!\n"); |
| 79 |
exit(3); |
| 80 |
} |
| 81 |
my_assert(memeq(p, 0, (size_t)1 << i)); |
| 82 |
memset(p, 0x56, (size_t)1 << i); |
| 83 |
*(unsigned char *)p = (unsigned char)i; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
typedef struct pair_s *pair_t; |
| 88 |
|
| 89 |
struct pair_s { |
| 90 |
char magic[16]; |
| 91 |
int checksum; |
| 92 |
pair_t car; |
| 93 |
pair_t cdr; |
| 94 |
}; |
| 95 |
|
| 96 |
static const char * const pair_magic = "PAIR_MAGIC_BYTES"; |
| 97 |
|
| 98 |
int is_pair(pair_t p) |
| 99 |
{ |
| 100 |
return memcmp(p->magic, pair_magic, sizeof(p->magic)) == 0; |
| 101 |
} |
| 102 |
|
| 103 |
#define PTR_HASH(p) (GC_HIDE_POINTER(p) >> 4) |
| 104 |
|
| 105 |
void GC_CALLBACK pair_dct(void *obj, void *cd) |
| 106 |
{ |
| 107 |
pair_t p = (pair_t)obj; |
| 108 |
int checksum; |
| 109 |
|
| 110 |
my_assert(cd == (void *)PTR_HASH(p)); |
| 111 |
/* Check that obj and its car and cdr are not trashed. */ |
| 112 |
# ifdef DEBUG_DISCLAIM_DESTRUCT |
| 113 |
printf("Destruct %p: (car= %p, cdr= %p)\n", |
| 114 |
(void *)p, (void *)p->car, (void *)p->cdr); |
| 115 |
# endif |
| 116 |
my_assert(GC_base(obj)); |
| 117 |
my_assert(is_pair(p)); |
| 118 |
my_assert(!p->car || is_pair(p->car)); |
| 119 |
my_assert(!p->cdr || is_pair(p->cdr)); |
| 120 |
checksum = 782; |
| 121 |
if (p->car) checksum += p->car->checksum; |
| 122 |
if (p->cdr) checksum += p->cdr->checksum; |
| 123 |
my_assert(p->checksum == checksum); |
| 124 |
|
| 125 |
/* Invalidate it. */ |
| 126 |
memset(p->magic, '*', sizeof(p->magic)); |
| 127 |
p->checksum = 0; |
| 128 |
p->car = NULL; |
| 129 |
p->cdr = NULL; |
| 130 |
} |
| 131 |
|
| 132 |
pair_t |
| 133 |
pair_new(pair_t car, pair_t cdr) |
| 134 |
{ |
| 135 |
pair_t p; |
| 136 |
struct GC_finalizer_closure *pfc = |
| 137 |
GC_NEW_ATOMIC(struct GC_finalizer_closure); |
| 138 |
|
| 139 |
if (NULL == pfc) { |
| 140 |
fprintf(stderr, "Out of memory!\n"); |
| 141 |
exit(3); |
| 142 |
} |
| 143 |
pfc->proc = pair_dct; |
| 144 |
p = (pair_t)GC_finalized_malloc(sizeof(struct pair_s), pfc); |
| 145 |
if (p == NULL) { |
| 146 |
fprintf(stderr, "Out of memory!\n"); |
| 147 |
exit(3); |
| 148 |
} |
| 149 |
pfc->cd = (void *)PTR_HASH(p); |
| 150 |
my_assert(!is_pair(p)); |
| 151 |
my_assert(memeq(p, 0, sizeof(struct pair_s))); |
| 152 |
memcpy(p->magic, pair_magic, sizeof(p->magic)); |
| 153 |
p->checksum = 782 + (car? car->checksum : 0) + (cdr? cdr->checksum : 0); |
| 154 |
p->car = car; |
| 155 |
GC_ptr_store_and_dirty(&p->cdr, cdr); |
| 156 |
GC_reachable_here(car); |
| 157 |
# ifdef DEBUG_DISCLAIM_DESTRUCT |
| 158 |
printf("Construct %p: (car= %p, cdr= %p)\n", |
| 159 |
(void *)p, (void *)p->car, (void *)p->cdr); |
| 160 |
# endif |
| 161 |
return p; |
| 162 |
} |
| 163 |
|
| 164 |
void |
| 165 |
pair_check_rec(pair_t p) |
| 166 |
{ |
| 167 |
while (p) { |
| 168 |
int checksum = 782; |
| 169 |
if (p->car) checksum += p->car->checksum; |
| 170 |
if (p->cdr) checksum += p->cdr->checksum; |
| 171 |
my_assert(p->checksum == checksum); |
| 172 |
if (rand() % 2) |
| 173 |
p = p->car; |
| 174 |
else |
| 175 |
p = p->cdr; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
#ifdef GC_PTHREADS |
| 180 |
# ifndef NTHREADS |
| 181 |
# define NTHREADS 6 |
| 182 |
# endif |
| 183 |
# include <errno.h> /* for EAGAIN */ |
| 184 |
# include <pthread.h> |
| 185 |
#else |
| 186 |
# undef NTHREADS |
| 187 |
# define NTHREADS 1 |
| 188 |
#endif |
| 189 |
|
| 190 |
#define POP_SIZE 1000 |
| 191 |
#if NTHREADS > 1 |
| 192 |
# define MUTATE_CNT (2000000/NTHREADS) |
| 193 |
#else |
| 194 |
# define MUTATE_CNT 10000000 |
| 195 |
#endif |
| 196 |
#define GROW_LIMIT (MUTATE_CNT/10) |
| 197 |
|
| 198 |
void *test(void *data) |
| 199 |
{ |
| 200 |
int i; |
| 201 |
pair_t pop[POP_SIZE]; |
| 202 |
memset(pop, 0, sizeof(pop)); |
| 203 |
for (i = 0; i < MUTATE_CNT; ++i) { |
| 204 |
int t = rand() % POP_SIZE; |
| 205 |
int j; |
| 206 |
|
| 207 |
switch (rand() % (i > GROW_LIMIT? 5 : 3)) { |
| 208 |
case 0: case 3: |
| 209 |
if (pop[t]) |
| 210 |
pop[t] = pop[t]->car; |
| 211 |
break; |
| 212 |
case 1: case 4: |
| 213 |
if (pop[t]) |
| 214 |
pop[t] = pop[t]->cdr; |
| 215 |
break; |
| 216 |
case 2: |
| 217 |
j = rand() % POP_SIZE; |
| 218 |
pop[t] = pair_new(pop[j], pop[rand() % POP_SIZE]); |
| 219 |
break; |
| 220 |
} |
| 221 |
if (rand() % 8 == 1) |
| 222 |
pair_check_rec(pop[rand() % POP_SIZE]); |
| 223 |
} |
| 224 |
return data; |
| 225 |
} |
| 226 |
|
| 227 |
int main(void) |
| 228 |
{ |
| 229 |
# if NTHREADS > 1 |
| 230 |
pthread_t th[NTHREADS]; |
| 231 |
int i, n; |
| 232 |
# endif |
| 233 |
|
| 234 |
GC_set_all_interior_pointers(0); /* for a stricter test */ |
| 235 |
# ifdef TEST_MANUAL_VDB |
| 236 |
GC_set_manual_vdb_allowed(1); |
| 237 |
# endif |
| 238 |
GC_INIT(); |
| 239 |
GC_init_finalized_malloc(); |
| 240 |
# ifndef NO_INCREMENTAL |
| 241 |
GC_enable_incremental(); |
| 242 |
# endif |
| 243 |
if (GC_get_find_leak()) |
| 244 |
printf("This test program is not designed for leak detection mode\n"); |
| 245 |
|
| 246 |
test_misc_sizes(); |
| 247 |
|
| 248 |
# if NTHREADS > 1 |
| 249 |
printf("Threaded disclaim test.\n"); |
| 250 |
for (i = 0; i < NTHREADS; ++i) { |
| 251 |
int err = pthread_create(&th[i], NULL, test, NULL); |
| 252 |
if (err) { |
| 253 |
fprintf(stderr, "Failed to create thread #%d: %s\n", i, |
| 254 |
strerror(err)); |
| 255 |
if (i > 1 && EAGAIN == err) break; |
| 256 |
exit(1); |
| 257 |
} |
| 258 |
} |
| 259 |
n = i; |
| 260 |
for (i = 0; i < n; ++i) { |
| 261 |
int err = pthread_join(th[i], NULL); |
| 262 |
if (err) { |
| 263 |
fprintf(stderr, "Failed to join thread #%d: %s\n", i, |
| 264 |
strerror(err)); |
| 265 |
exit(69); |
| 266 |
} |
| 267 |
} |
| 268 |
# else |
| 269 |
printf("Unthreaded disclaim test.\n"); |
| 270 |
test(NULL); |
| 271 |
# endif |
| 272 |
return 0; |
| 273 |
} |