Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Manouchehri/516fe3988254d4eaf8e04779ab59ef56 to your computer and use it in GitHub Desktop.
Save Manouchehri/516fe3988254d4eaf8e04779ab59ef56 to your computer and use it in GitHub Desktop.
AFL for Android build soong
From a4a400549761cba03af4a6a14caeb3785255a1e3 Mon Sep 17 00:00:00 2001
From: Joey Jiao <joeyjiaojg@163.com>
Date: Sun, 27 Oct 2019 13:39:43 +0800
Subject: [PATCH] AFL: Enable AFL by global paths
Change-Id: Iede00c60802f7b4856cde6d3b3c27201a68f3e5f
---
android/config.go | 21 +++++++++++++++++++++
android/variable.go | 4 ++++
cc/builder.go | 11 +++++++++++
cc/sanitize.go | 5 +++++
4 files changed, 41 insertions(+)
mode change 100644 => 100755 cc/builder.go
diff --git a/android/config.go b/android/config.go
index d1db87b..f12ef42 100644
--- a/android/config.go
+++ b/android/config.go
@@ -993,6 +993,27 @@ func (c *config) CFIEnabledForPath(path string) bool {
return PrefixInList(path, c.productVariables.CFIIncludePaths)
}
+func (c *config) AFLEnabledForPath(path string) bool {
+ if c.productVariables.AFLIncludePaths == nil {
+ return false
+ }
+ return PrefixInList(path, c.productVariables.AFLIncludePaths)
+}
+
+func (c *config) AFLEnabledForModule(module string) bool {
+ if c.productVariables.AFLIncludeModules == nil {
+ return false
+ }
+ return InList(module, c.productVariables.AFLIncludeModules)
+}
+
+func (c *config) AFLDisabledForPath(path string) bool {
+ if c.productVariables.AFLExcludePaths == nil {
+ return false
+ }
+ return PrefixInList(path, c.productVariables.AFLExcludePaths)
+}
+
func (c *config) XOMDisabledForPath(path string) bool {
if c.productVariables.XOMExcludePaths == nil {
return false
diff --git a/android/variable.go b/android/variable.go
index bc403a6..0f8e47d 100644
--- a/android/variable.go
+++ b/android/variable.go
@@ -247,6 +247,10 @@ type productVariables struct {
CFIExcludePaths []string `json:",omitempty"`
CFIIncludePaths []string `json:",omitempty"`
+ AFLIncludePaths []string `json:",omitempty"`
+ AFLIncludeModules []string `json:",omitempty"`
+ AFLExcludePaths []string `json:",omitempty"`
+
DisableScudo *bool `json:",omitempty"`
EnableXOM *bool `json:",omitempty"`
diff --git a/cc/builder.go b/cc/builder.go
old mode 100644
new mode 100755
index ebc5fcd..acef567
--- a/cc/builder.go
+++ b/cc/builder.go
@@ -20,6 +20,7 @@ package cc
import (
"fmt"
+ "os"
"path/filepath"
"runtime"
"strconv"
@@ -438,6 +439,16 @@ func TransformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles and
ccCmd = "${config.ClangBin}/" + ccCmd
}
+ if (!ctx.Host() && !ctx.Config().AFLDisabledForPath(ctx.ModuleDir()) && ctx.Config().AFLEnabledForPath(ctx.ModuleDir()) && !strings.Contains(ctx.ModuleName(), "ndk")) || (ctx.Config().AFLEnabledForModule(ctx.ModuleName())) {
+ os.Setenv("HOST_OUT", "out/host/linux-x86")
+ if strings.Contains(ccCmd, "clang++") {
+ ccCmd = "afl-clang-fast++"
+ } else {
+ ccCmd = "afl-clang-fast"
+ }
+ ccCmd = os.Getenv("HOST_OUT") + "/bin/" + ccCmd
+ }
+
var implicitOutputs android.WritablePaths
if coverage {
gcnoFile := android.ObjPathWithExt(ctx, subdir, srcFile, "gcno")
diff --git a/cc/sanitize.go b/cc/sanitize.go
index b7a36a6..2109c71 100644
--- a/cc/sanitize.go
+++ b/cc/sanitize.go
@@ -34,6 +34,7 @@ var (
asanCflags = []string{"-fno-omit-frame-pointer"}
asanLdflags = []string{"-Wl,-u,__asan_preinit"}
asanLibs = []string{"libasan"}
+ aflLibs = []string{"afl-llvm-rt"}
// TODO(pcc): Stop passing -hwasan-allow-ifunc here once it has been made
// the default.
@@ -386,6 +387,10 @@ func (sanitize *sanitize) begin(ctx BaseModuleContext) {
}
func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
+ if (!ctx.Host() && !ctx.Config().AFLDisabledForPath(ctx.ModuleDir()) && ctx.Config().AFLEnabledForPath(ctx.ModuleDir()) && !strings.Contains(ctx.ModuleName(), "ndk")) || (ctx.Config().AFLEnabledForModule(ctx.ModuleName())) {
+ deps.StaticLibs = append(deps.StaticLibs, aflLibs...)
+ }
+
if !sanitize.Properties.SanitizerEnabled { // || c.static() {
return deps
}
--
1.8.2.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment