1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2017 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker *
4*8d67ca89SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8d67ca89SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8d67ca89SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8d67ca89SAndroid Build Coastguard Worker *
8*8d67ca89SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8d67ca89SAndroid Build Coastguard Worker *
10*8d67ca89SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8d67ca89SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8d67ca89SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8d67ca89SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8d67ca89SAndroid Build Coastguard Worker * limitations under the License.
15*8d67ca89SAndroid Build Coastguard Worker */
16*8d67ca89SAndroid Build Coastguard Worker
17*8d67ca89SAndroid Build Coastguard Worker #include <stdint.h>
18*8d67ca89SAndroid Build Coastguard Worker #include <stdlib.h>
19*8d67ca89SAndroid Build Coastguard Worker
20*8d67ca89SAndroid Build Coastguard Worker #include "CHECK.h"
21*8d67ca89SAndroid Build Coastguard Worker
22*8d67ca89SAndroid Build Coastguard Worker // This library is built for all targets, including host tests, so __cfi_slowpath may not be
23*8d67ca89SAndroid Build Coastguard Worker // present. But it is only used in the bionic loader tests.
24*8d67ca89SAndroid Build Coastguard Worker extern "C" __attribute__((weak)) void __cfi_slowpath(uint64_t, void*);
25*8d67ca89SAndroid Build Coastguard Worker
26*8d67ca89SAndroid Build Coastguard Worker static int g_count;
27*8d67ca89SAndroid Build Coastguard Worker
28*8d67ca89SAndroid Build Coastguard Worker // Mock a CFI-enabled library without relying on the compiler.
29*8d67ca89SAndroid Build Coastguard Worker extern "C" __attribute__((no_sanitize("hwaddress"))) __attribute__((aligned(4096)))
__cfi_check(uint64_t,void *,void *)30*8d67ca89SAndroid Build Coastguard Worker void __cfi_check(uint64_t /*CallSiteTypeId*/, void* /*TargetAddr*/, void* /*Diag*/) {
31*8d67ca89SAndroid Build Coastguard Worker ++g_count;
32*8d67ca89SAndroid Build Coastguard Worker }
33*8d67ca89SAndroid Build Coastguard Worker
34*8d67ca89SAndroid Build Coastguard Worker // This code runs before hwasan is initialized.
35*8d67ca89SAndroid Build Coastguard Worker __attribute__((no_sanitize("hwaddress")))
preinit_ctor()36*8d67ca89SAndroid Build Coastguard Worker void preinit_ctor() {
37*8d67ca89SAndroid Build Coastguard Worker CHECK(g_count == 0);
38*8d67ca89SAndroid Build Coastguard Worker __cfi_slowpath(42, reinterpret_cast<void*>(&preinit_ctor));
39*8d67ca89SAndroid Build Coastguard Worker CHECK(g_count == 1);
40*8d67ca89SAndroid Build Coastguard Worker }
41*8d67ca89SAndroid Build Coastguard Worker
42*8d67ca89SAndroid Build Coastguard Worker __attribute__((section(".preinit_array"), used)) void (*preinit_ctor_p)(void) = preinit_ctor;
43*8d67ca89SAndroid Build Coastguard Worker
ctor()44*8d67ca89SAndroid Build Coastguard Worker __attribute__((constructor, used)) void ctor() {
45*8d67ca89SAndroid Build Coastguard Worker CHECK(g_count == 1);
46*8d67ca89SAndroid Build Coastguard Worker __cfi_slowpath(42, reinterpret_cast<void*>(&ctor));
47*8d67ca89SAndroid Build Coastguard Worker CHECK(g_count == 2);
48*8d67ca89SAndroid Build Coastguard Worker }
49*8d67ca89SAndroid Build Coastguard Worker
main(void)50*8d67ca89SAndroid Build Coastguard Worker int main(void) {
51*8d67ca89SAndroid Build Coastguard Worker CHECK(g_count == 2);
52*8d67ca89SAndroid Build Coastguard Worker __cfi_slowpath(42, reinterpret_cast<void*>(&main));
53*8d67ca89SAndroid Build Coastguard Worker CHECK(g_count == 3);
54*8d67ca89SAndroid Build Coastguard Worker return 0;
55*8d67ca89SAndroid Build Coastguard Worker }
56