1 /**
2  * Copyright (c) 2021-2024 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 <array>
17 #include <string>
18 
19 #include "securec.h"
20 
21 namespace ark::terminate {
22 #ifndef FUZZING_EXIT_ON_FAILED_ASSERT_FOR
23 auto constexpr FUZZING_EXIT_ON_FAILED_ASSERT_FOR = "";
24 #endif
25 
Terminate(const char *file)26 [[noreturn]] void Terminate(const char *file)
27 {
28     auto filepath = std::string(file);
29     std::string libsTmp;
30 
31     char *replace = std::getenv("FUZZING_EXIT_ON_FAILED_ASSERT");
32     if ((replace != nullptr) && (std::string(replace) == "false")) {
33         std::abort();
34     }
35 
36     char *libs = std::getenv("FUZZING_EXIT_ON_FAILED_ASSERT_FOR");
37     if (libs == nullptr) {
38         libsTmp = FUZZING_EXIT_ON_FAILED_ASSERT_FOR;
39         if (libsTmp.empty()) {
40             std::abort();
41         }
42         libs = libsTmp.data();
43     }
44 
45     char *context;
46     char *lib = strtok_s(libs, ",", &context);
47     while (lib != nullptr) {
48         if (filepath.find(lib) != std::string::npos) {
49             std::exit(1);
50         }
51         lib = strtok_s(nullptr, ",", &context);
52     }
53     std::abort();
54 }
55 
56 }  // namespace ark::terminate
57