Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(826)

Side by Side Diff: lib/Bitcode/NaCl/Reader/NaClBitcodeHeader.cpp

Issue 23753003: Report fatal translator errors to the browser (Closed) Base URL: http://git.chromium.org/native_client/pnacl-llvm.git@master
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 //===- NaClBitcodeHeader.cpp ----------------------------------------------===// 1 //===- NaClBitcodeHeader.cpp ----------------------------------------------===//
2 // PNaCl bitcode header reader. 2 // PNaCl bitcode header reader.
3 // 3 //
4 // The LLVM Compiler Infrastructure 4 // The LLVM Compiler Infrastructure
5 // 5 //
6 // This file is distributed under the University of Illinois Open Source 6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details. 7 // License. See LICENSE.TXT for details.
8 // 8 //
9 //===----------------------------------------------------------------------===// 9 //===----------------------------------------------------------------------===//
10 10
11 #include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h" 11 #include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h"
12 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" 12 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h"
13 #include "llvm/Bitcode/ReaderWriter.h"
13 #include "llvm/Support/ErrorHandling.h" 14 #include "llvm/Support/ErrorHandling.h"
14 #include "llvm/Support/Format.h" 15 #include "llvm/Support/Format.h"
15 #include "llvm/Support/raw_ostream.h" 16 #include "llvm/Support/raw_ostream.h"
16 #include "llvm/Support/StreamableMemoryObject.h" 17 #include "llvm/Support/StreamableMemoryObject.h"
17 18
18 #include <limits> 19 #include <limits>
19 #include <cstring> 20 #include <cstring>
20 #include <iomanip> 21 #include <iomanip>
21 22
22 using namespace llvm; 23 using namespace llvm;
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 case kBufferType: 112 case kBufferType:
112 ss << "["; 113 ss << "[";
113 for (size_t i = 0; i < Len; ++i) { 114 for (size_t i = 0; i < Len; ++i) {
114 if (i) 115 if (i)
115 ss << " "; 116 ss << " ";
116 ss << format("%02x", Data[i]); 117 ss << format("%02x", Data[i]);
117 } 118 }
118 ss << "]"; 119 ss << "]";
119 break; 120 break;
120 default: 121 default:
121 report_fatal_error("PNaCL bitcode file contains unknown field type"); 122 report_fatal_error("PNaCl bitcode file contains unknown field type");
122 } 123 }
123 return ss.str(); 124 return ss.str();
124 } 125 }
125 126
126 NaClBitcodeHeader::NaClBitcodeHeader() 127 NaClBitcodeHeader::NaClBitcodeHeader()
127 : HeaderSize(0), UnsupportedMessage(), IsSupportedFlag(false), 128 : HeaderSize(0), UnsupportedMessage(), IsSupportedFlag(false),
128 IsReadableFlag(false), PNaClVersion(0) {} 129 IsReadableFlag(false), PNaClVersion(0) {}
129 130
130 NaClBitcodeHeader::~NaClBitcodeHeader() { 131 NaClBitcodeHeader::~NaClBitcodeHeader() {
131 for (std::vector<NaClBitcodeHeaderField *>::const_iterator 132 for (std::vector<NaClBitcodeHeaderField *>::const_iterator
132 Iter = Fields.begin(), 133 Iter = Fields.begin(),
133 IterEnd = Fields.end(); 134 IterEnd = Fields.end();
134 Iter != IterEnd; ++Iter) { 135 Iter != IterEnd; ++Iter) {
135 delete *Iter; 136 delete *Iter;
136 } 137 }
137 } 138 }
138 139
139 bool NaClBitcodeHeader::ReadPrefix(const unsigned char *BufPtr, 140 bool NaClBitcodeHeader::ReadPrefix(const unsigned char *BufPtr,
140 const unsigned char *BufEnd, 141 const unsigned char *BufEnd,
141 unsigned &NumFields, unsigned &NumBytes) { 142 unsigned &NumFields, unsigned &NumBytes) {
142 // Must contain PEXE. 143 // Must contain PEXE.
143 if (!isNaClBitcode(BufPtr, BufEnd)) 144 if (!isNaClBitcode(BufPtr, BufEnd)) {
145 UnsupportedMessage = "Invalid PNaCl bitcode header";
146 if (isBitcode(BufPtr, BufEnd)) {
147 UnsupportedMessage += " (to run in Chrome, bitcode files must be "
148 "finalized using pnacl-finalize)";
149 }
144 return true; 150 return true;
151 }
145 BufPtr += WordSize; 152 BufPtr += WordSize;
146 153
147 // Read #Fields and number of bytes needed for the header. 154 // Read #Fields and number of bytes needed for the header.
148 if (BufPtr + WordSize > BufEnd) 155 if (BufPtr + WordSize > BufEnd)
149 return true; 156 return UnsupportedError("Bitcode read failure");
150 NumFields = static_cast<unsigned>(BufPtr[0]) | 157 NumFields = static_cast<unsigned>(BufPtr[0]) |
151 (static_cast<unsigned>(BufPtr[1]) << 8); 158 (static_cast<unsigned>(BufPtr[1]) << 8);
152 NumBytes = static_cast<unsigned>(BufPtr[2]) | 159 NumBytes = static_cast<unsigned>(BufPtr[2]) |
153 (static_cast<unsigned>(BufPtr[3]) << 8); 160 (static_cast<unsigned>(BufPtr[3]) << 8);
154 BufPtr += WordSize; 161 BufPtr += WordSize;
155 return false; 162 return false;
156 } 163 }
157 164
158 bool NaClBitcodeHeader::ReadFields(const unsigned char *BufPtr, 165 bool NaClBitcodeHeader::ReadFields(const unsigned char *BufPtr,
159 const unsigned char *BufEnd, 166 const unsigned char *BufEnd,
160 unsigned NumFields, unsigned NumBytes) { 167 unsigned NumFields, unsigned NumBytes) {
161 HeaderSize = NumBytes + (2 * WordSize); 168 HeaderSize = NumBytes + (2 * WordSize);
162 169
163 // Read in each field. 170 // Read in each field.
164 for (size_t i = 0; i < NumFields; ++i) { 171 for (size_t i = 0; i < NumFields; ++i) {
165 NaClBitcodeHeaderField *Field = new NaClBitcodeHeaderField(); 172 NaClBitcodeHeaderField *Field = new NaClBitcodeHeaderField();
166 Fields.push_back(Field); 173 Fields.push_back(Field);
167 if (!Field->Read(BufPtr, BufEnd - BufPtr)) 174 if (!Field->Read(BufPtr, BufEnd - BufPtr))
168 return true; 175 return UnsupportedError("Bitcode read failure");
169 size_t FieldSize = Field->GetTotalSize(); 176 size_t FieldSize = Field->GetTotalSize();
170 BufPtr += FieldSize; 177 BufPtr += FieldSize;
171 } 178 }
172 return false; 179 return false;
173 } 180 }
174 181
175 bool NaClBitcodeHeader::Read(const unsigned char *&BufPtr, 182 bool NaClBitcodeHeader::Read(const unsigned char *&BufPtr,
176 const unsigned char *&BufEnd) { 183 const unsigned char *&BufEnd) {
177 unsigned NumFields; 184 unsigned NumFields;
178 unsigned NumBytes; 185 unsigned NumBytes;
179 if (ReadPrefix(BufPtr, BufEnd, NumFields, NumBytes)) 186 if (ReadPrefix(BufPtr, BufEnd, NumFields, NumBytes))
180 return true; 187 return true; // ReadPrefix sets UnsupportedMessage
181 BufPtr += 2 * WordSize; 188 BufPtr += 2 * WordSize;
182 189
183 if (ReadFields(BufPtr, BufEnd, NumFields, NumBytes)) 190 if (ReadFields(BufPtr, BufEnd, NumFields, NumBytes))
184 return true; 191 return true; // ReadFields sets UnsupportedMessage
185 BufPtr += NumBytes; 192 BufPtr += NumBytes;
186 InstallFields(); 193 InstallFields();
187 return false; 194 return false;
188 } 195 }
189 196
190 bool NaClBitcodeHeader::Read(StreamableMemoryObject *Bytes) { 197 bool NaClBitcodeHeader::Read(StreamableMemoryObject *Bytes) {
191 unsigned NumFields; 198 unsigned NumFields;
192 unsigned NumBytes; 199 unsigned NumBytes;
193 { 200 {
194 unsigned char Buffer[2 * WordSize]; 201 unsigned char Buffer[2 * WordSize];
195 if (Bytes->readBytes(0, sizeof(Buffer), Buffer, NULL) || 202 if (Bytes->readBytes(0, sizeof(Buffer), Buffer, NULL) ||
196 ReadPrefix(Buffer, Buffer + sizeof(Buffer), NumFields, NumBytes)) 203 ReadPrefix(Buffer, Buffer + sizeof(Buffer), NumFields, NumBytes))
197 return true; 204 // ReadPrefix sets UnsupportedMessage but readBytes does not
jvoung (off chromium) 2013/09/13 20:20:23 readBytes could happen first w/ a chance to set th
Derek Schuff 2013/09/13 20:33:15 I mean that if If readBytes fails, ReadPrefix will
jvoung (off chromium) 2013/09/13 20:35:25 but is there a case where readBytes doesn't fail,
205 return UnsupportedError("Bitcode read failure");
198 } 206 }
199 uint8_t *Header = new uint8_t[NumBytes]; 207 uint8_t *Header = new uint8_t[NumBytes];
200 bool failed = 208 bool failed =
201 Bytes->readBytes(2 * WordSize, NumBytes, Header, NULL) || 209 Bytes->readBytes(2 * WordSize, NumBytes, Header, NULL) ||
202 ReadFields(Header, Header + NumBytes, NumFields, NumBytes); 210 ReadFields(Header, Header + NumBytes, NumFields, NumBytes);
203 delete[] Header; 211 delete[] Header;
204 if (failed) 212 if (failed)
205 return true; 213 return UnsupportedError("Bitcode read failure");
206 InstallFields(); 214 InstallFields();
207 return false; 215 return false;
208 } 216 }
209 217
210 NaClBitcodeHeaderField * 218 NaClBitcodeHeaderField *
211 NaClBitcodeHeader::GetTaggedField(NaClBitcodeHeaderField::Tag ID) const { 219 NaClBitcodeHeader::GetTaggedField(NaClBitcodeHeaderField::Tag ID) const {
212 for (std::vector<NaClBitcodeHeaderField *>::const_iterator 220 for (std::vector<NaClBitcodeHeaderField *>::const_iterator
213 Iter = Fields.begin(), 221 Iter = Fields.begin(),
214 IterEnd = Fields.end(); 222 IterEnd = Fields.end();
215 Iter != IterEnd; ++Iter) { 223 Iter != IterEnd; ++Iter) {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 << PNaClVersion << "\n"; 264 << PNaClVersion << "\n";
257 UnsupportedStream.flush(); 265 UnsupportedStream.flush();
258 } 266 }
259 if (Fields.size() != 1) { 267 if (Fields.size() != 1) {
260 IsSupportedFlag = false; 268 IsSupportedFlag = false;
261 IsReadableFlag = false; 269 IsReadableFlag = false;
262 if (!UpdatedUnsupportedMessage) 270 if (!UpdatedUnsupportedMessage)
263 UnsupportedMessage = "Unknown header field(s) found"; 271 UnsupportedMessage = "Unknown header field(s) found";
264 } 272 }
265 } 273 }
OLDNEW
« no previous file with comments | « include/llvm/Bitcode/NaCl/NaClBitcodeHeader.h ('k') | lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698