Add ubsan_standalone library name to the toolchain.

and export the library name to make. Refactor the code a bit to avoid repeating the library name
multiple times.

Bug: 22033465
Test: Ran external/clang/build.py for aosp-llvm

Change-Id: I25eb3858eb92e1dd493b09524d559802551b2547
diff --git a/cc/config/arm64_device.go b/cc/config/arm64_device.go
index 9d425c1..124b124 100644
--- a/cc/config/arm64_device.go
+++ b/cc/config/arm64_device.go
@@ -184,8 +184,8 @@
 	return t.toolchainClangCflags
 }
 
-func (toolchainArm64) AddressSanitizerRuntimeLibrary() string {
-	return "libclang_rt.asan-aarch64-android.so"
+func (toolchainArm64) SanitizerRuntimeLibraryArch() string {
+	return "aarch64"
 }
 
 func arm64ToolchainFactory(arch android.Arch) Toolchain {
diff --git a/cc/config/arm_device.go b/cc/config/arm_device.go
index f15d8da..85a891a 100644
--- a/cc/config/arm_device.go
+++ b/cc/config/arm_device.go
@@ -336,8 +336,8 @@
 	}
 }
 
-func (toolchainArm) AddressSanitizerRuntimeLibrary() string {
-	return "libclang_rt.asan-arm-android.so"
+func (toolchainArm) SanitizerRuntimeLibraryArch() string {
+	return "arm"
 }
 
 func armToolchainFactory(arch android.Arch) Toolchain {
diff --git a/cc/config/mips64_device.go b/cc/config/mips64_device.go
index f6132dc..e414c7e 100644
--- a/cc/config/mips64_device.go
+++ b/cc/config/mips64_device.go
@@ -177,8 +177,8 @@
 	return "${config.Mips64ClangLdflags}"
 }
 
-func (toolchainMips64) AddressSanitizerRuntimeLibrary() string {
-	return "libclang_rt.asan-mips64-android.so"
+func (toolchainMips64) SanitizerRuntimeLibraryArch() string {
+	return "mips64"
 }
 
 func mips64ToolchainFactory(arch android.Arch) Toolchain {
diff --git a/cc/config/mips_device.go b/cc/config/mips_device.go
index 3c77a48..e0ae3c7 100644
--- a/cc/config/mips_device.go
+++ b/cc/config/mips_device.go
@@ -226,8 +226,8 @@
 	return "${config.MipsClangLdflags}"
 }
 
-func (toolchainMips) AddressSanitizerRuntimeLibrary() string {
-	return "libclang_rt.asan-mips-android.so"
+func (toolchainMips) SanitizerRuntimeLibraryArch() string {
+	return "mips"
 }
 
 func mipsToolchainFactory(arch android.Arch) Toolchain {
diff --git a/cc/config/toolchain.go b/cc/config/toolchain.go
index c286a9a..5a4e032 100644
--- a/cc/config/toolchain.go
+++ b/cc/config/toolchain.go
@@ -71,7 +71,7 @@
 	ShlibSuffix() string
 	ExecutableSuffix() string
 
-	AddressSanitizerRuntimeLibrary() string
+	SanitizerRuntimeLibraryArch() string
 
 	AvailableLibraries() []string
 }
@@ -125,7 +125,7 @@
 	return ""
 }
 
-func (toolchainBase) AddressSanitizerRuntimeLibrary() string {
+func (toolchainBase) SanitizerRuntimeLibraryArch() string {
 	return ""
 }
 
@@ -188,3 +188,19 @@
 func inList(s string, list []string) bool {
 	return indexList(s, list) != -1
 }
+
+func AddressSanitizerRuntimeLibrary(t Toolchain) string {
+	arch := t.SanitizerRuntimeLibraryArch()
+	if arch == "" {
+		return ""
+	}
+	return "libclang_rt.asan-" + arch + "-android.so"
+}
+
+func UndefinedBehaviorSanitizerRuntimeLibrary(t Toolchain) string {
+	arch := t.SanitizerRuntimeLibraryArch()
+	if arch == "" {
+		return ""
+	}
+	return "libclang_rt.ubsan_standalone-" + arch + "-android.so"
+}
diff --git a/cc/config/x86_64_device.go b/cc/config/x86_64_device.go
index ec02bed..23c976a 100644
--- a/cc/config/x86_64_device.go
+++ b/cc/config/x86_64_device.go
@@ -232,6 +232,10 @@
 	return "${config.X86_64Ldflags}"
 }
 
+func (toolchainX86_64) SanitizerRuntimeLibraryArch() string {
+	return "x86_64"
+}
+
 func x86_64ToolchainFactory(arch android.Arch) Toolchain {
 	toolchainCflags := []string{
 		"${config.X86_64ToolchainCflags}",
diff --git a/cc/config/x86_device.go b/cc/config/x86_device.go
index 032b1f0..4667caa 100644
--- a/cc/config/x86_device.go
+++ b/cc/config/x86_device.go
@@ -251,8 +251,8 @@
 	return "${config.X86Ldflags}"
 }
 
-func (toolchainX86) AddressSanitizerRuntimeLibrary() string {
-	return "libclang_rt.asan-i686-android.so"
+func (toolchainX86) SanitizerRuntimeLibraryArch() string {
+	return "i686"
 }
 
 func x86ToolchainFactory(arch android.Arch) Toolchain {
diff --git a/cc/makevars.go b/cc/makevars.go
index 56698f2..23814c3 100644
--- a/cc/makevars.go
+++ b/cc/makevars.go
@@ -172,7 +172,8 @@
 		}, " "))
 
 		if target.Os.Class == android.Device {
-			ctx.Strict(secondPrefix+"ADDRESS_SANITIZER_RUNTIME_LIBRARY", strings.TrimSuffix(toolchain.AddressSanitizerRuntimeLibrary(), ".so"))
+			ctx.Strict(secondPrefix+"ADDRESS_SANITIZER_RUNTIME_LIBRARY", strings.TrimSuffix(config.AddressSanitizerRuntimeLibrary(toolchain), ".so"))
+			ctx.Strict(secondPrefix+"UBSAN_RUNTIME_LIBRARY", strings.TrimSuffix(config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain), ".so"))
 		}
 
 		// This is used by external/gentoo/...
diff --git a/cc/sanitize.go b/cc/sanitize.go
index b79e0c7..8023933 100644
--- a/cc/sanitize.go
+++ b/cc/sanitize.go
@@ -21,6 +21,7 @@
 	"github.com/google/blueprint"
 
 	"android/soong/android"
+	"android/soong/cc/config"
 )
 
 type sanitizerType int
@@ -239,7 +240,7 @@
 		flags.LdFlags = append(flags.LdFlags, "-Wl,-u,__asan_preinit")
 
 		// ASan runtime library must be the first in the link order.
-		runtimeLibrary := ctx.toolchain().AddressSanitizerRuntimeLibrary()
+		runtimeLibrary := config.AddressSanitizerRuntimeLibrary(ctx.toolchain())
 		if runtimeLibrary != "" {
 			flags.libFlags = append([]string{"${config.ClangAsanLibDir}/" + runtimeLibrary}, flags.libFlags...)
 		}