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 : /* Implement the functions common for ia32 and x86-64 architectures. */
8 : #include "native_client/src/trusted/validator_ragel/dfa_validate_common.h"
9 :
10 : #include <string.h>
11 :
12 : #include "native_client/src/shared/platform/nacl_check.h"
13 : #include "native_client/src/trusted/service_runtime/nacl_config.h"
14 : #include "native_client/src/trusted/validator_ragel/validator.h"
15 :
16 : /* Used as an argument to copy_func when unsupported instruction must be
17 : replaced with HLTs. */
18 : static const uint8_t kStubOutMem[MAX_INSTRUCTION_LENGTH] = {
19 : NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE,
20 : NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE,
21 : NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE,
22 : NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE,
23 : NACL_HALT_OPCODE
24 : };
25 :
26 0 : Bool NaClDfaProcessValidationError(const uint8_t *begin, const uint8_t *end,
27 0 : uint32_t info, void *callback_data) {
28 0 : UNREFERENCED_PARAMETER(begin);
29 0 : UNREFERENCED_PARAMETER(end);
30 0 : UNREFERENCED_PARAMETER(info);
31 0 : UNREFERENCED_PARAMETER(callback_data);
32 :
33 0 : return FALSE;
34 : }
35 :
36 59 : Bool NaClDfaStubOutCPUUnsupportedInstruction(const uint8_t *begin,
37 59 : const uint8_t *end,
38 59 : uint32_t info,
39 59 : void *callback_data) {
40 : /* Stub-out instructions unsupported on this CPU, but valid on other CPUs. */
41 59 : if ((info & VALIDATION_ERRORS_MASK) == CPUID_UNSUPPORTED_INSTRUCTION) {
42 1 : int *did_stubout = callback_data;
43 1 : *did_stubout = 1;
44 3 : memset((uint8_t *)begin, NACL_HALT_OPCODE, end - begin);
45 1 : return TRUE;
46 : } else {
47 58 : return FALSE;
48 : }
49 59 : }
50 :
51 159 : Bool NaClDfaProcessCodeCopyInstruction(const uint8_t *begin_new,
52 159 : const uint8_t *end_new,
53 159 : uint32_t info_new,
54 159 : void *callback_data) {
55 159 : struct CodeCopyCallbackData *data = callback_data;
56 159 : size_t instruction_length = end_new - begin_new;
57 :
58 : /* Sanity check: instruction must be no longer than 17 bytes. */
59 477 : CHECK(instruction_length <= MAX_INSTRUCTION_LENGTH);
60 :
61 477 : return data->copy_func(
62 : (uint8_t *)begin_new + data->existing_minus_new, /* begin_existing */
63 : (info_new & VALIDATION_ERRORS_MASK) == CPUID_UNSUPPORTED_INSTRUCTION ?
64 : (uint8_t *)kStubOutMem :
65 : (uint8_t *)begin_new,
66 : (uint8_t)instruction_length);
67 : }
68 :
69 0 : Bool NaClDfaCodeReplacementIsStubouted(const uint8_t *begin_existing,
70 0 : size_t instruction_length) {
71 :
72 : /* Unsupported instruction must have been replaced with HLTs. */
73 0 : if (memcmp(kStubOutMem, begin_existing, instruction_length) == 0)
74 0 : return TRUE;
75 : else
76 0 : return FALSE;
77 0 : }
|