| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | 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 | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 #include "gtest/gtest.h" | 7 #include "gtest/gtest.h" |
| 8 | 8 |
| 9 #include <fcntl.h> | 9 #include <fcntl.h> |
| 10 | 10 |
| 11 #include "native_client/src/include/nacl_compiler_annotations.h" | 11 #include "native_client/src/include/nacl_compiler_annotations.h" |
| 12 #include "native_client/src/shared/platform/nacl_host_desc.h" | 12 #include "native_client/src/shared/platform/nacl_host_desc.h" |
| 13 #include "native_client/src/shared/platform/nacl_log.h" | 13 #include "native_client/src/shared/platform/nacl_log.h" |
| 14 #include "native_client/src/shared/utils/types.h" | 14 #include "native_client/src/shared/utils/types.h" |
| 15 #include "native_client/src/trusted/desc/nacl_desc_io.h" | 15 #include "native_client/src/trusted/desc/nacl_desc_io.h" |
| 16 #include "native_client/src/trusted/validator/ncvalidate.h" | 16 #include "native_client/src/trusted/validator/ncvalidate.h" |
| 17 #include "native_client/src/trusted/validator/validation_cache.h" | 17 #include "native_client/src/trusted/validator/validation_cache.h" |
| 18 #include "native_client/src/trusted/cpu_features/arch/x86/cpu_x86.h" | 18 #include "native_client/src/trusted/cpu_features/arch/x86/cpu_x86.h" |
| 19 #include "native_client/src/trusted/service_runtime/include/sys/fcntl.h" | 19 #include "native_client/src/trusted/service_runtime/include/sys/fcntl.h" |
| 20 #include "native_client/src/trusted/validator/rich_file_info.h" |
| 20 #include "native_client/src/trusted/validator/validation_metadata.h" | 21 #include "native_client/src/trusted/validator/validation_metadata.h" |
| 21 | 22 |
| 22 #define CONTEXT_MARKER 31 | 23 #define CONTEXT_MARKER 31 |
| 23 #define QUERY_MARKER 37 | 24 #define QUERY_MARKER 37 |
| 24 | 25 |
| 25 #define CODE_SIZE 32 | 26 #define CODE_SIZE 32 |
| 26 | 27 |
| 27 // ret | 28 // ret |
| 28 const char ret[CODE_SIZE + 1] = | 29 const char ret[CODE_SIZE + 1] = |
| 29 "\xc3\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90" | 30 "\xc3\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90" |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 metadata.mtime = 100; | 246 metadata.mtime = 100; |
| 246 metadata_ptr = &metadata; | 247 metadata_ptr = &metadata; |
| 247 context.add_count_expected = 12; | 248 context.add_count_expected = 12; |
| 248 NaClValidationStatus status = Validate(); | 249 NaClValidationStatus status = Validate(); |
| 249 EXPECT_EQ(NaClValidationSucceeded, status); | 250 EXPECT_EQ(NaClValidationSucceeded, status); |
| 250 EXPECT_EQ(true, context.query_destroyed); | 251 EXPECT_EQ(true, context.query_destroyed); |
| 251 } | 252 } |
| 252 | 253 |
| 253 extern "C" { | 254 extern "C" { |
| 254 extern int SerializeNaClDescMetadata( | 255 extern int SerializeNaClDescMetadata( |
| 255 uint8_t known_file, | 256 const struct RichFileInfo *info, |
| 256 const char *file_name, | |
| 257 uint32_t file_name_length, | |
| 258 uint8_t **buffer, | 257 uint8_t **buffer, |
| 259 uint32_t *buffer_length); | 258 uint32_t *buffer_length); |
| 260 | 259 |
| 261 extern int DeserializeNaClDescMetadata( | 260 extern int DeserializeNaClDescMetadata( |
| 262 uint8_t *buffer, | 261 const uint8_t *buffer, |
| 263 uint32_t buffer_length, | 262 uint32_t buffer_length, |
| 264 uint8_t *known_file, | 263 struct RichFileInfo *info); |
| 265 char **file_name, | |
| 266 uint32_t *file_name_length); | |
| 267 } | 264 } |
| 268 | 265 |
| 269 class ValidationCachingSerializationTests : public ::testing::Test { | 266 class ValidationCachingSerializationTests : public ::testing::Test { |
| 270 protected: | 267 protected: |
| 268 struct RichFileInfo info; |
| 271 uint8_t *buffer; | 269 uint8_t *buffer; |
| 272 uint32_t buffer_length; | 270 uint32_t buffer_length; |
| 273 uint8_t known_file; | 271 struct RichFileInfo inp; |
| 274 char *file_name; | 272 struct RichFileInfo outp; |
| 275 uint32_t file_name_length; | |
| 276 | 273 |
| 277 void SetUp() { | 274 void SetUp() { |
| 278 buffer = 0; | 275 buffer = 0; |
| 279 buffer_length = 0; | 276 buffer_length = 0; |
| 280 known_file = 0; | 277 RichFileInfoCtor(&inp); |
| 281 file_name = 0; | 278 RichFileInfoCtor(&outp); |
| 282 file_name_length = 0; | 279 } |
| 280 |
| 281 void TearDown() { |
| 282 free(buffer); |
| 283 // Don't free the inp structure, it does not contain malloced memory. |
| 284 RichFileInfoDtor(&outp); |
| 283 } | 285 } |
| 284 }; | 286 }; |
| 285 | 287 |
| 286 TEST_F(ValidationCachingSerializationTests, NormalOperationSimple) { | 288 TEST_F(ValidationCachingSerializationTests, NormalOperationSimple) { |
| 287 EXPECT_EQ(0, SerializeNaClDescMetadata(0, "foo", 3, &buffer, &buffer_length)); | 289 inp.known_file = 0; |
| 288 EXPECT_EQ(0, DeserializeNaClDescMetadata(buffer, buffer_length, &known_file, | 290 inp.file_path = "foo"; |
| 289 &file_name, &file_name_length)); | 291 inp.file_path_length = 3; |
| 290 free(buffer); | 292 EXPECT_EQ(0, SerializeNaClDescMetadata(&inp, &buffer, &buffer_length)); |
| 293 EXPECT_EQ(0, DeserializeNaClDescMetadata(buffer, buffer_length, &outp)); |
| 291 | 294 |
| 292 EXPECT_EQ((uint8_t) 0, known_file); | 295 EXPECT_EQ((uint8_t) 0, outp.known_file); |
| 293 EXPECT_EQ((uint32_t) 0, file_name_length); | 296 EXPECT_EQ((uint32_t) 0, outp.file_path_length); |
| 294 EXPECT_EQ(NULL, file_name); | 297 EXPECT_EQ(NULL, outp.file_path); |
| 295 } | 298 } |
| 296 | 299 |
| 297 TEST_F(ValidationCachingSerializationTests, NormalOperationFull) { | 300 TEST_F(ValidationCachingSerializationTests, NormalOperationFull) { |
| 298 EXPECT_EQ(0, SerializeNaClDescMetadata(1, "foo", 3, &buffer, &buffer_length)); | 301 inp.known_file = 1; |
| 299 EXPECT_EQ(0, DeserializeNaClDescMetadata(buffer, buffer_length, &known_file, | 302 inp.file_path = "foo"; |
| 300 &file_name, &file_name_length)); | 303 inp.file_path_length = 3; |
| 301 free(buffer); | |
| 302 | 304 |
| 303 EXPECT_EQ((uint8_t) 1, known_file); | 305 EXPECT_EQ(0, SerializeNaClDescMetadata(&inp, &buffer, &buffer_length)); |
| 304 EXPECT_EQ((uint32_t) 3, file_name_length); | 306 EXPECT_EQ(0, DeserializeNaClDescMetadata(buffer, buffer_length, &outp)); |
| 305 EXPECT_EQ(0, memcmp("foo", file_name, file_name_length)); | 307 |
| 306 free(file_name); | 308 EXPECT_EQ((uint8_t) 1, outp.known_file); |
| 309 EXPECT_EQ((uint32_t) 3, outp.file_path_length); |
| 310 EXPECT_EQ(0, memcmp("foo", outp.file_path, outp.file_path_length)); |
| 307 } | 311 } |
| 308 | 312 |
| 309 TEST_F(ValidationCachingSerializationTests, BadSizeSimple) { | 313 TEST_F(ValidationCachingSerializationTests, BadSizeSimple) { |
| 310 EXPECT_EQ(0, SerializeNaClDescMetadata(0, NULL, 0, &buffer, &buffer_length)); | 314 inp.known_file = 0; |
| 315 EXPECT_EQ(0, SerializeNaClDescMetadata(&inp, &buffer, &buffer_length)); |
| 311 for (uint32_t i = -1; i <= buffer_length + 4; i++) { | 316 for (uint32_t i = -1; i <= buffer_length + 4; i++) { |
| 312 /* The only case that is OK. */ | 317 /* The only case that is OK. */ |
| 313 if (i == buffer_length) | 318 if (i == buffer_length) |
| 314 continue; | 319 continue; |
| 315 | 320 |
| 316 /* Wrong number of bytes, fail. */ | 321 /* Wrong number of bytes, fail. */ |
| 317 EXPECT_EQ(1, DeserializeNaClDescMetadata(buffer, i, &known_file, | 322 EXPECT_EQ(1, DeserializeNaClDescMetadata(buffer, i, &outp)); |
| 318 &file_name, &file_name_length)); | |
| 319 } | 323 } |
| 320 free(buffer); | |
| 321 } | 324 } |
| 322 | 325 |
| 323 TEST_F(ValidationCachingSerializationTests, BadSizeFull) { | 326 TEST_F(ValidationCachingSerializationTests, BadSizeFull) { |
| 324 EXPECT_EQ(0, SerializeNaClDescMetadata(1, "foo", 3, &buffer, &buffer_length)); | 327 inp.known_file = 1; |
| 328 inp.file_path = "foo"; |
| 329 inp.file_path_length = 3; |
| 330 EXPECT_EQ(0, SerializeNaClDescMetadata(&inp, &buffer, &buffer_length)); |
| 325 for (uint32_t i = -1; i <= buffer_length + 4; i++) { | 331 for (uint32_t i = -1; i <= buffer_length + 4; i++) { |
| 326 /* The only case that is OK. */ | 332 /* The only case that is OK. */ |
| 327 if (i == buffer_length) | 333 if (i == buffer_length) |
| 328 continue; | 334 continue; |
| 329 | 335 |
| 330 /* Wrong number of bytes, fail. */ | 336 /* Wrong number of bytes, fail. */ |
| 331 EXPECT_EQ(1, DeserializeNaClDescMetadata(buffer, i, &known_file, | 337 EXPECT_EQ(1, DeserializeNaClDescMetadata(buffer, i, &outp)); |
| 332 &file_name, &file_name_length)); | |
| 333 /* Paranoia. */ | 338 /* Paranoia. */ |
| 334 EXPECT_EQ(0, known_file); | 339 EXPECT_EQ(0, outp.known_file); |
| 335 /* Make sure we don't leak on failure. */ | 340 /* Make sure we don't leak on failure. */ |
| 336 EXPECT_EQ(NULL, file_name); | 341 EXPECT_EQ(NULL, outp.file_path); |
| 337 } | 342 } |
| 338 free(buffer); | |
| 339 } | 343 } |
| 340 | 344 |
| 341 extern "C" { | 345 static char *AN_ARBITRARY_FILE_PATH = NULL; |
| 342 extern int SetFileOriginInfo( | |
| 343 struct NaClDesc *desc, | |
| 344 uint8_t known_file, | |
| 345 const char *file_name, | |
| 346 uint32_t file_name_length); | |
| 347 | |
| 348 extern int GetFileOriginInfo( | |
| 349 struct NaClDesc *desc, | |
| 350 uint8_t *known_file, | |
| 351 char **file_name, | |
| 352 uint32_t *file_name_length); | |
| 353 } | |
| 354 | |
| 355 char *AN_ARBITRARY_FILE_PATH = NULL; | |
| 356 | 346 |
| 357 class ValidationCachingFileOriginTests : public ::testing::Test { | 347 class ValidationCachingFileOriginTests : public ::testing::Test { |
| 358 protected: | 348 protected: |
| 359 struct NaClDesc *desc; | 349 struct NaClDesc *desc; |
| 360 | 350 |
| 361 uint8_t known_file; | 351 struct RichFileInfo inp; |
| 362 char *file_name; | 352 struct RichFileInfo outp; |
| 363 uint32_t file_name_length; | |
| 364 | 353 |
| 365 void SetUp() { | 354 void SetUp() { |
| 366 struct NaClHostDesc *host_desc = NULL; | 355 struct NaClHostDesc *host_desc = NULL; |
| 367 int fd = open(AN_ARBITRARY_FILE_PATH, O_RDONLY); | 356 int fd = open(AN_ARBITRARY_FILE_PATH, O_RDONLY); |
| 368 | 357 |
| 369 desc = NULL; | 358 desc = NULL; |
| 370 known_file = 0; | 359 RichFileInfoCtor(&inp); |
| 371 file_name = NULL; | 360 RichFileInfoCtor(&outp); |
| 372 file_name_length = 0; | |
| 373 | 361 |
| 374 ASSERT_NE(-1, fd); | 362 ASSERT_NE(-1, fd); |
| 375 host_desc = NaClHostDescPosixMake(fd, NACL_ABI_O_RDONLY); | 363 host_desc = NaClHostDescPosixMake(fd, NACL_ABI_O_RDONLY); |
| 376 desc = (struct NaClDesc *) NaClDescIoDescMake(host_desc); | 364 desc = (struct NaClDesc *) NaClDescIoDescMake(host_desc); |
| 377 ASSERT_NE((struct NaClDesc *) NULL, desc); | 365 ASSERT_NE((struct NaClDesc *) NULL, desc); |
| 378 } | 366 } |
| 379 | 367 |
| 380 void TearDown() { | 368 void TearDown() { |
| 381 free(file_name); | 369 // Don't free the inp structure, it does not contain malloced memory. |
| 370 RichFileInfoDtor(&outp); |
| 382 NaClDescSafeUnref(desc); | 371 NaClDescSafeUnref(desc); |
| 383 } | 372 } |
| 384 }; | 373 }; |
| 385 | 374 |
| 386 TEST_F(ValidationCachingFileOriginTests, None) { | 375 TEST_F(ValidationCachingFileOriginTests, None) { |
| 387 EXPECT_EQ(1, GetFileOriginInfo(desc, &known_file, &file_name, | 376 EXPECT_EQ(1, GetFileOriginInfo(desc, &outp)); |
| 388 &file_name_length)); | |
| 389 } | 377 } |
| 390 | 378 |
| 391 TEST_F(ValidationCachingFileOriginTests, Simple) { | 379 TEST_F(ValidationCachingFileOriginTests, Simple) { |
| 392 EXPECT_EQ(0, SetFileOriginInfo(desc, 0, "foobar", 6)); | 380 inp.known_file = 0; |
| 393 EXPECT_EQ(0, GetFileOriginInfo(desc, &known_file, &file_name, | 381 inp.file_path = "foobar"; |
| 394 &file_name_length)); | 382 inp.file_path_length = 6; |
| 383 EXPECT_EQ(0, SetFileOriginInfo(desc, &inp)); |
| 384 EXPECT_EQ(0, GetFileOriginInfo(desc, &outp)); |
| 395 | 385 |
| 396 EXPECT_EQ(0, known_file); | 386 EXPECT_EQ(0, outp.known_file); |
| 397 EXPECT_EQ((uint32_t) 0, file_name_length); | 387 EXPECT_EQ((uint32_t) 0, outp.file_path_length); |
| 398 EXPECT_EQ(NULL, file_name); | 388 EXPECT_EQ(NULL, outp.file_path); |
| 399 } | 389 } |
| 400 | 390 |
| 401 | 391 |
| 402 TEST_F(ValidationCachingFileOriginTests, Full) { | 392 TEST_F(ValidationCachingFileOriginTests, Full) { |
| 403 EXPECT_EQ(0, SetFileOriginInfo(desc, 1, "foobar", 6)); | 393 inp.known_file = 1; |
| 404 EXPECT_EQ(0, GetFileOriginInfo(desc, &known_file, &file_name, | 394 inp.file_path = "foobar"; |
| 405 &file_name_length)); | 395 inp.file_path_length = 6; |
| 396 EXPECT_EQ(0, SetFileOriginInfo(desc, &inp)); |
| 397 EXPECT_EQ(0, GetFileOriginInfo(desc, &outp)); |
| 406 | 398 |
| 407 EXPECT_EQ(1, known_file); | 399 EXPECT_EQ(1, outp.known_file); |
| 408 EXPECT_EQ((uint32_t) 6, file_name_length); | 400 EXPECT_EQ((uint32_t) 6, outp.file_path_length); |
| 409 EXPECT_EQ(0, memcmp("foobar", file_name, file_name_length)); | 401 EXPECT_EQ(0, memcmp("foobar", outp.file_path, outp.file_path_length)); |
| 410 } | 402 } |
| 411 | 403 |
| 412 // Test driver function. | 404 // Test driver function. |
| 413 int main(int argc, char *argv[]) { | 405 int main(int argc, char *argv[]) { |
| 414 // One file we know must exist is this executable. | 406 // One file we know must exist is this executable. |
| 415 AN_ARBITRARY_FILE_PATH = argv[0]; | 407 AN_ARBITRARY_FILE_PATH = argv[0]; |
| 416 // The IllegalInst test touches the log mutex deep inside the validator. | 408 // The IllegalInst test touches the log mutex deep inside the validator. |
| 417 // This causes an SEH exception to be thrown on Windows if the mutex is not | 409 // This causes an SEH exception to be thrown on Windows if the mutex is not |
| 418 // initialized. | 410 // initialized. |
| 419 // http://code.google.com/p/nativeclient/issues/detail?id=1696 | 411 // http://code.google.com/p/nativeclient/issues/detail?id=1696 |
| 420 NaClLogModuleInit(); | 412 NaClLogModuleInit(); |
| 421 testing::InitGoogleTest(&argc, argv); | 413 testing::InitGoogleTest(&argc, argv); |
| 422 return RUN_ALL_TESTS(); | 414 return RUN_ALL_TESTS(); |
| 423 } | 415 } |
| OLD | NEW |