blob: 6f551c5f8af1407bd9884c6d7a691fbe340f4bcb [file] [log] [blame]
Evgenii Stepanov68ecec12017-01-31 13:19:30 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070017#include <assert.h>
18#include <stdint.h>
19#include <stdlib.h>
20
21// This library is built for all targets, including host tests, so __cfi_slowpath may not be
22// present. But it is only used in the bionic loader tests.
23extern "C" __attribute__((weak)) void __cfi_slowpath(uint64_t, void*);
24
Evgenii Stepanov1dfd76a2017-09-18 17:51:48 -070025static size_t g_count;
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070026static uint64_t g_last_type_id;
27static void* g_last_address;
28static void* g_last_diag;
29
30extern "C" {
31
Evgenii Stepanov1dfd76a2017-09-18 17:51:48 -070032// Make sure the library crosses at least one kLibraryAlignment(=256KB) boundary.
33char bss[1024 * 1024];
34
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070035// Mock a CFI-enabled library without relying on the compiler.
36__attribute__((aligned(4096))) void __cfi_check(uint64_t CallSiteTypeId, void* TargetAddr,
37 void* Diag) {
38 ++g_count;
39 g_last_type_id = CallSiteTypeId;
40 g_last_address = TargetAddr;
41 g_last_diag = Diag;
42}
43
Evgenii Stepanov1dfd76a2017-09-18 17:51:48 -070044size_t get_count() {
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070045 return g_count;
46}
47
48uint64_t get_last_type_id() {
49 return g_last_type_id;
50}
51
52void* get_last_address() {
53 return g_last_address;
54}
55
56void* get_last_diag() {
57 return g_last_diag;
58}
59
60void* get_global_address() {
61 return &g_count;
62}
63}
64
65// Check that CFI is set up in module constructors and destructors.
66struct A {
67 void check_cfi_self() {
68 g_last_type_id = 0;
69 assert(&__cfi_slowpath);
Evgenii Stepanovc3b3e862020-06-05 16:50:10 -070070 // CFI check for an address inside this DSO. This goes to the current module's __cfi_check,
71 // which updates g_last_type_id.
72 __cfi_slowpath(13, static_cast<void*>(&g_last_type_id));
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070073 assert(g_last_type_id == 13);
74 // CFI check for a libc function. This never goes into this module's __cfi_check, and must pass.
75 __cfi_slowpath(14, reinterpret_cast<void*>(&exit));
76 assert(g_last_type_id == 13);
77 }
78 A() {
79 check_cfi_self();
80 }
81 ~A() {
82 check_cfi_self();
83 }
84} a;