• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tudasc / TypeART / 23982824272

04 Apr 2026 04:23PM UTC coverage: 90.261%. First build
23982824272

Pull #185

github

web-flow
Merge 7ccc0df93 into b87694974
Pull Request #185: Support LLVM 22

101 of 108 new or added lines in 5 files covered. (93.52%)

4884 of 5411 relevant lines covered (90.26%)

39184.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.71
/lib/passes/TypeARTPass.cpp
1
// TypeART library
2
//
3
// Copyright (c) 2017-2025 TypeART Authors
4
// Distributed under the BSD 3-Clause license.
5
// (See accompanying file LICENSE.txt or copy at
6
// https://opensource.org/licenses/BSD-3-Clause)
7
//
8
// Project home: https://github.com/tudasc/TypeART
9
//
10
// SPDX-License-Identifier: BSD-3-Clause
11
//
12

13
#include "Commandline.h"
14
#include "TypeARTConfiguration.h"
15
#include "analysis/MemInstFinder.h"
16
#include "configuration/Configuration.h"
17
#include "configuration/EnvironmentConfiguration.h"
18
#include "configuration/FileConfiguration.h"
19
#include "configuration/PassBuilderUtil.h"
20
#include "configuration/PassConfiguration.h"
21
#include "configuration/TypeARTOptions.h"
22
#include "instrumentation/CallBackFunctionInserter.h"
23
#include "instrumentation/MemOpArgCollector.h"
24
#include "instrumentation/MemOpInstrumentation.h"
25
#include "instrumentation/TypeARTFunctions.h"
26
#include "instrumentation/TypeIDProvider.h"
27
#include "support/ConfigurationBase.h"
28
#include "support/Logger.h"
29
#include "support/ModuleDumper.h"
30
#include "support/Table.h"
31
#include "support/Util.h"
32
#include "typegen/TypeGenerator.h"
33

34
#include "llvm/ADT/DenseMap.h"
1,664✔
35
#include "llvm/ADT/STLExtras.h"
1,664✔
36
#include "llvm/ADT/ScopeExit.h"
1,664✔
37
#include "llvm/ADT/SmallVector.h"
38
#include "llvm/ADT/Statistic.h"
39
#include "llvm/ADT/StringRef.h"
40
#include "llvm/IR/DataLayout.h"
41
#include "llvm/IR/Function.h"
42
#include "llvm/IR/LegacyPassManager.h"
43
#include "llvm/IR/Module.h"
44
#include "llvm/IR/PassManager.h"
45
#include "llvm/Pass.h"
46
#include "llvm/Passes/PassBuilder.h"
47
#if LLVM_VERSION_MAJOR < 22
1,664✔
48
#include "llvm/Passes/PassPlugin.h"
1,688✔
49
#else
50
#include "llvm/Plugins/PassPlugin.h"
51
#endif
52
#include "llvm/Support/CommandLine.h"
53
#include "llvm/Support/raw_ostream.h"
54

55
#include <cassert>
56
#include <cstddef>
57
#include <llvm/ADT/DenseSet.h>
58
#include <llvm/Config/llvm-config.h>
59
#include <llvm/IR/Constant.h>
60
#include <llvm/Support/Error.h>
61
#include <memory>
4,992✔
62
#include <optional>
1,664✔
63
#include <sstream>
64
#include <string>
65
#include <utility>
66

67
namespace llvm {
68
class BasicBlock;
69
}  // namespace llvm
70

71
using namespace llvm;
72

73
extern llvm::cl::OptionCategory typeart_category;
74

75
static cl::opt<std::string> cl_typeart_configuration_file(
5,139✔
76
    "typeart-config", cl::init(""),
5,139✔
77
    cl::desc(
5,139✔
78
        "Location of the configuration file to configure the TypeART pass. Commandline arguments are prioritized."),
5,139✔
79
    cl::cat(typeart_category));
10,278✔
80

81
#define DEBUG_TYPE "typeart"
82

83
ALWAYS_ENABLED_STATISTIC(NumInstrumentedMallocs, "Number of instrumented mallocs");
84
ALWAYS_ENABLED_STATISTIC(NumInstrumentedFrees, "Number of instrumented frees");
85
ALWAYS_ENABLED_STATISTIC(NumInstrumentedAlloca, "Number of instrumented (stack) allocas");
86
ALWAYS_ENABLED_STATISTIC(NumInstrumentedGlobal, "Number of instrumented globals");
87

88
namespace typeart::pass {
89

90
std::optional<std::string> get_configuration_file_path() {
5,742✔
91
  if (!cl_typeart_configuration_file.empty()) {
5,742✔
92
    LOG_DEBUG("Using cl::opt for config file " << cl_typeart_configuration_file.getValue());
93
    return cl_typeart_configuration_file.getValue();
×
94
  }
95
  const char* config_file = std::getenv("TYPEART_CONFIG_FILE");
5,742✔
96
  if (config_file != nullptr) {
5,742✔
97
    LOG_DEBUG("Using env var for types file " << config_file)
98
    return std::string{config_file};
30✔
99
  }
100
  LOG_INFO("No configuration file set.")
5,712✔
101
  return {};
5,712✔
102
}
5,742✔
103

104
class TypeArtPass : public llvm::PassInfoMixin<TypeArtPass> {
105
  std::optional<config::TypeARTConfigOptions> pass_opts{std::nullopt};
×
106
  std::unique_ptr<config::Configuration> pass_config;
107

108
  std::unique_ptr<analysis::MemInstFinder> meminst_finder;
109
  std::unique_ptr<TypeGenerator> typeManager;
110
  InstrumentationHelper instrumentation_helper;
111
  std::unique_ptr<TAFunctionQuery> functions;
112
  std::unique_ptr<InstrumentationContext> instrumentation_context;
113

114
  const config::Configuration& configuration() const {
160,369✔
115
    return *pass_config;
160,369✔
116
  }
117

118
 public:
119
  TypeArtPass() = default;
×
120
  explicit TypeArtPass(config::TypeARTConfigOptions opts) : pass_opts(opts) {
10,278✔
121
    // LOG_INFO("Created with \n" << opts)
122
  }
10,278✔
123

124
  bool doInitialization(Module& m) {
5,736✔
125
    auto config_file_path = get_configuration_file_path();
5,736✔
126

127
    const auto init = config_file_path.has_value()
5,736✔
128
                          ? config::TypeARTConfigInit{config_file_path.value()}
30✔
129
                          : config::TypeARTConfigInit{{}, config::TypeARTConfigInit::FileConfigurationMode::Empty};
3,282✔
130

131
    auto typeart_config = [&](const auto& init_value) {
9,048✔
132
      if (init_value.mode == config::TypeARTConfigInit::FileConfigurationMode::Empty) {
5,736✔
133
        return config::make_typeart_configuration_from_opts(pass_opts.value_or(config::TypeARTConfigOptions{}));
22,824✔
134
      }
135
      return config::make_typeart_configuration(init_value);
30✔
136
    }(init);
5,736✔
137

138
    if (typeart_config) {
5,736✔
139
      LOG_INFO("Emitting TypeART configuration content\n" << typeart_config.get()->getOptions())
5,730✔
140
      pass_config = std::move(*typeart_config);
5,730✔
141
    } else {
5,730✔
142
      LOG_FATAL("Could not load TypeART configuration.")
6✔
143
      std::exit(EXIT_FAILURE);
6✔
144
    }
145

146
    meminst_finder = analysis::create_finder(configuration());
3,306✔
147

148
    const std::string types_file =
149
        configuration().getValueOr(config::ConfigStdArgs::types, {config::ConfigStdArgValues::types});
3,306✔
150

151
    const TypegenImplementation typesgen_parser =
3,306✔
152
        configuration().getValueOr(config::ConfigStdArgs::typegen, {config::ConfigStdArgValues::typegen});
3,306✔
153
    typeManager = make_typegen(types_file, typesgen_parser);
3,306✔
154

155
    LOG_DEBUG("Propagating type infos.");
156
    const auto [loaded, error] = typeManager->load();
3,306✔
157
    if (loaded) {
5,730✔
158
      LOG_DEBUG("Existing type configuration successfully loaded from " << types_file);
159
    } else {
1,305✔
160
      LOG_DEBUG("No valid existing type configuration found: " << types_file << ". Reason: " << error.message());
161
    }
162

163
    instrumentation_helper.setModule(m);
3,306✔
164
    ModuleData mdata{&m};
3,306✔
165
    const auto has_cu_types = typeManager->registerModule(mdata);
3,306✔
166

167
    declareInstrumentationFunctions(m);
3,306✔
168
    {
169
      auto type_id_handler = get_type_id_handler(m, &typeManager->getTypeDatabase(), configuration(), functions.get());
5,730✔
170
      // const bool heap   = configuration()[config::ConfigStdArgs::heap];
171
      if (has_cu_types) {
5,730✔
172
        LOG_DEBUG("Registering compilation unit types list")
173
        type_id_handler->registerModule(mdata);
1,952✔
174
      }
1,952✔
175

176
      auto arg_collector =
177
          std::make_unique<MemOpArgCollector>(configuration(), typeManager.get(), instrumentation_helper);
5,730✔
178
      // const bool instrument_stack_lifetime = configuration()[config::ConfigStdArgs::stack_lifetime];
179
      auto cb_provider    = make_callback_inserter(configuration(), std::move(type_id_handler), functions.get());
5,730✔
180
      auto mem_instrument = std::make_unique<MemOpInstrumentation>(configuration(), functions.get(),
11,460✔
181
                                                                   instrumentation_helper, std::move(cb_provider));
5,730✔
182

183
      instrumentation_context =
5,730✔
184
          std::make_unique<InstrumentationContext>(std::move(arg_collector), std::move(mem_instrument));
5,730✔
185
    }
3,306✔
186
    return true;
187
  }
3,306✔
188

189
  bool doFinalization() {
5,730✔
190
    /*
191
     * Persist the accumulated type definition information for this module.
192
     */
193
    // TODO: inline/hybrid types not supported in non-opaque mode
194
    const bool emit_type_file_always     = bool(LLVM_VERSION_MAJOR < 15);
5,730✔
195
    TypeSerializationImplementation mode = configuration()[config::ConfigStdArgs::type_serialization];
5,730✔
196
    if (emit_type_file_always || mode == TypeSerializationImplementation::FILE) {
4,042✔
197
      const std::string types_file = configuration()[config::ConfigStdArgs::types];
2,258✔
198
      LOG_DEBUG("Writing type file to " << types_file);
199

200
      const auto [stored, error] = typeManager->store();
2,258✔
201
      if (stored) {
2,258✔
202
        LOG_DEBUG("Success!");
203
      } else {
2,258✔
204
        LOG_FATAL("Failed writing type config to " << types_file << ". Reason: " << error.message());
×
205
      }
206
    }
2,258✔
207
    const bool print_stats = configuration()[config::ConfigStdArgs::stats];
5,730✔
208
    if (print_stats) {
5,730✔
209
      auto& out = llvm::errs();
5,667✔
210
      printStats(out);
5,667✔
211
    }
5,667✔
212
    return false;
5,730✔
213
  }
214

215
  void declareInstrumentationFunctions(Module& m) {
5,730✔
216
    functions = declare_instrumentation_functions(m, configuration());
5,730✔
217
  }
5,730✔
218

219
  void printStats(llvm::raw_ostream& out) {
5,667✔
220
    const auto scope_exit_cleanup_counter = llvm::make_scope_exit([&]() {
11,334✔
221
      NumInstrumentedAlloca  = 0;
5,667✔
222
      NumInstrumentedFrees   = 0;
5,667✔
223
      NumInstrumentedGlobal  = 0;
5,667✔
224
      NumInstrumentedMallocs = 0;
5,667✔
225
    });
5,667✔
226
    meminst_finder->printStats(out);
5,667✔
227

228
    const auto get_ta_mode = [&]() {
11,334✔
229
      const bool heap   = configuration()[config::ConfigStdArgs::heap];
5,667✔
230
      const bool stack  = configuration()[config::ConfigStdArgs::stack];
5,667✔
231
      const bool global = configuration()[config::ConfigStdArgs::global];
5,667✔
232

233
      if (heap) {
5,667✔
234
        if (stack) {
3,871✔
235
          return " [Heap & Stack]";
960✔
236
        }
237
        return " [Heap]";
2,911✔
238
      }
239

240
      if (stack) {
1,796✔
241
        return " [Stack]";
1,796✔
242
      }
243

NEW
244
      if (global) {
×
NEW
245
        return " [Global]";
×
246
      }
247

NEW
248
      LOG_ERROR("Did not find heap or stack, or combination thereof!");
×
NEW
249
      assert((heap || stack || global) && "Needs stack, heap, global or combination thereof");
×
NEW
250
      return " [Unknown]";
×
251
    };
5,667✔
252

253
    Table stats("TypeArtPass");
5,667✔
254
    stats.wrap_header_ = true;
5,667✔
255
    stats.title_ += get_ta_mode();
5,667✔
256
    stats.put(Row::make("Malloc", NumInstrumentedMallocs.getValue()));
5,667✔
257
    stats.put(Row::make("Free", NumInstrumentedFrees.getValue()));
5,667✔
258
    stats.put(Row::make("Alloca", NumInstrumentedAlloca.getValue()));
5,667✔
259
    stats.put(Row::make("Global", NumInstrumentedGlobal.getValue()));
5,667✔
260

261
    std::ostringstream stream;
5,667✔
262
    stats.print(stream);
5,667✔
263
    out << stream.str();
5,667✔
264
  }
5,667✔
265

266
  llvm::PreservedAnalyses run(llvm::Module& m, llvm::ModuleAnalysisManager&) {
5,730✔
267
    bool changed{false};
5,730✔
268
    changed |= doInitialization(m);
5,730✔
269
    const bool heap = configuration()[config::ConfigStdArgs::heap];  // Must happen after doInit
5,730✔
270
    dump_module(m, heap ? util::module::ModulePhase::kBase : util::module::ModulePhase::kOpt);
5,730✔
271
    changed |= runOnModule(m);
5,730✔
272
    dump_module(m, heap ? util::module::ModulePhase::kHeap : util::module::ModulePhase::kStack);
5,730✔
273
    changed |= doFinalization();
5,730✔
274
    return changed ? llvm::PreservedAnalyses::none() : llvm::PreservedAnalyses::all();
5,730✔
275
  }
276

277
  bool runOnModule(llvm::Module& m) {
5,730✔
278
    meminst_finder->runOnModule(m);
5,730✔
279
    const bool instrument_global = configuration()[config::ConfigStdArgs::global];
5,730✔
280
    bool globals_were_instrumented{false};
5,730✔
281
    if (instrument_global) {
5,730✔
282
      // declareInstrumentationFunctions(m);
283

284
      const auto& globalsList = meminst_finder->getModuleGlobals();
2,753✔
285
      if (!globalsList.empty()) {
2,753✔
286
        const auto global_count = instrumentation_context->handleGlobal(globalsList);
1,078✔
287
        NumInstrumentedGlobal += global_count;
1,078✔
288
        globals_were_instrumented = global_count > 0;
1,078✔
289
      }
1,078✔
290
    }
2,753✔
291

292
    llvm::DenseSet<const llvm::Constant*> tor_funcs;
5,730✔
293
    {
294
      const auto collect_funcs = [&tor_funcs](const auto* constant) -> bool {
7,220✔
295
        if (llvm::isa<llvm::Function>(constant)) {
1,490✔
296
          tor_funcs.insert(constant);
1,490✔
297
        }
1,490✔
298
        return false;
1,490✔
299
      };
300

301
      util::for_each_cdtor("llvm.global_ctors", m, collect_funcs);
5,730✔
302
      util::for_each_cdtor("llvm.global_dtors", m, collect_funcs);
5,730✔
303
    }
304

305
    const auto instrumented_function = llvm::count_if(m.functions(), [&](auto& f) {
176,734✔
306
                                         if (tor_funcs.contains(&f)) {
165,274✔
307
                                           LOG_DEBUG("Function is in LLVM global ctor or dtor " << f.getName())
308
                                           return false;
1,490✔
309
                                         }
310
                                         return runOnFunc(f);
163,784✔
311
                                       }) > 0;
171,004✔
312
    return instrumented_function || globals_were_instrumented;
5,730✔
313
  }
5,730✔
314

315
  bool runOnFunc(llvm::Function& f) {
163,784✔
316
    using namespace typeart;
317

318
    if (f.isDeclaration() || util::starts_with_any_of(f.getName(), "__typeart", "typeart", "__sanitizer", "__tysan")) {
163,784✔
319
      return false;
127,609✔
320
    }
321

322
    if (!meminst_finder->hasFunctionData(f)) {
36,175✔
NEW
323
      LOG_WARNING("No allocation data could be retrieved for function: " << f.getName());
×
NEW
324
      return false;
×
325
    }
326

327
    LOG_DEBUG("Running on function: " << f.getName())
328

329
    // FIXME this is required when "PassManagerBuilder::EP_OptimizerLast" is used as the function (constant) pointer are
330
    // nullpointer/invalidated
331
    // declareInstrumentationFunctions(*f.getParent());
332

333
    bool mod{false};
36,175✔
334
//  auto& c = f.getContext();
335
#if LLVM_VERSION_MAJOR > 19
336
    DataLayout dl(f.getParent()->getDataLayout());
14,955✔
337
#else
338
    DataLayout dl(f.getParent());
21,220✔
339
#endif
340

341
    const auto& fData   = meminst_finder->getFunctionData(f);
36,175✔
342
    const auto& mallocs = fData.mallocs;
36,175✔
343
    const auto& allocas = fData.allocas;
36,175✔
344
    const auto& frees   = fData.frees;
36,175✔
345

346
    const bool instrument_heap  = configuration()[config::ConfigStdArgs::heap];
36,175✔
347
    const bool instrument_stack = configuration()[config::ConfigStdArgs::stack];
36,175✔
348

349
    if (instrument_heap) {
36,175✔
350
      // instrument collected calls of bb:
351
      const auto heap_count = instrumentation_context->handleHeap(mallocs);
25,128✔
352
      const auto free_count = instrumentation_context->handleFree(frees);
25,128✔
353

354
      NumInstrumentedMallocs += heap_count;
25,128✔
355
      NumInstrumentedFrees += free_count;
25,128✔
356

357
      mod |= heap_count > 0 || free_count > 0;
25,128✔
358
    }
25,128✔
359

360
    if (instrument_stack) {
36,175✔
361
      const auto stack_count = instrumentation_context->handleStack(allocas);
13,657✔
362
      NumInstrumentedAlloca += stack_count;
13,657✔
363
      mod |= stack_count > 0;
13,657✔
364
    }
13,657✔
365

366
    return mod;
36,175✔
367
  }
163,784✔
368
};
369

370
class LegacyTypeArtPass : public llvm::ModulePass {
371
 private:
372
  TypeArtPass pass_impl_;
373

374
 public:
375
  static char ID;  // NOLINT
376

377
  LegacyTypeArtPass() : ModulePass(ID) {};
×
378

379
  bool doInitialization(llvm::Module&) override;
380

381
  bool runOnModule(llvm::Module& module) override;
382

383
  bool doFinalization(llvm::Module&) override;
384

385
  ~LegacyTypeArtPass() override = default;
×
386
};
387

388
bool LegacyTypeArtPass::doInitialization(llvm::Module& m) {
×
389
  return pass_impl_.doInitialization(m);
×
390
}
391

392
bool LegacyTypeArtPass::runOnModule(llvm::Module& module) {
×
393
  bool changed{false};
×
394
  changed |= pass_impl_.runOnModule(module);
×
395
  return changed;
×
396
}
397

398
bool LegacyTypeArtPass::doFinalization(llvm::Module&) {
×
399
  return pass_impl_.doFinalization();
×
400
  ;
401
}
402

403
}  // namespace typeart::pass
404

405
//.....................
406
// New PM
407
//.....................
408
llvm::PassPluginLibraryInfo getTypeartPassPluginInfo() {
5,139✔
409
  using namespace llvm;
410
  return {
10,278✔
411
    LLVM_PLUGIN_API_VERSION, "TypeART", LLVM_VERSION_STRING, [](PassBuilder& pass_builder) {
10,278✔
412
      pass_builder.registerPipelineStartEPCallback([](auto& MPM, OptimizationLevel) {
5,742✔
413
        auto parameters = typeart::util::pass::parsePassParameters(
603✔
414
            typeart::config::pass::parse_typeart_config, "typeart<heap;stats;type-serialization=hybrid>", "typeart");
603✔
415
        if (!parameters) {
603✔
416
          LOG_FATAL("Error parsing heap params: " << parameters.takeError())
×
417
          return;
×
418
        }
419
        MPM.addPass(typeart::pass::TypeArtPass(typeart::pass::TypeArtPass(parameters.get())));
603✔
420
      });
603✔
421
#if LLVM_VERSION_MAJOR > 19
422
      pass_builder.registerOptimizerLastEPCallback([](auto& MPM, OptimizationLevel, ThinOrFullLTOPhase) {
2,430✔
423
#else
424
      pass_builder.registerOptimizerLastEPCallback([](auto& MPM, OptimizationLevel) {
3,312✔
425
#endif
426
        auto parameters = typeart::util::pass::parsePassParameters(
603✔
427
            typeart::config::pass::parse_typeart_config, "typeart<no-heap;stack;stats;type-serialization=hybrid>",
603✔
428
            "typeart");
603✔
429
        if (!parameters) {
603✔
430
          LOG_FATAL("Error parsing stack params: " << parameters.takeError())
×
431
          return;
×
432
        }
433
        MPM.addPass(typeart::pass::TypeArtPass(typeart::pass::TypeArtPass(parameters.get())));
603✔
434
      });
603✔
435
      pass_builder.registerPipelineParsingCallback([](StringRef name, ModulePassManager& module_pm,
14,211✔
436
                                                      ArrayRef<PassBuilder::PipelineElement>) {
437
        if (typeart::util::pass::checkParametrizedPassName(name, "typeart")) {
9,072✔
438
          auto parameters =
439
              typeart::util::pass::parsePassParameters(typeart::config::pass::parse_typeart_config, name, "typeart");
9,072✔
440
          if (!parameters) {
9,072✔
441
            LOG_FATAL("Error parsing params: " << parameters.takeError())
×
442
            return false;
×
443
          }
444
          module_pm.addPass(typeart::pass::TypeArtPass(parameters.get()));
9,072✔
445
          return true;
9,072✔
446
        }
9,072✔
447
        LOG_FATAL("Not a valid parametrized pass name: " << name)
×
448
        return false;
×
449
      });
9,072✔
450
    }
5,139✔
451
  };
452
}
453

454
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo llvmGetPassPluginInfo() {
5,139✔
455
  return getTypeartPassPluginInfo();
5,139✔
456
}
457

458
//.....................
459
// Old PM
460
//.....................
461
char typeart::pass::LegacyTypeArtPass::ID = 0;  // NOLINT
462

463
static RegisterPass<typeart::pass::LegacyTypeArtPass> x("typeart", "TypeArt Pass");  // NOLINT
5,139✔
464

465
ModulePass* createTypeArtPass() {
×
466
  return new typeart::pass::LegacyTypeArtPass();
×
467
}
468

469
extern "C" void AddTypeArtPass(LLVMPassManagerRef pass_manager) {
×
470
  unwrap(pass_manager)->add(createTypeArtPass());
×
471
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc