Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions setup_utils/find_libfuzzer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python3
import os
import sys
import platform
import subprocess

def find_libfuzzer():
"""This function returns either None or path to libfuzzer.
it may also interact with stderr"""
uname = platform.system()

libpaths = []
if uname == "Darwin":
libpaths.append("lib/darwin/libclang_rt.fuzzer_no_main_osx.a")
elif uname == "Linux":
machine = platform.machine()
if machine == "x86_64":
libpaths.append("lib/linux/libclang_rt.fuzzer_no_main-x86_64.a")
elif machine == "i386":
libpaths.append("lib/linux/libclang_rt.fuzzer_no_main-i386.a")
elif machine == "i686":
libpaths.append("lib/linux/libclang_rt.fuzzer_no_main-i386.a")
elif machine == "aarch64":
libpaths.append("lib/linux/libclang_rt.fuzzer_no_main-aarch64.a")
else:
sys.stderr.write(f"Failed to identify platform machine (got {machine}); set $LIBFUZZER_LIB to point directly to your libfuzzer .a file if the build fails.\n")
libpaths.append("lib/*linux*/libclang_rt.fuzzer_no_main*.a")
else:
sys.stderr.write(f"Failed to identify platform (got {uname}); set $LIBFUZZER_LIB to point directly to your libfuzzer .a file if the build fails.\n")
libpaths.append("lib/*/libclang_rt.fuzzer_no_main*.a")

clang_bin = os.environ.get("CLANG_BIN", "clang")
try:
output = subprocess.check_output([clang_bin, "-print-search-dirs"], text=True, stderr=subprocess.DEVNULL)
except:
return

search_dirs = []
for line in output.splitlines():
if line.startswith("libraries: ="):
dirs = line[len("libraries: ="):].strip()
if dirs:
search_dirs = dirs.split(":")
break

if len(search_dirs) == 0:
sys.stderr.write("Failed to get search paths from clang, set $LIBFUZZER_LIB to point directly to your libfuzzer .a file.")
return

for libpath in libpaths:
for directory in search_dirs:
candidate = os.path.join(directory, libpath)
if os.path.isfile(candidate):
# FOUND!
return candidate

sys.stderr.write(f"Failed to find libFuzzer archive in search path {':'.join(search_dirs)}\n")
17 changes: 7 additions & 10 deletions setup_utils/find_libfuzzer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@ uname="$(uname)"
if [[ "$uname" == "Darwin" ]]; then
libpath="lib/darwin/libclang_rt.fuzzer_no_main_osx.a"
elif [[ "$uname" == "Linux" ]]; then
machine="$(uname -m)"
if [[ "$machine" == "x86_64" ]]; then
libpath="lib/linux/libclang_rt.fuzzer_no_main-x86_64.a"
elif [[ "$machine" == "i386" ]]; then
libpath="lib/linux/libclang_rt.fuzzer_no_main-i386.a"
elif [[ "$machine" == "i686" ]]; then
libpath="lib/linux/libclang_rt.fuzzer_no_main-i386.a"
elif [[ "$machine" == "aarch64" ]]; then
libpath="lib/linux/libclang_rt.fuzzer_no_main-aarch64.a"
libpath="lib/*linux*/libclang_rt.fuzzer_no_main*.a"
if [ -z "$CLANG_BIN" ]; then
machine="$("$CLANG_BIN" -dumpmachine)"
else
>&2 echo "Failed to identify platform machine (got $machine); set \$LIBFUZZER_LIB to point directly to your libfuzzer .a file."
machine="$(clang -dumpmachine)"
fi
if [ -f libpath]; then
>&2 echo "Failed to identify platform (got $machine); set \$LIBFUZZER_LIB to point directly to your libfuzzer .a file."
fi
else
>&2 echo "Failed to identify platform (got $uname); set \$LIBFUZZER_LIB to point directly to your libfuzzer .a file."
Expand Down