src/UserSpaceInstrumentation/AllocateInTraceeTest.cpp
[[nodiscard]] bool ProcessHasMapAtAddress(pid_t pid, uint64_t address, ProtState state) {
  auto maps_or_error = ReadFileToString(absl::StrFormat("/proc/%d/maps", pid));
  CHECK(maps_or_error.has_value());
  std::vector<std::string> lines = absl::StrSplit(maps_or_error.value(), '\n', absl::SkipEmpty());
  for (const auto& line : lines) {
    std::vector<std::string> tokens = absl::StrSplit(line, ' ', absl::SkipEmpty());
    if (state == ProtState::kWrite) {
      if (tokens.size() < 2 || tokens[1].size() < 2 || tokens[1][1] != 'w') {
        continue;
      }
    } else if (state == ProtState::kExec) {
      if (tokens.size() < 2 || tokens[1].size() < 3 || tokens[1][2] != 'x') {
        continue;
      }
    }
    std::vector<std::string> addresses = absl::StrSplit(tokens[0], '-');
    if (addresses.size() != 2) {
      continue;
    }
    if (std::stoull(addresses[0], nullptr, 16) == address) {
      return true;
    }
  }
  return false;
}
// Returns true if the target process has a writeable (or executable; depending on `state`) memory
// segment at `address`.
Returns true if the target process has a writeable (or executable; depending on `state`) memory
segment at `address`.
Returns true if the target process has a writeable ( or executable ; depending on ` state ` ) memory segment at ` address ` .
[[nodiscard]] bool ProcessHasMapAtAddress(pid_t pid, uint64_t address, ProtState state) {
  auto maps_or_error = ReadFileToString(absl::StrFormat("/proc/%d/maps", pid));
  CHECK(maps_or_error.has_value());
  std::vector<std::string> lines = absl::StrSplit(maps_or_error.value(), '\n', absl::SkipEmpty());
  for (const auto& line : lines) {
    std::vector<std::string> tokens = absl::StrSplit(line, ' ', absl::SkipEmpty());
    if (state == ProtState::kWrite) {
      if (tokens.size() < 2 || tokens[1].size() < 2 || tokens[1][1] != 'w') {
        continue;
      }
    } else if (state == ProtState::kExec) {
      if (tokens.size() < 2 || tokens[1].size() < 3 || tokens[1][2] != 'x') {
        continue;
      }
    }
    std::vector<std::string> addresses = absl::StrSplit(tokens[0], '-');
    if (addresses.size() != 2) {
      continue;
    }
    if (std::stoull(addresses[0], nullptr, 16) == address) {
      return true;
    }
  }
  return false;
}
[[ nodiscard ]] bool ProcessHasMapAtAddress ( pid_t pid , uint64_t address , ProtState state ) { auto maps_or_error = ReadFileToString ( absl :: StrFormat ( " " , pid ) ) ; CHECK ( maps_or_error . has_value ( ) ) ; std :: vector < std :: string > lines = absl :: StrSplit ( maps_or_error . value ( ) , ' \n ' , absl :: SkipEmpty ( ) ) ; for ( const auto & line : lines ) { std :: vector < std :: string > tokens = absl :: StrSplit ( line , ' ' , absl :: SkipEmpty ( ) ) ; if ( state == ProtState :: kWrite ) { if ( tokens . size ( ) < 2 || tokens [ 1 ] . size ( ) < 2 || tokens [ 1 ] [ 1 ] != ' ' ) { continue ; } } else if ( state == ProtState :: kExec ) { if ( tokens . size ( ) < 2 || tokens [ 1 ] . size ( ) < 3 || tokens [ 1 ] [ 2 ] != ' ' ) { continue ; } } std :: vector < std :: string > addresses = absl :: StrSplit ( tokens [ 0 ] , ' ' ) ; if ( addresses . size ( ) != 2 ) { continue ; } if ( std :: stoull ( addresses [ 0 ] , nullptr , 16 ) == address ) { return true ; } } return false ; }
Returns true if the target process has a writeable (or executable; depending on `state`) memory
segment at `address`.
Returns true if the target process has a writeable ( or executable ; depending on ` state ` ) memory segment at ` address ` .