OLD | NEW |
(Empty) | |
| 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 "gtest/gtest.h" |
| 8 |
| 9 #include "native_client/src/include/nacl_compiler_annotations.h" |
| 10 #include "native_client/src/shared/platform/nacl_log.h" |
| 11 #include "native_client/src/trusted/validator/ncvalidate.h" |
| 12 #include "native_client/src/trusted/validator/validation_cache.h" |
| 13 |
| 14 #define CONTEXT_MARKER 31 |
| 15 #define QUERY_MARKER 37 |
| 16 |
| 17 #define CODE_SIZE 32 |
| 18 |
| 19 // ret |
| 20 const char ret[CODE_SIZE + 1] = |
| 21 "\xc3\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90" |
| 22 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"; |
| 23 |
| 24 // pblendw $0xc0,%xmm0,%xmm2 |
| 25 const char sse41[CODE_SIZE + 1] = |
| 26 "\x66\x0f\x3a\x0e\xd0\xc0\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90" |
| 27 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"; |
| 28 |
| 29 struct MockContext { |
| 30 int marker; /* Sanity check that we're getting the right object. */ |
| 31 int query_result; |
| 32 bool set_validates_expected; |
| 33 bool query_destroyed; |
| 34 }; |
| 35 |
| 36 enum MockQueryState { |
| 37 QUERY_CREATED, |
| 38 QUERY_GET_CALLED, |
| 39 QUERY_SET_CALLED, |
| 40 QUERY_DESTROYED |
| 41 }; |
| 42 |
| 43 struct MockQuery { |
| 44 /* Sanity check that we're getting the right object. */ |
| 45 int marker; |
| 46 MockQueryState state; |
| 47 int add_count; |
| 48 MockContext *context; |
| 49 }; |
| 50 |
| 51 void *MockCreateQuery(void *handle) { |
| 52 MockContext *mcontext = (MockContext *) handle; |
| 53 MockQuery *mquery = (MockQuery *) malloc(sizeof(MockQuery)); |
| 54 EXPECT_EQ(CONTEXT_MARKER, mcontext->marker); |
| 55 mquery->marker = QUERY_MARKER; |
| 56 mquery->state = QUERY_CREATED; |
| 57 mquery->add_count = 0; |
| 58 mquery->context = mcontext; |
| 59 return mquery; |
| 60 } |
| 61 |
| 62 void MockAddData(void *query, const unsigned char *data, size_t length) { |
| 63 UNREFERENCED_PARAMETER(data); |
| 64 MockQuery *mquery = (MockQuery *) query; |
| 65 ASSERT_EQ(QUERY_MARKER, mquery->marker); |
| 66 EXPECT_EQ(QUERY_CREATED, mquery->state); |
| 67 /* Small data is suspicious. */ |
| 68 EXPECT_LE((size_t) 2, length); |
| 69 mquery->add_count += 1; |
| 70 } |
| 71 |
| 72 int MockQueryCodeValidates(void *query) { |
| 73 MockQuery *mquery = (MockQuery *) query; |
| 74 EXPECT_EQ(QUERY_MARKER, mquery->marker); |
| 75 EXPECT_EQ(QUERY_CREATED, mquery->state); |
| 76 /* Less than two pieces of data is suspicious. */ |
| 77 EXPECT_LE(2, mquery->add_count); |
| 78 mquery->state = QUERY_GET_CALLED; |
| 79 return mquery->context->query_result; |
| 80 } |
| 81 |
| 82 void MockSetCodeValidates(void *query) { |
| 83 MockQuery *mquery = (MockQuery *) query; |
| 84 ASSERT_EQ(QUERY_MARKER, mquery->marker); |
| 85 EXPECT_EQ(QUERY_GET_CALLED, mquery->state); |
| 86 EXPECT_EQ(true, mquery->context->set_validates_expected); |
| 87 mquery->state = QUERY_SET_CALLED; |
| 88 } |
| 89 |
| 90 void MockDestroyQuery(void *query) { |
| 91 MockQuery *mquery = (MockQuery *) query; |
| 92 ASSERT_EQ(QUERY_MARKER, mquery->marker); |
| 93 if (mquery->context->set_validates_expected) { |
| 94 EXPECT_EQ(QUERY_SET_CALLED, mquery->state); |
| 95 } else { |
| 96 EXPECT_EQ(QUERY_GET_CALLED, mquery->state); |
| 97 } |
| 98 mquery->state = QUERY_DESTROYED; |
| 99 mquery->context->query_destroyed = true; |
| 100 free(mquery); |
| 101 } |
| 102 |
| 103 class ValidationCachingInterfaceTests : public ::testing::Test { |
| 104 protected: |
| 105 MockContext context; |
| 106 NaClValidationCache cache; |
| 107 NaClCPUFeatures cpu_features; |
| 108 int bundle_size; |
| 109 |
| 110 unsigned char code_buffer[CODE_SIZE]; |
| 111 |
| 112 void SetUp() { |
| 113 context.marker = CONTEXT_MARKER; |
| 114 context.query_result = 1; |
| 115 context.set_validates_expected = false; |
| 116 context.query_destroyed = false; |
| 117 |
| 118 cache.handle = &context; |
| 119 cache.CreateQuery = MockCreateQuery; |
| 120 cache.AddData = MockAddData; |
| 121 cache.QueryKnownToValidate = MockQueryCodeValidates; |
| 122 cache.SetKnownToValidate = MockSetCodeValidates; |
| 123 cache.DestroyQuery = MockDestroyQuery; |
| 124 |
| 125 NaClSetAllCPUFeatures(&cpu_features); |
| 126 |
| 127 bundle_size = 32; |
| 128 |
| 129 memset(code_buffer, 0x90, sizeof(code_buffer)); |
| 130 } |
| 131 |
| 132 NaClValidationStatus Validate() { |
| 133 return NACL_SUBARCH_NAME(ApplyValidator, |
| 134 NACL_TARGET_ARCH, |
| 135 NACL_TARGET_SUBARCH)( |
| 136 NACL_SB_DEFAULT, |
| 137 NaClApplyCodeValidation, |
| 138 0, code_buffer, 32, |
| 139 bundle_size, &cpu_features, |
| 140 &cache); |
| 141 } |
| 142 }; |
| 143 |
| 144 TEST_F(ValidationCachingInterfaceTests, Sanity) { |
| 145 void *query = cache.CreateQuery(cache.handle); |
| 146 cache.AddData(query, NULL, 6); |
| 147 cache.AddData(query, NULL, 128); |
| 148 EXPECT_EQ(1, cache.QueryKnownToValidate(query)); |
| 149 cache.DestroyQuery(query); |
| 150 EXPECT_EQ(true, context.query_destroyed); |
| 151 } |
| 152 |
| 153 TEST_F(ValidationCachingInterfaceTests, NoCache) { |
| 154 NaClValidationStatus status = |
| 155 NACL_SUBARCH_NAME(ApplyValidator, |
| 156 NACL_TARGET_ARCH, |
| 157 NACL_TARGET_SUBARCH)( |
| 158 NACL_SB_DEFAULT, |
| 159 NaClApplyCodeValidation, |
| 160 0, code_buffer, CODE_SIZE, |
| 161 bundle_size, &cpu_features, |
| 162 NULL); |
| 163 EXPECT_EQ(NaClValidationSucceeded, status); |
| 164 } |
| 165 |
| 166 TEST_F(ValidationCachingInterfaceTests, CacheHit) { |
| 167 NaClValidationStatus status = Validate(); |
| 168 EXPECT_EQ(NaClValidationSucceeded, status); |
| 169 EXPECT_EQ(true, context.query_destroyed); |
| 170 } |
| 171 |
| 172 TEST_F(ValidationCachingInterfaceTests, CacheMiss) { |
| 173 context.query_result = 0; |
| 174 context.set_validates_expected = true; |
| 175 NaClValidationStatus status = Validate(); |
| 176 EXPECT_EQ(NaClValidationSucceeded, status); |
| 177 EXPECT_EQ(true, context.query_destroyed); |
| 178 } |
| 179 |
| 180 TEST_F(ValidationCachingInterfaceTests, SSE4Allowed) { |
| 181 memcpy(code_buffer, sse41, CODE_SIZE); |
| 182 context.query_result = 0; |
| 183 context.set_validates_expected = true; |
| 184 NaClValidationStatus status = Validate(); |
| 185 EXPECT_EQ(NaClValidationSucceeded, status); |
| 186 EXPECT_EQ(true, context.query_destroyed); |
| 187 } |
| 188 |
| 189 TEST_F(ValidationCachingInterfaceTests, SSE4Stubout) { |
| 190 memcpy(code_buffer, sse41, CODE_SIZE); |
| 191 context.query_result = 0; |
| 192 NaClSetCPUFeature(&cpu_features, NaClCPUFeature_SSE41, 0); |
| 193 NaClValidationStatus status = Validate(); |
| 194 EXPECT_EQ(NaClValidationSucceeded, status); |
| 195 EXPECT_EQ(true, context.query_destroyed); |
| 196 } |
| 197 |
| 198 TEST_F(ValidationCachingInterfaceTests, IllegalInst) { |
| 199 memcpy(code_buffer, ret, CODE_SIZE); |
| 200 context.query_result = 0; |
| 201 NaClValidationStatus status = Validate(); |
| 202 EXPECT_EQ(NaClValidationFailed, status); |
| 203 EXPECT_EQ(true, context.query_destroyed); |
| 204 } |
| 205 |
| 206 TEST_F(ValidationCachingInterfaceTests, IllegalCacheHit) { |
| 207 memcpy(code_buffer, ret, CODE_SIZE); |
| 208 NaClValidationStatus status = Validate(); |
| 209 // Success proves the cache shortcircuted validation. |
| 210 EXPECT_EQ(NaClValidationSucceeded, status); |
| 211 EXPECT_EQ(true, context.query_destroyed); |
| 212 } |
| 213 |
| 214 // Test driver function. |
| 215 int main(int argc, char *argv[]) { |
| 216 // The IllegalInst test touches the log mutex deep inside the validator. |
| 217 // This causes an SEH exception to be thrown on Windows if the mutex is not |
| 218 // initialized. |
| 219 // http://code.google.com/p/nativeclient/issues/detail?id=1696 |
| 220 NaClLogModuleInit(); |
| 221 testing::InitGoogleTest(&argc, argv); |
| 222 return RUN_ALL_TESTS(); |
| 223 } |
OLD | NEW |