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 <dlfcn.h> 17#include <stdlib.h> 18#include <stdio.h> 19#include <string.h> 20#include "gwp_asan.h" 21#include "test.h" 22 23#define TEST_NAME "gwp_asan_unwind_test" 24 25__attribute__((noinline)) int func1(size_t *frame_buf, size_t max_record_stack, size_t *nums_frame) 26{ 27 int res = 1; 28 res += res; 29 *nums_frame = libc_gwp_asan_unwind_fast(frame_buf, max_record_stack, NULL); 30 return res; 31} 32 33__attribute__((noinline)) int func2(size_t *frame_buf, size_t max_record_stack, size_t *nums_frame) 34{ 35 int res = 2; 36 res += func1(frame_buf, max_record_stack, nums_frame); 37 return res; 38} 39 40__attribute__((noinline)) int func3(size_t *frame_buf, size_t max_record_stack, size_t *nums_frame) 41{ 42 int res = 3; 43 res += func2(frame_buf, max_record_stack, nums_frame); 44 return res; 45} 46 47int main() 48{ 49 size_t frame_buf[256]; 50 size_t max_record_stack = 10; 51 size_t nums_frames = 0; 52 size_t expect_ret = 7; 53 size_t min_stack_num = 3; 54 int res = func3((size_t *)frame_buf, max_record_stack, &nums_frames); 55 if (res != expect_ret || nums_frames < min_stack_num) { 56 t_error("FAIL:gwp_asan unwind test return is incorrect nums_frames:%d res:%d!\n", 57 nums_frames, res); 58 } 59 for (size_t i = 0; i < max_record_stack; i++) { 60 if (frame_buf[i]) { 61 Dl_info info; 62 if (dladdr((void *)frame_buf[i], &info)) { 63 size_t offset = frame_buf[i] - (uintptr_t)info.dli_fbase; 64 if (i < min_stack_num && !strstr(info.dli_fname, TEST_NAME)) { 65 t_error("FAIL:gwp_asan unwind file name is incorrect expect %s but get %s!\n", 66 TEST_NAME, info.dli_fname); 67 } 68 } else { 69 t_error("FAIL:gwp_asan unwind failed, dladdr %p return 0!\n", (void*)frame_buf[i]); 70 } 71 } 72 } 73 74 return t_status; 75} 76