Changeset 937 in svn for trunk/disassembler.c


Ignore:
Timestamp:
2012-06-28T11:38:30+02:00 (3 years ago)
Author:
Xuefer
Message:

still WIP: rewrite sandbox to use callback

File:
1 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/* }}} */
Note: See TracChangeset for help on using the changeset viewer.