Skip to content

Commit 165d336

Browse files
authored
Merge pull request #37 from trailofbits/ww/make-extraction-errors-fatal
extractor: Make extraction errors fatal
2 parents 8110c3f + c14c8e6 commit 165d336

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

cmd/get-bc/main.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,9 @@ func main() {
4242
// Parse command line
4343
var args = os.Args
4444

45-
shared.Extract(args)
45+
exitCode := shared.Extract(args)
4646

4747
shared.LogInfo("Calling %v DID NOT TELL US WHAT HAPPENED\n", os.Args)
4848

49-
// could be more honest about our success here
50-
os.Exit(0)
51-
49+
os.Exit(exitCode)
5250
}

shared/extractor.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type ExtractionArgs struct {
5858
SortBitcodeFiles bool // sort the arguments to linking and archiving (debugging too)
5959
BuildBitcodeModule bool // buld an archive rather than a module
6060
KeepTemp bool // keep temporary linking folder
61+
StrictExtract bool // turn extraction failures into errors
6162
LinkArgSize int // maximum size of a llvm-link command line
6263
InputType int
6364
ObjectTypeInArchive int // Type of file that can be put into an archive
@@ -67,7 +68,7 @@ type ExtractionArgs struct {
6768
LlvmArchiverName string
6869
ArchiverName string
6970
ArArgs []string
70-
Extractor func(string) []string
71+
Extractor func(string) ([]string, bool)
7172
}
7273

7374
//for printing out the parsed arguments, some have been skipped.
@@ -85,10 +86,11 @@ ea.OutputFile: %v
8586
ea.LlvmArchiverName: %v
8687
ea.LlvmLinkerName: %v
8788
ea.ArchiverName: %v
89+
ea.StrictExtract: %v
8890
`
8991
return fmt.Sprintf(format, ea.Verbose, ea.WriteManifest, ea.SortBitcodeFiles, ea.BuildBitcodeModule,
9092
ea.KeepTemp, ea.LinkArgSize, ea.InputFile, ea.OutputFile, ea.LlvmArchiverName,
91-
ea.LlvmLinkerName, ea.ArchiverName)
93+
ea.LlvmLinkerName, ea.ArchiverName, ea.StrictExtract)
9294
}
9395

9496
//ParseSwitches parses the command line into an ExtractionArgs object.
@@ -106,6 +108,7 @@ func ParseSwitches(args []string) (ea ExtractionArgs) {
106108
flagSet.StringVar(&ea.LlvmLinkerName, "l", "llvm-link", "the llvm linker (i.e. llvm-link)")
107109
flagSet.IntVar(&ea.LinkArgSize, "n", 0, "maximum llvm-link command line size (in bytes)")
108110
flagSet.BoolVar(&ea.KeepTemp, "t", false, "keep temporary linking folder")
111+
flagSet.BoolVar(&ea.StrictExtract, "S", false, "exit with an error if extraction fails")
109112

110113
err := flagSet.Parse(args[1:])
111114

@@ -257,7 +260,11 @@ func resolveTool(defaultPath string, envPath string, usrPath string) (path strin
257260

258261
func handleExecutable(ea ExtractionArgs) (success bool) {
259262
// get the list of bitcode paths
260-
artifactPaths := ea.Extractor(ea.InputFile)
263+
var artifactPaths []string
264+
artifactPaths, success = ea.Extractor(ea.InputFile)
265+
if !success && ea.StrictExtract {
266+
return
267+
}
261268

262269
if len(artifactPaths) < 20 {
263270
// naert: to avoid saturating the log when dealing with big file lists
@@ -304,7 +311,11 @@ func handleThinArchive(ea ExtractionArgs) (success bool) {
304311
for index, obj := range objectFiles {
305312
LogInfo("obj = '%v'\n", obj)
306313
if len(obj) > 0 {
307-
artifacts := ea.Extractor(obj)
314+
var artifacts []string
315+
artifacts, success = ea.Extractor(obj)
316+
if !success && ea.StrictExtract {
317+
return
318+
}
308319
LogInfo("\t%v\n", artifacts)
309320
artifactFiles = append(artifactFiles, artifacts...)
310321
for _, bc := range artifacts {
@@ -456,8 +467,11 @@ func handleArchive(ea ExtractionArgs) (success bool) {
456467
for i := 1; i <= instance; i++ {
457468

458469
if obj != "" && extractFile(ea, inputFile, obj, i) {
459-
460-
artifacts := ea.Extractor(obj)
470+
var artifacts []string
471+
artifacts, success = ea.Extractor(obj)
472+
if !success && ea.StrictExtract {
473+
return
474+
}
461475
LogInfo("\t%v\n", artifacts)
462476
artifactFiles = append(artifactFiles, artifacts...)
463477
for _, bc := range artifacts {
@@ -671,43 +685,45 @@ func linkBitcodeFiles(ea ExtractionArgs, filesToLink []string) (success bool) {
671685
return
672686
}
673687

674-
func extractSectionDarwin(inputFile string) (contents []string) {
688+
func extractSectionDarwin(inputFile string) (contents []string, success bool) {
675689
machoFile, err := macho.Open(inputFile)
676690
if err != nil {
677691
LogError("Mach-O file %s could not be read.", inputFile)
678692
return
679693
}
680694
section := machoFile.Section(DarwinSectionName)
681695
if section == nil {
682-
LogWarning("The %s section of %s is missing!\n", DarwinSectionName, inputFile)
696+
LogError("The %s section of %s is missing!\n", DarwinSectionName, inputFile)
683697
return
684698
}
685699
sectionContents, errContents := section.Data()
686700
if errContents != nil {
687-
LogWarning("Error reading the %s section of Mach-O file %s.", DarwinSectionName, inputFile)
701+
LogError("Error reading the %s section of Mach-O file %s.", DarwinSectionName, inputFile)
688702
return
689703
}
690704
contents = strings.Split(strings.TrimSuffix(string(sectionContents), "\n"), "\n")
705+
success = true
691706
return
692707
}
693708

694-
func extractSectionUnix(inputFile string) (contents []string) {
709+
func extractSectionUnix(inputFile string) (contents []string, success bool) {
695710
elfFile, err := elf.Open(inputFile)
696711
if err != nil {
697712
LogError("ELF file %s could not be read.", inputFile)
698713
return
699714
}
700715
section := elfFile.Section(ELFSectionName)
701716
if section == nil {
702-
LogWarning("Error reading the %s section of ELF file %s.", ELFSectionName, inputFile)
717+
LogError("Error reading the %s section of ELF file %s.", ELFSectionName, inputFile)
703718
return
704719
}
705720
sectionContents, errContents := section.Data()
706721
if errContents != nil {
707-
LogWarning("Error reading the %s section of ELF file %s.", ELFSectionName, inputFile)
722+
LogError("Error reading the %s section of ELF file %s.", ELFSectionName, inputFile)
708723
return
709724
}
710725
contents = strings.Split(strings.TrimSuffix(string(sectionContents), "\n"), "\n")
726+
success = true
711727
return
712728
}
713729

0 commit comments

Comments
 (0)