LLVM 22.0.0git
LibraryScanner.h
Go to the documentation of this file.
1//===- LibraryScanner.h - Scanner for Shared Libraries ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file provides functionality for scanning dynamic (shared) libraries.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_LIBRARYSCANNER_H
14#define LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_LIBRARYSCANNER_H
15
18#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/StringSet.h"
22#include "llvm/Support/Error.h"
24
25#include <atomic>
26#include <mutex>
27#include <queue>
28#include <shared_mutex>
29#include <string>
30
31namespace llvm {
32namespace orc {
33
34class LibraryManager;
35
37 friend class PathResolver;
38
39public:
40 LibraryPathCache() = default;
41
42 void clear(bool isRealPathCache = false) {
43 std::unique_lock<std::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_lock<std::shared_mutex> lock(Mtx);
56 Seen.insert(CanonPath);
57 }
58
59 bool hasSeen(StringRef CanonPath) const {
60 std::shared_lock<std::shared_mutex> lock(Mtx);
61 return Seen.contains(CanonPath);
62 }
63
64 bool hasSeenOrMark(StringRef CanonPath) {
65 std::string s = CanonPath.str();
66 {
67 std::shared_lock<std::shared_mutex> lock(Mtx);
68 if (Seen.contains(s))
69 return true;
70 }
71 {
72 std::unique_lock<std::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_lock<std::shared_mutex> lock(Mtx);
88 RealPathCache.insert({Path, Info});
89 }
90
91 std::optional<PathInfo> read_realpath(StringRef Path) const {
92 std::shared_lock<std::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<PathInfo> RealPathCache;
102
103#ifndef _WIN32
104 StringMap<std::string> ReadlinkCache;
105 StringMap<mode_t> LstatCache;
106
107 void insert_link(StringRef Path, const std::string &s) {
108 std::unique_lock<std::shared_mutex> lock(Mtx);
109 ReadlinkCache.insert({Path, s});
110 }
111
112 std::optional<std::string> read_link(StringRef Path) const {
113 std::shared_lock<std::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_lock<std::shared_mutex> lock(Mtx);
123 LstatCache.insert({Path, m});
124 }
125
126 std::optional<mode_t> read_lstat(StringRef Path) const {
127 std::shared_lock<std::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/// Resolves file system paths with optional caching of results.
139///
140/// Supports lstat, readlink, and realpath operations. Can resolve paths
141/// relative to a base and handle symbolic links. Caches results to reduce
142/// repeated system calls when enabled.
144private:
145 std::shared_ptr<LibraryPathCache> LibPathCache;
146
147public:
148 PathResolver(std::shared_ptr<LibraryPathCache> cache)
149 : LibPathCache(std::move(cache)) {}
150
151 std::optional<std::string> resolve(StringRef Path, std::error_code &ec) {
152 return realpathCached(Path, ec);
153 }
154#ifndef _WIN32
155 mode_t lstatCached(StringRef Path);
156 std::optional<std::string> readlinkCached(StringRef Path);
157#endif
158 std::optional<std::string> realpathCached(StringRef Path, std::error_code &ec,
159 StringRef base = "",
160 bool baseIsResolved = false,
161 long symloopLevel = 40);
162};
163
164/// Performs placeholder substitution in dynamic library paths.
165///
166/// Configures known placeholders (like @loader_path) and replaces them
167/// in input paths with their resolved values.
169public:
170 void configure(StringRef loaderPath);
171
172 std::string substitute(StringRef input) const {
173 for (const auto &[ph, value] : Placeholders) {
174 if (input.starts_with_insensitive(ph))
175 return (Twine(value) + input.drop_front(ph.size())).str();
176 }
177 return input.str();
178 }
179
180private:
181 StringMap<std::string> Placeholders;
182};
183
184/// Validates and normalizes dynamic library paths.
185///
186/// Uses a `PathResolver` to resolve paths to their canonical form and
187/// checks whether they point to valid shared libraries.
189public:
190 DylibPathValidator(PathResolver &PR) : LibPathResolver(PR) {}
191
192 static bool isSharedLibrary(StringRef Path);
193
194 std::optional<std::string> normalize(StringRef Path) const {
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 /// Validate the given path as a shared library.
204 std::optional<std::string> validate(StringRef Path) const {
205 auto realOpt = normalize(Path);
206 if (!realOpt)
207 return std::nullopt;
208
209 if (!isSharedLibrary(*realOpt))
210 return std::nullopt;
211
212 return realOpt;
213 }
214
215private:
216 PathResolver &LibPathResolver;
217};
218
224
229
231public:
233 StringRef PlaceholderPrefix = "")
234 : Kind(Cfg.type), PlaceholderPrefix(PlaceholderPrefix) {
235 for (auto &path : Cfg.Paths)
236 Paths.emplace_back(path.str());
237 }
238
239 std::optional<std::string> resolve(StringRef libStem,
240 const DylibSubstitutor &Subst,
241 DylibPathValidator &Validator) const;
242 SearchPathType searchPathType() const { return Kind; }
243
244private:
245 std::vector<std::string> Paths;
246 SearchPathType Kind;
247 std::string PlaceholderPrefix;
248};
249
251public:
253 std::vector<SearchPathResolver> Resolvers)
254 : Substitutor(std::move(Substitutor)), Validator(Validator),
255 Resolvers(std::move(Resolvers)) {}
256
257 std::optional<std::string> resolve(StringRef Stem,
258 bool VariateLibStem = false) const;
259
260private:
261 std::optional<std::string> tryWithExtensions(StringRef libstem) const;
262
263 DylibSubstitutor Substitutor;
264 DylibPathValidator &Validator;
265 std::vector<SearchPathResolver> Resolvers;
266};
267
269public:
270 DylibResolver(DylibPathValidator &Validator) : Validator(Validator) {}
271
272 void configure(StringRef loaderPath,
273 ArrayRef<SearchPathConfig> SearchPathCfg) {
274 DylibSubstitutor Substitutor;
275 Substitutor.configure(loaderPath);
276
277 std::vector<SearchPathResolver> Resolvers;
278 for (const auto &cfg : SearchPathCfg) {
279 Resolvers.emplace_back(cfg,
280 cfg.type == SearchPathType::RPath ? "@rpath" : "");
281 }
282
283 impl_ = std::make_unique<DylibResolverImpl>(
284 std::move(Substitutor), Validator, std::move(Resolvers));
285 }
286
287 std::optional<std::string> resolve(StringRef libStem,
288 bool VariateLibStem = false) const {
289 if (!impl_)
290 return std::nullopt;
291 return impl_->resolve(libStem, VariateLibStem);
292 }
293
294 static std::string resolvelinkerFlag(StringRef libStem,
295 StringRef loaderPath) {
296 DylibSubstitutor Substitutor;
297 Substitutor.configure(loaderPath);
298 return Substitutor.substitute(libStem);
299 }
300
301private:
302 DylibPathValidator &Validator;
303 std::unique_ptr<DylibResolverImpl> impl_;
304};
305
306enum class PathType : uint8_t { User, System, Unknown };
307
309
311 std::string BasePath; // Canonical base directory path
312 PathType Kind; // User or System
313 std::atomic<ScanState> State;
314
317};
318
319/// Scans and tracks libraries for symbol resolution.
320///
321/// Maintains a list of library paths to scan, caches scanned units,
322/// and resolves paths canonically for consistent tracking.
324public:
325 explicit LibraryScanHelper(const std::vector<std::string> &SPaths,
326 std::shared_ptr<LibraryPathCache> LibPathCache,
327 std::shared_ptr<PathResolver> 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)
334 addBasePath(p);
335 }
336
337 void
338 addBasePath(const std::string &P,
339 PathType Kind =
340 PathType::Unknown); // Add a canonical directory for scanning
341 std::vector<std::shared_ptr<LibrarySearchPath>>
342 getNextBatch(PathType Kind, size_t batchSize);
343
344 bool leftToScan(PathType K) const;
345 void resetToScan();
346
347 bool isTrackedBasePath(StringRef P) const;
348 std::vector<std::shared_ptr<LibrarySearchPath>> getAllUnits() const;
349
351 SmallVector<StringRef> SearchPaths;
352 for (const auto &[_, SP] : LibSearchPaths)
353 SearchPaths.push_back(SP->BasePath);
354 return SearchPaths;
355 }
356
357 PathResolver &getPathResolver() const { return *LibPathResolver; }
358
359 LibraryPathCache &getCache() const { return *LibPathCache; }
360
362 return LibPathCache->hasSeenOrMark(P);
363 }
364
365 std::optional<std::string> resolve(StringRef P, std::error_code &ec) const {
366 return LibPathResolver->resolve(P.str(), ec);
367 }
368
369private:
370 std::string resolveCanonical(StringRef P, std::error_code &ec) const;
371 PathType classifyKind(StringRef P) const;
372
373 mutable std::shared_mutex Mtx;
374 std::shared_ptr<LibraryPathCache> LibPathCache;
375 std::shared_ptr<PathResolver> LibPathResolver;
376
378 LibSearchPaths; // key: canonical path
379 std::deque<StringRef> UnscannedUsr;
380 std::deque<StringRef> UnscannedSys;
381};
382
383/// Loads an object file and provides access to it.
384///
385/// Owns the underlying `ObjectFile` and ensures it is valid.
386/// Any errors encountered during construction are stored and
387/// returned when attempting to access the file.
389public:
390 /// Construct an object file loader from the given path.
392 auto ObjOrErr = loadObjectFileWithOwnership(Path);
393 if (ObjOrErr)
394 Obj = std::move(*ObjOrErr);
395 else {
396 consumeError(std::move(Err));
397 Err = ObjOrErr.takeError();
398 }
399 }
400
403
406
407 /// Get the loaded object file, or return an error if loading failed.
409 if (Err)
410 return std::move(Err);
411 return *Obj.getBinary();
412 }
413
414 static bool isArchitectureCompatible(const object::ObjectFile &Obj);
415
416private:
418 Error Err = Error::success();
419
421 loadObjectFileWithOwnership(StringRef FilePath);
422};
423
424/// Scans libraries, resolves dependencies, and registers them.
426public:
427 using ShouldScanFn = std::function<bool(StringRef)>;
428
431 ShouldScanFn ShouldScanCall = [](StringRef path) { return true; })
432 : ScanHelper(H), LibMgr(LibMgr),
433 ShouldScanCall(std::move(ShouldScanCall)) {}
434
435 void scanNext(PathType Kind, size_t batchSize = 1);
436
437 /// Dependency info for a library.
453
454private:
455 LibraryScanHelper &ScanHelper;
456 LibraryManager &LibMgr;
457 ShouldScanFn ShouldScanCall;
458
459 std::optional<std::string> shouldScan(StringRef FilePath);
460 Expected<LibraryDepsInfo> extractDeps(StringRef FilePath);
461
462 void handleLibrary(StringRef P, PathType K, int level = 1);
463
464 void scanBaseDir(std::shared_ptr<LibrarySearchPath> U);
465};
466
468
469} // end namespace orc
470} // end namespace llvm
471
472#endif // LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_LIBRARYSCANNER_H
This file defines the BumpPtrAllocator interface.
Analysis containing CSE Info
Definition CSEInfo.cpp:27
std::deque< BasicBlock * > PathType
This file provides a collection of function (or more generally, callable) type erasure utilities supp...
#define _
#define H(x, y, z)
Definition MD5.cpp:56
#define P(N)
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.
Definition Debug.h:72
Tile Register Pre configure
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
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",...
Definition StringMap.h:133
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition StringMap.h:321
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
std::string str() const
str - Get the contents as an std::string.
Definition StringRef.h:225
LLVM_ABI bool starts_with_insensitive(StringRef Prefix) const
Check if this string starts with the given Prefix, ignoring case.
Definition StringRef.cpp:41
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition StringRef.h:611
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.
Definition StringSaver.h:22
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
This class is the base class for all object file types.
Definition ObjectFile.h:231
Validates and normalizes dynamic library paths.
std::optional< std::string > normalize(StringRef Path) const
static bool isSharedLibrary(StringRef Path)
DylibPathValidator(PathResolver &PR)
std::optional< std::string > validate(StringRef Path) const
Validate the given path as a shared library.
DylibResolverImpl(DylibSubstitutor Substitutor, DylibPathValidator &Validator, std::vector< SearchPathResolver > Resolvers)
std::optional< std::string > resolve(StringRef Stem, bool VariateLibStem=false) const
static std::string resolvelinkerFlag(StringRef libStem, StringRef loaderPath)
std::optional< std::string > resolve(StringRef libStem, bool VariateLibStem=false) const
DylibResolver(DylibPathValidator &Validator)
void configure(StringRef loaderPath, ArrayRef< SearchPathConfig > SearchPathCfg)
Performs placeholder substitution in dynamic library paths.
std::string substitute(StringRef input) const
void configure(StringRef loaderPath)
Manages library metadata and state for symbol resolution.
bool hasSeen(StringRef CanonPath) const
bool hasSeenOrMark(StringRef CanonPath)
void clear(bool isRealPathCache=false)
void markSeen(const std::string &CanonPath)
Scans and tracks libraries for symbol resolution.
std::vector< std::shared_ptr< LibrarySearchPath > > getNextBatch(PathType Kind, size_t batchSize)
SmallVector< StringRef > getSearchPaths() const
LibraryPathCache & getCache() const
bool isTrackedBasePath(StringRef P) const
std::optional< std::string > resolve(StringRef P, std::error_code &ec) const
bool hasSeenOrMark(StringRef P) const
bool leftToScan(PathType K) const
std::vector< std::shared_ptr< LibrarySearchPath > > getAllUnits() const
PathResolver & getPathResolver() const
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)
void scanNext(PathType Kind, size_t batchSize=1)
std::function< bool(StringRef)> ShouldScanFn
LibraryScanner(LibraryScanHelper &H, LibraryManager &LibMgr, ShouldScanFn ShouldScanCall=[](StringRef path) { return true;})
ObjectFileLoader & operator=(ObjectFileLoader &&)=default
ObjectFileLoader(ObjectFileLoader &&)=default
ObjectFileLoader & operator=(const ObjectFileLoader &)=delete
ObjectFileLoader(StringRef Path)
Construct an object file loader from the given path.
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.
Resolves file system paths with optional caching of results.
std::optional< std::string > readlinkCached(StringRef Path)
PathResolver(std::shared_ptr< LibraryPathCache > cache)
mode_t lstatCached(StringRef Path)
std::optional< std::string > resolve(StringRef Path, std::error_code &ec)
std::optional< std::string > realpathCached(StringRef Path, std::error_code &ec, StringRef base="", bool baseIsResolved=false, long symloopLevel=40)
SearchPathType searchPathType() const
std::optional< std::string > resolve(StringRef libStem, const DylibSubstitutor &Subst, DylibPathValidator &Validator) const
SearchPathResolver(const SearchPathConfig &Cfg, StringRef PlaceholderPrefix="")
LibraryScanner::LibraryDepsInfo LibraryDepsInfo
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.
Definition Debug.cpp:207
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1867
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1083
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:867
LibrarySearchPath(std::string Base, PathType K)
std::atomic< ScanState > State
ArrayRef< StringRef > Paths