| 1 | #include <stdio.h> |
|---|
| 2 | #include "xcache.h" |
|---|
| 3 | #include "ext/standard/flock_compat.h" |
|---|
| 4 | #ifdef HAVE_SYS_FILE_H |
|---|
| 5 | # include <sys/file.h> |
|---|
| 6 | #endif |
|---|
| 7 | #include <sys/types.h> |
|---|
| 8 | #include <sys/stat.h> |
|---|
| 9 | #include <fcntl.h> |
|---|
| 10 | |
|---|
| 11 | #include "stack.h" |
|---|
| 12 | #include "xcache_globals.h" |
|---|
| 13 | #include "coverager.h" |
|---|
| 14 | #include "utils.h" |
|---|
| 15 | typedef HashTable *coverager_t; |
|---|
| 16 | #define PCOV_HEADER_MAGIC 0x564f4350 |
|---|
| 17 | |
|---|
| 18 | char *xc_coveragedump_dir = NULL; |
|---|
| 19 | static zend_compile_file_t *origin_compile_file; |
|---|
| 20 | |
|---|
| 21 | #undef DEBUG |
|---|
| 22 | /* dumper */ |
|---|
| 23 | static void xc_destroy_coverage(void *pDest) /* {{{ */ |
|---|
| 24 | { |
|---|
| 25 | coverager_t cov = *(coverager_t*) pDest; |
|---|
| 26 | #ifdef DEBUG |
|---|
| 27 | fprintf(stderr, "destroy %p\n", cov); |
|---|
| 28 | #endif |
|---|
| 29 | zend_hash_destroy(cov); |
|---|
| 30 | efree(cov); |
|---|
| 31 | } |
|---|
| 32 | /* }}} */ |
|---|
| 33 | void xcache_mkdirs_ex(char *root, int rootlen, char *path, int pathlen TSRMLS_DC) /* {{{ */ |
|---|
| 34 | { |
|---|
| 35 | char *fullpath; |
|---|
| 36 | struct stat st; |
|---|
| 37 | |
|---|
| 38 | #ifdef DEBUG |
|---|
| 39 | fprintf(stderr, "mkdirs %s %d %s %d\n", root, rootlen, path, pathlen); |
|---|
| 40 | #endif |
|---|
| 41 | fullpath = do_alloca(rootlen + pathlen + 1); |
|---|
| 42 | memcpy(fullpath, root, rootlen); |
|---|
| 43 | memcpy(fullpath + rootlen, path, pathlen); |
|---|
| 44 | fullpath[rootlen + pathlen] = '\0'; |
|---|
| 45 | |
|---|
| 46 | if (stat(fullpath, &st) != 0) { |
|---|
| 47 | char *chr; |
|---|
| 48 | |
|---|
| 49 | chr = strrchr(path, PHP_DIR_SEPARATOR); |
|---|
| 50 | if (chr && chr != path) { |
|---|
| 51 | *chr = '\0'; |
|---|
| 52 | xcache_mkdirs_ex(root, rootlen, path, chr - path TSRMLS_CC); |
|---|
| 53 | *chr = PHP_DIR_SEPARATOR; |
|---|
| 54 | } |
|---|
| 55 | #ifdef DEBUG |
|---|
| 56 | fprintf(stderr, "mkdir %s\n", fullpath); |
|---|
| 57 | #endif |
|---|
| 58 | #if PHP_MAJOR_VERSION > 5 |
|---|
| 59 | php_stream_mkdir(fullpath, 0700, REPORT_ERRORS, NULL); |
|---|
| 60 | #else |
|---|
| 61 | mkdir(fullpath, 0700); |
|---|
| 62 | #endif |
|---|
| 63 | } |
|---|
| 64 | free_alloca(fullpath); |
|---|
| 65 | } |
|---|
| 66 | /* }}} */ |
|---|
| 67 | static void xc_coverager_save_cov(char *srcfile, char *outfilename, coverager_t cov TSRMLS_DC) /* {{{ */ |
|---|
| 68 | { |
|---|
| 69 | long *buf = NULL, *p; |
|---|
| 70 | long covlines, *phits; |
|---|
| 71 | int fd = -1; |
|---|
| 72 | int size; |
|---|
| 73 | int newfile; |
|---|
| 74 | struct stat srcstat, outstat; |
|---|
| 75 | HashPosition pos; |
|---|
| 76 | char *contents = NULL; |
|---|
| 77 | long len; |
|---|
| 78 | |
|---|
| 79 | if (stat(srcfile, &srcstat) != 0) { |
|---|
| 80 | return; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | newfile = 0; |
|---|
| 84 | if (stat(outfilename, &outstat) != 0) { |
|---|
| 85 | newfile = 1; |
|---|
| 86 | } |
|---|
| 87 | else { |
|---|
| 88 | if (srcstat.st_mtime > outstat.st_mtime) { |
|---|
| 89 | newfile = 1; |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | fd = open(outfilename, O_RDWR | O_CREAT, 0600); |
|---|
| 94 | if (fd < 0) { |
|---|
| 95 | char *chr; |
|---|
| 96 | chr = strrchr(srcfile, PHP_DIR_SEPARATOR); |
|---|
| 97 | if (chr) { |
|---|
| 98 | *chr = '\0'; |
|---|
| 99 | xcache_mkdirs_ex(xc_coveragedump_dir, strlen(xc_coveragedump_dir), srcfile, chr - srcfile TSRMLS_CC); |
|---|
| 100 | *chr = PHP_DIR_SEPARATOR; |
|---|
| 101 | } |
|---|
| 102 | fd = open(outfilename, O_RDWR | O_CREAT, 0600); |
|---|
| 103 | if (fd < 0) { |
|---|
| 104 | goto bailout; |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | if (flock(fd, LOCK_EX) != SUCCESS) { |
|---|
| 108 | goto bailout; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | if (newfile) { |
|---|
| 112 | #ifdef DEBUG |
|---|
| 113 | fprintf(stderr, "new file\n"); |
|---|
| 114 | #endif |
|---|
| 115 | } |
|---|
| 116 | else if (outstat.st_size) { |
|---|
| 117 | len = outstat.st_size; |
|---|
| 118 | contents = emalloc(len); |
|---|
| 119 | if (read(fd, (void *) contents, len) != len) { |
|---|
| 120 | goto bailout; |
|---|
| 121 | } |
|---|
| 122 | #ifdef DEBUG |
|---|
| 123 | fprintf(stderr, "oldsize %d\n", (int) len); |
|---|
| 124 | #endif |
|---|
| 125 | do { |
|---|
| 126 | p = (long *) contents; |
|---|
| 127 | len -= sizeof(long); |
|---|
| 128 | if (len < 0) { |
|---|
| 129 | break; |
|---|
| 130 | } |
|---|
| 131 | if (*p++ != PCOV_HEADER_MAGIC) { |
|---|
| 132 | #ifdef DEBUG |
|---|
| 133 | fprintf(stderr, "wrong magic in file %s\n", outfilename); |
|---|
| 134 | #endif |
|---|
| 135 | break; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | p += 2; /* skip covliens */ |
|---|
| 139 | len -= sizeof(long) * 2; |
|---|
| 140 | if (len < 0) { |
|---|
| 141 | break; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | for (; len >= sizeof(long) * 2; len -= sizeof(long) * 2, p += 2) { |
|---|
| 145 | if (zend_hash_index_find(cov, p[0], (void**)&phits) == SUCCESS) { |
|---|
| 146 | if (p[1] <= 0) { |
|---|
| 147 | /* already marked */ |
|---|
| 148 | continue; |
|---|
| 149 | } |
|---|
| 150 | if (*phits > 0) { |
|---|
| 151 | p[1] += *phits; |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | zend_hash_index_update(cov, p[0], &p[1], sizeof(p[1]), NULL); |
|---|
| 155 | } |
|---|
| 156 | } while (0); |
|---|
| 157 | efree(contents); |
|---|
| 158 | contents = NULL; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | /* serialize */ |
|---|
| 163 | size = (zend_hash_num_elements(cov) + 1) * sizeof(long) * 2 + sizeof(long); |
|---|
| 164 | p = buf = emalloc(size); |
|---|
| 165 | *p++ = PCOV_HEADER_MAGIC; |
|---|
| 166 | p += 2; /* for covlines */ |
|---|
| 167 | covlines = 0; |
|---|
| 168 | |
|---|
| 169 | zend_hash_internal_pointer_reset_ex(cov, &pos); |
|---|
| 170 | while (zend_hash_get_current_data_ex(cov, (void**)&phits, &pos) == SUCCESS) { |
|---|
| 171 | *p++ = pos->h; |
|---|
| 172 | if (*phits <= 0) { |
|---|
| 173 | *p++ = 0; |
|---|
| 174 | } |
|---|
| 175 | else { |
|---|
| 176 | *p++ = *phits; |
|---|
| 177 | covlines ++; |
|---|
| 178 | } |
|---|
| 179 | zend_hash_move_forward_ex(cov, &pos); |
|---|
| 180 | } |
|---|
| 181 | p = buf + 1; |
|---|
| 182 | p[0] = 0; |
|---|
| 183 | p[1] = covlines; |
|---|
| 184 | |
|---|
| 185 | ftruncate(fd, 0); |
|---|
| 186 | lseek(fd, 0, SEEK_SET); |
|---|
| 187 | write(fd, (char *) buf, size); |
|---|
| 188 | |
|---|
| 189 | bailout: |
|---|
| 190 | if (contents) efree(contents); |
|---|
| 191 | if (fd >= 0) close(fd); |
|---|
| 192 | if (buf) efree(buf); |
|---|
| 193 | } |
|---|
| 194 | /* }}} */ |
|---|
| 195 | void xc_coverager_request_init(TSRMLS_D) /* {{{ */ |
|---|
| 196 | { |
|---|
| 197 | if (XG(coveragedumper)) { |
|---|
| 198 | XG(coverages) = emalloc(sizeof(HashTable)); |
|---|
| 199 | zend_hash_init(XG(coverages), 0, NULL, xc_destroy_coverage, 0); |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | /* }}} */ |
|---|
| 203 | void xc_coverager_request_shutdown(TSRMLS_D) /* {{{ */ |
|---|
| 204 | { |
|---|
| 205 | coverager_t *pcov; |
|---|
| 206 | zstr s; |
|---|
| 207 | char *outfilename; |
|---|
| 208 | int dumpdir_len, outfilelen, alloc_len = 0; |
|---|
| 209 | uint size; |
|---|
| 210 | |
|---|
| 211 | if (!XG(coverages)) { |
|---|
| 212 | return; |
|---|
| 213 | } |
|---|
| 214 | if (XG(coveragedumper)) { |
|---|
| 215 | dumpdir_len = strlen(xc_coveragedump_dir); |
|---|
| 216 | alloc_len = dumpdir_len + 1 + 128; |
|---|
| 217 | outfilename = emalloc(alloc_len); |
|---|
| 218 | strcpy(outfilename, xc_coveragedump_dir); |
|---|
| 219 | |
|---|
| 220 | zend_hash_internal_pointer_reset(XG(coverages)); |
|---|
| 221 | while (zend_hash_get_current_data(XG(coverages), (void **) &pcov) == SUCCESS) { |
|---|
| 222 | zend_hash_get_current_key_ex(XG(coverages), &s, &size, NULL, 0, NULL); |
|---|
| 223 | outfilelen = dumpdir_len + size + 5; |
|---|
| 224 | if (alloc_len < outfilelen) { |
|---|
| 225 | alloc_len = outfilelen + 128; |
|---|
| 226 | outfilename = erealloc(outfilename, alloc_len); |
|---|
| 227 | } |
|---|
| 228 | strcpy(outfilename + dumpdir_len, ZSTR_S(s)); |
|---|
| 229 | strcpy(outfilename + dumpdir_len + size - 1, ".pcov"); |
|---|
| 230 | |
|---|
| 231 | #ifdef DEBUG |
|---|
| 232 | fprintf(stderr, "outfilename %s\n", outfilename); |
|---|
| 233 | #endif |
|---|
| 234 | xc_coverager_save_cov(ZSTR_S(s), outfilename, *pcov TSRMLS_CC); |
|---|
| 235 | zend_hash_move_forward(XG(coverages)); |
|---|
| 236 | } |
|---|
| 237 | efree(outfilename); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | zend_hash_destroy(XG(coverages)); |
|---|
| 241 | efree(XG(coverages)); |
|---|
| 242 | XG(coverages) = NULL; |
|---|
| 243 | } |
|---|
| 244 | /* }}} */ |
|---|
| 245 | |
|---|
| 246 | /* helper func to store hits into coverages */ |
|---|
| 247 | static coverager_t xc_coverager_get(char *filename TSRMLS_DC) /* {{{ */ |
|---|
| 248 | { |
|---|
| 249 | int len = strlen(filename) + 1; |
|---|
| 250 | coverager_t cov, *pcov; |
|---|
| 251 | |
|---|
| 252 | if (zend_hash_find(XG(coverages), filename, len, (void **) &pcov) == SUCCESS) { |
|---|
| 253 | #ifdef DEBUG |
|---|
| 254 | fprintf(stderr, "got coverage %s %p\n", filename, *pcov); |
|---|
| 255 | #endif |
|---|
| 256 | return *pcov; |
|---|
| 257 | } |
|---|
| 258 | else { |
|---|
| 259 | cov = emalloc(sizeof(HashTable)); |
|---|
| 260 | zend_hash_init(cov, 0, NULL, NULL, 0); |
|---|
| 261 | zend_hash_add(XG(coverages), filename, len, (void **) &cov, sizeof(cov), NULL); |
|---|
| 262 | #ifdef DEBUG |
|---|
| 263 | fprintf(stderr, "new coverage %s %p\n", filename, cov); |
|---|
| 264 | #endif |
|---|
| 265 | return cov; |
|---|
| 266 | } |
|---|
| 267 | } |
|---|
| 268 | /* }}} */ |
|---|
| 269 | static void xc_coverager_add_hits(HashTable *cov, long line, long hits TSRMLS_DC) /* {{{ */ |
|---|
| 270 | { |
|---|
| 271 | long *poldhits; |
|---|
| 272 | |
|---|
| 273 | if (line == 0) { |
|---|
| 274 | return; |
|---|
| 275 | } |
|---|
| 276 | if (zend_hash_index_find(cov, line, (void**)&poldhits) == SUCCESS) { |
|---|
| 277 | if (hits == -1) { |
|---|
| 278 | /* already marked */ |
|---|
| 279 | return; |
|---|
| 280 | } |
|---|
| 281 | if (*poldhits != -1) { |
|---|
| 282 | hits += *poldhits; |
|---|
| 283 | } |
|---|
| 284 | } |
|---|
| 285 | zend_hash_index_update(cov, line, &hits, sizeof(hits), NULL); |
|---|
| 286 | } |
|---|
| 287 | /* }}} */ |
|---|
| 288 | |
|---|
| 289 | static int xc_coverager_get_op_array_size_no_tail(zend_op_array *op_array) /* {{{ */ |
|---|
| 290 | { |
|---|
| 291 | zend_uint size; |
|---|
| 292 | |
|---|
| 293 | size = op_array->size; |
|---|
| 294 | #ifdef ZEND_ENGINE_2 |
|---|
| 295 | if (op_array->opcodes[size - 1].opcode == ZEND_HANDLE_EXCEPTION) { |
|---|
| 296 | size --; |
|---|
| 297 | #endif |
|---|
| 298 | if (op_array->opcodes[size - 1].opcode == ZEND_RETURN) { |
|---|
| 299 | size --; |
|---|
| 300 | /* it's not real php statement */ |
|---|
| 301 | if (op_array->opcodes[size - 1].opcode == ZEND_EXT_STMT) { |
|---|
| 302 | size --; |
|---|
| 303 | } |
|---|
| 304 | } |
|---|
| 305 | #ifdef ZEND_ENGINE_2 |
|---|
| 306 | } |
|---|
| 307 | #endif |
|---|
| 308 | return size; |
|---|
| 309 | } |
|---|
| 310 | /* }}} */ |
|---|
| 311 | |
|---|
| 312 | /* prefill */ |
|---|
| 313 | static int xc_coverager_init_op_array(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| 314 | { |
|---|
| 315 | zend_uint size; |
|---|
| 316 | coverager_t cov; |
|---|
| 317 | zend_uint i; |
|---|
| 318 | |
|---|
| 319 | if (op_array->type != ZEND_USER_FUNCTION) { |
|---|
| 320 | return 0; |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | size = xc_coverager_get_op_array_size_no_tail(op_array); |
|---|
| 324 | cov = xc_coverager_get(op_array->filename TSRMLS_CC); |
|---|
| 325 | for (i = 0; i < size; i ++) { |
|---|
| 326 | switch (op_array->opcodes[i].opcode) { |
|---|
| 327 | case ZEND_EXT_STMT: |
|---|
| 328 | #if 0 |
|---|
| 329 | case ZEND_EXT_FCALL_BEGIN: |
|---|
| 330 | case ZEND_EXT_FCALL_END: |
|---|
| 331 | #endif |
|---|
| 332 | xc_coverager_add_hits(cov, op_array->opcodes[i].lineno, -1 TSRMLS_CC); |
|---|
| 333 | break; |
|---|
| 334 | } |
|---|
| 335 | } |
|---|
| 336 | return 0; |
|---|
| 337 | } |
|---|
| 338 | /* }}} */ |
|---|
| 339 | static void xc_coverager_init_compile_result(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| 340 | { |
|---|
| 341 | xc_compile_result_t cr; |
|---|
| 342 | |
|---|
| 343 | xc_compile_result_init_cur(&cr, op_array TSRMLS_CC); |
|---|
| 344 | xc_apply_op_array(&cr, (apply_func_t) xc_coverager_init_op_array TSRMLS_CC); |
|---|
| 345 | xc_compile_result_free(&cr); |
|---|
| 346 | } |
|---|
| 347 | /* }}} */ |
|---|
| 348 | static zend_op_array *xc_compile_file_for_coverage(zend_file_handle *h, int type TSRMLS_DC) /* {{{ */ |
|---|
| 349 | { |
|---|
| 350 | zend_op_array *op_array; |
|---|
| 351 | |
|---|
| 352 | op_array = origin_compile_file(h, type TSRMLS_CC); |
|---|
| 353 | if (XG(coveragedumper) && XG(coverages)) { |
|---|
| 354 | xc_coverager_init_compile_result(op_array TSRMLS_CC); |
|---|
| 355 | } |
|---|
| 356 | return op_array; |
|---|
| 357 | } |
|---|
| 358 | /* }}} */ |
|---|
| 359 | |
|---|
| 360 | /* hits */ |
|---|
| 361 | void xc_coverager_handle_ext_stmt(zend_op_array *op_array, zend_uchar op) /* {{{ */ |
|---|
| 362 | { |
|---|
| 363 | TSRMLS_FETCH(); |
|---|
| 364 | |
|---|
| 365 | if (XG(coveragedumper) && XG(coverages)) { |
|---|
| 366 | int size = xc_coverager_get_op_array_size_no_tail(op_array); |
|---|
| 367 | int oplineno = (*EG(opline_ptr)) - op_array->opcodes; |
|---|
| 368 | if (oplineno < size) { |
|---|
| 369 | xc_coverager_add_hits(xc_coverager_get(op_array->filename TSRMLS_CC), (*EG(opline_ptr))->lineno, 1 TSRMLS_CC); |
|---|
| 370 | } |
|---|
| 371 | } |
|---|
| 372 | } |
|---|
| 373 | /* }}} */ |
|---|
| 374 | |
|---|
| 375 | /* init/destroy */ |
|---|
| 376 | int xc_coverager_init(int module_number TSRMLS_DC) /* {{{ */ |
|---|
| 377 | { |
|---|
| 378 | if (xc_coveragedump_dir) { |
|---|
| 379 | int len = strlen(xc_coveragedump_dir); |
|---|
| 380 | if (len) { |
|---|
| 381 | if (xc_coveragedump_dir[len - 1] == '/') { |
|---|
| 382 | xc_coveragedump_dir[len - 1] = '\0'; |
|---|
| 383 | } |
|---|
| 384 | } |
|---|
| 385 | } |
|---|
| 386 | if (xc_coveragedump_dir && xc_coveragedump_dir[0]) { |
|---|
| 387 | origin_compile_file = zend_compile_file; |
|---|
| 388 | zend_compile_file = xc_compile_file_for_coverage; |
|---|
| 389 | CG(extended_info) = 1; |
|---|
| 390 | } |
|---|
| 391 | return SUCCESS; |
|---|
| 392 | } |
|---|
| 393 | /* }}} */ |
|---|
| 394 | void xc_coverager_destroy() /* {{{ */ |
|---|
| 395 | { |
|---|
| 396 | if (origin_compile_file == xc_compile_file_for_coverage) { |
|---|
| 397 | zend_compile_file = origin_compile_file; |
|---|
| 398 | } |
|---|
| 399 | if (xc_coveragedump_dir) { |
|---|
| 400 | pefree(xc_coveragedump_dir, 1); |
|---|
| 401 | xc_coveragedump_dir = NULL; |
|---|
| 402 | } |
|---|
| 403 | } |
|---|
| 404 | /* }}} */ |
|---|
| 405 | |
|---|
| 406 | /* user api */ |
|---|
| 407 | PHP_FUNCTION(xcache_coverager_decode) /* {{{ */ |
|---|
| 408 | { |
|---|
| 409 | char *str; |
|---|
| 410 | int len; |
|---|
| 411 | long *p; |
|---|
| 412 | |
|---|
| 413 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len) == FAILURE) { |
|---|
| 414 | return; |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | array_init(return_value); |
|---|
| 418 | |
|---|
| 419 | p = (long*) str; |
|---|
| 420 | len -= sizeof(long); |
|---|
| 421 | if (len < 0) { |
|---|
| 422 | return; |
|---|
| 423 | } |
|---|
| 424 | if (*p++ != PCOV_HEADER_MAGIC) { |
|---|
| 425 | #ifdef DEBUG |
|---|
| 426 | fprintf(stderr, "wrong magic in xcache_coverager_decode"); |
|---|
| 427 | #endif |
|---|
| 428 | return; |
|---|
| 429 | } |
|---|
| 430 | |
|---|
| 431 | for (; len >= sizeof(long) * 2; len -= sizeof(long) * 2, p += 2) { |
|---|
| 432 | add_index_long(return_value, p[0], p[1]); |
|---|
| 433 | } |
|---|
| 434 | } |
|---|
| 435 | /* }}} */ |
|---|
| 436 | |
|---|