sanitizer_file.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //===-- sanitizer_file.h ---------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===---------------------------------------------------------------------===//
  8. //
  9. // This file is shared between run-time libraries of sanitizers.
  10. // It declares filesystem-related interfaces. This is separate from
  11. // sanitizer_common.h so that it's simpler to disable all the filesystem
  12. // support code for a port that doesn't use it.
  13. //
  14. //===---------------------------------------------------------------------===//
  15. #ifndef SANITIZER_FILE_H
  16. #define SANITIZER_FILE_H
  17. #include "sanitizer_interface_internal.h"
  18. #include "sanitizer_internal_defs.h"
  19. #include "sanitizer_libc.h"
  20. #include "sanitizer_mutex.h"
  21. namespace __sanitizer {
  22. struct ReportFile {
  23. void Write(const char *buffer, uptr length);
  24. bool SupportsColors();
  25. void SetReportPath(const char *path);
  26. const char *GetReportPath();
  27. // Don't use fields directly. They are only declared public to allow
  28. // aggregate initialization.
  29. // Protects fields below.
  30. StaticSpinMutex *mu;
  31. // Opened file descriptor. Defaults to stderr. It may be equal to
  32. // kInvalidFd, in which case new file will be opened when necessary.
  33. fd_t fd;
  34. // Path prefix of report file, set via __sanitizer_set_report_path.
  35. char path_prefix[kMaxPathLength];
  36. // Full path to report, obtained as <path_prefix>.PID
  37. char full_path[kMaxPathLength];
  38. // PID of the process that opened fd. If a fork() occurs,
  39. // the PID of child will be different from fd_pid.
  40. uptr fd_pid;
  41. private:
  42. void ReopenIfNecessary();
  43. };
  44. extern ReportFile report_file;
  45. enum FileAccessMode {
  46. RdOnly,
  47. WrOnly,
  48. RdWr
  49. };
  50. // Returns kInvalidFd on error.
  51. fd_t OpenFile(const char *filename, FileAccessMode mode,
  52. error_t *errno_p = nullptr);
  53. void CloseFile(fd_t);
  54. // Return true on success, false on error.
  55. bool ReadFromFile(fd_t fd, void *buff, uptr buff_size,
  56. uptr *bytes_read = nullptr, error_t *error_p = nullptr);
  57. bool WriteToFile(fd_t fd, const void *buff, uptr buff_size,
  58. uptr *bytes_written = nullptr, error_t *error_p = nullptr);
  59. // Scoped file handle closer.
  60. struct FileCloser {
  61. explicit FileCloser(fd_t fd) : fd(fd) {}
  62. ~FileCloser() { CloseFile(fd); }
  63. fd_t fd;
  64. };
  65. bool SupportsColoredOutput(fd_t fd);
  66. // OS
  67. const char *GetPwd();
  68. bool FileExists(const char *filename);
  69. char *FindPathToBinary(const char *name);
  70. bool IsPathSeparator(const char c);
  71. bool IsAbsolutePath(const char *path);
  72. // Returns true on success, false on failure.
  73. bool CreateDir(const char *pathname);
  74. // Starts a subprocess and returs its pid.
  75. // If *_fd parameters are not kInvalidFd their corresponding input/output
  76. // streams will be redirect to the file. The files will always be closed
  77. // in parent process even in case of an error.
  78. // The child process will close all fds after STDERR_FILENO
  79. // before passing control to a program.
  80. pid_t StartSubprocess(const char *filename, const char *const argv[],
  81. const char *const envp[], fd_t stdin_fd = kInvalidFd,
  82. fd_t stdout_fd = kInvalidFd, fd_t stderr_fd = kInvalidFd);
  83. // Checks if specified process is still running
  84. bool IsProcessRunning(pid_t pid);
  85. // Waits for the process to finish and returns its exit code.
  86. // Returns -1 in case of an error.
  87. int WaitForProcess(pid_t pid);
  88. // Maps given file to virtual memory, and returns pointer to it
  89. // (or NULL if mapping fails). Stores the size of mmaped region
  90. // in '*buff_size'.
  91. void *MapFileToMemory(const char *file_name, uptr *buff_size);
  92. void *MapWritableFileToMemory(void *addr, uptr size, fd_t fd, OFF_T offset);
  93. } // namespace __sanitizer
  94. #endif // SANITIZER_FILE_H