1#!/bin/bash
2#
3# Copyright 2021 Google Inc. All rights reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17set -euo pipefail
18
19source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
20
21NM="$1"
22READELF="$2"
23
24# This should be abstracted to a unit-test library when it has more uses.
25function assert_contains_regex() {
26    local needle="$1"
27    local haystack="$2"
28    local message="${3:-Expected regexp "$needle" not found in\n"$haystack"}"
29    echo "${haystack}" | grep "${needle}" && return 0
30
31    echo -e "$message"
32    exit 1
33}
34
35# Test that a library is a static library.
36function test_is_static_library() {
37    local filepath="$(readlink -f $1)"; shift
38    local metadata="$($READELF -h ${filepath})"
39    assert_contains_regex "Type:.*REL (Relocatable file)" "${metadata}"
40}
41
42# Test that a library is a shared library.
43function test_is_shared_library() {
44    local filepath="$(readlink -f $1)"; shift
45    local metadata="$($READELF -h ${filepath})"
46    assert_contains_regex "Type:.*DYN (Shared object file)" "${metadata}"
47}
48
49# Test that the shared library contains a symbol
50function test_shared_library_symbols() {
51    local filepath="$(readlink -f $1)"; shift
52    local symbols=("$@"); shift
53    local nm_output="$($NM -D "${filepath}")"
54    for symbol in "${symbols[@]}"; do
55        assert_contains_regex "${symbol}" "${nm_output}"
56    done
57}
58
59# Test file contents of //bionic/linker:ld-android
60function test_ld-android() {
61    local shared_library="$(rlocation __main__/bionic/linker/ld-android/ld-android.so)"
62    local static_library="$(rlocation __main__/bionic/linker/libld-android_bp2build_cc_library_static.a)"
63
64    test_is_shared_library "${shared_library}"
65    test_is_static_library "${static_library}"
66
67    symbols=(
68        __loader_add_thread_local_dtor
69        __loader_android_create_namespace
70        __loader_android_dlopen_ext
71        __loader_android_dlwarning
72        __loader_android_get_application_target_sdk_version
73        __loader_android_get_exported_namespace
74        __loader_android_get_LD_LIBRARY_PATH
75        __loader_android_init_anonymous_namespace
76        __loader_android_link_namespaces
77        __loader_android_link_namespaces_all_libs
78        __loader_android_set_application_target_sdk_version
79        __loader_android_update_LD_LIBRARY_PATH
80        __loader_cfi_fail
81        __loader_dladdr
82        __loader_dlclose
83        __loader_dlerror
84        __loader_dl_iterate_phdr
85        __loader_dlopen
86        __loader_dlsym
87        __loader_dlvsym
88        __loader_remove_thread_local_dtor
89        __loader_shared_globals
90        _db_dlactivity
91    )
92
93    test_shared_library_symbols "${shared_library}" "${symbols[@]}"
94}
95
96function test_libdl_android() {
97    local shared_library="$(rlocation __main__/bionic/libdl/libdl_android/libdl_android.so)"
98    local static_library="$(rlocation __main__/bionic/libdl/liblibdl_android_bp2build_cc_library_static.a)"
99
100    test_is_shared_library "${shared_library}"
101    test_is_static_library "${static_library}"
102
103    symbols=(
104        android_create_namespace
105        android_dlwarning
106        android_get_exported_namespace
107        android_get_LD_LIBRARY_PATH
108        android_init_anonymous_namespace
109        android_link_namespaces
110        android_set_application_target_sdk_version
111        android_update_LD_LIBRARY_PATH
112    )
113
114    test_shared_library_symbols "${shared_library}" "${symbols[@]}"
115}
116
117function test_libc() {
118    local shared_library="$(rlocation __main__/bionic/libc/libc/libc.so)"
119    local static_library="$(rlocation __main__/bionic/libc/liblibc_bp2build_cc_library_static.a)"
120
121    test_is_shared_library "${shared_library}"
122    test_is_static_library "${static_library}"
123
124    symbols=(
125        __libc_get_static_tls_bounds
126        __libc_register_thread_exit_callback
127        __libc_iterate_dynamic_tls
128        __libc_register_dynamic_tls_listeners
129        android_reset_stack_guards
130        ffsl
131        ffsll
132        pidfd_getfd
133        pidfd_open
134        pidfd_send_signal
135        process_madvise
136        _Unwind_Backtrace  # apex llndk
137        _Unwind_DeleteException  # apex llndk
138        _Unwind_Find_FDE  # apex llndk
139        _Unwind_FindEnclosingFunction  # apex llndk
140        _Unwind_GetCFA  # apex llndk
141        _Unwind_GetDataRelBase  # apex llndk
142        _Unwind_GetGR  # apex llndk
143        _Unwind_GetIP  # apex llndk
144        _Unwind_GetIPInfo  # apex llndk
145        _Unwind_GetLanguageSpecificData  # apex llndk
146        _Unwind_GetRegionStart  # apex llndk
147        _Unwind_GetTextRelBase  # apex llndk
148        _Unwind_RaiseException  # apex llndk
149        _Unwind_Resume  # apex llndk
150        _Unwind_Resume_or_Rethrow  # apex llndk
151        _Unwind_SetGR  # apex llndk
152        _Unwind_SetIP  # apex llndk
153    )
154
155    test_shared_library_symbols "${shared_library}" "${symbols[@]}"
156}
157
158test_ld-android
159test_libdl_android
160test_libc
161