xref: /aosp_15_r20/art/libarttools/include_testing/tools/testing.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2024 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_LIBARTTOOLS_INCLUDE_TESTING_TOOLS_TESTING_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_LIBARTTOOLS_INCLUDE_TESTING_TOOLS_TESTING_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <signal.h>
21*795d594fSAndroid Build Coastguard Worker #include <stdio.h>
22*795d594fSAndroid Build Coastguard Worker #include <stdlib.h>
23*795d594fSAndroid Build Coastguard Worker #include <sys/wait.h>
24*795d594fSAndroid Build Coastguard Worker #include <unistd.h>
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker #include <functional>
27*795d594fSAndroid Build Coastguard Worker #include <memory>
28*795d594fSAndroid Build Coastguard Worker #include <string>
29*795d594fSAndroid Build Coastguard Worker #include <utility>
30*795d594fSAndroid Build Coastguard Worker #include <vector>
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker #include "android-base/logging.h"
33*795d594fSAndroid Build Coastguard Worker #include "android-base/scopeguard.h"
34*795d594fSAndroid Build Coastguard Worker #include "android-base/strings.h"
35*795d594fSAndroid Build Coastguard Worker #include "base/file_utils.h"
36*795d594fSAndroid Build Coastguard Worker #include "base/globals.h"
37*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker namespace art {
40*795d594fSAndroid Build Coastguard Worker namespace tools {
41*795d594fSAndroid Build Coastguard Worker 
42*795d594fSAndroid Build Coastguard Worker using ::android::base::make_scope_guard;
43*795d594fSAndroid Build Coastguard Worker using ::android::base::ScopeGuard;
44*795d594fSAndroid Build Coastguard Worker using ::android::base::Split;
45*795d594fSAndroid Build Coastguard Worker 
GetArtBin(const std::string & name)46*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] static std::string GetArtBin(const std::string& name) {
47*795d594fSAndroid Build Coastguard Worker   CHECK(kIsTargetAndroid);
48*795d594fSAndroid Build Coastguard Worker   return ART_FORMAT("{}/bin/{}", GetArtRoot(), name);
49*795d594fSAndroid Build Coastguard Worker }
50*795d594fSAndroid Build Coastguard Worker 
GetBin(const std::string & name)51*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] static std::string GetBin(const std::string& name) {
52*795d594fSAndroid Build Coastguard Worker   for (const std::string& path : Split(getenv("PATH"), ":")) {
53*795d594fSAndroid Build Coastguard Worker     if (std::filesystem::exists(path + "/" + name)) {
54*795d594fSAndroid Build Coastguard Worker       return path + "/" + name;
55*795d594fSAndroid Build Coastguard Worker     }
56*795d594fSAndroid Build Coastguard Worker   }
57*795d594fSAndroid Build Coastguard Worker   LOG(FATAL) << ART_FORMAT("Binary '{}' not found", name);
58*795d594fSAndroid Build Coastguard Worker   UNREACHABLE();
59*795d594fSAndroid Build Coastguard Worker }
60*795d594fSAndroid Build Coastguard Worker 
61*795d594fSAndroid Build Coastguard Worker // Executes the command. If `wait` is true, waits for the process to finish and keeps it in a
62*795d594fSAndroid Build Coastguard Worker // waitable state; otherwise, returns immediately after fork. When the current scope exits, destroys
63*795d594fSAndroid Build Coastguard Worker // the process.
64*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] static std::pair<pid_t, std::unique_ptr<ScopeGuard<std::function<void()>>>>
ScopedExec(std::vector<std::string> & args,bool wait)65*795d594fSAndroid Build Coastguard Worker ScopedExec(std::vector<std::string>& args, bool wait) {
66*795d594fSAndroid Build Coastguard Worker   std::vector<char*> execv_args;
67*795d594fSAndroid Build Coastguard Worker   execv_args.reserve(args.size() + 1);
68*795d594fSAndroid Build Coastguard Worker   for (std::string& arg : args) {
69*795d594fSAndroid Build Coastguard Worker     execv_args.push_back(arg.data());
70*795d594fSAndroid Build Coastguard Worker   }
71*795d594fSAndroid Build Coastguard Worker   execv_args.push_back(nullptr);
72*795d594fSAndroid Build Coastguard Worker 
73*795d594fSAndroid Build Coastguard Worker   pid_t pid = fork();
74*795d594fSAndroid Build Coastguard Worker   if (pid == 0) {
75*795d594fSAndroid Build Coastguard Worker     execv(execv_args[0], execv_args.data());
76*795d594fSAndroid Build Coastguard Worker     PLOG(FATAL) << "Failed to call execv";
77*795d594fSAndroid Build Coastguard Worker     UNREACHABLE();
78*795d594fSAndroid Build Coastguard Worker   } else if (pid > 0) {
79*795d594fSAndroid Build Coastguard Worker     if (wait) {
80*795d594fSAndroid Build Coastguard Worker       siginfo_t info;
81*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(TEMP_FAILURE_RETRY(waitid(P_PID, pid, &info, WEXITED | WNOWAIT)), 0);
82*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(info.si_code, CLD_EXITED);
83*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(info.si_status, 0);
84*795d594fSAndroid Build Coastguard Worker     }
85*795d594fSAndroid Build Coastguard Worker     std::function<void()> cleanup([=] {
86*795d594fSAndroid Build Coastguard Worker       siginfo_t info;
87*795d594fSAndroid Build Coastguard Worker       if (!wait) {
88*795d594fSAndroid Build Coastguard Worker         CHECK_EQ(kill(pid, SIGKILL), 0);
89*795d594fSAndroid Build Coastguard Worker       }
90*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(TEMP_FAILURE_RETRY(waitid(P_PID, pid, &info, WEXITED)), 0);
91*795d594fSAndroid Build Coastguard Worker     });
92*795d594fSAndroid Build Coastguard Worker     return std::make_pair(
93*795d594fSAndroid Build Coastguard Worker         pid,
94*795d594fSAndroid Build Coastguard Worker         std::make_unique<ScopeGuard<std::function<void()>>>(make_scope_guard(std::move(cleanup))));
95*795d594fSAndroid Build Coastguard Worker   } else {
96*795d594fSAndroid Build Coastguard Worker     PLOG(FATAL) << "Failed to call fork";
97*795d594fSAndroid Build Coastguard Worker     UNREACHABLE();
98*795d594fSAndroid Build Coastguard Worker   }
99*795d594fSAndroid Build Coastguard Worker }
100*795d594fSAndroid Build Coastguard Worker 
101*795d594fSAndroid Build Coastguard Worker }  // namespace tools
102*795d594fSAndroid Build Coastguard Worker }  // namespace art
103*795d594fSAndroid Build Coastguard Worker 
104*795d594fSAndroid Build Coastguard Worker #endif  // ART_LIBARTTOOLS_INCLUDE_TESTING_TOOLS_TESTING_H_
105