aboutsummaryrefslogtreecommitdiff
path: root/interceptor.cc
blob: 1f9f93d1f844fe3d9404c80b3d2f736d2ffdbd99 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 * 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 <fcntl.h>
#include <spawn.h>
#include <unistd.h>

#include <algorithm>
#include <array>
#include <filesystem>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <memory>
#include <optional>
#include <regex>
#include <sstream>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>

#include <android-base/unique_fd.h>

#include <google/protobuf/util/delimited_message_util.h>

#include "interceptor_utils.h"

namespace fs = std::filesystem;

// UTILITY function declarations

// process applicable calls (i.e. programs that we might be able to handle)
static std::optional<interceptor::Command> process_command(const char* filename, char* const argv[],
                                                           char* const envp[],
                                                           bool* should_recurse);

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

// execute potentially modified command
using executor_t = std::function<int(const char* filename, char* const argv[], char* const envp[])>;
static int execute(const char* filename, char* const argv[], char* const envp[],
                   const executor_t& executor);

// 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[]) {
  return execute(filename, argv, envp,
                 [&](const char* file, char* const arguments[], char* const environment[]) {
                   return old_execve(file, arguments, environment);
                 });
}
}  // extern "C"

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

extern "C" {
int posix_spawn(pid_t* pid, const char* filename, const posix_spawn_file_actions_t* file_actions,
                const posix_spawnattr_t* attrp, char* const argv[], char* const envp[]) {
  return execute(filename, argv, envp,
                 [&](const char* file, char* const arguments[], char* const environment[]) {
                   return old_posix_spawn(pid, file, file_actions, attrp, arguments, environment);
                 });
}
}  // extern "C"

// LIBRARY IMPLEMENTATION

namespace interceptor {

static Command instantiate_command(const char* program, char* const argv[], char* const envp[]) {
  Command result;
  result.set_program(program);
  result.set_current_directory(fs::current_path());

  for (auto current_argument = argv; *current_argument; ++current_argument) {
    result.add_arguments(*current_argument);
  }

  for (auto current_env_var = envp; *current_env_var; ++current_env_var) {
    const std::string s(*current_env_var);
    const auto pos = s.find('=');
    if (pos == std::string::npos) {
      continue;
    }

    (*result.mutable_environment_variables())[s.substr(0, pos)] = s.substr(pos + 1);
  }

  return result;
}

static std::optional<std::string> command_getenv(const Command& command, const char* key) {
  const auto& env = command.environment_variables();
  if (const auto iter = env.find(key); iter != env.cend()) {
    return {iter->second};
  }
  return {};
}

static bool default_make_relative(Command* command) {
  // determine the ROOT_DIR
  fs::path root_directory;
  if (const auto root = command_getenv(*command, kEnvRootDirectory)) {
    root_directory = fs::path(*root) / "";
  } else {
    // there is no ROOT_DIR that we can use to make calls relative
    return false;
  }

  // determine the relative path to ROOT_DIR from the current working dir
  auto relative_root = fs::relative(root_directory) / "";
  if (relative_root == "./") {
    relative_root.clear();
  }

  // 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 (relative_root.native().find(root_directory) != std::string::npos) {
    return false;
  }

  command->set_current_directory(fs::relative(command->current_directory(), root_directory));

  // replacement functor
  const auto replace_all = [&](auto& str) {
    size_t start = 0;
    auto pos = std::string::npos;
    while ((pos = str.find(root_directory, start)) != std::string::npos) {
      str.replace(pos, root_directory.native().length(), relative_root);
      start = pos + relative_root.native().length();
    }
  };

  // now go and replace everything
  replace_all(*command->mutable_program());
  std::for_each(command->mutable_arguments()->begin(), command->mutable_arguments()->end(), replace_all);

  return true;
}

struct InputsOutputs {
  Inputs inputs;
  Outputs outputs;
};

class Analyzer {
 public:
  static std::unique_ptr<Analyzer> get(const interceptor::Command&);
  virtual ~Analyzer() = default;

  void set_inputs_outputs(Command* command) const;
  virtual bool make_relative(Command*) const { return false; }
  virtual bool should_recurse(Command*) const { return true; }

 protected:
  virtual InputsOutputs determine_inputs_outputs(const Command&) const { return {}; }
};

void Analyzer::set_inputs_outputs(Command* command) const {
  auto [inputs, outputs] = determine_inputs_outputs(*command);

  *command->mutable_inputs() = {inputs.begin(), inputs.end()};
  *command->mutable_outputs() = {outputs.begin(), outputs.end()};

  for (const auto& input : command->inputs()) {
    if (!fs::is_regular_file(input)) {
      std::cerr << "missing input: " << input << "\n" << *command << "\n";
      exit(1);
    }
  }
}

class CompileLinkerAnalyzer : public Analyzer {
  InputsOutputs determine_inputs_outputs(const Command& command) const final {
    static constexpr std::array kSkipNextArguments{
        "-isystem", "-I", "-L", "-m", "-soname", "-z",
    };
    static constexpr std::string_view kOutputOption = "-Wp,-MMD,";

    InputsOutputs result;
    bool next_is_out = false;
    bool skip_next = false;
    // skip arguments[0] as this is the program itself
    for (auto it = command.arguments().cbegin() + 1; it != command.arguments().cend(); ++it) {
      const auto& argument = *it;
      if (argument == "-o") {
        next_is_out = true;
        continue;
      }
      if (next_is_out) {
        result.outputs.push_back(argument);
        next_is_out = false;
        continue;
      }
      if (argument.rfind(kOutputOption, 0) == 0) {
        result.outputs.push_back(argument.substr(kOutputOption.size()));
      }
      if (skip_next) {
        skip_next = false;
        continue;
      }
      if (std::find(kSkipNextArguments.cbegin(), kSkipNextArguments.cend(), argument) !=
          kSkipNextArguments.cend()) {
        skip_next = true;
      }
      // ignore test compilations
      if (argument == "/dev/null" || argument == "-") {
        return {};
      }
      if (argument[0] == '-') {  // ignore flags
        continue;
      }
      result.inputs.push_back(argument);
    }

    return result;
  }

  bool make_relative(Command* command) const final { return default_make_relative(command); }

  // do not recurse the interceptor into the subprocesses of compilers/linkers;
  // otherwise we will trace (and get confused by) ld.lld/cc1-plus and friends.
  bool should_recurse(Command*) const final { return false; }
};

class ArchiverAnalyzer : public Analyzer {
  InputsOutputs determine_inputs_outputs(const Command& command) const final {
    InputsOutputs result;

    const auto& arguments = command.arguments();

    if (arguments.size() < 3) {
      return result;
    }
    // skip arguments[0] as this is the program itself
    // skip arguments[1] are the archiver flags
    // arguments[2] is the output
    result.outputs.push_back(arguments[2]);
    // arguments[3:] are the inputs
    result.inputs.insert(result.inputs.cend(), arguments.cbegin() + 3, arguments.cend());
    return result;
  }

  bool make_relative(Command* command) const final { return default_make_relative(command); }
  bool should_recurse(Command*) const final { return false; }
};

static const std::initializer_list<
    std::pair<std::regex, std::function<std::unique_ptr<Analyzer>()>>>
    analyzers{
        {
            std::regex("^(.*/)?(clang|clang\\+\\+|gcc|g\\+\\+|ld(\\.lld)?|llvm-strip)$"),
            []() { return std::make_unique<CompileLinkerAnalyzer>(); },
        },
        {
            std::regex("^(.*/)?(llvm-)?ar$"),
            []() { return std::make_unique<ArchiverAnalyzer>(); },
        },
    };

std::unique_ptr<Analyzer> Analyzer::get(const Command& command) {
  for (const auto& [regex, analyzer_factory] : analyzers) {
    if (std::regex_match(command.arguments()[0], regex)) {
      return analyzer_factory();
    }
  }
  return std::make_unique<Analyzer>();
}

}  // namespace interceptor

/// UTILITY FUNCTIONS

static std::optional<interceptor::Command> process_command(const char* filename, char* const argv[],
                                                           char* const envp[],
                                                           bool* should_recurse) {
  // 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.

  auto command = interceptor::instantiate_command(filename, argv, envp);

  const auto analyzer = interceptor::Analyzer::get(command);

  bool transformed = false;

  // 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.
  if (command_getenv(command, kEnvMakeRelative)) {
    transformed |= analyzer->make_relative(&command);
  }

  analyzer->set_inputs_outputs(&command);
  *should_recurse = analyzer->should_recurse(&command);

  log(command);

  if (transformed) {
    return command;
  }
  return {};
}

static void log(const interceptor::Command& command) {
  if (const auto log = command_getenv(command, kEnvCommandLog)) {
    interceptor::Message message;
    *message.mutable_command() = command;
    message.mutable_command()->clear_environment_variables();

    std::ostringstream os;
    google::protobuf::util::SerializeDelimitedToOstream(message, &os);

    const auto& str = os.str();
    android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(log->c_str(), O_WRONLY | O_APPEND)));
    if (!fd.ok()) {
      std::cerr << "Could not open " << std::quoted(*log) << ": " << strerror(errno);
      return;
    }

    if (TEMP_FAILURE_RETRY(write(fd, str.data(), str.size())) < str.size()) {
      std::cerr << "Could not write " << std::quoted(*log) << ": " << strerror(errno);
      return;
    }
  }
}

static std::vector<const char*> argv_vector(const interceptor::Command& command) {
  std::vector<const char*> result;
  result.reserve(command.arguments().size() + 1);
  result[command.arguments().size()] = nullptr;
  for (const auto& arg : command.arguments()) {
    result.push_back(arg.c_str());
  }
  return result;
}

static void disable_ld_preload(char* const envp[]) {
  for (auto current_env_var = envp; *current_env_var; ++current_env_var) {
    const std::string_view s(*current_env_var);
    if (s.rfind("LD_PRELOAD=", 0) == 0) {
      *current_env_var[0] = '\0';
    }
  }
}

int execute(const char* filename, char* const argv[], char* const envp[],
            const executor_t& executor) {
  // pass on to process_command(), if unhandled, fall back to the original executor
  bool should_recurse = true;
  auto command = process_command(filename, argv, envp, &should_recurse);

  if (!should_recurse) {
    disable_ld_preload(envp);
  }

  if (command.has_value()) {
    // pass down the transformed command to the executor
    return executor(command->program().c_str(), const_cast<char**>(argv_vector(*command).data()),
                    envp);
  }
  // else fall back to the original call
  return executor(filename, argv, envp);
}