LCOV - code coverage report
Current view: directory - src/trusted/validator/x86/decoder - ncop_exps.c (source / functions) Found Hit Coverage
Test: coverage.lcov Lines: 276 204 73.9 %
Date: 2014-07-02 Functions: 0 0 -

       1                 : /*
       2                 :  * Copyright (c) 2012 The Native Client Authors. All rights reserved.
       3                 :  * Use of this source code is governed by a BSD-style license that can be
       4                 :  * found in the LICENSE file.
       5                 :  */
       6                 : 
       7                 : #include "native_client/src/trusted/validator/x86/decoder/ncop_exps.h"
       8                 : 
       9                 : #include <stdio.h>
      10                 : #include <ctype.h>
      11                 : #include <string.h>
      12                 : #include <assert.h>
      13                 : #include <sys/stat.h>
      14                 : 
      15                 : #include "native_client/src/include/portability.h"
      16                 : #include "native_client/src/shared/platform/nacl_log.h"
      17                 : #include "native_client/src/shared/utils/types.h"
      18                 : #include "native_client/src/trusted/validator/x86/decoder/gen/ncop_expr_node_flag_impl.h"
      19                 : #include "native_client/src/trusted/validator/x86/decoder/gen/ncop_expr_node_kind_impl.h"
      20                 : #include "native_client/src/trusted/validator/x86/decoder/nc_decode_tables_types.h"
      21                 : #include "native_client/src/trusted/validator/x86/decoder/nc_inst_state_internal.h"
      22                 : 
      23                 : /* To turn on debugging of instruction decoding, change value of
      24                 :  * DEBUGGING to 1.
      25                 :  *
      26                 :  * WARNING: Debugging messages inside of print messages must be sent to the
      27                 :  * same gio stream as being printed, since they may be used by another
      28                 :  * nacl log message that has locked the access to NaClLogGetGio().
      29                 :  */
      30                 : #define DEBUGGING 0
      31                 : 
      32                 : #include "native_client/src/shared/utils/debugging.h"
      33                 : 
      34                 : #include "native_client/src/trusted/validator/x86/decoder/ncopcode_desc_inl.c"
      35                 : #include "native_client/src/trusted/validator/x86/decoder/ncop_exps_inl.c"
      36                 : 
      37             144 : void NaClPrintExpFlags(struct Gio* file, NaClExpFlags flags) {
      38             144 :   if (flags == 0) {
      39               6 :     gprintf(file, "0");
      40                 :   } else {
      41                 :     NaClExpFlag f;
      42             138 :     Bool is_first = TRUE;
      43            2346 :     for (f = 0; f < NaClExpFlagEnumSize; f++) {
      44            2208 :       if (flags & NACL_EFLAG(f)) {
      45             271 :         if (is_first) {
      46             138 :           is_first = FALSE;
      47                 :         } else {
      48             133 :           gprintf(file, " | ");
      49                 :         }
      50             271 :         gprintf(file, "%s", NaClExpFlagName(f));
      51                 :       }
      52                 :     }
      53                 :   }
      54             144 : }
      55                 : 
      56                 : typedef struct NaClExpKindDescriptor {
      57                 :   /* The name of the expression operator. */
      58                 :   NaClExpKind name;
      59                 :   /* The rank (i.e. number of children) the expression operator has. */
      60                 :   const int rank;
      61                 : } NaClExpKindDescriptor;
      62                 : 
      63                 : /* The print names of valid NaClExpKind values. */
      64                 : static const NaClExpKindDescriptor
      65                 : g_NaClExpKindDesc[NaClExpKindEnumSize + 1]= {
      66                 :   {UndefinedExp, 0},
      67                 :   {ExprRegister, 0},
      68                 :   {OperandReference, 1},
      69                 :   {ExprConstant, 0},
      70                 :   {ExprSegmentAddress, 2},
      71                 :   {ExprMemOffset, 4},
      72                 :   {ExprNaClIllegal, 0},
      73                 : };
      74                 : 
      75            3954 : int NaClExpKindRank(NaClExpKind kind) {
      76            3954 :   assert(kind == g_NaClExpKindDesc[kind].name);
      77            3954 :   return g_NaClExpKindDesc[kind].rank;
      78                 : }
      79                 : 
      80                 : /* Returns the register defined by the given node. */
      81               0 : NaClOpKind NaClGetExpRegister(NaClExp* node) {
      82               0 :   return NaClGetExpRegisterInline(node);
      83                 : }
      84                 : 
      85                 : /* Returns the name of the register defined by the indexed node in the
      86                 :  * vector of nodes.
      87                 :  */
      88             424 : NaClOpKind NaClGetExpVectorRegister(NaClExpVector* vector,
      89                 :                                     int node) {
      90             424 :   return NaClGetExpRegisterInline(&vector->node[node]);
      91                 : }
      92                 : 
      93                 : static int NaClPrintDisassembledExp(struct Gio* file,
      94                 :                                     NaClInstState* state,
      95                 :                                     uint32_t index);
      96                 : 
      97                 : /* Print the characters in the given string using lower case. */
      98             761 : static void NaClPrintLower(struct Gio* file, char* str) {
      99            3957 :   while (*str) {
     100            2435 :     gprintf(file, "%c", tolower(*str));
     101            2435 :     ++str;
     102                 :   }
     103             761 : }
     104                 : 
     105                 : /* Print out the given constant expression node to the given file. */
     106             241 : static void NaClPrintDisassembledConst(
     107                 :     struct Gio* file, NaClInstState* state, NaClExp* node) {
     108             241 :   assert(node->kind == ExprConstant);
     109             241 :   if (node->flags & NACL_EFLAG(ExprJumpTarget)) {
     110              78 :     NaClPcAddress target = NaClInstStatePrintableAddress(state)
     111              39 :         + state->bytes.length + (NaClPcNumber) NaClGetExprSignedValue(node);
     112              39 :     gprintf(file, "0x%"NACL_PRIxNaClPcAddress, target);
     113             202 :   }else if (node->flags & NACL_EFLAG(ExprUnsignedHex)) {
     114             109 :     gprintf(file, "0x%"NACL_PRIx64, NaClGetExprUnsignedValue(node));
     115              93 :   } else if (node->flags & NACL_EFLAG(ExprSignedHex)) {
     116              80 :     int64_t val = NaClGetExprSignedValue(node);
     117              80 :     if (val < 0) {
     118              34 :       val = -val;
     119              34 :       gprintf(file, "-0x%"NACL_PRIx64, val);
     120                 :     } else {
     121              46 :       gprintf(file, "0x%"NACL_PRIx64, val);
     122                 :     }
     123              13 :   } else if (node->flags & NACL_EFLAG(ExprUnsignedInt)) {
     124               0 :     gprintf(file, "%"NACL_PRIu64, NaClGetExprUnsignedValue(node));
     125                 :   } else {
     126                 :     /* Assume ExprSignedInt. */
     127              13 :     gprintf(file, "%"NACL_PRId64, NaClGetExprSignedValue(node));
     128                 :   }
     129             241 : }
     130                 : 
     131                 : #define NACLOP_REG_PREFIX "Reg"
     132                 : 
     133             816 : size_t NaClOpRegName(NaClOpKind reg, char* buffer, size_t buffer_size) {
     134             816 :   const char* name = NaClOpKindName(reg);
     135                 :   char* str;
     136                 :   size_t index;
     137                 : 
     138                 :   /* Fail if no room to put register name. */
     139             816 :   if (buffer_size == 0) return 0;
     140             816 :   buffer[0] = '\0';  /* To be safe, in case we exit prematurely. */
     141                 : 
     142                 :   /* Get name for register. */
     143             816 :   name = NaClOpKindName(reg);
     144             816 :   if (NULL == name) return 0;
     145                 : 
     146                 :   /* Strip off 'Reg' prefix from register name, if it exists. */
     147             816 :   str = strstr(name, NACLOP_REG_PREFIX);
     148             816 :   if (str != name) return 0;
     149             816 :   str += strlen(NACLOP_REG_PREFIX);
     150                 : 
     151                 :   /* Copy the name, converting characters to lower case. */
     152            3015 :   for (index = 0; (index + 1) < buffer_size; ++index) {
     153            3015 :     char ch = tolower(str[index]);
     154            3015 :     if ('\0' == ch) break;
     155            2199 :     buffer[index] = tolower(str[index]);
     156                 :   }
     157                 : 
     158                 :   /* Be sure to add null character at end. */
     159             816 :   buffer[index] = '\0';
     160             816 :   return index;
     161                 : }
     162                 : 
     163                 : #define MAX_REGISTER_SIZE 256
     164                 : 
     165                 : /* Print out the disassembled representation of the given register
     166                 :  * to the given file.
     167                 :  */
     168             816 : static void NaClPrintDisassembledRegKind(struct Gio* file, NaClOpKind reg) {
     169                 :   char buffer[MAX_REGISTER_SIZE];
     170             816 :   NaClOpRegName(reg, buffer, MAX_REGISTER_SIZE);
     171             816 :   gprintf(file, "%c%s", '%', buffer);
     172             816 : }
     173                 : 
     174             571 : static INLINE void NaClPrintDisassembledReg(struct Gio* file, NaClExp* node) {
     175             571 :   NaClPrintDisassembledRegKind(file, NaClGetExpRegisterInline(node));
     176             571 : }
     177                 : 
     178              34 : void NaClExpVectorPrint(struct Gio* file, NaClInstState* state) {
     179                 :   uint32_t i;
     180              34 :   NaClExpVector* vector = NaClInstStateExpVector(state);
     181              34 :   gprintf(file, "NaClExpVector[%d] = {\n", vector->number_expr_nodes);
     182             178 :   for (i = 0; i < vector->number_expr_nodes; i++) {
     183             144 :     NaClExp* node = &vector->node[i];
     184             144 :     gprintf(file, "  { %s[%d] , ",
     185                 :             NaClExpKindName(node->kind),
     186                 :             NaClExpKindRank(node->kind));
     187             144 :     switch (node->kind) {
     188                 :       case ExprRegister:
     189              57 :         NaClPrintDisassembledReg(file, node);
     190              57 :         break;
     191                 :       case ExprConstant:
     192              25 :         NaClPrintDisassembledConst(file, state, node);
     193              25 :         break;
     194                 :       default:
     195              62 :         gprintf(file, "%"NACL_PRIu64, NaClGetExprUnsignedValue(node));
     196              62 :         break;
     197                 :     }
     198             144 :     gprintf(file, ", ");
     199             144 :     NaClPrintExpFlags(file, node->flags);
     200             144 :     gprintf(file, " },\n");
     201                 :   }
     202              34 :   gprintf(file, "};\n");
     203              34 : }
     204                 : 
     205                 : /* Print out the given (memory offset) expression node to the given file.
     206                 :  * Returns the index of the node following the given (indexed) memory offset.
     207                 :  */
     208             212 : static int NaClPrintDisassembledMemOffset(struct Gio* file,
     209                 :                                       NaClInstState *state,
     210                 :                                       int index) {
     211             212 :   NaClExpVector* vector = NaClInstStateExpVector(state);
     212             212 :   int r1_index = index + 1;
     213             212 :   int r2_index = r1_index + NaClExpWidth(vector, r1_index);
     214             212 :   int scale_index = r2_index + NaClExpWidth(vector, r2_index);
     215             212 :   int disp_index = scale_index + NaClExpWidth(vector, scale_index);
     216             212 :   NaClOpKind r1 = NaClGetExpVectorRegister(vector, r1_index);
     217             212 :   NaClOpKind r2 = NaClGetExpVectorRegister(vector, r2_index);
     218             212 :   uint64_t scale = NaClGetExprUnsignedValue(&vector->node[scale_index]);
     219             212 :   int64_t disp = NaClGetExprSignedValue(&vector->node[disp_index]);
     220             212 :   assert(ExprMemOffset == vector->node[index].kind);
     221             212 :   gprintf(file,"[");
     222             212 :   if (r1 != RegUnknown) {
     223             180 :     NaClPrintDisassembledRegKind(file, r1);
     224                 :   }
     225             212 :   if (r2 != RegUnknown) {
     226              65 :     if (r1 != RegUnknown) {
     227              56 :       gprintf(file, "+");
     228                 :     }
     229              65 :     NaClPrintDisassembledRegKind(file, r2);
     230              65 :     gprintf(file, "*%d", (uint32_t) scale);
     231                 :   }
     232             212 :   if (disp != 0) {
     233             149 :     if ((r1 != RegUnknown || r2 != RegUnknown) &&
     234              67 :         !NaClIsExpNegativeConstant(vector, disp_index)) {
     235              34 :       gprintf(file, "+");
     236                 :     }
     237                 :     /* Recurse to handle print using format flags. */
     238              82 :     NaClPrintDisassembledExp(file, state, disp_index);
     239             130 :   } else if (r1 == RegUnknown && r2 == RegUnknown) {
     240                 :     /* be sure to generate case: [0x0]. */
     241               8 :     NaClPrintDisassembledExp(file, state, disp_index);
     242                 :   }
     243             212 :   gprintf(file, "]");
     244             212 :   return disp_index + NaClExpWidth(vector, disp_index);
     245                 : }
     246                 : 
     247                 : /* Retrurns true if the segment register of the indexed segment address is DS,
     248                 :  * and DS has been marked (by the instruction) as the default register
     249                 :  * for the segment address.
     250                 :  */
     251             200 : static Bool IsSegmentAddressDsRegPair(NaClInstState* state,
     252                 :                                       int index) {
     253             200 :   NaClExpVector* vector = NaClInstStateExpVector(state);
     254             200 :   NaClExp* segment_address = &vector->node[index];
     255             200 :   NaClExp* segment_register =
     256             200 :       &vector->node[NaClGetExpKidIndex(vector, index, 0)];
     257             200 :   return NaClHasBit(segment_address->flags, NACL_EFLAG(ExprDSrCase)) &&
     258               0 :       (segment_register->kind == ExprRegister) &&
     259               0 :       (RegDS == NaClGetExpRegisterInline(segment_register));
     260                 : }
     261                 : 
     262                 : /* Retrurns true if the segment register of the index segment address is ES,
     263                 :  * and ES has been marked (by the instruction) as the default register
     264                 :  * for the segment address.
     265                 :  */
     266             200 : static Bool IsSegmentAddressEsRegPair(NaClInstState* state,
     267                 :                                       int index) {
     268             200 :   NaClExpVector* vector = NaClInstStateExpVector(state);
     269             200 :   NaClExp* segment_address = &vector->node[index];
     270             200 :   NaClExp* segment_register =
     271             200 :       &vector->node[NaClGetExpKidIndex(vector, index, 0)];
     272             200 :   return NaClHasBit(segment_address->flags, NACL_EFLAG(ExprESrCase)) &&
     273               0 :       (segment_register->kind == ExprRegister) &&
     274               0 :       (RegES == NaClGetExpRegisterInline(segment_register));
     275                 : }
     276                 : 
     277                 : /* Print out the given (segment address) expression node to the
     278                 :  * given file. Returns the index of the node following the
     279                 :  * given (indexed) segment address.
     280                 :  */
     281             200 : static int NaClPrintDisassembledSegmentAddr(struct Gio* file,
     282                 :                                             NaClInstState* state,
     283                 :                                             int index) {
     284                 :   int memory_address;
     285             200 :   NaClExpVector* vector = NaClInstStateExpVector(state);
     286                 :   /* If segment register is default. If so, do not print. */
     287             400 :   if (IsSegmentAddressDsRegPair(state, index) ||
     288             200 :       IsSegmentAddressEsRegPair(state, index)) {
     289                 :     /* Segment register matches default.  Don't print. */
     290                 :   } else {
     291                 :     /* Print the segment register associated with the segment address. */
     292             200 :     NaClPrintDisassembledExp(file, state, index + 1);
     293             200 :     gprintf(file, ":");
     294                 :   }
     295             200 :   memory_address = NaClGetExpKidIndex(vector, index, 1);
     296             200 :   if (vector->node[memory_address].kind == ExprRegister) {
     297                 :     /* Special case segment address, where the register corresponds to
     298                 :      * a memory address. Print out the register in '[]' brackets to
     299                 :      * communicate that it is a memory reference.
     300                 :      */
     301                 :     int result;
     302               0 :     gprintf(file, "[");
     303               0 :     result = NaClPrintDisassembledExp(file, state, memory_address);
     304               0 :     gprintf(file, "]");
     305               0 :     return result;
     306                 :   } else {
     307                 :     /* print out memory address associated with segment address. */
     308             200 :     return NaClPrintDisassembledExp(file, state, memory_address);
     309                 :   }
     310                 : }
     311                 : 
     312                 : /* Print out the given expression node to the given file.
     313                 :  * Returns the index of the node following the given indexed
     314                 :  * expression.
     315                 :  */
     316            1800 : static int NaClPrintDisassembledExp(struct Gio* file,
     317                 :                                     NaClInstState* state,
     318                 :                                     uint32_t index) {
     319                 :   NaClExp* node;
     320            1800 :   NaClExpVector* vector = NaClInstStateExpVector(state);
     321            1800 :   assert(index < vector->number_expr_nodes);
     322            1800 :   node = &vector->node[index];
     323            1800 :   switch (node->kind) {
     324                 :     default:
     325               0 :       gprintf(file, "undefined");
     326               0 :       return index + 1;
     327                 :     case ExprRegister:
     328             514 :       NaClPrintDisassembledReg(file, node);
     329             514 :       return index + 1;
     330                 :     case OperandReference:
     331             655 :       return NaClPrintDisassembledExp(file, state, index + 1);
     332                 :     case ExprConstant:
     333             216 :       NaClPrintDisassembledConst(file, state, node);
     334             216 :       return index + 1;
     335                 :     case ExprSegmentAddress:
     336             200 :       return NaClPrintDisassembledSegmentAddr(file, state, index);
     337                 :     case ExprMemOffset:
     338             212 :       return NaClPrintDisassembledMemOffset(file, state, index);
     339                 :     case ExprNaClIllegal:
     340               3 :       gprintf(file, "*NaClIllegal*");
     341               3 :       return index + 1;
     342                 :   }
     343                 : }
     344                 : 
     345                 : /* Returns true if there is a segment override in the segment address
     346                 :  * node defined by vector[seg_addr_index].
     347                 :  *
     348                 :  * Parameters:
     349                 :  *   vector - The node expression tree associated with the instruction.
     350                 :  *   seg_addr_index - The index to the segment address node to check.
     351                 :  *   seg_eflag - The expr flag that must be associated with the
     352                 :  *      segment address node to be considered for an override.
     353                 :  *   seg_reg - The expected (i.e. default) segment register
     354                 :  *      to be associated with the segment address.
     355                 :  */
     356               2 : static Bool NaClHasSegmentOverride(NaClExpVector* vector,
     357                 :                                    int seg_addr_index,
     358                 :                                    NaClExpFlag seg_eflag,
     359                 :                                    NaClOpKind seg_reg) {
     360               2 :   NaClExp* seg_node = &vector->node[seg_addr_index];
     361               2 :   if (seg_node->flags & NACL_EFLAG(seg_eflag)) {
     362               1 :     int seg_index = seg_addr_index + 1;
     363               1 :     NaClExp* node = &vector->node[seg_index];
     364               2 :     if ((ExprRegister == node->kind) &&
     365               1 :         (seg_reg != NaClGetExpRegisterInline(node))) {
     366               0 :       return TRUE;
     367                 :     }
     368                 :   }
     369               2 :   return FALSE;
     370                 : }
     371                 : 
     372                 : /* Prints out the segment register associated with the segment
     373                 :  * address node defined by vector[seg_addr_index].
     374                 :  *
     375                 :  * Parameters:
     376                 :  *    file - The Gio file to print the segment register to.
     377                 :  *    is_first - True if the first operand of the instruction.
     378                 :  *    vector - The node expression tree associated with the instruction.
     379                 :  *   seg_addr_index - The index to the segment address node to check.
     380                 :  */
     381               0 : static void NaClPrintSegmentOverride(struct Gio* file,
     382                 :                                      Bool* is_first,
     383                 :                                      NaClInstState* state,
     384                 :                                      NaClExpVector* vector,
     385                 :                                      int seg_addr_index) {
     386               0 :   int seg_index = seg_addr_index + 1;
     387               0 :   if (*is_first) {
     388               0 :     gprintf(file, " ");
     389               0 :     *is_first = FALSE;
     390                 :   } else {
     391               0 :     gprintf(file, ", ");
     392                 :   }
     393               0 :   NaClPrintDisassembledExp(file, state, seg_index);
     394               0 : }
     395                 : 
     396                 : /* Print the flag name if the flag is defined for the corresponding operand.
     397                 :  * Used to print out set/use/zero extend information for partial instructions.
     398                 :  */
     399               0 : static void NaClPrintAddOperandFlag(struct Gio* f,
     400                 :                                     const NaClOp* op,
     401                 :                                     NaClOpFlag flag,
     402                 :                                     const char* flag_name) {
     403               0 :   if (op->flags & NACL_OPFLAG(flag)) {
     404               0 :     gprintf(f, "%s", flag_name);
     405                 :   }
     406               0 : }
     407                 : 
     408                 : /* Print the given instruction opcode of the give state, to the
     409                 :  * given file.
     410                 :  */
     411             761 : static void NaClPrintDisassembled(struct Gio* file,
     412                 :                                   NaClInstState* state,
     413                 :                                   const NaClInst* inst) {
     414             761 :   uint32_t tree_index = 0;
     415             761 :   Bool is_first = TRUE;
     416             761 :   Bool not_printed_prefix_segment = TRUE;
     417                 :   NaClExp* node;
     418             761 :   NaClExpVector* vector = NaClInstStateExpVector(state);
     419                 : 
     420                 :   /* Print the name of the instruction. */
     421             761 :   if (NaClHasBit(inst->flags, NACL_IFLAG(PartialInstruction))) {
     422                 :     /* Instruction has been simplified. Print out corresponding
     423                 :      * hints to the reader, so that they know that the instruction
     424                 :      * has been simplified.
     425                 :      */
     426               0 :     gprintf(file, "[P] ");
     427               0 :     NaClPrintLower(file, (char*) NaClMnemonicName(inst->name));
     428               0 :     if (NaClHasBit(inst->flags, NACL_IFLAG(NaClIllegal))) {
     429               0 :       gprintf(file, "(illegal)");
     430                 :     }
     431                 :   } else {
     432             761 :     NaClPrintLower(file, (char*) NaClMnemonicName(inst->name));
     433                 :   }
     434                 : 
     435                 :   /* Use the generated expression tree to print out (non-implicit) operands
     436                 :    * of the instruction.
     437                 :    */
     438            2278 :   while (tree_index < vector->number_expr_nodes) {
     439             756 :     node = &vector->node[tree_index];
     440            1512 :     if (node->kind != OperandReference ||
     441             756 :         (NACL_EMPTY_EFLAGS == (node->flags & NACL_EFLAG(ExprImplicit)))) {
     442             655 :       if (is_first) {
     443             359 :         gprintf(file, " ");
     444             359 :         is_first = FALSE;
     445                 :       } else {
     446             296 :         gprintf(file, ", ");
     447                 :       }
     448             655 :       NaClPrintDisassembledExp(file, state, tree_index);
     449                 : 
     450                 :       /* If this is a partial instruction, add set/use information
     451                 :        * so that that it is more clear what was matched.
     452                 :        */
     453            1310 :       if (NaClHasBit(inst->flags, NACL_IFLAG(PartialInstruction)) &&
     454               0 :           node->kind == OperandReference) {
     455               0 :         const NaClOp* op =
     456               0 :             NaClGetInstOperandInline(state->decoder_tables,
     457                 :                                      inst,
     458               0 :                                      (uint8_t) NaClGetExprUnsignedValue(node));
     459               0 :         if (NaClHasBit(op->flags, (NACL_OPFLAG(OpSet) |
     460                 :                                    NACL_OPFLAG(OpUse) |
     461                 :                                    NACL_OPFLAG(OperandZeroExtends_v)))) {
     462               0 :           gprintf(file, " (");
     463               0 :           NaClPrintAddOperandFlag(file, op, OpSet, "s");
     464               0 :           NaClPrintAddOperandFlag(file, op, OpUse, "u");
     465               0 :           NaClPrintAddOperandFlag(file, op, OperandZeroExtends_v, "z");
     466               0 :           gprintf(file, ")");
     467                 :         }
     468                 :       }
     469             202 :     } else if (not_printed_prefix_segment &&
     470             202 :                (OperandReference == node->kind) &&
     471             101 :                (node->flags & NACL_EFLAG(ExprImplicit))) {
     472                 :       /* Print out segment override of implicit segment address, if
     473                 :        * applicable.
     474                 :        */
     475             101 :       if (OperandReference == node->kind) {
     476             101 :         int seg_addr_index = tree_index + 1;
     477             101 :         if (ExprSegmentAddress == vector->node[seg_addr_index].kind) {
     478               1 :           if (NaClHasSegmentOverride(vector, seg_addr_index,
     479                 :                                      ExprDSrCase, RegDS)) {
     480               0 :             NaClPrintSegmentOverride(file, &is_first, state, vector,
     481                 :                                      seg_addr_index);
     482               1 :           } else if (NaClHasSegmentOverride(vector, seg_addr_index,
     483                 :                                             ExprESrCase, RegES)) {
     484               0 :             NaClPrintSegmentOverride(file, &is_first, state, vector,
     485                 :                                      seg_addr_index);
     486                 :           }
     487                 :         }
     488                 :       }
     489                 :     }
     490                 :     /* Skip over expression to next expresssion. */
     491             756 :     tree_index += NaClExpWidth(vector, tree_index);
     492                 :   }
     493             761 : }
     494                 : 
     495             761 : void NaClInstStateInstPrint(struct Gio* file, NaClInstState* state) {
     496                 :   int i;
     497                 :   const NaClInst* inst;
     498                 : 
     499                 :   /* Print out the address and the inst bytes. */
     500             761 :   int length = NaClInstStateLength(state);
     501                 : 
     502                 :   DEBUG_OR_ERASE(
     503                 :       NaClInstPrint(file, state->decoder_tables, NaClInstStateInst(state)));
     504                 :   DEBUG(NaClExpVectorPrint(file, state));
     505             761 :   gprintf(file, "%"NACL_PRIxNaClPcAddressAll": ",
     506                 :           NaClInstStatePrintableAddress(state));
     507            3070 :   for (i = 0; i < length; ++i) {
     508            2309 :     gprintf(file, "%02"NACL_PRIx8" ", NaClInstStateByte(state, i));
     509                 :   }
     510            9867 :   for (i = length; i < NACL_MAX_BYTES_PER_X86_INSTRUCTION; ++i) {
     511            9106 :     gprintf(file, "   ");
     512                 :   }
     513                 : 
     514                 :   /* Print out the assembly instruction it disassembles to. */
     515             761 :   inst = NaClInstStateInst(state);
     516             761 :   NaClPrintDisassembled(file, state, inst);
     517             761 :   gprintf(file, "\n");
     518             761 : }
     519                 : 
     520                 : /* Defines a buffer size big enough to hold an instruction. */
     521                 : #define MAX_INST_TEXT_SIZE 256
     522                 : 
     523               0 : char* NaClInstStateInstructionToString(struct NaClInstState* state) {
     524                 :   /* Print to a memory buffer, and then duplicate. */
     525                 :   struct GioMemoryFile filemem;
     526               0 :   struct Gio *file = (struct Gio*) &filemem;
     527                 :   char buffer[MAX_INST_TEXT_SIZE];
     528                 :   char* result;
     529                 : 
     530                 :   /* Note: Be sure to leave an extra byte to add the null character to
     531                 :    * the end of the string.
     532                 :    */
     533               0 :   GioMemoryFileCtor(&filemem, buffer, MAX_INST_TEXT_SIZE - 1);
     534               0 :   NaClInstStateInstPrint(file, state);
     535               0 :   buffer[filemem.curpos < MAX_INST_TEXT_SIZE
     536               0 :          ? filemem.curpos : MAX_INST_TEXT_SIZE] ='\0';
     537               0 :   result = strdup(buffer);
     538               0 :   GioMemoryFileDtor(file);
     539               0 :   return result;
     540                 : }
     541                 : 
     542            3810 : int NaClExpWidth(NaClExpVector* vector, int node) {
     543                 :   int i;
     544            3810 :   int count = 1;
     545            3810 :   int num_kids = NaClExpKindRank(vector->node[node].kind);
     546            5816 :   for (i = 0; i < num_kids; i++) {
     547            2006 :     count += NaClExpWidth(vector, node + count);
     548                 :   }
     549            3810 :   return count;
     550                 : }
     551                 : 
     552             600 : int NaClGetExpKidIndex(NaClExpVector* vector, int node, int kid) {
     553             600 :   node++;
     554            1400 :   while (kid-- > 0) {
     555             200 :     node += NaClExpWidth(vector, node);
     556                 :   }
     557             600 :   return node;
     558                 : }
     559                 : 
     560               0 : int NaClGetExpParentIndex(NaClExpVector* vector, int index) {
     561                 :   int node_rank;
     562               0 :   int num_kids = 1;
     563               0 :   while (index > 0) {
     564               0 :     --index;
     565               0 :     node_rank = NaClExpKindRank(vector->node[index].kind);
     566               0 :     if (node_rank >= num_kids) {
     567               0 :       return index;
     568                 :     } else {
     569               0 :       num_kids -= (node_rank - 1);
     570                 :     }
     571                 :   }
     572               0 :   return -1;
     573                 : }
     574                 : 
     575               0 : int NaClGetNthExpKind(NaClExpVector* vector,
     576                 :                       NaClExpKind kind,
     577                 :                       int n) {
     578               0 :   if (n > 0) {
     579                 :     uint32_t i;
     580               0 :     for (i = 0; i < vector->number_expr_nodes; ++i) {
     581               0 :       if (kind == vector->node[i].kind) {
     582               0 :         --n;
     583               0 :         if (n == 0) return i;
     584                 :       }
     585                 :     }
     586                 :   }
     587               0 :   return -1;
     588                 : }
     589                 : 
     590              67 : Bool NaClIsExpNegativeConstant(NaClExpVector* vector, int index) {
     591              67 :   NaClExp* node = &vector->node[index];
     592              67 :   switch (node->kind) {
     593                 :     case ExprConstant:
     594             134 :       if (node->flags & NACL_EFLAG(ExprUnsignedHex) ||
     595              67 :           node->flags & NACL_EFLAG(ExprUnsignedInt)) {
     596               0 :         return FALSE;
     597                 :       } else {
     598                 :         /* Assume signed value. */
     599              67 :         return NaClGetExprSignedValue(node) < 0;
     600                 :       }
     601                 :       break;
     602                 :     default:
     603               0 :       break;
     604                 :   }
     605               0 :   return FALSE;
     606                 : }
     607                 : 
     608                 : /* Dummy routine to allow unreferenced NaClGetInstNumberOperandsInline
     609                 :  * inline.
     610                 :  */
     611               0 : uint8_t NaClNcopExpsDummyNaClGetInstNumberOperands(const NaClInst* inst) {
     612               0 :   return NaClGetInstNumberOperandsInline(inst);
     613                 : }

Generated by: LCOV version 1.7