Skip to content

Commit

Permalink
Add better error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
mschwager committed Feb 1, 2024
1 parent ac30448 commit ef79d75
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions ext/cruzzy/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
require 'open3'
require 'tempfile'

CLANG = 'clang'
FUZZER_NO_MAIN_LIB = 'FUZZER_NO_MAIN_LIB'
CC = 'clang'
CXX = 'clang++'
FUZZER_NO_MAIN_LIB_ENV = 'FUZZER_NO_MAIN_LIB'

find_executable(CLANG)
find_executable(CC)

def get_clang_file_name(file_name)
stdout, status = Open3.capture2(CLANG, '--print-file-name', file_name)
stdout, status = Open3.capture2(CC, '--print-file-name', file_name)
status.success? && File.exist?(stdout.strip) ? stdout.strip : false
end

Expand All @@ -19,16 +20,20 @@ def merge_asan_libfuzzer_lib(asan_lib, fuzzer_no_main_lib)
Tempfile.create do |file|
file.write(File.open(asan_lib).read)

stdout, status = Open3.capture2(
_, status = Open3.capture2(
'ar',
'd',
file.path,
'asan_preinit.cc.o',
'asan_preinit.cpp.o'
)
unless status.success?
puts("The 'ar' archive command failed.")
exit(1)
end

stdout, status = Open3.capture2(
ENV['CXX'],
_, status = Open3.capture2(
CXX,
'-Wl,--whole-archive',
fuzzer_no_main_lib,
file.path,
Expand All @@ -39,6 +44,10 @@ def merge_asan_libfuzzer_lib(asan_lib, fuzzer_no_main_lib)
'-o',
'asan_with_fuzzer.so'
)
unless status.success?
puts("The 'clang' shared object merging command failed.")
exit(1)
end
end
end

Expand All @@ -50,11 +59,11 @@ def merge_asan_libfuzzer_lib(asan_lib, fuzzer_no_main_lib)
fuzzer_no_main_lib = fuzzer_no_main_libs.map { |lib| get_clang_file_name(lib) }.find(&:itself)

unless fuzzer_no_main_lib
puts("Could not find fuzzer_no_main using #{CLANG}.")
fuzzer_no_main_lib = ENV.fetch(FUZZER_NO_MAIN_LIB, nil)
puts("Could not find fuzzer_no_main using #{CC}.")
fuzzer_no_main_lib = ENV.fetch(FUZZER_NO_MAIN_LIB_ENV, nil)
if fuzzer_no_main_lib.nil?
puts("Could not find fuzzer_no_main in #{FUZZER_NO_MAIN_LIB}.")
puts("Please include #{CLANG} in your path or specify #{FUZZER_NO_MAIN_LIB} ENV variable.")
puts("Could not find fuzzer_no_main in #{FUZZER_NO_MAIN_LIB_ENV}.")
puts("Please include #{CC} in your path or specify #{FUZZER_NO_MAIN_LIB_ENV} ENV variable.")
exit(1)
end
end
Expand All @@ -72,7 +81,7 @@ def merge_asan_libfuzzer_lib(asan_lib, fuzzer_no_main_lib)
asan_lib = asan_libs.map { |lib| get_clang_file_name(lib) }.find(&:itself)

unless asan_lib
puts("Could not find asan using #{CLANG}.")
puts("Could not find asan using #{CC}.")
exit(1)
end

Expand Down

0 comments on commit ef79d75

Please sign in to comment.