Changes needed to run on OSS-Fuzz.

Bump libunwindstack to build with system libc++.

Bug: 69150303
Change-Id: I6a4fae6bb201f0e2164a6d04ad116f1b61e3bf0f
diff --git a/BUILD.gn b/BUILD.gn
index 773c50b..6e4bab5 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -75,7 +75,7 @@
       if (is_linux) {
         deps += [ "tools:pipestats" ]
       }
-      if (use_libfuzzer) {
+      if (is_fuzzer) {
         deps += [ ":fuzzers" ]
       }
     }
diff --git a/gn/standalone/BUILD.gn b/gn/standalone/BUILD.gn
index 863b972..7c1e56d 100644
--- a/gn/standalone/BUILD.gn
+++ b/gn/standalone/BUILD.gn
@@ -191,7 +191,7 @@
   ]
   if (is_android) {
     cflags += [ "-Oz" ]
-  } else if (use_libfuzzer) {
+  } else if (is_fuzzer) {
     cflags += [ "-O1" ]
   } else {
     cflags += [ "-O3" ]
diff --git a/gn/standalone/BUILDCONFIG.gn b/gn/standalone/BUILDCONFIG.gn
index d634c3f..773f87d 100644
--- a/gn/standalone/BUILDCONFIG.gn
+++ b/gn/standalone/BUILDCONFIG.gn
@@ -15,7 +15,12 @@
 declare_args() {
   is_debug = true
   is_clang = true
+  is_system_compiler = false
   is_lto = false
+
+  extra_cflags = ""
+  extra_cxxflags = ""
+  extra_ldflags = ""
 }
 
 declare_args() {
diff --git a/gn/standalone/fuzzer.gni b/gn/standalone/fuzzer.gni
index 4a2ae14..c6e3ed5 100644
--- a/gn/standalone/fuzzer.gni
+++ b/gn/standalone/fuzzer.gni
@@ -17,9 +17,13 @@
 template("perfetto_fuzzer_test") {
   forward_variables_from(invoker, "*")
 
-  if (use_libfuzzer) {
+  if (is_fuzzer) {
     executable(target_name) {
-      deps += [ "//buildtools:libfuzzer" ]
+      if (use_libfuzzer) {
+        deps += [ "//buildtools:libfuzzer" ]
+      } else {
+        ldflags = [ link_fuzzer ]
+      }
     }
   } else {
     not_needed(invoker, "*")
diff --git a/gn/standalone/sanitizers/BUILD.gn b/gn/standalone/sanitizers/BUILD.gn
index 31718f2..b0cbf13 100644
--- a/gn/standalone/sanitizers/BUILD.gn
+++ b/gn/standalone/sanitizers/BUILD.gn
@@ -87,7 +87,7 @@
     ]
     defines += [ "UNDEFINED_SANITIZER" ]
   }
-  if (use_libfuzzer) {
+  if (is_fuzzer) {
     cflags += [ "-fsanitize=fuzzer-no-link" ]
     if (is_asan) {
       cflags += [
diff --git a/gn/standalone/sanitizers/vars.gni b/gn/standalone/sanitizers/vars.gni
index d1f2760..32613d1 100644
--- a/gn/standalone/sanitizers/vars.gni
+++ b/gn/standalone/sanitizers/vars.gni
@@ -28,8 +28,17 @@
   # Undefined Behaviour Sanitizer.
   is_ubsan = false
 
-  # # Compile for fuzzing with LLVM LibFuzzer.
-  use_libfuzzer = false
+  # Compile for fuzzing.
+  is_fuzzer = false
+}
+
+declare_args() {
+  # Link in LLVM LibFuzzer.
+  use_libfuzzer = is_fuzzer
+
+  # If is_fuzzer=true and use_libfuzzer=false, add this flag to ldflags when
+  # linking fuzzer executables.
+  link_fuzzer = ""
 }
 
 declare_args() {
@@ -37,6 +46,8 @@
       is_asan || is_lsan || is_tsan || is_msan || is_ubsan || use_libfuzzer
 }
 
-assert(!using_sanitizer || is_clang, "is_*san requires is_clang=true'")
+assert(!using_sanitizer || is_clang || is_system_compiler,
+       "is_*san requires is_clang=true'")
 assert(!is_msan || is_linux, "msan only supported on linux")
 assert(!is_tsan || (is_linux || is_mac), "tsan only supported on linux and mac")
+assert(!is_fuzzer || use_libfuzzer || link_fuzzer != "")
diff --git a/gn/standalone/toolchain/BUILD.gn b/gn/standalone/toolchain/BUILD.gn
index 9f4e983..1ffb94f 100644
--- a/gn/standalone/toolchain/BUILD.gn
+++ b/gn/standalone/toolchain/BUILD.gn
@@ -18,7 +18,10 @@
 
 declare_args() {
   if (is_clang) {
-    if (is_linux) {
+    if (is_system_compiler) {
+      cc = "\$CC "
+      cxx = "\$CXX "
+    } else if (is_linux) {
       cc = linux_clang_bin
       cxx = linux_clangxx_bin
     } else {
@@ -66,7 +69,7 @@
 
     tool("cc") {
       depfile = "{{output}}.d"
-      command = "$cc_wrapper $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
+      command = "$cc_wrapper $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} ${extra_cflags} -c {{source}} -o {{output}}"
       depsformat = "gcc"
       outputs = [
         "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
@@ -76,7 +79,7 @@
 
     tool("cxx") {
       depfile = "{{output}}.d"
-      command = "$cc_wrapper $cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
+      command = "$cc_wrapper $cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}}  ${extra_cflags} ${extra_cxxflags} -c {{source}} -o {{output}}"
       depsformat = "gcc"
       outputs = [
         "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
@@ -118,7 +121,7 @@
         rpath = "-Wl,-install_name,@rpath/$soname"
       }
 
-      command = "$cc_wrapper $cxx -shared {{ldflags}} {{inputs}} {{solibs}} {{libs}} $rpath -o {{output}}"
+      command = "$cc_wrapper $cxx -shared {{ldflags}} ${extra_ldflags} {{inputs}} {{solibs}} {{libs}} $rpath -o {{output}}"
       outputs = [
         "{{root_out_dir}}/$soname",
       ]
@@ -128,7 +131,7 @@
     }
 
     tool("link") {
-      command = "$cc_wrapper $cxx {{ldflags}} {{inputs}} {{solibs}} {{libs}} -o {{output}}"
+      command = "$cc_wrapper $cxx {{ldflags}} ${extra_ldflags} {{inputs}} {{solibs}} {{libs}} -o {{output}}"
       outputs = [
         "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
       ]
diff --git a/infra/fuzzing/Dockerfile b/infra/fuzzing/Dockerfile
deleted file mode 100644
index 3eeb1b5..0000000
--- a/infra/fuzzing/Dockerfile
+++ /dev/null
@@ -1,44 +0,0 @@
-# Copyright (C) 2018 The Android Open Source Project
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-# Build with
-# cd infra/fuzzing
-# $ sudo docker build -t fuzzing .
-
-# Run with
-# $ CONTAINER=$(sudo docker run -d --cap-add=SYS_PTRACE fuzzing)
-
-# Get outputs with
-# $ sudo docker cp -a ${CONTAINER}:/home/perfetto/perfetto/fuzz_out /tmp
-
-FROM debian:latest
-
-ENV DEBIAN_FRONTEND noninteractive
-
-RUN echo deb http://deb.debian.org/debian testing main > /etc/apt/sources.list.d/testing.list
-RUN apt-get update
-RUN apt-get -y install python git curl
-# gcc-7 for sysroot
-RUN apt-get -y -t testing install gcc-7
-
-# pip for installing certiain test script dependencies
-RUN curl https://bootstrap.pypa.io/get-pip.py | python -
-
-RUN useradd -m perfetto
-USER perfetto:perfetto
-WORKDIR /home/perfetto
-
-RUN git clone https://android.googlesource.com/platform/external/perfetto/
-
-CMD cd perfetto/ && tools/continuous_fuzz
diff --git a/infra/oss-fuzz/build_fuzzers b/infra/oss-fuzz/build_fuzzers
new file mode 100755
index 0000000..71983746
--- /dev/null
+++ b/infra/oss-fuzz/build_fuzzers
@@ -0,0 +1,16 @@
+#!/bin/bash
+
+set -euo pipefail
+
+mkdir -p $WORK/build
+
+GN_ARGS="is_clang=true is_debug=false is_fuzzer=true use_libfuzzer=false \
+link_fuzzer=\"-lFuzzingEngine\" is_asan=true is_hermetic_clang=false \
+use_custom_libcxx=false extra_cflags=\"$CFLAGS\" extra_cxxflags=\"$CXXFLAGS\" \
+extra_ldflags=\"$CXXFLAGS\" is_system_compiler=true"
+
+OUTDIR=$WORK/build
+$SRC/perfetto/tools/gn gen "$OUTDIR" --args="${GN_ARGS}" --check
+
+$SRC/perfetto/tools/ninja -C $WORK/build fuzzers -v
+cp $WORK/build/*fuzzer $OUT
diff --git a/infra/oss-fuzz/init_container b/infra/oss-fuzz/init_container
new file mode 100755
index 0000000..3b382cd
--- /dev/null
+++ b/infra/oss-fuzz/init_container
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+# No op for now. Will allow us to install further system packages without
+# having to touch the oss-fuzz repo.
diff --git a/tools/build_all_configs.py b/tools/build_all_configs.py
index d1be88b..6642e71 100755
--- a/tools/build_all_configs.py
+++ b/tools/build_all_configs.py
@@ -45,7 +45,7 @@
   'linux_msan': ['is_clang=true', 'is_debug=false', 'is_msan=true'],
   'linux_tsan': ['is_clang=true', 'is_debug=false', 'is_tsan=true'],
   'linux_ubsan': ['is_clang=true', 'is_debug=false', 'is_ubsan=true'],
-  'linux_fuzzer': ['is_clang=true', 'is_debug=false', 'use_libfuzzer=true',
+  'linux_fuzzer': ['is_clang=true', 'is_debug=false', 'is_fuzzer=true',
                    'is_asan=true'],
 }
 
diff --git a/tools/install-build-deps b/tools/install-build-deps
index 15baaef..4045513 100755
--- a/tools/install-build-deps
+++ b/tools/install-build-deps
@@ -170,7 +170,7 @@
   # These dependencies are for libunwindstack, which is used by src/profiling.
   ('buildtools/android-core',
    'https://android.googlesource.com/platform/system/core.git',
-   'a5913bdd07ae8c26306490c39a15a103a41671b4',
+   '73d03dc8047edd1b3a39ebff6770826deec6c956',
    'all'
   ),
 
diff --git a/tools/run_all_fuzzers b/tools/run_all_fuzzers
index c8957a1..a3193dd 100755
--- a/tools/run_all_fuzzers
+++ b/tools/run_all_fuzzers
@@ -18,9 +18,8 @@
 
 OUTDIR=out/linux_fuzzer_run
 
-tools/gn gen "$OUTDIR"
-EDITOR=true tools/gn args "$OUTDIR" \
-  --args="is_clang=true is_debug=false use_libfuzzer=true is_asan=true" \
+tools/gn gen "$OUTDIR" \
+  --args="is_clang=true is_debug=false is_fuzzer=true is_asan=true" \
   --check
 
 tools/ninja -C $OUTDIR