Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | #include "ia32_modrm.h" |
michael@0 | 2 | #include "ia32_reg.h" |
michael@0 | 3 | #include "x86_imm.h" |
michael@0 | 4 | |
michael@0 | 5 | /* NOTE: when decoding ModR/M and SIB, we have to add 1 to all register |
michael@0 | 6 | * values obtained from decoding the ModR/M or SIB byte, since they |
michael@0 | 7 | * are encoded with eAX = 0 and the tables in ia32_reg.c use eAX = 1. |
michael@0 | 8 | * ADDENDUM: this is only the case when the register value is used |
michael@0 | 9 | * directly as an index into the register table, not when it is added to |
michael@0 | 10 | * a genregs offset. */ |
michael@0 | 11 | |
michael@0 | 12 | /* -------------------------------- ModR/M, SIB */ |
michael@0 | 13 | /* ModR/M flags */ |
michael@0 | 14 | #define MODRM_RM_SIB 0x04 /* R/M == 100 */ |
michael@0 | 15 | #define MODRM_RM_NOREG 0x05 /* R/B == 101 */ |
michael@0 | 16 | |
michael@0 | 17 | /* if (MODRM.MOD_NODISP && MODRM.RM_NOREG) then just disp32 */ |
michael@0 | 18 | #define MODRM_MOD_NODISP 0x00 /* mod == 00 */ |
michael@0 | 19 | #define MODRM_MOD_DISP8 0x01 /* mod == 01 */ |
michael@0 | 20 | #define MODRM_MOD_DISP32 0x02 /* mod == 10 */ |
michael@0 | 21 | #define MODRM_MOD_NOEA 0x03 /* mod == 11 */ |
michael@0 | 22 | |
michael@0 | 23 | /* 16-bit modrm flags */ |
michael@0 | 24 | #define MOD16_MOD_NODISP 0 |
michael@0 | 25 | #define MOD16_MOD_DISP8 1 |
michael@0 | 26 | #define MOD16_MOD_DISP16 2 |
michael@0 | 27 | #define MOD16_MOD_REG 3 |
michael@0 | 28 | |
michael@0 | 29 | #define MOD16_RM_BXSI 0 |
michael@0 | 30 | #define MOD16_RM_BXDI 1 |
michael@0 | 31 | #define MOD16_RM_BPSI 2 |
michael@0 | 32 | #define MOD16_RM_BPDI 3 |
michael@0 | 33 | #define MOD16_RM_SI 4 |
michael@0 | 34 | #define MOD16_RM_DI 5 |
michael@0 | 35 | #define MOD16_RM_BP 6 |
michael@0 | 36 | #define MOD16_RM_BX 7 |
michael@0 | 37 | |
michael@0 | 38 | /* SIB flags */ |
michael@0 | 39 | #define SIB_INDEX_NONE 0x04 |
michael@0 | 40 | #define SIB_BASE_EBP 0x05 |
michael@0 | 41 | #define SIB_SCALE_NOBASE 0x00 |
michael@0 | 42 | |
michael@0 | 43 | /* Convenience struct for modR/M bitfield */ |
michael@0 | 44 | struct modRM_byte { |
michael@0 | 45 | unsigned int mod : 2; |
michael@0 | 46 | unsigned int reg : 3; |
michael@0 | 47 | unsigned int rm : 3; |
michael@0 | 48 | }; |
michael@0 | 49 | |
michael@0 | 50 | /* Convenience struct for SIB bitfield */ |
michael@0 | 51 | struct SIB_byte { |
michael@0 | 52 | unsigned int scale : 2; |
michael@0 | 53 | unsigned int index : 3; |
michael@0 | 54 | unsigned int base : 3; |
michael@0 | 55 | }; |
michael@0 | 56 | |
michael@0 | 57 | |
michael@0 | 58 | #if 0 |
michael@0 | 59 | int modrm_rm[] = {0,1,2,3,MODRM_RM_SIB,MODRM_MOD_DISP32,6,7}; |
michael@0 | 60 | int modrm_reg[] = {0, 1, 2, 3, 4, 5, 6, 7}; |
michael@0 | 61 | int modrm_mod[] = {0, MODRM_MOD_DISP8, MODRM_MOD_DISP32, MODRM_MOD_NOEA}; |
michael@0 | 62 | int sib_scl[] = {0, 2, 4, 8}; |
michael@0 | 63 | int sib_idx[] = {0, 1, 2, 3, SIB_INDEX_NONE, 5, 6, 7 }; |
michael@0 | 64 | int sib_bas[] = {0, 1, 2, 3, 4, SIB_SCALE_NOBASE, 6, 7 }; |
michael@0 | 65 | #endif |
michael@0 | 66 | |
michael@0 | 67 | /* this is needed to replace x86_imm_signsized() which does not sign-extend |
michael@0 | 68 | * to dest */ |
michael@0 | 69 | static unsigned int imm32_signsized( unsigned char *buf, size_t buf_len, |
michael@0 | 70 | int32_t *dest, unsigned int size ) { |
michael@0 | 71 | if ( size > buf_len ) { |
michael@0 | 72 | return 0; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | switch (size) { |
michael@0 | 76 | case 1: |
michael@0 | 77 | *dest = *((signed char *) buf); |
michael@0 | 78 | break; |
michael@0 | 79 | case 2: |
michael@0 | 80 | *dest = *((signed short *) buf); |
michael@0 | 81 | break; |
michael@0 | 82 | case 4: |
michael@0 | 83 | default: |
michael@0 | 84 | *dest = *((signed int *) buf); |
michael@0 | 85 | break; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | return size; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | |
michael@0 | 92 | |
michael@0 | 93 | static void byte_decode(unsigned char b, struct modRM_byte *modrm) { |
michael@0 | 94 | /* generic bitfield-packing routine */ |
michael@0 | 95 | |
michael@0 | 96 | modrm->mod = b >> 6; /* top 2 bits */ |
michael@0 | 97 | modrm->reg = (b & 56) >> 3; /* middle 3 bits */ |
michael@0 | 98 | modrm->rm = b & 7; /* bottom 3 bits */ |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | |
michael@0 | 102 | static size_t sib_decode( unsigned char *buf, size_t buf_len, x86_ea_t *ea, |
michael@0 | 103 | unsigned int mod ) { |
michael@0 | 104 | /* set Address Expression fields (scale, index, base, disp) |
michael@0 | 105 | * according to the contents of the SIB byte. |
michael@0 | 106 | * b points to the SIB byte in the instruction-stream buffer; the |
michael@0 | 107 | * byte after b[0] is therefore the byte after the SIB |
michael@0 | 108 | * returns number of bytes 'used', including the SIB byte */ |
michael@0 | 109 | size_t size = 1; /* start at 1 for SIB byte */ |
michael@0 | 110 | struct SIB_byte sib; |
michael@0 | 111 | |
michael@0 | 112 | if ( buf_len < 1 ) { |
michael@0 | 113 | return 0; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | byte_decode( *buf, (struct modRM_byte *)(void*)&sib ); /* get bit-fields */ |
michael@0 | 117 | |
michael@0 | 118 | if ( sib.base == SIB_BASE_EBP && ! mod ) { /* if base == 101 (ebp) */ |
michael@0 | 119 | /* IF BASE == EBP, deal with exception */ |
michael@0 | 120 | /* IF (ModR/M did not create a Disp */ |
michael@0 | 121 | /* ... create a 32-bit Displacement */ |
michael@0 | 122 | imm32_signsized( &buf[1], buf_len, &ea->disp, sizeof(int32_t)); |
michael@0 | 123 | ea->disp_size = sizeof(int32_t); |
michael@0 | 124 | ea->disp_sign = (ea->disp < 0) ? 1 : 0; |
michael@0 | 125 | size += 4; /* add sizeof disp to count */ |
michael@0 | 126 | |
michael@0 | 127 | } else { |
michael@0 | 128 | /* ELSE BASE refers to a General Register */ |
michael@0 | 129 | ia32_handle_register( &ea->base, sib.base + 1 ); |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | /* set scale to 1, 2, 4, 8 */ |
michael@0 | 133 | ea->scale = 1 << sib.scale; |
michael@0 | 134 | |
michael@0 | 135 | if (sib.index != SIB_INDEX_NONE) { |
michael@0 | 136 | /* IF INDEX is not 'ESP' (100) */ |
michael@0 | 137 | ia32_handle_register( &ea->index, sib.index + 1 ); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | return (size); /* return number of bytes processed */ |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | static size_t modrm_decode16( unsigned char *buf, unsigned int buf_len, |
michael@0 | 144 | x86_op_t *op, struct modRM_byte *modrm ) { |
michael@0 | 145 | /* 16-bit mode: hackish, but not as hackish as 32-bit mode ;) */ |
michael@0 | 146 | size_t size = 1; /* # of bytes decoded [1 for modR/M byte] */ |
michael@0 | 147 | x86_ea_t * ea = &op->data.expression; |
michael@0 | 148 | |
michael@0 | 149 | switch( modrm->rm ) { |
michael@0 | 150 | case MOD16_RM_BXSI: |
michael@0 | 151 | ia32_handle_register(&ea->base, REG_WORD_OFFSET + 3); |
michael@0 | 152 | ia32_handle_register(&ea->index, REG_WORD_OFFSET + 6); |
michael@0 | 153 | break; |
michael@0 | 154 | case MOD16_RM_BXDI: |
michael@0 | 155 | ia32_handle_register(&ea->base, REG_WORD_OFFSET + 3); |
michael@0 | 156 | ia32_handle_register(&ea->index, REG_WORD_OFFSET + 7); |
michael@0 | 157 | case MOD16_RM_BPSI: |
michael@0 | 158 | op->flags |= op_ss_seg; |
michael@0 | 159 | ia32_handle_register(&ea->base, REG_WORD_OFFSET + 5); |
michael@0 | 160 | ia32_handle_register(&ea->index, REG_WORD_OFFSET + 6); |
michael@0 | 161 | break; |
michael@0 | 162 | case MOD16_RM_BPDI: |
michael@0 | 163 | op->flags |= op_ss_seg; |
michael@0 | 164 | ia32_handle_register(&ea->base, REG_WORD_OFFSET + 5); |
michael@0 | 165 | ia32_handle_register(&ea->index, REG_WORD_OFFSET + 7); |
michael@0 | 166 | break; |
michael@0 | 167 | case MOD16_RM_SI: |
michael@0 | 168 | ia32_handle_register(&ea->base, REG_WORD_OFFSET + 6); |
michael@0 | 169 | break; |
michael@0 | 170 | case MOD16_RM_DI: |
michael@0 | 171 | ia32_handle_register(&ea->base, REG_WORD_OFFSET + 7); |
michael@0 | 172 | break; |
michael@0 | 173 | case MOD16_RM_BP: |
michael@0 | 174 | if ( modrm->mod != MOD16_MOD_NODISP ) { |
michael@0 | 175 | op->flags |= op_ss_seg; |
michael@0 | 176 | ia32_handle_register(&ea->base, |
michael@0 | 177 | REG_WORD_OFFSET + 5); |
michael@0 | 178 | } |
michael@0 | 179 | break; |
michael@0 | 180 | case MOD16_RM_BX: |
michael@0 | 181 | ia32_handle_register(&ea->base, REG_WORD_OFFSET + 3); |
michael@0 | 182 | break; |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | /* move to byte after ModR/M */ |
michael@0 | 186 | ++buf; |
michael@0 | 187 | --buf_len; |
michael@0 | 188 | |
michael@0 | 189 | if ( modrm->mod == MOD16_MOD_DISP8 ) { |
michael@0 | 190 | imm32_signsized( buf, buf_len, &ea->disp, sizeof(char) ); |
michael@0 | 191 | ea->disp_sign = (ea->disp < 0) ? 1 : 0; |
michael@0 | 192 | ea->disp_size = sizeof(char); |
michael@0 | 193 | size += sizeof(char); |
michael@0 | 194 | } else if ( modrm->mod == MOD16_MOD_DISP16 ) { |
michael@0 | 195 | imm32_signsized( buf, buf_len, &ea->disp, sizeof(short) ); |
michael@0 | 196 | ea->disp_sign = (ea->disp < 0) ? 1 : 0; |
michael@0 | 197 | ea->disp_size = sizeof(short); |
michael@0 | 198 | size += sizeof(short); |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | return size; |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | /* TODO : Mark index modes |
michael@0 | 205 | Use addressing mode flags to imply arrays (index), structure (disp), |
michael@0 | 206 | two-dimensional arrays [disp + index], classes [ea reg], and so on. |
michael@0 | 207 | */ |
michael@0 | 208 | size_t ia32_modrm_decode( unsigned char *buf, unsigned int buf_len, |
michael@0 | 209 | x86_op_t *op, x86_insn_t *insn, size_t gen_regs ) { |
michael@0 | 210 | /* create address expression and/or fill operand based on value of |
michael@0 | 211 | * ModR/M byte. Calls sib_decode as appropriate. |
michael@0 | 212 | * flags specifies whether Reg or mod+R/M fields are being decoded |
michael@0 | 213 | * returns the number of bytes in the instruction, including modR/M */ |
michael@0 | 214 | struct modRM_byte modrm; |
michael@0 | 215 | size_t size = 1; /* # of bytes decoded [1 for modR/M byte] */ |
michael@0 | 216 | x86_ea_t * ea; |
michael@0 | 217 | |
michael@0 | 218 | |
michael@0 | 219 | byte_decode(*buf, &modrm); /* get bitfields */ |
michael@0 | 220 | |
michael@0 | 221 | /* first, handle the case where the mod field is a register only */ |
michael@0 | 222 | if ( modrm.mod == MODRM_MOD_NOEA ) { |
michael@0 | 223 | op->type = op_register; |
michael@0 | 224 | ia32_handle_register(&op->data.reg, modrm.rm + gen_regs); |
michael@0 | 225 | /* increase insn size by 1 for modrm byte */ |
michael@0 | 226 | return 1; |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | /* then deal with cases where there is an effective address */ |
michael@0 | 230 | ea = &op->data.expression; |
michael@0 | 231 | op->type = op_expression; |
michael@0 | 232 | op->flags |= op_pointer; |
michael@0 | 233 | |
michael@0 | 234 | if ( insn->addr_size == 2 ) { |
michael@0 | 235 | /* gah! 16 bit mode! */ |
michael@0 | 236 | return modrm_decode16( buf, buf_len, op, &modrm); |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | /* move to byte after ModR/M */ |
michael@0 | 240 | ++buf; |
michael@0 | 241 | --buf_len; |
michael@0 | 242 | |
michael@0 | 243 | if (modrm.mod == MODRM_MOD_NODISP) { /* if mod == 00 */ |
michael@0 | 244 | |
michael@0 | 245 | /* IF MOD == No displacement, just Indirect Register */ |
michael@0 | 246 | if (modrm.rm == MODRM_RM_NOREG) { /* if r/m == 101 */ |
michael@0 | 247 | /* IF RM == No Register, just Displacement */ |
michael@0 | 248 | /* This is an Intel Moronic Exception TM */ |
michael@0 | 249 | imm32_signsized( buf, buf_len, &ea->disp, |
michael@0 | 250 | sizeof(int32_t) ); |
michael@0 | 251 | ea->disp_size = sizeof(int32_t); |
michael@0 | 252 | ea->disp_sign = (ea->disp < 0) ? 1 : 0; |
michael@0 | 253 | size += 4; /* add sizeof disp to count */ |
michael@0 | 254 | |
michael@0 | 255 | } else if (modrm.rm == MODRM_RM_SIB) { /* if r/m == 100 */ |
michael@0 | 256 | /* ELSE IF an SIB byte is present */ |
michael@0 | 257 | /* TODO: check for 0 retval */ |
michael@0 | 258 | size += sib_decode( buf, buf_len, ea, modrm.mod); |
michael@0 | 259 | /* move to byte after SIB for displacement */ |
michael@0 | 260 | ++buf; |
michael@0 | 261 | --buf_len; |
michael@0 | 262 | } else { /* modR/M specifies base register */ |
michael@0 | 263 | /* ELSE RM encodes a general register */ |
michael@0 | 264 | ia32_handle_register( &ea->base, modrm.rm + 1 ); |
michael@0 | 265 | } |
michael@0 | 266 | } else { /* mod is 01 or 10 */ |
michael@0 | 267 | if (modrm.rm == MODRM_RM_SIB) { /* rm == 100 */ |
michael@0 | 268 | /* IF base is an AddrExpr specified by an SIB byte */ |
michael@0 | 269 | /* TODO: check for 0 retval */ |
michael@0 | 270 | size += sib_decode( buf, buf_len, ea, modrm.mod); |
michael@0 | 271 | /* move to byte after SIB for displacement */ |
michael@0 | 272 | ++buf; |
michael@0 | 273 | --buf_len; |
michael@0 | 274 | } else { |
michael@0 | 275 | /* ELSE base is a general register */ |
michael@0 | 276 | ia32_handle_register( &ea->base, modrm.rm + 1 ); |
michael@0 | 277 | } |
michael@0 | 278 | |
michael@0 | 279 | /* ELSE mod + r/m specify a disp##[base] or disp##(SIB) */ |
michael@0 | 280 | if (modrm.mod == MODRM_MOD_DISP8) { /* mod == 01 */ |
michael@0 | 281 | /* If this is an 8-bit displacement */ |
michael@0 | 282 | imm32_signsized( buf, buf_len, &ea->disp, |
michael@0 | 283 | sizeof(char)); |
michael@0 | 284 | ea->disp_size = sizeof(char); |
michael@0 | 285 | ea->disp_sign = (ea->disp < 0) ? 1 : 0; |
michael@0 | 286 | size += 1; /* add sizeof disp to count */ |
michael@0 | 287 | |
michael@0 | 288 | } else { |
michael@0 | 289 | /* Displacement is dependent on address size */ |
michael@0 | 290 | imm32_signsized( buf, buf_len, &ea->disp, |
michael@0 | 291 | insn->addr_size); |
michael@0 | 292 | ea->disp_size = insn->addr_size; |
michael@0 | 293 | ea->disp_sign = (ea->disp < 0) ? 1 : 0; |
michael@0 | 294 | size += 4; |
michael@0 | 295 | } |
michael@0 | 296 | } |
michael@0 | 297 | |
michael@0 | 298 | return size; /* number of bytes found in instruction */ |
michael@0 | 299 | } |
michael@0 | 300 | |
michael@0 | 301 | void ia32_reg_decode( unsigned char byte, x86_op_t *op, size_t gen_regs ) { |
michael@0 | 302 | struct modRM_byte modrm; |
michael@0 | 303 | byte_decode( byte, &modrm ); /* get bitfields */ |
michael@0 | 304 | |
michael@0 | 305 | /* set operand to register ID */ |
michael@0 | 306 | op->type = op_register; |
michael@0 | 307 | ia32_handle_register(&op->data.reg, modrm.reg + gen_regs); |
michael@0 | 308 | |
michael@0 | 309 | return; |
michael@0 | 310 | } |