| 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 #ifndef BASE_FILES_FILE_H_ | 5 #ifndef BASE_FILES_FILE_H_ |
| 6 #define BASE_FILES_FILE_H_ | 6 #define BASE_FILES_FILE_H_ |
| 7 | 7 |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
| 10 #include <windows.h> | 10 #include <windows.h> |
| 11 #endif | 11 #endif |
| 12 | 12 |
| 13 #if defined(OS_POSIX) | 13 #if defined(OS_POSIX) |
| 14 #include <sys/stat.h> | 14 #include <sys/stat.h> |
| 15 #endif | 15 #endif |
| 16 | 16 |
| 17 #include <string> | 17 #include <string> |
| 18 | 18 |
| 19 #include "base/base_export.h" | 19 #include "base/base_export.h" |
| 20 #include "base/basictypes.h" | 20 #include "base/basictypes.h" |
| 21 #include "base/files/scoped_file.h" | 21 #include "base/files/scoped_file.h" |
| 22 #include "base/gtest_prod_util.h" |
| 22 #include "base/move.h" | 23 #include "base/move.h" |
| 23 #include "base/time/time.h" | 24 #include "base/time/time.h" |
| 24 | 25 |
| 25 #if defined(OS_WIN) | 26 #if defined(OS_WIN) |
| 26 #include "base/win/scoped_handle.h" | 27 #include "base/win/scoped_handle.h" |
| 27 #endif | 28 #endif |
| 28 | 29 |
| 30 FORWARD_DECLARE_TEST(FileTest, MemoryCorruption); |
| 31 |
| 29 namespace base { | 32 namespace base { |
| 30 | 33 |
| 31 class FilePath; | 34 class FilePath; |
| 32 | 35 |
| 33 #if defined(OS_WIN) | 36 #if defined(OS_WIN) |
| 34 typedef HANDLE PlatformFile; | 37 typedef HANDLE PlatformFile; |
| 35 #elif defined(OS_POSIX) | 38 #elif defined(OS_POSIX) |
| 36 typedef int PlatformFile; | 39 typedef int PlatformFile; |
| 37 | 40 |
| 38 #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) | 41 #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 #if defined(OS_WIN) | 292 #if defined(OS_WIN) |
| 290 static Error OSErrorToFileError(DWORD last_error); | 293 static Error OSErrorToFileError(DWORD last_error); |
| 291 #elif defined(OS_POSIX) | 294 #elif defined(OS_POSIX) |
| 292 static Error OSErrorToFileError(int saved_errno); | 295 static Error OSErrorToFileError(int saved_errno); |
| 293 #endif | 296 #endif |
| 294 | 297 |
| 295 // Converts an error value to a human-readable form. Used for logging. | 298 // Converts an error value to a human-readable form. Used for logging. |
| 296 static std::string ErrorToString(Error error); | 299 static std::string ErrorToString(Error error); |
| 297 | 300 |
| 298 private: | 301 private: |
| 302 FRIEND_TEST_ALL_PREFIXES(::FileTest, MemoryCorruption); |
| 303 |
| 304 #if defined(OS_POSIX) |
| 305 // Encloses a single ScopedFD, saving a cheap tamper resistent memory checksum |
| 306 // alongside it. This checksum is validated at every access, allowing early |
| 307 // detection of memory corruption. |
| 308 |
| 309 // TODO(gavinp): This is in place temporarily to help us debug |
| 310 // https://crbug.com/424562 , which can't be reproduced in valgrind. Remove |
| 311 // this code after we have fixed this issue. |
| 312 class MemoryCheckingScopedFD { |
| 313 public: |
| 314 MemoryCheckingScopedFD(); |
| 315 MemoryCheckingScopedFD(int fd); |
| 316 ~MemoryCheckingScopedFD(); |
| 317 |
| 318 bool is_valid() const { Check(); return file_.is_valid(); } |
| 319 int get() const { Check(); return file_.get(); } |
| 320 |
| 321 void reset() { Check(); file_.reset(); UpdateChecksum(); } |
| 322 void reset(int fd) { Check(); file_.reset(fd); UpdateChecksum(); } |
| 323 int release() { |
| 324 Check(); |
| 325 int fd = file_.release(); |
| 326 UpdateChecksum(); |
| 327 return fd; |
| 328 } |
| 329 |
| 330 private: |
| 331 FRIEND_TEST_ALL_PREFIXES(::FileTest, MemoryCorruption); |
| 332 |
| 333 // Computes the checksum for the current value of |file_|. Returns via an |
| 334 // out parameter to guard against implicit conversions of unsigned integral |
| 335 // types. |
| 336 void ComputeMemoryChecksum(unsigned int* out_checksum) const; |
| 337 |
| 338 // Confirms that the current |file_| and |file_memory_checksum_| agree, |
| 339 // failing a CHECK if they do not. |
| 340 void Check() const; |
| 341 |
| 342 void UpdateChecksum(); |
| 343 |
| 344 ScopedFD file_; |
| 345 unsigned int file_memory_checksum_; |
| 346 }; |
| 347 #endif |
| 348 |
| 299 void SetPlatformFile(PlatformFile file); | 349 void SetPlatformFile(PlatformFile file); |
| 300 | 350 |
| 301 #if defined(OS_WIN) | 351 #if defined(OS_WIN) |
| 302 win::ScopedHandle file_; | 352 win::ScopedHandle file_; |
| 303 #elif defined(OS_POSIX) | 353 #elif defined(OS_POSIX) |
| 304 ScopedFD file_; | 354 MemoryCheckingScopedFD file_; |
| 305 #endif | 355 #endif |
| 306 | 356 |
| 307 Error error_details_; | 357 Error error_details_; |
| 308 bool created_; | 358 bool created_; |
| 309 bool async_; | 359 bool async_; |
| 310 }; | 360 }; |
| 311 | 361 |
| 312 } // namespace base | 362 } // namespace base |
| 313 | 363 |
| 314 #endif // BASE_FILES_FILE_H_ | 364 #endif // BASE_FILES_FILE_H_ |
| OLD | NEW |