Home | History | Annotate | Download | only in libs
      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__((aligned(4096))) void __cfi_check(uint64_t /*CallSiteTypeId*/,
     31                                                            void* /*TargetAddr*/, void* /*Diag*/) {
     32   ++g_count;
     33 }
     34 
     35 void preinit_ctor() {
     36   CHECK(g_count == 0);
     37   __cfi_slowpath(42, reinterpret_cast<void*>(&preinit_ctor));
     38   CHECK(g_count == 1);
     39 }
     40 
     41 __attribute__((section(".preinit_array"), used)) void (*preinit_ctor_p)(void) = preinit_ctor;
     42 
     43 __attribute__((constructor, used)) void ctor() {
     44   CHECK(g_count == 1);
     45   __cfi_slowpath(42, reinterpret_cast<void*>(&ctor));
     46   CHECK(g_count == 2);
     47 }
     48 
     49 int main(void) {
     50   CHECK(g_count == 2);
     51   __cfi_slowpath(42, reinterpret_cast<void*>(&main));
     52   CHECK(g_count == 3);
     53   return 0;
     54 }
     55