blob: 48b2019b52abb9bf6da7c183cceb8ddafb28b46e [file] [log] [blame]
# Copyright 2022 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//build/components/fuchsia_unittest_package.gni")
import("//build/cpp/cpp_fuzzer.gni")
import("//build/fuzz.gni")
import("//build/test.gni")
import("//build/testing/boot_tests/zbi_test.gni")
import("//build/testing/host_test_data.gni")
import("//build/toolchain/zircon/user_basic_redirect.gni")
is_elf = current_os != "mac" && current_os != "win"
test("elfldltl-unittests") {
sources = [
"abi-ptr-tests.cc",
"abi-span-tests.cc",
"abi-tests.cc",
"container-tests.cc",
"diagnostics-tests.cc",
"dwarf-tests.cc",
"dynamic-tests.cc",
"field-tests.cc",
"file-tests.cc",
"initfini-tests.cc",
"internal-tests.cc",
"layout-tests.cc",
"link-map-list-tests.cc",
"load-tests.cc",
"load-tests.h",
"loadinfo-mapped-memory-tests.cc",
"loadinfo-mutable-memory-tests.cc",
"mapped-fd-file-tests.cc",
"memory-tests.cc",
"note-tests.cc",
"phdr-tests.cc",
"relocation-tests.cc",
"resolve-tests.cc",
"soname-tests.cc",
"symbol-tests.cc",
"symbol-tests.h",
]
if (is_elf) {
sources += [ "self-tests.cc" ]
}
if (is_fuchsia) {
sources += [
"mapped-vmo-file-tests.cc",
"segment-with-vmo-tests.cc",
]
}
deps = [
"..",
"../testing",
"//sdk/lib/stdcompat",
"//src/lib/fxl/test:gtest_main",
"//src/lib/symbolizer-markup",
"//src/lib/trivial-allocator",
"//third_party/googletest:gmock",
]
if (is_elf) {
deps += [ ":loader-tests" ]
}
data_deps = [ ":resolve_test_dsos" ]
}
resolve_dsos = [
"data/first-32be.so",
"data/first-32le.so",
"data/first-64be.so",
"data/first-64le.so",
"data/second-32be.so",
"data/second-32le.so",
"data/second-64be.so",
"data/second-64le.so",
"data/gnu_unique-32be.so",
"data/gnu_unique-32le.so",
"data/gnu_unique-64be.so",
"data/gnu_unique-64le.so",
]
if (is_host) {
host_test_data("resolve_test_dsos") {
sources = resolve_dsos
outputs = [ "$root_out_dir/test_data/elfldltl/{{source_file_part}}" ]
}
} else {
resource("resolve_test_dsos") {
sources = resolve_dsos
outputs = [ "data/test_data/elfldltl/{{source_file_part}}" ]
}
}
if (current_os == "linux") {
config("linux-static-pie.config") {
# Avoid libc references from assert() in libc++ header code.
defines = [ "NDEBUG=1" ]
include_dirs = [ "//third_party/linux-syscall-support/src" ]
}
static_library("linux-static-pie") {
sources = [ "linux-static-pie.cc" ]
configs -= [ "//build/config/linux:implicit-host-libs" ]
configs += [ ":linux-static-pie.config" ]
deps = [ ".." ]
}
if (current_toolchain == host_toolchain) {
host_test("linux-static-pie-test") {
deps = [ ":linux-static-pie-test(//build/toolchain:linux_$host_cpu)" ]
binary_path = get_label_info(deps[0], "root_out_dir") + "/" +
get_label_info(deps[0], "name")
}
} else {
test("linux-static-pie-test") {
disable_syslog_backend = true
sources = [ "linux-static-pie-test.cc" ]
configs -= [
"//build/config/linux:default-pie",
"//build/config/linux:implicit-host-libs",
]
configs += [ ":linux-static-pie.config" ]
cflags = [ "-fPIE" ]
ldflags = [
"-nostartfiles",
"-nostdlib++",
"-static-pie",
"-nolibc",
]
deps = [
":linux-static-pie",
"//zircon/system/ulib/c/string:hermetic",
]
exclude_toolchain_tags = [ "instrumented" ]
}
}
}
if (is_fuchsia) {
static_library("static-pie") {
sources = [ "fuchsia-static-pie.cc" ]
deps = [
"..",
"//src/zircon/lib/zircon",
]
# Avoid libc references from assert() in libc++ header code.
defines = [ "NDEBUG=1" ]
}
# The test doesn't get normal C library setup before its code runs, so it
# needs to use the basic machine ABI.
user_basic_redirect("fuchsia-static-pie-test.basic") {
testonly = true
public_deps = [ ":fuchsia-static-pie-test" ]
}
if (toolchain_environment == "user.basic") {
# This is only actually used in the user.basic environment through the
# redirect above. Usually it's harmless enough for targets to be defined in
# additional toolchains where they aren't used. However, this target used
# to be built in $default_toolchain and Ninja quirks mean that later
# rebuilds in existing build directories that survive across the change
# where it should no longer be built in $default_toolchain will wind up
# trying to rebuild the $default_toolchain instantiation of this target
# (even though nothing will use that binary); but that doesn't link
# correctly any more since this target is now only intended to be used in
# the user.basic toolchain.
test("fuchsia-static-pie-test") {
visibility = [ ":*" ]
sources = [ "fuchsia-static-pie-test.cc" ]
# Avoid libc references from assert() in libc++ header code.
defines = [ "NDEBUG=1" ]
ldflags = [
# A standalone binary should indicate its stack requirements.
# They should be minimal for this test.
"-Wl,-z,stack-size=0x1000",
]
deps = [
":static-pie",
"//src/zircon/lib/zircon",
"//zircon/system/ulib/c/stdlib:hermetic",
"//zircon/system/ulib/c/string:hermetic",
]
exclude_toolchain_tags = [ "instrumented" ]
# TODO(https://fxbug.dev/42077192): __ehdr_start bug in ld.bfd aarch64-elf
if (current_cpu == "arm64") {
exclude_toolchain_tags += [ "gcc" ]
}
}
}
fuchsia_unittest_package("elfldltl-static-pie-test") {
deps = [
":fuchsia-static-pie-test.basic",
"//src/sys/testing/elftest",
]
}
}
source_set("test-data") {
visibility = [ ":*" ]
testonly = true
public = [ "test-data.h" ]
}
template("loader_test_lib") {
loadable_module(target_name) {
visibility = [ ":*" ]
testonly = true
forward_variables_from(invoker,
[
"cflags",
"defines",
"sources",
"ldflags",
])
deps = [
":test-data",
"//zircon/system/public",
]
# These tests are trying to test something specific, specific segment
# types, etc, instrumentation will likely perturbe this, so disable
# it entirely.
exclude_toolchain_tags = [ "instrumented" ]
if (is_linux) {
configs -= [
"//build/config/linux:default-pie",
"//build/config/linux:implicit-host-libs",
]
configs += [ "//build/config/zircon:nolibc" ]
}
if (is_fuchsia) {
# These shared objects aren't necessarily static pie's, this config
# disables sanitizers and other default libraries from being linked in.
configs += [ "//build/config/fuchsia:static-pie-config" ]
}
}
}
loader_test_lib("elfldltl-test-ret24") {
sources = [ "ret24.cc" ]
ldflags = [ "-Wl,-e,Return24" ]
}
loader_test_lib("elfldltl-test-relro") {
sources = [ "relro.cc" ]
ldflags = [ "-Wl,-e,relro_data" ]
}
loader_test_sizes = [
{
data = 0
bss = 1
},
{
data = 65536
bss = 1
},
{
data = 0
bss = 65536
},
]
foreach(sizes, loader_test_sizes) {
loader_test_lib("elfldltest-no-execute-data-${sizes.data}-bss-${sizes.bss}") {
sources = [ "no-x-segment.cc" ]
defines = [
"DATA_SIZE=${sizes.data}",
"BSS_SIZE=${sizes.bss}",
]
ldflags = [ "-Wl,-e,test_data" ]
}
}
loader_test_lib("elfldltl-test-symbolic") {
sources = [ "symbolic.cc" ]
# TODO(https://fxbug.dev/42069056): clang will still perform interprocedural
# optimization in a PIC, this flag disallows that and requires the PLT call
# always be made so we can properly test applying plt relocations.
if (!is_gcc) {
cflags = [ "-fsemantic-interposition" ]
}
}
loader_test_data_deps = [
"elfldltl-test-relro",
"elfldltl-test-ret24",
"elfldltl-test-symbolic",
]
foreach(sizes, loader_test_sizes) {
loader_test_data_deps +=
[ "elfldltest-no-execute-data-${sizes.data}-bss-${sizes.bss}" ]
}
if (is_host && is_elf) {
host_test_data("loader-test-libs") {
deps = []
sources = []
foreach(lib, loader_test_data_deps) {
deps += [ ":$lib" ]
sources += [ "$root_out_dir/$lib.so" ]
}
outputs = [ "$root_out_dir/test_data/elfldltl/{{source_file_part}}" ]
}
} else {
# On Fuchsia these are just included in /pkg/lib implicitly by dint
# of being in data_deps of the test code.
group("loader-test-libs") {
testonly = true
deps = []
foreach(lib, loader_test_data_deps) {
deps += [ ":$lib" ]
}
}
}
source_set("loader-tests") {
visibility = [ ":*" ]
testonly = true
sources = [ "loader-tests.cc" ]
deps = [
":test-data",
"..",
"../testing",
"//sdk/lib/stdcompat",
"//third_party/googletest:gmock",
"//zircon/system/ulib/fbl",
]
data_deps = [ ":loader-test-libs" ]
}
# Make that into a bootable ZBI with just the kernel.
zbi_test("zbi-static-pie-test") {
args = [
"userboot.test.next=test/fuchsia-static-pie-test",
"kernel.enable-serial-syscalls=true",
]
deps = [
":fuchsia-static-pie-test.basic",
"//zircon/kernel",
]
assert_no_deps = [
"//zircon/system/core/*",
"//zircon/system/dev/*",
]
}
fuchsia_unittest_package("elfldltl-tests") {
deps = [ ":elfldltl-unittests" ]
manifest = "meta/elfldltl-tests.cml"
}
cpp_fuzzer("dynamic-fuzzer") {
sources = [ "dynamic-fuzzer.cc" ]
deps = [
"..",
"../testing",
]
}
cpp_fuzzer("loadinfo-fuzzer") {
sources = [ "loadinfo-fuzzer.cc" ]
deps = [
"..",
"../testing",
]
}
cpp_fuzzer("note-fuzzer") {
sources = [ "note-fuzzer.cc" ]
deps = [
"..",
"../testing",
]
}
cpp_fuzzer("relocation-fuzzer") {
sources = [ "relocation-fuzzer.cc" ]
deps = [
"..",
"../testing",
]
}
cpp_fuzzer("symbol-fuzzer") {
sources = [ "symbol-fuzzer.cc" ]
deps = [
"..",
"../testing",
]
}
fuchsia_fuzzer_component("dynamic-fuzzer-component") {
manifest = "meta/dynamic-fuzzer.cml"
deps = [ ":dynamic-fuzzer" ]
}
fuchsia_fuzzer_component("loadinfo-fuzzer-component") {
manifest = "meta/loadinfo-fuzzer.cml"
deps = [ ":loadinfo-fuzzer" ]
}
fuchsia_fuzzer_component("note-fuzzer-component") {
manifest = "meta/note-fuzzer.cml"
deps = [ ":note-fuzzer" ]
}
fuchsia_fuzzer_component("relocation-fuzzer-component") {
manifest = "meta/relocation-fuzzer.cml"
deps = [ ":relocation-fuzzer" ]
}
fuchsia_fuzzer_component("symbol-fuzzer-component") {
manifest = "meta/symbol-fuzzer.cml"
deps = [ ":symbol-fuzzer" ]
}
if (is_fuchsia) {
fuchsia_fuzzer_package("elfldltl-fuzzers") {
cpp_fuzzer_components = [
":dynamic-fuzzer-component",
":loadinfo-fuzzer-component",
":note-fuzzer-component",
":relocation-fuzzer-component",
":symbol-fuzzer-component",
]
}
} else {
group("elfldltl-fuzzers") {
testonly = true
deps = [
":dynamic-fuzzer",
":loadinfo-fuzzer",
":note-fuzzer",
":relocation-fuzzer",
":symbol-fuzzer",
]
}
}
group("tests") {
testonly = true
deps = [
":elfldltl-fuzzers",
":elfldltl-static-pie-test",
":elfldltl-tests",
":elfldltl-unittests($host_toolchain)",
]
if (host_os == "linux") {
deps += [ ":linux-static-pie-test($host_toolchain)" ]
}
}
group("boot_tests") {
testonly = true
deps = [ ":zbi-static-pie-test" ]
}