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

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

Issue 222313004: PNaCl bitcode reader: restrict some record sizes (Closed) Base URL: http://git.chromium.org/native_client/pnacl-llvm.git@master
Patch Set: Created 6 years, 8 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 //===- NaClBitcodeReader.cpp ----------------------------------------------===// 1 //===- NaClBitcodeReader.cpp ----------------------------------------------===//
2 // Internal NaClBitcodeReader implementation 2 // Internal NaClBitcodeReader implementation
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
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 Type *ResultTy = 0; 321 Type *ResultTy = 0;
322 unsigned TypeCode = Stream.readRecord(Entry.ID, Record); 322 unsigned TypeCode = Stream.readRecord(Entry.ID, Record);
323 switch (TypeCode) { 323 switch (TypeCode) {
324 default: { 324 default: {
325 std::string Message; 325 std::string Message;
326 raw_string_ostream StrM(Message); 326 raw_string_ostream StrM(Message);
327 StrM << "Unknown type code in type table: " << TypeCode; 327 StrM << "Unknown type code in type table: " << TypeCode;
328 StrM.flush(); 328 StrM.flush();
329 return Error(Message); 329 return Error(Message);
330 } 330 }
331
331 case naclbitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] 332 case naclbitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
332 // TYPE_CODE_NUMENTRY contains a count of the number of types in the 333 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
333 // type list. This allows us to reserve space. 334 // type list. This allows us to reserve space.
334 if (Record.size() < 1) 335 if (Record.size() != 1)
335 return Error("Invalid TYPE_CODE_NUMENTRY record"); 336 return Error("Invalid TYPE_CODE_NUMENTRY record");
336 TypeList.resize(Record[0]); 337 TypeList.resize(Record[0]);
338 // No type was defined, skip the checks that follow the switch.
337 continue; 339 continue;
338 case naclbitc::TYPE_CODE_VOID: // VOID 340
341 case naclbitc::TYPE_CODE_VOID: // VOID
342 if (Record.size() != 0)
343 return Error("Invalid TYPE_CODE_VOID record");
339 ResultTy = Type::getVoidTy(Context); 344 ResultTy = Type::getVoidTy(Context);
340 break; 345 break;
341 case naclbitc::TYPE_CODE_FLOAT: // FLOAT 346
347 case naclbitc::TYPE_CODE_FLOAT: // FLOAT
348 if (Record.size() != 0)
349 return Error("Invalid TYPE_CODE_FLOAT record");
342 ResultTy = Type::getFloatTy(Context); 350 ResultTy = Type::getFloatTy(Context);
343 break; 351 break;
344 case naclbitc::TYPE_CODE_DOUBLE: // DOUBLE 352
353 case naclbitc::TYPE_CODE_DOUBLE: // DOUBLE
354 if (Record.size() != 0)
355 return Error("Invalid TYPE_CODE_DOUBLE record");
345 ResultTy = Type::getDoubleTy(Context); 356 ResultTy = Type::getDoubleTy(Context);
346 break; 357 break;
347 case naclbitc::TYPE_CODE_INTEGER: // INTEGER: [width]
348 if (Record.size() < 1)
349 return Error("Invalid Integer type record");
350 358
359 case naclbitc::TYPE_CODE_INTEGER: // INTEGER: [width]
360 if (Record.size() != 1)
361 return Error("Invalid TYPE_CODE_INTEGER record");
351 ResultTy = IntegerType::get(Context, Record[0]); 362 ResultTy = IntegerType::get(Context, Record[0]);
352 break; 363 break;
364
353 case naclbitc::TYPE_CODE_FUNCTION: { 365 case naclbitc::TYPE_CODE_FUNCTION: {
354 // FUNCTION: [vararg, retty, paramty x N] 366 // FUNCTION: [vararg, retty, paramty x N]
355 if (Record.size() < 2) 367 if (Record.size() < 2)
356 return Error("Invalid FUNCTION type record"); 368 return Error("Invalid TYPE_CODE_FUNCTION record");
357 SmallVector<Type*, 8> ArgTys; 369 SmallVector<Type *, 8> ArgTys;
358 for (unsigned i = 2, e = Record.size(); i != e; ++i) { 370 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
359 if (Type *T = getTypeByID(Record[i])) 371 if (Type *T = getTypeByID(Record[i]))
360 ArgTys.push_back(T); 372 ArgTys.push_back(T);
361 else 373 else
362 break; 374 break;
363 } 375 }
364 376
365 ResultTy = getTypeByID(Record[1]); 377 ResultTy = getTypeByID(Record[1]);
366 if (ResultTy == 0 || ArgTys.size() < Record.size()-2) 378 if (ResultTy == 0 || ArgTys.size() < Record.size() - 2)
367 return Error("invalid type in function type"); 379 return Error("invalid type in function type");
368 380
369 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); 381 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
370 break; 382 break;
371 } 383 }
372 } 384 }
373 385
374 if (NumRecords >= TypeList.size()) 386 if (NumRecords >= TypeList.size())
375 return Error("invalid TYPE table"); 387 return Error("invalid TYPE table");
376 assert(ResultTy && "Didn't read a type?"); 388 assert(ResultTy && "Didn't read a type?");
(...skipping 690 matching lines...) Expand 10 before | Expand all | Expand 10 after
1067 raw_string_ostream StrM(Message); 1079 raw_string_ostream StrM(Message);
1068 StrM << "Unknown instruction record: <" << BitCode; 1080 StrM << "Unknown instruction record: <" << BitCode;
1069 for (unsigned I = 0, E = Record.size(); I != E; ++I) { 1081 for (unsigned I = 0, E = Record.size(); I != E; ++I) {
1070 StrM << " " << Record[I]; 1082 StrM << " " << Record[I];
1071 } 1083 }
1072 StrM << ">"; 1084 StrM << ">";
1073 return Error(StrM.str()); 1085 return Error(StrM.str());
1074 } 1086 }
1075 1087
1076 case naclbitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks] 1088 case naclbitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks]
1077 if (Record.size() < 1 || Record[0] == 0) 1089 if (Record.size() != 1 || Record[0] == 0)
1078 return Error("Invalid DECLAREBLOCKS record"); 1090 return Error("Invalid DECLAREBLOCKS record");
1079 // Create all the basic blocks for the function. 1091 // Create all the basic blocks for the function.
1080 FunctionBBs.resize(Record[0]); 1092 FunctionBBs.resize(Record[0]);
1081 for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i) { 1093 for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i) {
1082 BasicBlockInfo &BBInfo = FunctionBBs[i]; 1094 BasicBlockInfo &BBInfo = FunctionBBs[i];
1083 BBInfo.BB = BasicBlock::Create(Context, "", F); 1095 BBInfo.BB = BasicBlock::Create(Context, "", F);
1084 } 1096 }
1085 CurBB = FunctionBBs.at(0).BB; 1097 CurBB = FunctionBBs.at(0).BB;
1086 continue; 1098 continue;
1087 1099
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1137 break; 1149 break;
1138 } 1150 }
1139 1151
1140 case naclbitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [opval, opval, pred] 1152 case naclbitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [opval, opval, pred]
1141 // new form of select 1153 // new form of select
1142 // handles select i1 or select [N x i1] 1154 // handles select i1 or select [N x i1]
1143 unsigned OpNum = 0; 1155 unsigned OpNum = 0;
1144 Value *TrueVal, *FalseVal, *Cond; 1156 Value *TrueVal, *FalseVal, *Cond;
1145 if (popValue(Record, &OpNum, NextValueNo, &TrueVal) || 1157 if (popValue(Record, &OpNum, NextValueNo, &TrueVal) ||
1146 popValue(Record, &OpNum, NextValueNo, &FalseVal) || 1158 popValue(Record, &OpNum, NextValueNo, &FalseVal) ||
1147 popValue(Record, &OpNum, NextValueNo, &Cond)) 1159 popValue(Record, &OpNum, NextValueNo, &Cond) ||
1160 OpNum != Record.size())
1148 return Error("Invalid SELECT record"); 1161 return Error("Invalid SELECT record");
1149 1162
1150 TrueVal = ConvertOpToScalar(TrueVal, CurBBNo); 1163 TrueVal = ConvertOpToScalar(TrueVal, CurBBNo);
1151 FalseVal = ConvertOpToScalar(FalseVal, CurBBNo); 1164 FalseVal = ConvertOpToScalar(FalseVal, CurBBNo);
1152 1165
1153 // expect i1 1166 // expect i1
1154 if (Cond->getType() != Type::getInt1Ty(Context)) 1167 if (Cond->getType() != Type::getInt1Ty(Context))
1155 return Error("Invalid SELECT condition type"); 1168 return Error("Invalid SELECT condition type");
1156 1169
1157 I = SelectInst::Create(Cond, TrueVal, FalseVal); 1170 I = SelectInst::Create(Cond, TrueVal, FalseVal);
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
1310 Op = ConvertOpToType(Op, T->getPointerTo(), CurBBNo); 1323 Op = ConvertOpToType(Op, T->getPointerTo(), CurBBNo);
1311 if (Op == 0) return true; 1324 if (Op == 0) return true;
1312 I = new LoadInst(Op, "", false, (1 << Record[OpNum]) >> 1); 1325 I = new LoadInst(Op, "", false, (1 << Record[OpNum]) >> 1);
1313 break; 1326 break;
1314 } 1327 }
1315 case naclbitc::FUNC_CODE_INST_STORE: { 1328 case naclbitc::FUNC_CODE_INST_STORE: {
1316 // STORE: [ptr, val, align] 1329 // STORE: [ptr, val, align]
1317 unsigned OpNum = 0; 1330 unsigned OpNum = 0;
1318 Value *Val, *Ptr; 1331 Value *Val, *Ptr;
1319 if (popValue(Record, &OpNum, NextValueNo, &Ptr) || 1332 if (popValue(Record, &OpNum, NextValueNo, &Ptr) ||
1320 popValue(Record, &OpNum, NextValueNo, &Val)) 1333 popValue(Record, &OpNum, NextValueNo, &Val) ||
1321 return Error("Invalid STORE record"); 1334 OpNum+1 != Record.size())
1322 if (OpNum+1 != Record.size())
1323 return Error("Invalid STORE record"); 1335 return Error("Invalid STORE record");
1324 Val = ConvertOpToScalar(Val, CurBBNo); 1336 Val = ConvertOpToScalar(Val, CurBBNo);
1325 Ptr = ConvertOpToType(Ptr, Val->getType()->getPointerTo(), CurBBNo); 1337 Ptr = ConvertOpToType(Ptr, Val->getType()->getPointerTo(), CurBBNo);
1326 I = new StoreInst(Val, Ptr, false, (1 << Record[OpNum]) >> 1); 1338 I = new StoreInst(Val, Ptr, false, (1 << Record[OpNum]) >> 1);
1327 break; 1339 break;
1328 } 1340 }
1329 case naclbitc::FUNC_CODE_INST_CALL: 1341 case naclbitc::FUNC_CODE_INST_CALL:
1330 case naclbitc::FUNC_CODE_INST_CALL_INDIRECT: { 1342 case naclbitc::FUNC_CODE_INST_CALL_INDIRECT: {
1331 // CALL: [cc, fnid, arg0, arg1...] 1343 // CALL: [cc, fnid, arg0, arg1...]
1332 // CALL_INDIRECT: [cc, fnid, fnty, args...] 1344 // CALL_INDIRECT: [cc, fnid, fnty, args...]
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after
1696 if (M->MaterializeAllPermanently(ErrMsg)) { 1708 if (M->MaterializeAllPermanently(ErrMsg)) {
1697 delete M; 1709 delete M;
1698 return 0; 1710 return 0;
1699 } 1711 }
1700 1712
1701 // TODO: Restore the use-lists to the in-memory state when the bitcode was 1713 // TODO: Restore the use-lists to the in-memory state when the bitcode was
1702 // written. We must defer until the Module has been fully materialized. 1714 // written. We must defer until the Module has been fully materialized.
1703 1715
1704 return M; 1716 return M;
1705 } 1717 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698