summaryrefslogtreecommitdiff
path: root/interceptor/interceptor.cc
blob: 98b8e5e8088bd74ea353b5383c8397cdf5db0dba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "interceptor.h"

#include <dlfcn.h>
#include <unistd.h>

#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iterator>
#include <sstream>
#include <string>
#include <string_view>
#include <utility>

#include <android-base/strings.h>

namespace fs = std::filesystem;

// Options passed via environment variables from the interceptor starter
constexpr static auto ENV_command_log = "INTERCEPTOR_command_log";
constexpr static auto ENV_root_dir = "INTERCEPTOR_root_dir";

// UTILITY function declarations

// process applicable calls (i.e. programs that we might be able to handle)
static void process_command(const char* filename, char* const argv[], char* const envp[]);

// log command if logging is enabled
static void log(const interceptor::Command&, const std::string& prefix);

// execute potentially modified command
static void exec(const interceptor::Command&);

// OVERLOADS for LD_PRELOAD USE

// Intercept execve calls, for that capture the original execve call
static auto const old_execve = reinterpret_cast<decltype(execve)*>(dlsym(RTLD_NEXT, "execve"));

extern "C" {
int execve(const char* filename, char* const argv[], char* const envp[]) {
  // pass on to process_command(), if unhandled, fall back to the original
  // execve
  process_command(filename, argv, envp);
  return old_execve(filename, argv, envp);
}
}  // extern "C"

// LIBRARY IMPLEMENTATION

namespace interceptor {

Command::Command(const char* program, char* const argv[], char* const envp[])
    : program_(program), cwd_(fs::current_path()), argv_(argv), envp_(envp) {}

const ArgVec& Command::args() const {
  if (!args_.has_value()) {
    args_ = ArgVec();
    for (auto current_arg = argv_; *current_arg; ++current_arg) {
      args_->emplace_back(*current_arg);
    }
  }
  return *args_;
}

const EnvMap& Command::env() const {
  if (!env_.has_value()) {
    env_ = EnvMap();
    for (auto current_env = envp_; *current_env; ++current_env) {
      const std::string_view s(*current_env);
      const auto pos = s.find('=');
      if (pos == EnvMap::key_type::npos) {
        continue;
      }
      env_->emplace(s.substr(0, pos), s.substr(pos + 1));
    }
  }
  return *env_;
}

const std::string& Command::program() const {
  return program_;
}

// TODO: chain output iterators instead and find a common expression
static std::string escape(std::string in) {
  in = android::base::StringReplace(in, "\t", "\\t", true);
  in = android::base::StringReplace(in, "\n", "\\n", true);
  return in;
}

std::string Command::repr() const {
  std::ostringstream os;
  os << R"({"cmd": )";
  {
    std::ostringstream cmd;
    cmd << program();
    if (args().size() > 1) cmd << ' ';
    std::transform(args().cbegin() + 1, args().cend(), std::ostream_iterator<std::string>(cmd, " "),
                   escape);
    os << std::quoted(cmd.str());
  }

  os << R"(, "cwd": )" << std::quoted(cwd_);

  os << "}";
  return os.str();
}

void Command::make_relative() {
  // determine the ROOT_DIR
  std::string root_dir;
  if (auto it = env().find(ENV_root_dir); it != env().cend()) {
    root_dir = it->second;
    if (root_dir[root_dir.size() - 1] != '/') root_dir += '/';
  } else {
    return;
  }

  // determine the relative path to ROOT_DIR from the current working dir
  std::string rel_root = fs::relative(root_dir);
  if (rel_root[rel_root.size() - 1] != '/') rel_root += '/';
  if (rel_root == "./") rel_root = "";

  // TODO: This is generally bad as this means we can't make anything relative.
  // This happens if the out dir is outside of the root.
  if (rel_root.find(root_dir) != std::string::npos) {
    return;
  }

  cwd_ = fs::relative(cwd_, root_dir);

  // replacement functor
  const auto replace_all = [&](auto& str) {
    auto pos = std::string::npos;
    while ((pos = str.find(root_dir)) != std::string::npos) {
      str.replace(pos, root_dir.length(), rel_root);
    }
  };

  if (!args_.has_value()) args();

  // now go and replace everything
  replace_all(program_);
  std::for_each(args_->begin(), args_->end(), replace_all);
}

}  // namespace interceptor

/// UTILITY FUNCTIONS

static void process_command(const char* filename, char* const argv[], char* const envp[]) {
  // First, try to find out whether we at all can handle this command. If not,
  // simply return and fall back to the original handler.

  if (!fs::is_regular_file(filename)) {
    return;
  }

  // Ok, we can handle that one, let's transform it.

  interceptor::Command command(filename, argv, envp);

  // rewrite all command line arguments (including the program itself) to use
  // paths relative to ROOT_DIR. This is essential for reproducible builds and
  // furthermore necessary to produce cache hits in RBE.
  command.make_relative();

  log(command, "");

  // pass down the transformed command to execve
  exec(command);
}

static void log(const interceptor::Command& command, const std::string& prefix) {
  const auto& env = command.env();

  if (const auto env_it = env.find(ENV_command_log); env_it != env.cend()) {
    std::ofstream file;
    file.open(std::string(env_it->second), std::ofstream::out | std::ofstream::app);
    if (file.is_open()) {
      file << prefix << command.repr() << ",\n";
    }
  }
}

static void exec(const interceptor::Command& command) {
  std::vector<const char*> c_args;
  c_args.reserve(command.args().size() + 1);
  c_args[command.args().size()] = nullptr;
  for (const auto& arg : command.args()) {
    c_args.push_back(arg.data());
  }
  // TODO: at this point, we could free some memory that is held in Command.
  //       While the args vector is reused for args, we could free the EnvMap
  //       and the original args.
  old_execve(command.program().c_str(), const_cast<char**>(c_args.data()), command.envp());
}