Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : /*!
12 : * \file stub_reg.cpp
13 : * \brief
14 : */
15 : #include "stub_reg.h"
16 : #include <string>
17 : #include <dlfcn.h>
18 : #include <sys/types.h>
19 : #include <sys/stat.h>
20 : #include <fcntl.h>
21 : #include <unistd.h>
22 : #include <csignal>
23 : #include "securec.h"
24 :
25 : const char* g_regStubs[INTRI_TYPE_MAX]{
26 : "AscendC",
27 : "cceprint",
28 : "npuchk",
29 : };
30 :
31 : namespace AscendC {
32 : const int SYM_LEN_MAX = 511;
33 :
34 4 : void StubReg(IntriTypeT type, const char* stub) { g_regStubs[type] = stub; }
35 :
36 0 : void StubInit(void)
37 : {
38 : char buf[SYM_LEN_MAX + 1];
39 : static bool gStubInited = false;
40 0 : if (gStubInited) {
41 0 : return;
42 : }
43 0 : gStubInited = true;
44 :
45 0 : int32_t logfd = open("stub_reg.log", O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
46 0 : for (int32_t s = 0; s < INTRI_TYPE_MAX; s++) {
47 0 : const char* stub = g_regStubs[s];
48 0 : if (stub == nullptr) {
49 0 : continue;
50 : }
51 0 : int32_t slen = strnlen(stub, SYM_LEN_MAX);
52 0 : for (int32_t i = 0; i < INTRI_FMT_NUM; i++) {
53 0 : IntriFmtT* fmt = IntriFmtGet(i);
54 0 : int ret = snprintf_s(buf, SYM_LEN_MAX, SYM_LEN_MAX, fmt->fmt, slen, stub);
55 0 : if (ret <= 0) {
56 0 : std::cout << "Get intri format error!" << std::endl;
57 0 : raise(SIGABRT);
58 : }
59 0 : PfIntriFun fun = (PfIntriFun)dlsym(RTLD_DEFAULT, buf);
60 0 : IntriFunAdd(i, static_cast<IntriTypeT>(s), fun);
61 0 : dprintf(logfd, "%s: [%s] -> %p\n", stub, buf, fun);
62 : #if defined(__NPU_ARCH__) && ((__NPU_ARCH__ == 3003) || (__NPU_ARCH__ == 3103) || (__NPU_ARCH__ == 3113))
63 : memset_s(buf, SYM_LEN_MAX + 1, '\0', SYM_LEN_MAX + 1);
64 : #endif
65 : }
66 : }
67 0 : close(logfd);
68 0 : logfd = -1;
69 : }
70 : } // namespace AscendC
|