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 ApplyValidator API for the x86-32 architecture. */
8 :
9 : #include "native_client/src/trusted/validator/ncvalidate.h"
10 : #include "native_client/src/trusted/validator/x86/ncval_seg_sfi/ncvalidate.h"
11 : #include "native_client/src/trusted/validator/x86/ncval_seg_sfi/ncvalidate_detailed.h"
12 : #include <assert.h>
13 :
14 : /* Be sure the correct compile flags are defined for this. */
15 : #if NACL_ARCH(NACL_TARGET_ARCH) != NACL_x86
16 : # error("Can't compile, target is for x86-32")
17 : #else
18 : # if NACL_TARGET_SUBARCH != 32
19 : # error("Can't compile, target is for x86-32")
20 : # endif
21 : #endif
22 :
23 : static NaClValidationStatus NCApplyValidatorSilently_x86_32(
24 : uintptr_t guest_addr,
25 : uint8_t *data,
26 : size_t size,
27 : int bundle_size,
28 3 : NaClCPUFeaturesX86 *cpu_features) {
29 : struct NCValidatorState *vstate;
30 3 : int validator_result = 0;
31 :
32 3 : vstate = NCValidateInit(guest_addr, size, bundle_size, cpu_features);
33 3 : if (vstate == NULL) return NaClValidationFailedOutOfMemory;
34 3 : NCValidateSegment(data, guest_addr, size, vstate);
35 3 : validator_result = NCValidateFinish(vstate);
36 3 : NCValidateFreeState(&vstate);
37 3 : return (validator_result == 0)
38 : ? NaClValidationSucceeded : NaClValidationFailed;
39 : }
40 :
41 : NaClValidationStatus NCApplyValidatorStubout_x86_32(
42 : uintptr_t guest_addr,
43 : uint8_t *data,
44 : size_t size,
45 : int bundle_size,
46 3 : NaClCPUFeaturesX86 *cpu_features) {
47 : struct NCValidatorState *vstate;
48 :
49 3 : vstate = NCValidateInitDetailed(guest_addr, size, bundle_size, cpu_features);
50 3 : if (vstate == NULL) return NaClValidationFailedOutOfMemory;
51 3 : NCValidateSetStubOutMode(vstate, 1);
52 3 : NCValidateSegment(data, guest_addr, size, vstate);
53 3 : NCValidateFinish(vstate);
54 3 : NCValidateFreeState(&vstate);
55 3 : return NaClValidationSucceeded;
56 : }
57 :
58 : NaClValidationStatus NACL_SUBARCH_NAME(ApplyValidator, NACL_TARGET_ARCH, 32) (
59 : enum NaClSBKind sb_kind,
60 : NaClApplyValidationKind kind,
61 : uintptr_t guest_addr,
62 : uint8_t *data,
63 : size_t size,
64 : int bundle_size,
65 6 : NaClCPUFeaturesX86 *cpu_features) {
66 6 : NaClValidationStatus status = NaClValidationFailedNotImplemented;
67 6 : assert(NACL_SB_DEFAULT == sb_kind);
68 6 : if (bundle_size == 16 || bundle_size == 32) {
69 6 : if (!NaClArchSupported(cpu_features))
70 0 : return NaClValidationFailedCpuNotSupported;
71 6 : switch (kind) {
72 : case NaClApplyCodeValidation:
73 3 : status = NCApplyValidatorSilently_x86_32(
74 : guest_addr, data, size, bundle_size, cpu_features);
75 3 : break;
76 : case NaClApplyValidationDoStubout:
77 3 : status = NCApplyValidatorStubout_x86_32(
78 : guest_addr, data, size, bundle_size, cpu_features);
79 : break;
80 : default:
81 : /* If reached, it isn't implemented (yet). */
82 : break;
83 : }
84 : }
85 6 : return status;
86 : }
87 :
88 : NaClValidationStatus NACL_SUBARCH_NAME(ApplyValidatorCodeReplacement, x86, 32)
89 : (enum NaClSBKind sb_kind,
90 : uintptr_t guest_addr,
91 : uint8_t *data_old,
92 : uint8_t *data_new,
93 : size_t size,
94 : int bundle_size,
95 0 : NaClCPUFeaturesX86 *cpu_features) {
96 0 : NaClValidationStatus status = NaClValidationFailedNotImplemented;
97 0 : assert(NACL_SB_DEFAULT == sb_kind);
98 0 : if (bundle_size == 16 || bundle_size == 32) {
99 0 : if (!NaClArchSupported(cpu_features)) {
100 0 : status = NaClValidationFailedCpuNotSupported;
101 : } else {
102 0 : status = NCValidateSegmentPair(data_old, data_new, guest_addr,
103 : size, bundle_size, cpu_features)
104 : ? NaClValidationSucceeded : NaClValidationFailed;
105 : }
106 : }
107 0 : return status;
108 : }
|