Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: src/trusted/validator_x86/ncvalidate_iter.c

Issue 5738003: Resurrect Petr's 64-bit dynamic code modification CL:... (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client/
Patch Set: '' Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright 2009 The Native Client Authors. All rights reserved. 2 * Copyright 2009 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can 3 * Use of this source code is governed by a BSD-style license that can
4 * be found in the LICENSE file. 4 * be found in the LICENSE file.
5 */ 5 */
6 6
7 /* 7 /*
8 * ncvalidate_iter.c 8 * ncvalidate_iter.c
9 * Validate x86 instructions for Native Client 9 * Validate x86 instructions for Native Client
10 * 10 *
11 */ 11 */
12 12
13 #include <assert.h> 13 #include <assert.h>
14 #include <string.h> 14 #include <string.h>
15 15
16 #include "native_client/src/trusted/validator_x86/ncvalidate_iter.h" 16 #include "native_client/src/trusted/validator_x86/ncvalidate_iter.h"
17 17 #include "native_client/src/shared/platform/nacl_check.h"
18 #include "native_client/src/shared/platform/nacl_log.h" 18 #include "native_client/src/shared/platform/nacl_log.h"
19 #include "native_client/src/trusted/validator_x86/nc_inst_iter.h" 19 #include "native_client/src/trusted/validator_x86/nc_inst_iter.h"
20 #include "native_client/src/trusted/validator_x86/nc_inst_state_internal.h" 20 #include "native_client/src/trusted/validator_x86/nc_inst_state_internal.h"
21 #include "native_client/src/trusted/validator_x86/nc_segment.h" 21 #include "native_client/src/trusted/validator_x86/nc_segment.h"
22 #include "native_client/src/trusted/validator_x86/ncop_exps.h" 22 #include "native_client/src/trusted/validator_x86/ncop_exps.h"
23 #include "native_client/src/trusted/validator_x86/ncvalidate_iter_internal.h" 23 #include "native_client/src/trusted/validator_x86/ncvalidate_iter_internal.h"
24 #include "native_client/src/trusted/validator_x86/ncvalidator_registry.h" 24 #include "native_client/src/trusted/validator_x86/ncvalidator_registry.h"
25 25
26 /* To turn on debugging of instruction decoding, change value of 26 /* To turn on debugging of instruction decoding, change value of
27 * DEBUGGING to 1. 27 * DEBUGGING to 1.
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 NaClLogV_mu(level, format, ap); 252 NaClLogV_mu(level, format, ap);
253 va_end(ap); 253 va_end(ap);
254 NaClLogUnlock(); 254 NaClLogUnlock();
255 NaClRecordErrorReported(state, level); 255 NaClRecordErrorReported(state, level);
256 } 256 }
257 if (state->do_stub_out && (level <= LOG_ERROR)) { 257 if (state->do_stub_out && (level <= LOG_ERROR)) {
258 memset(inst->mpc, kNaClFullStop, inst->length); 258 memset(inst->mpc, kNaClFullStop, inst->length);
259 } 259 }
260 } 260 }
261 261
262 void NaClValidatorTwoInstMessage(int level,
263 NaClValidatorState* state,
264 NaClInstState* inst1,
265 NaClInstState* inst2,
266 const char* format,
267 ...) {
268 level = NaClRecordIfValidatorError(state, level);
269 if (NaClPrintValidatorMessages(state, level)) {
270 va_list ap;
271 struct Gio* g = NaClLogGetGio();
272
273 NaClLogLock();
274 /* TODO(karl) - Make printing of instruction state possible via format. */
275 va_start(ap, format);
276 NaClLog_mu(level, "VALIDATOR: %s", NaClLogLevelLabel(level));
277 NaClLogV_mu(level, format, ap);
278 va_end(ap);
279
280 /* TODO(karl): empty fmt strings not allowed */
281 NaClLog_mu(level, "\n%45s ", "VALIDATOR:");
282 NaClInstStateInstPrint(g, inst1);
283 /* TODO(karl): empty fmt strings not allowed */
284 NaClLog_mu(level, "%45s ", "VALIDATOR:");
285 NaClInstStateInstPrint(g, inst2);
286
287 NaClLogUnlock();
288 NaClRecordErrorReported(state, level);
289 }
290 if (state->do_stub_out && (level <= LOG_ERROR)) {
291 memset(inst1->mpc, kNaClFullStop, inst1->length);
292 memset(inst2->mpc, kNaClFullStop, inst2->length);
293 }
294 }
295
262 Bool NaClValidatorQuit(NaClValidatorState* state) { 296 Bool NaClValidatorQuit(NaClValidatorState* state) {
263 return !state->validates_ok && (state->quit_after_error_count == 0); 297 return !state->validates_ok && (state->quit_after_error_count == 0);
264 } 298 }
265 299
266 void NaClRegisterValidator( 300 void NaClRegisterValidator(
267 NaClValidatorState* state, 301 NaClValidatorState* state,
268 NaClValidator validator, 302 NaClValidator validator,
269 NaClValidatorPostValidate post_validate, 303 NaClValidatorPostValidate post_validate,
270 NaClValidatorPrintStats print_stats, 304 NaClValidatorPrintStats print_stats,
271 NaClValidatorMemoryCreate create_memory, 305 NaClValidatorMemoryCreate create_memory,
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 void* NaClGetValidatorLocalMemory(NaClValidator validator, 477 void* NaClGetValidatorLocalMemory(NaClValidator validator,
444 const NaClValidatorState* state) { 478 const NaClValidatorState* state) {
445 int i; 479 int i;
446 for (i = 0; i < state->number_validators; ++i) { 480 for (i = 0; i < state->number_validators; ++i) {
447 if (state->validators[i].validator == validator) { 481 if (state->validators[i].validator == validator) {
448 return state->local_memory[i]; 482 return state->local_memory[i];
449 } 483 }
450 } 484 }
451 return NULL; 485 return NULL;
452 } 486 }
487
488 /*
489 * Check that iter_new is a valid replacement for iter_old.
490 * If a validation error occurs, state->validates_ok will be set to false by
491 * NaClValidatorInstMessage when it is given LOG_ERROR, see the end of this
492 * function.
493 */
494 static void NaClValidateInstReplacement(NaClInstIter* iter_old,
495 NaClInstIter* iter_new,
496 struct NaClValidatorState* state) {
497 NaClInstState *istate_old, *istate_new;
498 NaClExpVector *exp_old, *exp_new;
499 uint32_t i;
500
501 istate_old = NaClInstIterGetState(iter_old);
502 istate_new = NaClInstIterGetState(iter_new);
503
504 /* Location/length must match */
505 if (istate_new->vpc != istate_old->vpc ||
506 istate_new->length != istate_old->length) {
507 NaClValidatorTwoInstMessage(LOG_ERROR, state, istate_old, istate_new,
508 "Code modification: instructions length/addresses do not match");
509 return;
510 }
511
512
513 do {
514 /* fast check if the replacement is identical */
515 if (!memcmp(istate_old->mpc, istate_new->mpc, istate_old->length))
516 return;
517
518 if (istate_old->num_prefix_bytes != istate_new->num_prefix_bytes)
519 break;
520 if (istate_old->num_rex_prefixes != istate_new->num_rex_prefixes)
521 break;
522 if (istate_old->rexprefix != istate_new->rexprefix)
523 break;
524 if (istate_old->modrm != istate_new->modrm)
525 break;
526 if (istate_old->has_sib != istate_new->has_sib)
527 break;
528 if (istate_old->has_sib && istate_old->sib != istate_new->sib)
529 break;
530 if (istate_old->operand_size != istate_new->operand_size)
531 break;
532 if (istate_old->address_size != istate_new->address_size)
533 break;
534 if (istate_old->prefix_mask != istate_new->prefix_mask)
535 break;
536
537 /*
538 * these are pointers, but they reference entries in a static table,
539 * so if the two instructions are the same, then these pointers must
540 * reference the same entry
541 */
542 if (istate_old->inst != istate_new->inst)
543 break;
544
545 exp_old = NaClInstStateExpVector(istate_old);
546 exp_new = NaClInstStateExpVector(istate_new);
547
548 /* check if the instruction operands are identical */
549 if (exp_old->number_expr_nodes != exp_new->number_expr_nodes)
550 break;
551
552 for (i = 0; i < exp_old->number_expr_nodes; i++) {
553 if (exp_old->node[i].kind != exp_new->node[i].kind) {
554 goto error_exit;
555 }
556 if (exp_old->node[i].flags != exp_new->node[i].flags) {
557 goto error_exit;
558 }
559
560 /*
561 * allow some constants to be different; however it is important not to
562 * allow modification of sandboxing instructions. Note neither of the
563 * instructions allowed for modification is used for sandboxing
564 */
565 if (exp_old->node[i].value != exp_new->node[i].value) {
566 if (exp_old->node[i].kind == ExprConstant) {
567
568 /* allow different constants in direct calls */
569 if (istate_old->inst->name == InstCall)
570 if (exp_old->node[i].flags & NACL_EFLAG(ExprJumpTarget))
571 if (exp_old->node[NaClGetExpParentIndex(exp_old, i)].kind
572 == OperandReference)
573 continue;
574
575 /*
576 * allow different constants in operand of mov
577 * e.g. mov $rax, 0xdeadbeef
578 */
579 if (istate_old->inst->name == InstMov)
580 if (exp_old->node[i].flags & NACL_EFLAG(ExprUsed))
581 if (exp_old->node[NaClGetExpParentIndex(exp_old, i)].kind
582 == OperandReference)
583 continue;
584 /*
585 * allow different displacements in memory reference of mov
586 * instructions e.g. mov $rax, [$r15+$rbx*2+0x7fff]
587 */
588 if (istate_old->inst->name == InstMov)
589 if (exp_old->node[NaClGetExpParentIndex(exp_old, i)].kind
590 == ExprMemOffset)
591 /* displacement is the fourth node after ExprMemOffset node */
592 if (i - NaClGetExpParentIndex(exp_old, i) == 4)
593 continue;
594 }
595
596 goto error_exit;
597 }
598 }
599
600 /* This return signifies there is no error in validation. */
601 return;
602 } while (0);
603
604 error_exit:
605 /* This logging function is the mechanism that sets the validator state
606 * to indicate an error.
607 */
608 NaClValidatorTwoInstMessage(LOG_ERROR, state, istate_old, istate_new,
609 "Code modification: failed to modify instruction");
610 }
611
612 /*
613 * Validate a new code block as a replacement for an existing block.
614 * Note: The code location (vbase) must be bundle aligned and the segment size
615 * must also be a multiple of bundle size (checked in
616 * NaClValidateCodeReplacement).This is to ensure all NaCl psuedo-instructions
617 * and guard sequences are properly inspected and maintained.
618 */
619 void NaClValidateSegmentPair(uint8_t* mbase_old, uint8_t* mbase_new,
620 NaClPcAddress vbase, size_t size,
621 struct NaClValidatorState* state) {
622 NaClSegment segment_old, segment_new;
623 NaClInstIter *iter_old, *iter_new;
624
625 NaClValidatorStateInitializeValidators(state);
626 NaClSegmentInitialize(mbase_old, vbase, size, &segment_old);
627 NaClSegmentInitialize(mbase_new, vbase, size, &segment_new);
628 iter_old = NaClInstIterCreateWithLookback(&segment_old, kLookbackSize);
629 iter_new = NaClInstIterCreateWithLookback(&segment_new, kLookbackSize);
630 while (NaClInstIterHasNext(iter_old) &&
631 NaClInstIterHasNext(iter_new)) {
632 state->cur_inst_state = NaClInstIterGetState(iter_new);
633 state->cur_inst = NaClInstStateInst(state->cur_inst_state);
634 state->cur_inst_vector = NaClInstStateExpVector(state->cur_inst_state);
635 NaClApplyValidators(state, iter_new);
636 NaClValidateInstReplacement(iter_old, iter_new, state);
637 if (state->quit) break;
638 NaClInstIterAdvance(iter_old);
639 NaClInstIterAdvance(iter_new);
640 }
641
642 if (NaClInstIterHasNext(iter_old) ||
643 NaClInstIterHasNext(iter_new)) {
644 NaClValidatorMessage(LOG_ERROR, state,
645 "Code modification: code segments have different number of instructions\n");
646 }
647
648 state->cur_inst_state = NULL;
649 state->cur_inst = NULL;
650 state->cur_inst_vector = NULL;
651 NaClApplyPostValidators(state, iter_new);
652 NaClInstIterDestroy(iter_old);
653 NaClInstIterDestroy(iter_new);
654 NaClValidatorStatePrintStats(state);
655 }
656
OLDNEW
« no previous file with comments | « src/trusted/validator_x86/ncvalidate_iter.h ('k') | tests/dynamic_code_loading/dynamic_load_test.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698