1 /* 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 17 #include <assert.h> 18 #include <stdint.h> 19 #include <stdlib.h> 20 21 #include "libs_utils.h" 22 23 // This library is built for all targets, including host tests, so __cfi_slowpath may not be 24 // present. But it is only used in the bionic loader tests. 25 extern "C" __attribute__((weak)) void __cfi_slowpath(uint64_t, void*); 26 27 static int g_count; 28 29 // Mock a CFI-enabled library without relying on the compiler. 30 extern "C" __attribute__((no_sanitize("hwaddress"))) __attribute__((aligned(4096))) 31 void __cfi_check(uint64_t /*CallSiteTypeId*/, void* /*TargetAddr*/, void* /*Diag*/) { 32 ++g_count; 33 } 34 35 // This code runs before hwasan is initialized. 36 __attribute__((no_sanitize("hwaddress"))) 37 void preinit_ctor() { 38 CHECK(g_count == 0); 39 __cfi_slowpath(42, reinterpret_cast<void*>(&preinit_ctor)); 40 CHECK(g_count == 1); 41 } 42 43 __attribute__((section(".preinit_array"), used)) void (*preinit_ctor_p)(void) = preinit_ctor; 44 45 __attribute__((constructor, used)) void ctor() { 46 CHECK(g_count == 1); 47 __cfi_slowpath(42, reinterpret_cast<void*>(&ctor)); 48 CHECK(g_count == 2); 49 } 50 51 int main(void) { 52 CHECK(g_count == 2); 53 __cfi_slowpath(42, reinterpret_cast<void*>(&main)); 54 CHECK(g_count == 3); 55 return 0; 56 } 57