LLVM: include/llvm/ExecutionEngine/Orc/TargetProcess/LibraryScanner.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13#ifndef LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_LIBRARYSCANNER_H

14#define LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_LIBRARYSCANNER_H

15

24

25#include

26#include

27#include

28#include <shared_mutex>

29#include

30

31namespace llvm {

32namespace orc {

33

35

38

39public:

41

42 void clear(bool isRealPathCache = false) {

43 std::unique_lockstd::shared\_mutex lock(Mtx);

44 Seen.clear();

45 if (isRealPathCache) {

46 RealPathCache.clear();

47#ifndef _WIN32

48 ReadlinkCache.clear();

49 LstatCache.clear();

50#endif

51 }

52 }

53

54 void markSeen(const std::string &CanonPath) {

55 std::unique_lockstd::shared\_mutex lock(Mtx);

56 Seen.insert(CanonPath);

57 }

58

60 std::shared_lockstd::shared\_mutex lock(Mtx);

61 return Seen.contains(CanonPath);

62 }

63

65 std::string s = CanonPath.str();

66 {

67 std::shared_lockstd::shared\_mutex lock(Mtx);

68 if (Seen.contains(s))

69 return true;

70 }

71 {

72 std::unique_lockstd::shared\_mutex lock(Mtx);

73 Seen.insert(s);

74 }

75 return false;

76 }

77

78private:

79 mutable std::shared_mutex Mtx;

80

81 struct PathInfo {

82 std::string canonicalPath;

83 std::error_code ErrnoCode;

84 };

85

86 void insert_realpath(StringRef Path, const PathInfo &Info) {

87 std::unique_lockstd::shared\_mutex lock(Mtx);

89 }

90

91 std::optional read_realpath(StringRef Path) const {

92 std::shared_lockstd::shared\_mutex lock(Mtx);

93 auto It = RealPathCache.find(Path);

94 if (It != RealPathCache.end())

95 return It->second;

96

97 return std::nullopt;

98 }

99

100 StringSet<> Seen;

101 StringMap RealPathCache;

102

103#ifndef _WIN32

104 StringMapstd::string ReadlinkCache;

105 StringMap<mode_t> LstatCache;

106

107 void insert_link(StringRef Path, const std::string &s) {

108 std::unique_lockstd::shared\_mutex lock(Mtx);

109 ReadlinkCache.insert({Path, s});

110 }

111

112 std::optionalstd::string read_link(StringRef Path) const {

113 std::shared_lockstd::shared\_mutex lock(Mtx);

114 auto It = ReadlinkCache.find(Path);

115 if (It != ReadlinkCache.end())

116 return It->second;

117

118 return std::nullopt;

119 }

120

121 void insert_lstat(StringRef Path, mode_t m) {

122 std::unique_lockstd::shared\_mutex lock(Mtx);

123 LstatCache.insert({Path, m});

124 }

125

126 std::optional<mode_t> read_lstat(StringRef Path) const {

127 std::shared_lockstd::shared\_mutex lock(Mtx);

128 auto It = LstatCache.find(Path);

129 if (It != LstatCache.end())

130 return It->second;

131

132 return std::nullopt;

133 }

134

135#endif

136};

137

138

139

140

141

142

144private:

145 std::shared_ptr LibPathCache;

146

147public:

149 : LibPathCache(std::move(cache)) {}

150

154#ifndef _WIN32

157#endif

160 bool baseIsResolved = false,

161 long symloopLevel = 40);

162};

163

164

165

166

167

169public:

171

173 for (const auto &[ph, value] : Placeholders) {

175 return (Twine(value) + input.drop_front(ph.size())).str();

176 }

177 return input.str();

178 }

179

180private:

182};

183

184

185

186

187

189public:

191

193

195 std::error_code ec;

196 auto real = LibPathResolver.resolve(Path, ec);

197 if (!real || ec)

198 return std::nullopt;

199

200 return real;

201 }

202

203

206 if (!realOpt)

207 return std::nullopt;

208

210 return std::nullopt;

211

212 return realOpt;

213 }

214

215private:

217};

218

224

229

231public:

233 StringRef PlaceholderPrefix = "")

234 : Kind(Cfg.type), PlaceholderPrefix(PlaceholderPrefix) {

236 Paths.emplace_back(path.str());

237 }

238

243

244private:

245 std::vectorstd::string Paths;

247 std::string PlaceholderPrefix;

248};

249

251public:

253 std::vector Resolvers)

254 : Substitutor(std::move(Substitutor)), Validator(Validator),

255 Resolvers(std::move(Resolvers)) {}

256

258 bool VariateLibStem = false) const;

259

260private:

261 std::optionalstd::string tryWithExtensions(StringRef libstem) const;

262

265 std::vector Resolvers;

266};

267

269public:

271

275 Substitutor.configure(loaderPath);

276

277 std::vector Resolvers;

278 for (const auto &cfg : SearchPathCfg) {

279 Resolvers.emplace_back(cfg,

281 }

282

283 impl_ = std::make_unique(

284 std::move(Substitutor), Validator, std::move(Resolvers));

285 }

286

288 bool VariateLibStem = false) const {

289 if (!impl_)

290 return std::nullopt;

291 return impl_->resolve(libStem, VariateLibStem);

292 }

293

300

301private:

303 std::unique_ptr impl_;

304};

305

307

309

318

319

320

321

322

324public:

326 std::shared_ptr LibPathCache,

327 std::shared_ptr LibPathResolver)

328 : LibPathCache(std::move(LibPathCache)),

329 LibPathResolver(std::move(LibPathResolver)) {

331 "orc", dbgs() << "LibraryScanHelper::LibraryScanHelper: base paths : "

332 << SPaths.size() << "\n";);

333 for (const auto &p : SPaths)

335 }

336

337 void

340 PathType::Unknown);

341 std::vector<std::shared_ptr>

343

346

348 std::vector<std::shared_ptr> getAllUnits() const;

349

352 for (const auto &[_, SP] : LibSearchPaths)

353 SearchPaths.push_back(SP->BasePath);

354 return SearchPaths;

355 }

356

358

360

362 return LibPathCache->hasSeenOrMark(P);

363 }

364

366 return LibPathResolver->resolve(P.str(), ec);

367 }

368

369private:

370 std::string resolveCanonical(StringRef P, std::error_code &ec) const;

372

373 mutable std::shared_mutex Mtx;

374 std::shared_ptr LibPathCache;

375 std::shared_ptr LibPathResolver;

376

378 LibSearchPaths;

379 std::deque UnscannedUsr;

380 std::deque UnscannedSys;

381};

382

383

384

385

386

387

389public:

390

392 auto ObjOrErr = loadObjectFileWithOwnership(Path);

393 if (ObjOrErr)

394 Obj = std::move(*ObjOrErr);

395 else {

397 Err = ObjOrErr.takeError();

398 }

399 }

400

403

406

407

409 if (Err)

410 return std::move(Err);

411 return *Obj.getBinary();

412 }

413

415

416private:

419

421 loadObjectFileWithOwnership(StringRef FilePath);

422};

423

424

426public:

428

432 : ScanHelper(H), LibMgr(LibMgr),

433 ShouldScanCall(std::move(ShouldScanCall)) {}

434

436

437

453

454private:

458

459 std::optionalstd::string shouldScan(StringRef FilePath);

461

463

464 void scanBaseDir(std::shared_ptr U);

465};

466

468

469}

470}

471

472#endif

This file defines the BumpPtrAllocator interface.

Analysis containing CSE Info

std::deque< BasicBlock * > PathType

This file defines the SmallVector class.

StringSet - A set-like wrapper for the StringMap.

#define DEBUG_WITH_TYPE(TYPE,...)

DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.

Tile Register Pre configure

ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...

Lightweight error class with error context and mandatory checking.

static ErrorSuccess success()

Create a success value.

Tagged union holding either a T or a Error.

void push_back(const T &Elt)

This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.

StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...

bool insert(MapEntryTy *KeyValue)

insert - Insert the specified key/value pair into the map.

StringRef - Represent a constant reference to a string, i.e.

std::string str() const

str - Get the contents as an std::string.

LLVM_ABI bool starts_with_insensitive(StringRef Prefix) const

Check if this string starts with the given Prefix, ignoring case.

StringRef drop_front(size_t N=1) const

Return a StringRef equal to 'this' but with the first N elements dropped.

Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.

Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...

This class is the base class for all object file types.

Validates and normalizes dynamic library paths.

Definition LibraryScanner.h:188

std::optional< std::string > normalize(StringRef Path) const

Definition LibraryScanner.h:194

static bool isSharedLibrary(StringRef Path)

DylibPathValidator(PathResolver &PR)

Definition LibraryScanner.h:190

std::optional< std::string > validate(StringRef Path) const

Validate the given path as a shared library.

Definition LibraryScanner.h:204

DylibResolverImpl(DylibSubstitutor Substitutor, DylibPathValidator &Validator, std::vector< SearchPathResolver > Resolvers)

Definition LibraryScanner.h:252

std::optional< std::string > resolve(StringRef Stem, bool VariateLibStem=false) const

static std::string resolvelinkerFlag(StringRef libStem, StringRef loaderPath)

Definition LibraryScanner.h:294

std::optional< std::string > resolve(StringRef libStem, bool VariateLibStem=false) const

Definition LibraryScanner.h:287

DylibResolver(DylibPathValidator &Validator)

Definition LibraryScanner.h:270

void configure(StringRef loaderPath, ArrayRef< SearchPathConfig > SearchPathCfg)

Definition LibraryScanner.h:272

Performs placeholder substitution in dynamic library paths.

Definition LibraryScanner.h:168

std::string substitute(StringRef input) const

Definition LibraryScanner.h:172

void configure(StringRef loaderPath)

Manages library metadata and state for symbol resolution.

Definition LibraryScanner.h:36

bool hasSeen(StringRef CanonPath) const

Definition LibraryScanner.h:59

bool hasSeenOrMark(StringRef CanonPath)

Definition LibraryScanner.h:64

void clear(bool isRealPathCache=false)

Definition LibraryScanner.h:42

LibraryPathCache()=default

friend class PathResolver

Definition LibraryScanner.h:37

void markSeen(const std::string &CanonPath)

Definition LibraryScanner.h:54

Scans and tracks libraries for symbol resolution.

Definition LibraryScanner.h:323

std::vector< std::shared_ptr< LibrarySearchPath > > getNextBatch(PathType Kind, size_t batchSize)

SmallVector< StringRef > getSearchPaths() const

Definition LibraryScanner.h:350

LibraryPathCache & getCache() const

Definition LibraryScanner.h:359

bool isTrackedBasePath(StringRef P) const

std::optional< std::string > resolve(StringRef P, std::error_code &ec) const

Definition LibraryScanner.h:365

bool hasSeenOrMark(StringRef P) const

Definition LibraryScanner.h:361

bool leftToScan(PathType K) const

std::vector< std::shared_ptr< LibrarySearchPath > > getAllUnits() const

PathResolver & getPathResolver() const

Definition LibraryScanner.h:357

void addBasePath(const std::string &P, PathType Kind=PathType::Unknown)

LibraryScanHelper(const std::vector< std::string > &SPaths, std::shared_ptr< LibraryPathCache > LibPathCache, std::shared_ptr< PathResolver > LibPathResolver)

Definition LibraryScanner.h:325

void scanNext(PathType Kind, size_t batchSize=1)

std::function< bool(StringRef)> ShouldScanFn

Definition LibraryScanner.h:427

LibraryScanner(LibraryScanHelper &H, LibraryManager &LibMgr, ShouldScanFn ShouldScanCall=[](StringRef path) { return true;})

Definition LibraryScanner.h:429

ObjectFileLoader & operator=(ObjectFileLoader &&)=default

ObjectFileLoader(ObjectFileLoader &&)=default

ObjectFileLoader & operator=(const ObjectFileLoader &)=delete

ObjectFileLoader(StringRef Path)

Construct an object file loader from the given path.

Definition LibraryScanner.h:391

ObjectFileLoader(const ObjectFileLoader &)=delete

static bool isArchitectureCompatible(const object::ObjectFile &Obj)

Expected< object::ObjectFile & > getObjectFile()

Get the loaded object file, or return an error if loading failed.

Definition LibraryScanner.h:408

Resolves file system paths with optional caching of results.

Definition LibraryScanner.h:143

std::optional< std::string > readlinkCached(StringRef Path)

PathResolver(std::shared_ptr< LibraryPathCache > cache)

Definition LibraryScanner.h:148

mode_t lstatCached(StringRef Path)

std::optional< std::string > resolve(StringRef Path, std::error_code &ec)

Definition LibraryScanner.h:151

std::optional< std::string > realpathCached(StringRef Path, std::error_code &ec, StringRef base="", bool baseIsResolved=false, long symloopLevel=40)

SearchPathType searchPathType() const

Definition LibraryScanner.h:242

std::optional< std::string > resolve(StringRef libStem, const DylibSubstitutor &Subst, DylibPathValidator &Validator) const

SearchPathResolver(const SearchPathConfig &Cfg, StringRef PlaceholderPrefix="")

Definition LibraryScanner.h:232

ScanState

Definition LibraryScanner.h:308

@ Scanning

Definition LibraryScanner.h:308

@ Scanned

Definition LibraryScanner.h:308

@ NotScanned

Definition LibraryScanner.h:308

LibraryScanner::LibraryDepsInfo LibraryDepsInfo

Definition LibraryScanner.h:467

SearchPathType

Definition LibraryScanner.h:219

@ RunPath

Definition LibraryScanner.h:222

@ RPath

Definition LibraryScanner.h:220

@ UsrOrSys

Definition LibraryScanner.h:221

PathType

Definition LibraryScanner.h:306

@ Unknown

Definition LibraryScanner.h:306

@ User

Definition LibraryScanner.h:306

@ System

Definition LibraryScanner.h:306

This is an optimization pass for GlobalISel generic memory operations.

LLVM_ABI raw_ostream & dbgs()

dbgs() - This returns a reference to a raw_ostream for debugging messages.

OutputIt move(R &&Range, OutputIt Out)

Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.

BumpPtrAllocatorImpl<> BumpPtrAllocator

The standard BumpPtrAllocator which just uses the default template parameters.

void consumeError(Error Err)

Consume a Error without doing anything.

Implement std::hash so that hash_code can be used in STL containers.

Dependency info for a library.

Definition LibraryScanner.h:438

llvm::BumpPtrAllocator Alloc

Definition LibraryScanner.h:439

void addRunPath(StringRef s)

Definition LibraryScanner.h:449

SmallVector< StringRef, 2 > runPath

Definition LibraryScanner.h:443

SmallVector< StringRef, 4 > deps

Definition LibraryScanner.h:444

void addDep(StringRef s)

Definition LibraryScanner.h:451

llvm::StringSaver Saver

Definition LibraryScanner.h:440

void addRPath(StringRef s)

Definition LibraryScanner.h:447

SmallVector< StringRef, 2 > rpath

Definition LibraryScanner.h:442

bool isPIE

Definition LibraryScanner.h:445

LibrarySearchPath(std::string Base, PathType K)

Definition LibraryScanner.h:315

std::atomic< ScanState > State

Definition LibraryScanner.h:313

std::string BasePath

Definition LibraryScanner.h:311

PathType Kind

Definition LibraryScanner.h:312

Definition LibraryScanner.h:225

ArrayRef< StringRef > Paths

Definition LibraryScanner.h:226

SearchPathType type

Definition LibraryScanner.h:227