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 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
476 return FILE_ERROR_NOT_A_DIRECTORY; | 476 return FILE_ERROR_NOT_A_DIRECTORY; |
477 default: | 477 default: |
478 #if !defined(OS_NACL) // NaCl build has no metrics code. | 478 #if !defined(OS_NACL) // NaCl build has no metrics code. |
479 UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Posix", | 479 UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Posix", |
480 saved_errno); | 480 saved_errno); |
481 #endif | 481 #endif |
482 return FILE_ERROR_FAILED; | 482 return FILE_ERROR_FAILED; |
483 } | 483 } |
484 } | 484 } |
485 | 485 |
486 File::MemoryCheckingScopedFD::MemoryCheckingScopedFD() { | |
487 UpdateChecksum(); | |
488 } | |
489 | |
490 File::MemoryCheckingScopedFD::MemoryCheckingScopedFD(int fd) : file_(fd) { | |
491 UpdateChecksum(); | |
492 } | |
493 | |
494 File::MemoryCheckingScopedFD::~MemoryCheckingScopedFD() {} | |
495 | |
496 // static | |
497 void File::MemoryCheckingScopedFD::ComputeMemoryChecksum( | |
498 unsigned int* out_checksum) const { | |
Nico
2014/11/06 22:53:43
instead of
void foo(int* out)
why not
int f
gavinp
2014/11/06 23:52:54
I put a significant comment in the .h explaining t
| |
499 // Use a single iteration of a linear congruentional generator to provide a | |
500 // cheap checksum unlikely to be accidentally matched by a random memory | |
501 // corruption. | |
502 | |
503 // These constants were chosen to ensure that the function is invertible; see | |
Nico
2014/11/06 22:53:43
Why is it useful that it's invertible?
gavinp
2014/11/06 23:52:54
I've changed the comment to say more directly what
| |
504 // The Hull-Dubell Theorem for a proof. | |
505 | |
506 // This code uses "unsigned int" throughout for its defined modular semantics, | |
507 // which implicitly gives us a divisor of 2**32 or 2**64, depending. | |
508 | |
509 const unsigned int kMultiplier = 13035 * 4 + 1; | |
510 COMPILE_ASSERT((kMultiplier - 1 & 3) == 0, pred_must_be_multiple_of_four); | |
Nico
2014/11/06 22:53:43
move ) a bit to the left, to the right of 1
gavinp
2014/11/06 23:52:54
That ) is needed, since & is lower priority than =
| |
511 const unsigned int kIncrement = 1595649551; | |
512 COMPILE_ASSERT(kIncrement & 1, must_be_coprime_to_powers_of_two); | |
513 | |
514 *out_checksum = | |
515 static_cast<unsigned int>(file_.get()) * kMultiplier + kIncrement; | |
516 } | |
517 | |
518 void File::MemoryCheckingScopedFD::Check() const { | |
519 unsigned int computed_checksum; | |
520 ComputeMemoryChecksum(&computed_checksum); | |
521 CHECK_EQ(file_memory_checksum_, computed_checksum) << "corrupted fd memory"; | |
522 } | |
523 | |
524 void File::MemoryCheckingScopedFD::UpdateChecksum() { | |
525 ComputeMemoryChecksum(&file_memory_checksum_); | |
526 } | |
527 | |
486 void File::SetPlatformFile(PlatformFile file) { | 528 void File::SetPlatformFile(PlatformFile file) { |
487 DCHECK(!file_.is_valid()); | 529 DCHECK(!file_.is_valid()); |
488 file_.reset(file); | 530 file_.reset(file); |
489 } | 531 } |
490 | 532 |
491 } // namespace base | 533 } // namespace base |
OLD | NEW |