Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <limits.h> | |
| 6 #include <set> | |
| 7 | |
| 8 #include "base/logging.h" | |
| 9 #include "base/platform_file.h" | |
| 10 #include "tools/ipc_fuzzer/message_lib/message_file.h" | |
| 11 #include "tools/ipc_fuzzer/message_lib/message_file_format.h" | |
| 12 #include "tools/ipc_fuzzer/message_lib/message_names.h" | |
| 13 | |
| 14 namespace ipc_fuzzer { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // Helper class to write a MessageVector + message names to a file. | |
| 19 class Writer { | |
| 20 public: | |
| 21 Writer(const base::FilePath& path); | |
| 22 ~Writer(); | |
| 23 bool Write(const MessageVector& messages); | |
| 24 | |
| 25 private: | |
| 26 bool OpenFile(); | |
| 27 | |
| 28 // Helper to append data to file_. | |
| 29 bool WriteBlob(const void *buffer, size_t size); | |
| 30 | |
| 31 // Collects a set of MessageVector message types. Corresponding message | |
| 32 // names need to be included in the file. | |
| 33 bool CollectMessageTypes(); | |
| 34 | |
| 35 bool WriteHeader(); | |
| 36 bool WriteMessages(); | |
| 37 | |
| 38 // Each name table entry is a message type + string table offset. | |
| 39 bool WriteNameTable(); | |
| 40 | |
| 41 // String table contains the actual message names. | |
| 42 bool WriteStringTable(); | |
| 43 | |
| 44 typedef std::set<uint32> TypesSet; | |
| 45 base::FilePath path_; | |
| 46 base::PlatformFile file_; | |
| 47 const MessageVector* messages_; | |
| 48 TypesSet types_; | |
| 49 | |
| 50 DISALLOW_COPY_AND_ASSIGN(Writer); | |
| 51 }; | |
| 52 | |
| 53 Writer::Writer(const base::FilePath& path) | |
| 54 : path_(path), | |
| 55 file_(base::kInvalidPlatformFileValue), | |
| 56 messages_(NULL) { | |
| 57 } | |
| 58 | |
| 59 Writer::~Writer() { | |
| 60 if (file_ != base::kInvalidPlatformFileValue) | |
| 61 base::ClosePlatformFile(file_); | |
| 62 } | |
| 63 | |
| 64 bool Writer::OpenFile() { | |
| 65 file_ = base::CreatePlatformFile( | |
| 66 path_, | |
| 67 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE, | |
| 68 NULL, | |
| 69 NULL); | |
| 70 if (file_ == base::kInvalidPlatformFileValue) { | |
| 71 LOG(ERROR) << "Failed to create IPC message file: " << path_.value(); | |
| 72 return false; | |
| 73 } | |
| 74 return true; | |
| 75 } | |
| 76 | |
| 77 bool Writer::WriteBlob(const void *buffer, size_t size) { | |
| 78 if (size > INT_MAX) | |
| 79 return false; | |
| 80 const char* char_buffer = static_cast<const char*>(buffer); | |
| 81 int ret = base::WritePlatformFileAtCurrentPos(file_, char_buffer, size); | |
| 82 if (ret != size) { | |
| 83 LOG(ERROR) << "Failed to write " << size << " bytes."; | |
| 84 return false; | |
| 85 } | |
| 86 return true; | |
| 87 } | |
| 88 | |
| 89 bool Writer::CollectMessageTypes() { | |
| 90 for (size_t i = 0; i < messages_->size(); ++i) { | |
| 91 uint32_t type = (*messages_)[i]->type(); | |
| 92 if (!MessageNames::Get().TypeExists(type)) { | |
| 93 LOG(ERROR) << "Unknown message type: " << type; | |
| 94 return false; | |
|
Tom Sepez
2013/12/06 18:57:16
Symptomatic of a missing message file in all_messa
aedla
2013/12/09 18:08:32
I actually do this check in the logging code. On f
| |
| 95 } | |
| 96 types_.insert(type); | |
| 97 } | |
| 98 return true; | |
| 99 } | |
| 100 | |
| 101 bool Writer::WriteHeader() { | |
| 102 FileHeader header; | |
| 103 if (messages_->size() > UINT_MAX) | |
| 104 return false; | |
| 105 header.message_count = messages_->size(); | |
| 106 header.name_count = types_.size(); | |
| 107 if (!WriteBlob(&header, sizeof(FileHeader))) | |
| 108 return false; | |
| 109 return true; | |
| 110 } | |
| 111 | |
| 112 bool Writer::WriteMessages() { | |
| 113 for (size_t i = 0; i < messages_->size(); ++i) { | |
| 114 IPC::Message* message = (*messages_)[i]; | |
| 115 if (!WriteBlob(message->data(), message->size())) | |
| 116 return false; | |
| 117 } | |
| 118 return true; | |
| 119 } | |
| 120 | |
| 121 bool Writer::WriteNameTable() { | |
| 122 size_t string_table_offset = 0; | |
| 123 NameTableEntry entry; | |
| 124 | |
| 125 for (TypesSet::iterator it = types_.begin(); it != types_.end(); ++it) { | |
| 126 if (string_table_offset > UINT_MAX) | |
| 127 return false; | |
| 128 entry.type = *it; | |
| 129 entry.string_table_offset = string_table_offset; | |
| 130 if (!WriteBlob(&entry, sizeof(NameTableEntry))) | |
| 131 return false; | |
| 132 const std::string& name = MessageNames::Get().TypeToName(*it); | |
| 133 string_table_offset += name.length() + 1; | |
| 134 } | |
| 135 return true; | |
| 136 } | |
| 137 | |
| 138 bool Writer::WriteStringTable() { | |
| 139 for (TypesSet::iterator it = types_.begin(); it != types_.end(); ++it) { | |
| 140 const std::string& name = MessageNames::Get().TypeToName(*it); | |
| 141 if (!WriteBlob(name.c_str(), name.length() + 1)) | |
| 142 return false; | |
| 143 } | |
| 144 return true; | |
| 145 } | |
| 146 | |
| 147 bool Writer::Write(const MessageVector& messages) { | |
| 148 messages_ = &messages; | |
| 149 | |
| 150 if (!OpenFile()) | |
| 151 return false; | |
| 152 if (!CollectMessageTypes()) | |
| 153 return false; | |
| 154 if (!WriteHeader()) | |
| 155 return false; | |
| 156 if (!WriteMessages()) | |
| 157 return false; | |
| 158 if (!WriteNameTable()) | |
| 159 return false; | |
| 160 if (!WriteStringTable()) | |
| 161 return false; | |
| 162 | |
| 163 return true; | |
| 164 } | |
| 165 | |
| 166 } // namespace | |
| 167 | |
| 168 bool MessageFile::Write(const base::FilePath& path, | |
| 169 const MessageVector& messages) { | |
| 170 Writer writer(path); | |
| 171 return writer.Write(messages); | |
| 172 } | |
| 173 | |
| 174 } // namespace ipc_fuzzer | |
| OLD | NEW |