• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *   http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <cstdio>
17 #include <dlfcn.h>
18 #include <cstring>
19 #include "cfi_util.h"
20 
21 struct AA {
22     virtual void Test();
23 };
24 
Test()25 void AA::Test()
26 {
27     printf("AA::Test()\n");
28 }
29 
ChangeToAnotherObj()30 void ChangeToAnotherObj()
31 {
32     AA *a = new AA;
33     void *handle = dlopen("libcfi_cross_dso_test_lib.z.so", RTLD_NOW);
34     if (handle == nullptr) {
35         return;
36     }
37     void *(*create_B)() = (void *(*)())dlsym(handle, "CreateObj");
38     void *p = create_B();
39     a->Test();
40     memcpy(&a, &p, sizeof(void *));
41     a->Test();
42 }
43 
main()44 int main()
45 {
46     if (DEBUG) {
47         ShowCfiLogFile();
48     }
49     ClearCfiLog();
50     if (DEBUG) {
51         ShowCfiLogFile();
52     }
53     ChangeToAnotherObj();
54     FindAndCheck("runtime error: control flow integrity check for type 'AA' failed during virtual call");
55     if (DEBUG) {
56         ShowCfiLogFile();
57     }
58     return 0;
59 }