@@ -83,7 +83,6 @@ OffloadTargetInfo::OffloadTargetInfo(const StringRef Target,
83
83
const OffloadBundlerConfig &BC)
84
84
: BundlerConfig(BC) {
85
85
86
- // TODO: Add error checking from ClangOffloadBundler.cpp
87
86
// <kind>-<triple>[-<target id>[:target features]]
88
87
// <triple> := <arch>-<vendor>-<os>-<env>
89
88
SmallVector<StringRef, 6 > Components;
@@ -1514,6 +1513,9 @@ Error OffloadBundler::UnbundleFiles() {
1514
1513
StringMap<StringRef> Worklist;
1515
1514
auto Output = BundlerConfig.OutputFileNames .begin ();
1516
1515
for (auto &Triple : BundlerConfig.TargetNames ) {
1516
+ if (!checkOffloadBundleID (Triple))
1517
+ return createStringError (errc::invalid_argument,
1518
+ " invalid bundle id from bundle config" );
1517
1519
Worklist[Triple] = *Output;
1518
1520
++Output;
1519
1521
}
@@ -1533,6 +1535,9 @@ Error OffloadBundler::UnbundleFiles() {
1533
1535
1534
1536
StringRef CurTriple = **CurTripleOrErr;
1535
1537
assert (!CurTriple.empty ());
1538
+ if (!checkOffloadBundleID (CurTriple))
1539
+ return createStringError (errc::invalid_argument,
1540
+ " invalid bundle id read from the bundle" );
1536
1541
1537
1542
auto Output = Worklist.begin ();
1538
1543
for (auto E = Worklist.end (); Output != E; Output++) {
@@ -1591,6 +1596,8 @@ Error OffloadBundler::UnbundleFiles() {
1591
1596
return createFileError (E.second , EC);
1592
1597
1593
1598
// If this entry has a host kind, copy the input file to the output file.
1599
+ // We don't need to check E.getKey() here through checkOffloadBundleID
1600
+ // because the entire WorkList has been checked above.
1594
1601
auto OffloadInfo = OffloadTargetInfo (E.getKey (), BundlerConfig);
1595
1602
if (OffloadInfo.hasHostKind ())
1596
1603
OutputFile.write (Input.getBufferStart (), Input.getBufferSize ());
@@ -1820,6 +1827,10 @@ Error OffloadBundler::UnbundleArchive() {
1820
1827
// archive.
1821
1828
while (!CodeObject.empty ()) {
1822
1829
SmallVector<StringRef> CompatibleTargets;
1830
+ if (!checkOffloadBundleID (CodeObject)) {
1831
+ return createStringError (errc::invalid_argument,
1832
+ " Invalid bundle id read from code object" );
1833
+ }
1823
1834
auto CodeObjectInfo = OffloadTargetInfo (CodeObject, BundlerConfig);
1824
1835
if (getCompatibleOffloadTargets (CodeObjectInfo, CompatibleTargets,
1825
1836
BundlerConfig)) {
@@ -1901,3 +1912,11 @@ Error OffloadBundler::UnbundleArchive() {
1901
1912
1902
1913
return Error::success ();
1903
1914
}
1915
+
1916
+ bool clang::checkOffloadBundleID (const llvm::StringRef Str) {
1917
+ // <kind>-<triple>[-<target id>[:target features]]
1918
+ // <triple> := <arch>-<vendor>-<os>-<env>
1919
+ SmallVector<StringRef, 6 > Components;
1920
+ Str.split (Components, ' -' , /* MaxSplit=*/ 5 );
1921
+ return Components.size () == 5 || Components.size () == 6 ;
1922
+ }
0 commit comments