| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/files/file.h" | 5 #include "base/files/file.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
| 10 #include <unistd.h> | 10 #include <unistd.h> |
| (...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 return FILE_ERROR_NOT_A_DIRECTORY; | 413 return FILE_ERROR_NOT_A_DIRECTORY; |
| 414 default: | 414 default: |
| 415 #if !defined(OS_NACL) // NaCl build has no metrics code. | 415 #if !defined(OS_NACL) // NaCl build has no metrics code. |
| 416 UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Posix", | 416 UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Posix", |
| 417 saved_errno); | 417 saved_errno); |
| 418 #endif | 418 #endif |
| 419 return FILE_ERROR_FAILED; | 419 return FILE_ERROR_FAILED; |
| 420 } | 420 } |
| 421 } | 421 } |
| 422 | 422 |
| 423 File::MemoryCheckingScopedFD::MemoryCheckingScopedFD() { | |
| 424 UpdateChecksum(); | |
| 425 } | |
| 426 | |
| 427 File::MemoryCheckingScopedFD::MemoryCheckingScopedFD(int fd) : file_(fd) { | |
| 428 UpdateChecksum(); | |
| 429 } | |
| 430 | |
| 431 File::MemoryCheckingScopedFD::~MemoryCheckingScopedFD() {} | |
| 432 | |
| 433 // static | |
| 434 void File::MemoryCheckingScopedFD::ComputeMemoryChecksum( | |
| 435 unsigned int* out_checksum) const { | |
| 436 // Use a single iteration of a linear congruentional generator (lcg) to | |
| 437 // provide a cheap checksum unlikely to be accidentally matched by a random | |
| 438 // memory corruption. | |
| 439 | |
| 440 // By choosing constants that satisfy the Hull-Duebell Theorem on lcg cycle | |
| 441 // length, we insure that each distinct fd value maps to a distinct checksum, | |
| 442 // which maximises the utility of our checksum. | |
| 443 | |
| 444 // This code uses "unsigned int" throughout for its defined modular semantics, | |
| 445 // which implicitly gives us a divisor that is a power of two. | |
| 446 | |
| 447 const unsigned int kMultiplier = 13035 * 4 + 1; | |
| 448 COMPILE_ASSERT(((kMultiplier - 1) & 3) == 0, pred_must_be_multiple_of_four); | |
| 449 const unsigned int kIncrement = 1595649551; | |
| 450 COMPILE_ASSERT(kIncrement & 1, must_be_coprime_to_powers_of_two); | |
| 451 | |
| 452 *out_checksum = | |
| 453 static_cast<unsigned int>(file_.get()) * kMultiplier + kIncrement; | |
| 454 } | |
| 455 | |
| 456 void File::MemoryCheckingScopedFD::Check() const { | |
| 457 unsigned int computed_checksum; | |
| 458 ComputeMemoryChecksum(&computed_checksum); | |
| 459 CHECK_EQ(file_memory_checksum_, computed_checksum) << "corrupted fd memory"; | |
| 460 } | |
| 461 | |
| 462 void File::MemoryCheckingScopedFD::UpdateChecksum() { | |
| 463 ComputeMemoryChecksum(&file_memory_checksum_); | |
| 464 } | |
| 465 | |
| 466 // NaCl doesn't implement system calls to open files directly. | 423 // NaCl doesn't implement system calls to open files directly. |
| 467 #if !defined(OS_NACL) | 424 #if !defined(OS_NACL) |
| 468 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here? | 425 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here? |
| 469 void File::DoInitialize(const FilePath& path, uint32 flags) { | 426 void File::DoInitialize(const FilePath& path, uint32 flags) { |
| 470 ThreadRestrictions::AssertIOAllowed(); | 427 ThreadRestrictions::AssertIOAllowed(); |
| 471 DCHECK(!IsValid()); | 428 DCHECK(!IsValid()); |
| 472 | 429 |
| 473 int open_flags = 0; | 430 int open_flags = 0; |
| 474 if (flags & FLAG_CREATE) | 431 if (flags & FLAG_CREATE) |
| 475 open_flags = O_CREAT | O_EXCL; | 432 open_flags = O_CREAT | O_EXCL; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 565 return !HANDLE_EINTR(fsync(file_.get())); | 522 return !HANDLE_EINTR(fsync(file_.get())); |
| 566 #endif | 523 #endif |
| 567 } | 524 } |
| 568 | 525 |
| 569 void File::SetPlatformFile(PlatformFile file) { | 526 void File::SetPlatformFile(PlatformFile file) { |
| 570 DCHECK(!file_.is_valid()); | 527 DCHECK(!file_.is_valid()); |
| 571 file_.reset(file); | 528 file_.reset(file); |
| 572 } | 529 } |
| 573 | 530 |
| 574 } // namespace base | 531 } // namespace base |
| OLD | NEW |