Changeset 937


Ignore:
Timestamp:
2012-06-28T11:38:30+02:00 (22 months ago)
Author:
moo
Message:

still WIP: rewrite sandbox to use callback

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/disassembler.c

    r930 r937  
    44#include "processor.h" 
    55 
    6 #define return_value dst 
    7  
    8 /* sandbox {{{ */ 
    9 #undef TG 
    10 #undef OG 
    11 #define TG(x) (sandbox->tmp_##x) 
    12 #define OG(x) (sandbox->orig_##x) 
    13 /* }}} */ 
    14  
    156#ifndef HAVE_XCACHE_OPCODE_SPEC_DEF 
    167#error disassembler cannot be built without xcache/opcode_spec_def.h 
    178#endif 
    18 static void xc_dasm(xc_sandbox_t *sandbox, zval *dst, zend_op_array *op_array TSRMLS_DC) /* {{{ */ 
     9static void xc_dasm(zval *output, zend_op_array *op_array TSRMLS_DC) /* {{{ */ 
    1910{ 
    20     Bucket *b; 
     11    const Bucket *b; 
    2112    zval *zv, *list; 
    2213    xc_compile_result_t cr; 
     
    3122 
    3223    /* go */ 
    33     array_init(dst); 
     24    array_init(output); 
    3425 
    3526    ALLOC_INIT_ZVAL(zv); 
    3627    array_init(zv); 
    3728    xc_dasm_zend_op_array(&dasm, zv, op_array TSRMLS_CC); 
    38     add_assoc_zval_ex(dst, ZEND_STRS("op_array"), zv); 
     29    add_assoc_zval_ex(output, ZEND_STRS("op_array"), zv); 
    3930 
    4031    buf = emalloc(bufsize); 
     
    4233    ALLOC_INIT_ZVAL(list); 
    4334    array_init(list); 
    44     b = TG(internal_function_tail) ? TG(internal_function_tail)->pListNext : TG(function_table).pListHead; 
    45     for (; b; b = b->pListNext) { 
     35    for (b = xc_sandbox_user_function_begin(); b; b = b->pListNext) { 
    4636        int keysize, keyLength; 
    4737 
     
    7565        add_u_assoc_zval_ex(list, BUCKET_KEY_TYPE(b), ZSTR(buf), keyLength, zv); 
    7666    } 
    77     add_assoc_zval_ex(dst, ZEND_STRS("function_table"), list); 
     67    add_assoc_zval_ex(output, ZEND_STRS("function_table"), list); 
    7868     
    7969    ALLOC_INIT_ZVAL(list); 
    8070    array_init(list); 
    81     b = TG(internal_class_tail) ? TG(internal_class_tail)->pListNext : TG(class_table).pListHead; 
    82     for (; b; b = b->pListNext) { 
     71    for (b = xc_sandbox_user_class_begin(); b; b = b->pListNext) { 
    8372        int keysize, keyLength; 
    8473 
     
    112101    } 
    113102    efree(buf); 
    114     add_assoc_zval_ex(dst, ZEND_STRS("class_table"), list); 
     103    add_assoc_zval_ex(output, ZEND_STRS("class_table"), list); 
    115104 
    116105    /*xc_apply_op_array(&cr, (apply_func_t) xc_redo_pass_two TSRMLS_CC);*/ 
    117106    xc_compile_result_free(&cr); 
    118  
    119     return; 
    120107} 
    121108/* }}} */ 
    122 void xc_dasm_string(zval *dst, zval *source TSRMLS_DC) /* {{{ */ 
     109typedef struct xc_dasm_sandboxed_t { /* {{{ */ 
     110    enum Type { 
     111        xc_dasm_file_t 
     112        , xc_dasm_string_t 
     113    } type; 
     114    union { 
     115        zval *zfilename; 
     116        struct { 
     117            zval *source; 
     118            char *eval_name; 
     119        } compile_string; 
     120    } input; 
     121 
     122    zval *output; 
     123} xc_dasm_sandboxed_t; /* {{{ */ 
     124 
     125zend_op_array *xc_dasm_sandboxed(void *data TSRMLS_DC) 
    123126{ 
    124     int catched; 
     127    zend_bool catched = 0; 
    125128    zend_op_array *op_array = NULL; 
    126     xc_sandbox_t sandbox; 
    127     char *eval_name = zend_make_compiled_string_description("runtime-created function" TSRMLS_CC); 
     129    xc_dasm_sandboxed_t *sandboxed_dasm = (xc_dasm_sandboxed_t *) data; 
    128130 
    129     xc_sandbox_init(&sandbox, eval_name TSRMLS_CC); 
    130  
    131     catched = 0; 
    132131    zend_try { 
    133         op_array = compile_string(source, eval_name TSRMLS_CC); 
     132        if (sandboxed_dasm->type == xc_dasm_file_t) { 
     133            op_array = compile_filename(ZEND_REQUIRE, sandboxed_dasm->input.zfilename TSRMLS_CC); 
     134        } 
     135        else { 
     136            op_array = compile_string(sandboxed_dasm->input.compile_string.source, sandboxed_dasm->input.compile_string.eval_name TSRMLS_CC); 
     137        } 
    134138    } zend_catch { 
    135139        catched = 1; 
     
    137141 
    138142    if (catched || !op_array) { 
    139         goto err_compile; 
     143#define return_value sandboxed_dasm->output 
     144        RETVAL_FALSE; 
     145#undef return_value 
     146        return NULL; 
    140147    } 
    141148 
    142     xc_dasm(&sandbox, dst, op_array TSRMLS_CC); 
    143  
    144     /* free */ 
    145     efree(eval_name); 
    146 #ifdef ZEND_ENGINE_2 
    147     destroy_op_array(op_array TSRMLS_CC); 
    148 #else 
    149     destroy_op_array(op_array); 
    150 #endif 
    151     efree(op_array); 
    152     xc_sandbox_free(&sandbox, 0 TSRMLS_CC); 
    153     return; 
    154  
    155 err_compile: 
    156     efree(eval_name); 
    157     xc_sandbox_free(&sandbox, 0 TSRMLS_CC); 
    158  
    159     RETURN_FALSE; 
    160 } 
    161 /* }}} */ 
    162 void xc_dasm_file(zval *dst, const char *filename TSRMLS_DC) /* {{{ */ 
    163 { 
    164     int catched; 
    165     zend_op_array *op_array = NULL; 
    166     xc_sandbox_t sandbox; 
    167     zval *zfilename; 
    168  
    169     MAKE_STD_ZVAL(zfilename); 
    170     zfilename->value.str.val = estrdup(filename); 
    171     zfilename->value.str.len = strlen(filename); 
    172     zfilename->type = IS_STRING; 
    173  
    174     xc_sandbox_init(&sandbox, zfilename->value.str.val TSRMLS_CC); 
    175  
    176     catched = 0; 
    177     zend_try { 
    178         op_array = compile_filename(ZEND_REQUIRE, zfilename TSRMLS_CC); 
    179     } zend_catch { 
    180         catched = 1; 
    181     } zend_end_try(); 
    182  
    183     if (catched || !op_array) { 
    184         goto err_compile; 
    185     } 
    186  
    187     xc_dasm(&sandbox, dst, op_array TSRMLS_CC); 
     149    xc_dasm(sandboxed_dasm->output, op_array TSRMLS_CC); 
    188150 
    189151    /* free */ 
     
    194156#endif 
    195157    efree(op_array); 
    196     xc_sandbox_free(&sandbox, 0 TSRMLS_CC); 
    197     zval_dtor(zfilename); 
    198     FREE_ZVAL(zfilename); 
    199     return; 
    200158 
    201 err_compile: 
    202     xc_sandbox_free(&sandbox, 0 TSRMLS_CC); 
     159    return NULL; 
     160} /* }}} */ 
     161void xc_dasm_string(zval *output, zval *source TSRMLS_DC) /* {{{ */ 
     162{ 
     163    xc_dasm_sandboxed_t sandboxed_dasm; 
     164    char *eval_name = zend_make_compiled_string_description("runtime-created function" TSRMLS_CC); 
     165 
     166    sandboxed_dasm.output = output; 
     167    sandboxed_dasm.type = xc_dasm_string_t; 
     168    sandboxed_dasm.input.compile_string.source = source; 
     169    sandboxed_dasm.input.compile_string.eval_name = eval_name; 
     170    xc_sandbox(&xc_dasm_sandboxed, (void *) &sandboxed_dasm, eval_name TSRMLS_CC); 
     171    efree(eval_name); 
     172} 
     173/* }}} */ 
     174void xc_dasm_file(zval *output, const char *filename TSRMLS_DC) /* {{{ */ 
     175{ 
     176    zval *zfilename; 
     177    xc_dasm_sandboxed_t sandboxed_dasm; 
     178 
     179    MAKE_STD_ZVAL(zfilename); 
     180    zfilename->value.str.val = estrdup(filename); 
     181    zfilename->value.str.len = strlen(filename); 
     182    zfilename->type = IS_STRING; 
     183 
     184    sandboxed_dasm.output = output; 
     185    sandboxed_dasm.type = xc_dasm_file_t; 
     186    sandboxed_dasm.input.zfilename = zfilename; 
     187    xc_sandbox(&xc_dasm_sandboxed, (void *) &sandboxed_dasm, zfilename->value.str.val TSRMLS_CC); 
    203188 
    204189    zval_dtor(zfilename); 
    205190    FREE_ZVAL(zfilename); 
    206     RETURN_FALSE; 
    207191} 
    208192/* }}} */ 
  • trunk/utils.c

    r935 r937  
    582582/* }}} */ 
    583583 
    584 /* sandbox {{{ */ 
     584typedef struct { /* sandbox {{{ */ 
     585    ZEND_24(NOTHING, const) char *filename; 
     586 
     587    HashTable orig_included_files; 
     588    HashTable *tmp_included_files; 
     589 
     590#ifdef HAVE_XCACHE_CONSTANT 
     591    HashTable *orig_zend_constants; 
     592    HashTable tmp_zend_constants; 
     593#endif 
     594    HashTable *orig_function_table; 
     595    HashTable *orig_class_table; 
     596    HashTable *orig_auto_globals; 
     597    HashTable tmp_function_table; 
     598    HashTable tmp_class_table; 
     599    HashTable tmp_auto_globals; 
     600#ifdef HAVE_XCACHE_CONSTANT 
     601    Bucket    *tmp_internal_constant_tail; 
     602#endif 
     603    Bucket    *tmp_internal_function_tail; 
     604    Bucket    *tmp_internal_class_tail; 
     605 
     606#ifdef XCACHE_ERROR_CACHING 
     607    int orig_user_error_handler_error_reporting; 
     608    zend_uint compilererror_cnt; 
     609    zend_uint compilererror_size; 
     610    xc_compilererror_t *compilererrors; 
     611#endif 
     612 
     613#ifdef ZEND_COMPILE_IGNORE_INTERNAL_CLASSES 
     614    zend_uint orig_compiler_options; 
     615#endif 
     616} xc_sandbox_t; 
     617 
    585618#undef TG 
    586619#undef OG 
     
    731764/* }}} */ 
    732765#endif 
     766 
    733767xc_sandbox_t *xc_sandbox_init(xc_sandbox_t *sandbox, ZEND_24(NOTHING, const) char *filename TSRMLS_DC) /* {{{ */ 
    734768{ 
     
    825859/* }}} */ 
    826860#endif 
    827 static void xc_sandbox_install(xc_sandbox_t *sandbox, xc_install_action_t install TSRMLS_DC) /* {{{ */ 
     861static void xc_sandbox_install(xc_sandbox_t *sandbox TSRMLS_DC) /* {{{ */ 
    828862{ 
    829863    zend_uint i; 
     
    874908#endif 
    875909 
    876     if (install != XC_InstallNoBinding) { 
    877910#ifdef ZEND_COMPILE_DELAYED_BINDING 
    878         zend_do_delayed_early_binding(CG(active_op_array) TSRMLS_CC); 
    879 #else 
    880         xc_undo_pass_two(CG(active_op_array) TSRMLS_CC); 
    881         xc_foreach_early_binding_class(CG(active_op_array), xc_early_binding_cb, (void *) sandbox TSRMLS_CC); 
    882         xc_redo_pass_two(CG(active_op_array) TSRMLS_CC); 
    883 #endif 
    884     } 
     911    zend_do_delayed_early_binding(CG(active_op_array) TSRMLS_CC); 
     912#else 
     913    xc_undo_pass_two(CG(active_op_array) TSRMLS_CC); 
     914    xc_foreach_early_binding_class(CG(active_op_array), xc_early_binding_cb, (void *) sandbox TSRMLS_CC); 
     915    xc_redo_pass_two(CG(active_op_array) TSRMLS_CC); 
     916#endif 
    885917 
    886918#ifdef XCACHE_ERROR_CACHING 
     
    898930} 
    899931/* }}} */ 
    900 void xc_sandbox_free(xc_sandbox_t *sandbox, xc_install_action_t install TSRMLS_DC) /* {{{ */ 
     932void xc_sandbox_free(xc_sandbox_t *sandbox, zend_op_array *op_array TSRMLS_DC) /* {{{ */ 
    901933{ 
    902934    XG(sandbox) = NULL; 
     
    916948#endif 
    917949 
    918     if (install != XC_NoInstall) { 
     950    if (op_array) { 
     951        zend_op_array *old_active_op_array = CG(active_op_array); 
    919952        CG(in_compilation)    = 1; 
    920953        CG(compiled_filename) = ZEND_24(NOTHING, (char *)) sandbox->filename; 
    921954        CG(zend_lineno)       = 0; 
    922         xc_sandbox_install(sandbox, install TSRMLS_CC); 
     955 
     956        CG(active_op_array) = op_array; 
     957        xc_sandbox_install(sandbox TSRMLS_CC); 
     958        CG(active_op_array) = old_active_op_array; 
     959 
    923960        CG(in_compilation)    = 0; 
    924961        CG(compiled_filename) = NULL; 
     
    961998} 
    962999/* }}} */ 
     1000const Bucket *xc_sandbox_user_function_begin() /* {{{ */ 
     1001{ 
     1002    xc_sandbox_t *sandbox = (xc_sandbox_t *) XG(sandbox); 
     1003    assert(sandbox); 
     1004    return TG(internal_function_tail) ? TG(internal_function_tail)->pListNext : TG(function_table).pListHead; 
     1005} /* {{{ */ 
     1006const Bucket *xc_sandbox_user_class_begin() /* {{{ */ 
     1007{ 
     1008    xc_sandbox_t *sandbox = (xc_sandbox_t *) XG(sandbox); 
     1009    assert(sandbox); 
     1010    return TG(internal_class_tail) ? TG(internal_class_tail)->pListNext : TG(class_table).pListHead; 
     1011} /* {{{ */ 
     1012#ifdef XCACHE_ERROR_CACHING 
     1013xc_compilererror_t *xc_sandbox_compilererrors() /* {{{ */ 
     1014{ 
     1015    xc_sandbox_t *sandbox = (xc_sandbox_t *) XG(sandbox); 
     1016    assert(sandbox); 
     1017    return sandbox->compilererrors; 
     1018} /* }}} */ 
     1019zend_uint xc_sandbox_compilererror_cnt() /* {{{ */ 
     1020{ 
     1021    xc_sandbox_t *sandbox = (xc_sandbox_t *) XG(sandbox); 
     1022    assert(sandbox); 
     1023    return sandbox->compilererror_cnt; 
     1024} /* }}} */ 
     1025#endif 
     1026 
     1027 
    9631028int xc_vtrace(const char *fmt, va_list args) /* {{{ */ 
    9641029{ 
  • trunk/utils.h

    r935 r937  
    8080#endif 
    8181 
    82 /* sandbox */ 
    83 typedef struct { 
    84     ZEND_24(NOTHING, const) char *filename; 
    85  
    86     HashTable orig_included_files; 
    87     HashTable *tmp_included_files; 
    88  
    89 #ifdef HAVE_XCACHE_CONSTANT 
    90     HashTable *orig_zend_constants; 
    91     HashTable tmp_zend_constants; 
    92 #endif 
    93     HashTable *orig_function_table; 
    94     HashTable *orig_class_table; 
    95     HashTable *orig_auto_globals; 
    96     HashTable tmp_function_table; 
    97     HashTable tmp_class_table; 
    98     HashTable tmp_auto_globals; 
    99 #ifdef HAVE_XCACHE_CONSTANT 
    100     Bucket    *tmp_internal_constant_tail; 
    101 #endif 
    102     Bucket    *tmp_internal_function_tail; 
    103     Bucket    *tmp_internal_class_tail; 
    104  
     82/* return op_array to install */ 
     83typedef zend_op_array *(*xc_sandboxed_func_t)(void *data TSRMLS_DC); 
     84zend_op_array *xc_sandbox(xc_sandboxed_func_t sandboxed_func, void *data, ZEND_24(NOTHING, const) char *filename TSRMLS_DC); 
     85const Bucket *xc_sandbox_user_function_begin(); 
     86const Bucket *xc_sandbox_user_class_begin(); 
     87zend_uint xc_sandbox_compilererror_cnt(); 
    10588#ifdef XCACHE_ERROR_CACHING 
    106     int orig_user_error_handler_error_reporting; 
    107     zend_uint compilererror_cnt; 
    108     zend_uint compilererror_size; 
    109     xc_compilererror_t *compilererrors; 
     89xc_compilererror_t *xc_sandbox_compilererrors(); 
     90zend_uint xc_sandbox_compilererror_cnt(); 
    11091#endif 
    11192 
    112 #ifdef ZEND_COMPILE_IGNORE_INTERNAL_CLASSES 
    113     zend_uint orig_compiler_options; 
    114 #endif 
    115 } xc_sandbox_t; 
    116  
    117 typedef enum _xc_install_action_t { 
    118     XC_NoInstall, 
    119     XC_Install, 
    120     XC_InstallNoBinding 
    121 } xc_install_action_t; 
    122  
    12393void xc_zend_class_add_ref(zend_class_entry ZESW(*ce, **ce)); 
    124 xc_sandbox_t *xc_sandbox_init(xc_sandbox_t *sandbox, ZEND_24(NOTHING, const) char *filename TSRMLS_DC); 
    125 void xc_sandbox_free(xc_sandbox_t *sandbox, xc_install_action_t install TSRMLS_DC); 
    12694 
    12795typedef zend_bool (*xc_if_func_t)(void *data); 
  • trunk/xcache.c

    r936 r937  
    10071007/* }}} */ 
    10081008#endif 
     1009typedef struct xc_compiler_t { /* {{{ */ 
     1010    /* XCache cached compile state */ 
     1011    const char *filename; 
     1012    size_t filename_len; 
     1013    const char *opened_path; 
     1014    char opened_path_buffer[MAXPATHLEN]; 
     1015 
     1016    xc_entry_hash_t entry_hash; 
     1017    xc_entry_php_t new_entry; 
     1018    xc_entry_data_php_t new_php; 
     1019} xc_compiler_t; 
     1020/* }}} */ 
    10091021typedef struct xc_entry_resolve_path_data_t { /* {{{ */ 
    10101022    xc_compiler_t *compiler; 
     
    17981810    /* }}} */ 
    17991811#ifdef XCACHE_ERROR_CACHING 
    1800     compiler->new_php.compilererrors = ((xc_sandbox_t *) XG(sandbox))->compilererrors; 
    1801     compiler->new_php.compilererror_cnt = ((xc_sandbox_t *) XG(sandbox))->compilererror_cnt; 
     1812    compiler->new_php.compilererrors = xc_sandbox_compilererrors(); 
     1813    compiler->new_php.compilererror_cnt = xc_sandbox_compilererror_cnt(); 
    18021814#endif 
    18031815#ifndef ZEND_COMPILE_DELAYED_BINDING 
     
    18821894} 
    18831895/* }}} */ 
    1884 static zend_op_array *xc_compile_file_ex(xc_compiler_t *compiler, zend_file_handle *h, int type TSRMLS_DC) /* {{{ */ 
     1896typedef struct xc_sandboxed_compiler_t { /* {{{ */ 
     1897    xc_compiler_t *compiler; 
     1898    /* input */ 
     1899    zend_file_handle *h; 
     1900    int type; 
     1901 
     1902    /* sandbox output */ 
     1903    xc_entry_php_t *stored_entry; 
     1904    xc_entry_data_php_t *stored_php; 
     1905} xc_sandboxed_compiler_t; /* {{{ */ 
     1906 
     1907static zend_op_array *xc_compile_file_sandboxed(void *data TSRMLS_DC) /* {{{ */ 
     1908{ 
     1909    xc_sandboxed_compiler_t *sandboxed_compiler = (xc_sandboxed_compiler_t *) data; 
     1910    xc_compiler_t *compiler = sandboxed_compiler->compiler; 
     1911    zend_bool catched = 0; 
     1912    xc_cache_t *cache = xc_php_caches[compiler->entry_hash.cacheid]; 
     1913    xc_entry_php_t *stored_entry; 
     1914    xc_entry_data_php_t *stored_php; 
     1915 
     1916    /* {{{ compile */ 
     1917    /* make compile inside sandbox */ 
     1918#ifdef HAVE_XCACHE_CONSTANT 
     1919    compiler->new_php.constinfos  = NULL; 
     1920#endif 
     1921    compiler->new_php.funcinfos   = NULL; 
     1922    compiler->new_php.classinfos  = NULL; 
     1923#ifdef ZEND_ENGINE_2_1 
     1924    compiler->new_php.autoglobals = NULL; 
     1925#endif 
     1926    memset(&compiler->new_php.op_array_info, 0, sizeof(compiler->new_php.op_array_info)); 
     1927 
     1928    XG(initial_compile_file_called) = 0; 
     1929    zend_try { 
     1930        compiler->new_php.op_array = NULL; 
     1931        xc_compile_php(compiler, sandboxed_compiler->h, sandboxed_compiler->type TSRMLS_CC); 
     1932    } zend_catch { 
     1933        catched = 1; 
     1934    } zend_end_try(); 
     1935 
     1936    if (catched 
     1937     || !compiler->new_php.op_array /* possible ? */ 
     1938     || !XG(initial_compile_file_called)) { 
     1939        goto err_aftersandbox; 
     1940    } 
     1941 
     1942    /* }}} */ 
     1943#ifdef SHOW_DPRINT 
     1944    compiler->new_entry.php = &compiler->new_php; 
     1945    xc_dprint(&compiler->new_entry, 0 TSRMLS_CC); 
     1946#endif 
     1947 
     1948    stored_entry = NULL; 
     1949    stored_php = NULL; 
     1950    ENTER_LOCK_EX(cache) { /* {{{ php_store/entry_store */ 
     1951        /* php_store */ 
     1952        stored_php = xc_php_store_unlocked(cache, &compiler->new_php TSRMLS_CC); 
     1953        if (!stored_php) { 
     1954            /* error */ 
     1955            break; 
     1956        } 
     1957        /* entry_store */ 
     1958        compiler->new_entry.php = stored_php; 
     1959        stored_entry = xc_entry_php_store_unlocked(cache, compiler->entry_hash.entryslotid, &compiler->new_entry TSRMLS_CC); 
     1960        if (stored_entry) { 
     1961            xc_php_addref_unlocked(stored_php); 
     1962            TRACE(" cached %d:%s, holding", compiler->new_entry.file_inode, stored_entry->entry.name.str.val); 
     1963            xc_entry_hold_php_unlocked(cache, stored_entry TSRMLS_CC); 
     1964        } 
     1965    } LEAVE_LOCK_EX(cache); 
     1966    /* }}} */ 
     1967    TRACE("%s", stored_entry ? "stored" : "store failed"); 
     1968 
     1969    if (catched || !stored_php) { 
     1970        goto err_aftersandbox; 
     1971    } 
     1972 
     1973    cache->compiling = 0; 
     1974    xc_free_php(&compiler->new_php TSRMLS_CC); 
     1975 
     1976    if (stored_entry) { 
     1977        if (compiler->new_php.op_array) { 
     1978#ifdef ZEND_ENGINE_2 
     1979            destroy_op_array(compiler->new_php.op_array TSRMLS_CC); 
     1980#else 
     1981            destroy_op_array(compiler->new_php.op_array); 
     1982#endif 
     1983            efree(compiler->new_php.op_array); 
     1984            compiler->new_php.op_array = NULL; 
     1985            sandboxed_compiler->h = NULL; 
     1986        } 
     1987        sandboxed_compiler->stored_entry = stored_entry; 
     1988        sandboxed_compiler->stored_php = stored_php; 
     1989        /* sandbox no install */ 
     1990        return NULL; 
     1991    } 
     1992    else { 
     1993        /* install it with sandbox */ 
     1994        return compiler->new_php.op_array; 
     1995    } 
     1996 
     1997err_aftersandbox: 
     1998    xc_free_php(&compiler->new_php TSRMLS_CC); 
     1999 
     2000    cache->compiling = 0; 
     2001    if (catched) { 
     2002        cache->errors ++; 
     2003        zend_bailout(); 
     2004    } 
     2005    return compiler->new_php.op_array; 
     2006} /* }}} */ 
     2007static zend_op_array *xc_compile_file_cached(xc_compiler_t *compiler, zend_file_handle *h, int type TSRMLS_DC) /* {{{ */ 
    18852008{ 
    18862009    /* 
     
    18992022                return old; 
    19002023            } 
    1901             php = compile(); 
    1902             entry = create entries[entry]; 
     2024 
     2025            inside_sandbox { 
     2026                php = compile; 
     2027                entry = create entries[entry]; 
     2028            } 
    19032029        } 
    19042030 
     
    19122038    zend_bool gaveup = 0; 
    19132039    zend_bool catched = 0; 
    1914     xc_sandbox_t sandbox; 
     2040    zend_op_array *op_array; 
    19152041    xc_cache_t *cache = xc_php_caches[compiler->entry_hash.cacheid]; 
     2042    xc_sandboxed_compiler_t sandboxed_compiler; 
    19162043 
    19172044    /* stale clogs precheck */ 
     
    20022129    /* }}} */ 
    20032130 
    2004     /* {{{ compile */ 
    2005     /* make compile inside sandbox */ 
    2006  
    2007 #ifdef HAVE_XCACHE_CONSTANT 
    2008     compiler->new_php.constinfos  = NULL; 
    2009 #endif 
    2010     compiler->new_php.funcinfos   = NULL; 
    2011     compiler->new_php.classinfos  = NULL; 
    2012 #ifdef ZEND_ENGINE_2_1 
    2013     compiler->new_php.autoglobals = NULL; 
    2014 #endif 
    2015     memset(&compiler->new_php.op_array_info, 0, sizeof(compiler->new_php.op_array_info)); 
    2016  
    2017     XG(initial_compile_file_called) = 0; 
    2018     zend_try { 
    2019         xc_sandbox_init(&sandbox, h->opened_path ? h->opened_path : h->filename TSRMLS_CC); 
    2020         compiler->new_php.op_array = NULL; 
    2021         xc_compile_php(compiler, h, type TSRMLS_CC); 
    2022     } zend_catch { 
    2023         catched = 1; 
    2024     } zend_end_try(); 
    2025  
    2026     if (catched 
    2027      || !compiler->new_php.op_array /* possible ? */ 
    2028      || !XG(initial_compile_file_called)) { 
    2029         goto err_aftersandbox; 
    2030     } 
    2031  
    2032     /* }}} */ 
    2033 #ifdef SHOW_DPRINT 
    2034     compiler->new_entry.php = &compiler->new_php; 
    2035     xc_dprint(&compiler->new_entry, 0 TSRMLS_CC); 
    2036 #endif 
    2037     ENTER_LOCK_EX(cache) { /* {{{ php_store/entry_store */ 
    2038         /* php_store */ 
    2039         stored_php = xc_php_store_unlocked(cache, &compiler->new_php TSRMLS_CC); 
    2040         if (!stored_php) { 
    2041             /* error */ 
    2042             break; 
    2043         } 
    2044         /* entry_store */ 
    2045         compiler->new_entry.php = stored_php; 
    2046         stored_entry = xc_entry_php_store_unlocked(cache, compiler->entry_hash.entryslotid, &compiler->new_entry TSRMLS_CC); 
    2047         if (stored_entry) { 
    2048             xc_php_addref_unlocked(stored_php); 
    2049             TRACE(" cached %d:%s, holding", compiler->new_entry.file_inode, stored_entry->entry.name.str.val); 
    2050             xc_entry_hold_php_unlocked(cache, stored_entry TSRMLS_CC); 
    2051         } 
    2052     } LEAVE_LOCK_EX(cache); 
    2053     /* }}} */ 
    2054     TRACE("%s", stored_entry ? "stored" : "store failed"); 
    2055  
    2056     if (catched || !stored_php) { 
    2057         goto err_aftersandbox; 
    2058     } 
    2059  
    2060     cache->compiling = 0; 
    2061     xc_free_php(&compiler->new_php TSRMLS_CC); 
    2062  
    2063     if (stored_entry) { 
    2064         if (compiler->new_php.op_array) { 
    2065 #ifdef ZEND_ENGINE_2 
    2066             destroy_op_array(compiler->new_php.op_array TSRMLS_CC); 
    2067 #else 
    2068             destroy_op_array(compiler->new_php.op_array); 
    2069 #endif 
    2070             efree(compiler->new_php.op_array); 
    2071             compiler->new_php.op_array = NULL; 
    2072             h = NULL; 
    2073         } 
    2074         xc_sandbox_free(&sandbox, XC_NoInstall TSRMLS_CC); 
     2131    sandboxed_compiler.compiler = compiler; 
     2132    sandboxed_compiler.h = h; 
     2133    sandboxed_compiler.type = type; 
     2134    sandboxed_compiler.stored_php = NULL; 
     2135    sandboxed_compiler.stored_entry = NULL; 
     2136    op_array = xc_sandbox(xc_compile_file_sandboxed, (void *) &sandboxed_compiler, h->opened_path ? h->opened_path : h->filename TSRMLS_CC); 
     2137    if (sandboxed_compiler.stored_entry) { 
    20752138        return xc_compile_restore(stored_entry, stored_php, h TSRMLS_CC); 
    20762139    } 
    20772140    else { 
    2078         zend_op_array *old_active_op_array = CG(active_op_array); 
    2079         /* install it */ 
    2080         CG(active_op_array) = compiler->new_php.op_array; 
    2081         xc_sandbox_free(&sandbox, XC_Install TSRMLS_CC); 
    2082         CG(active_op_array) = old_active_op_array; 
    2083     } 
    2084     return compiler->new_php.op_array; 
    2085  
    2086 err_aftersandbox: 
    2087     xc_free_php(&compiler->new_php TSRMLS_CC); 
    2088     xc_sandbox_free(&sandbox, XC_NoInstall TSRMLS_CC); 
    2089  
    2090     cache->compiling = 0; 
    2091     if (catched) { 
    2092         cache->errors ++; 
    2093         zend_bailout(); 
    2094     } 
    2095     return compiler->new_php.op_array; 
     2141        return op_array; 
     2142    } 
    20962143} 
    20972144/* }}} */ 
     
    21312178    /* }}} */ 
    21322179 
    2133     op_array = xc_compile_file_ex(&compiler, h, type TSRMLS_CC); 
     2180    op_array = xc_compile_file_cached(&compiler, h, type TSRMLS_CC); 
    21342181 
    21352182    xc_entry_free_key_php(&compiler.new_entry TSRMLS_CC); 
  • trunk/xcache.h

    r936 r937  
    465465} xc_entry_hash_t; 
    466466/* }}} */ 
    467 typedef struct xc_compiler_t { /* {{{ */ 
    468     const char *filename; 
    469     size_t filename_len; 
    470     const char *opened_path; 
    471     char opened_path_buffer[MAXPATHLEN]; 
    472  
    473     xc_entry_hash_t entry_hash; 
    474     xc_entry_php_t new_entry; 
    475     xc_entry_data_php_t new_php; 
    476 } xc_compiler_t; 
    477 /* }}} */ 
    478467 
    479468extern zend_module_entry xcache_module_entry; 
Note: See TracChangeset for help on using the changeset viewer.