Top Level Namespace

Defined Under Namespace

Modules: Ruzzy Classes: Integer

Constant Summary collapse

LOGGER =
Logger.new($stderr)
CC =

These ENV variables really shouldn’t be used because we don’t support compilers other than clang, like gcc, etc. Instead prefer to properly include clang in your PATH. But they’re here if you really need them. Also note that technically Ruby does not support C extensions compiled with a different compiler than Ruby itself was compiled with. So we’re on somewhat shaky ground here. For more information see: github.com/rubygems/rubygems/issues/1508

ENV.fetch('CC', 'clang')
CXX =
ENV.fetch('CXX', 'clang++')
AR =
ENV.fetch('AR', 'ar')
FUZZER_NO_MAIN_LIB_ENV =
'FUZZER_NO_MAIN_LIB'

Instance Method Summary collapse

Instance Method Details

#get_clang_file_name(file_name) ⇒ Object



31
32
33
34
35
36
37
# File 'ext/cruzzy/extconf.rb', line 31

def get_clang_file_name(file_name)
  stdout, status = Open3.capture2(CC, '--print-file-name', file_name)
  success = status.success?
  exists = success ? File.exist?(stdout.strip) : false
  LOGGER.debug("Search for #{file_name} using #{CC}: success=#{success} exists=#{exists}")
  success && exists ? stdout.strip : false
end

#merge_sanitizer_libfuzzer_lib(sanitizer_lib, fuzzer_no_main_lib, merged_output, *preinits) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'ext/cruzzy/extconf.rb', line 39

def merge_sanitizer_libfuzzer_lib(sanitizer_lib, fuzzer_no_main_lib, merged_output, *preinits)
  # https://github.com/google/atheris/blob/master/native_extension_fuzzing.md#why-this-is-necessary
  Tempfile.create do |file|
    LOGGER.debug("Creating #{sanitizer_lib} sanitizer archive at #{file.path}")

    file.write(File.open(sanitizer_lib).read)

    _, status = Open3.capture2(
      AR,
      'd',
      file.path,
      *preinits
    )
    unless status.success?
      LOGGER.error("The #{AR} archive command failed.")
      exit(1)
    end

    LOGGER.debug("Merging sanitizer at #{file.path} with libFuzzer at #{fuzzer_no_main_lib} to #{merged_output}")

    _, status = Open3.capture2(
      CXX,
      '-Wl,--whole-archive',
      fuzzer_no_main_lib,
      file.path,
      '-Wl,--no-whole-archive',
      '-lpthread',
      '-ldl',
      '-lstdc++',
      '-shared',
      '-o',
      merged_output
    )
    unless status.success?
      LOGGER.error("The #{CXX} shared object merging command failed.")
      exit(1)
    end
  end
end