1*01c3243fSZachary Turner //===--------------------- ModuleCache.cpp ----------------------*- C++ -*-===//
2*01c3243fSZachary Turner //
3*01c3243fSZachary Turner //                     The LLVM Compiler Infrastructure
4*01c3243fSZachary Turner //
5*01c3243fSZachary Turner // This file is distributed under the University of Illinois Open Source
6*01c3243fSZachary Turner // License. See LICENSE.TXT for details.
7*01c3243fSZachary Turner //
8*01c3243fSZachary Turner //===----------------------------------------------------------------------===//
9*01c3243fSZachary Turner 
10*01c3243fSZachary Turner #include "lldb/Target/ModuleCache.h"
11*01c3243fSZachary Turner 
12*01c3243fSZachary Turner #include "lldb/Core/Log.h"
13*01c3243fSZachary Turner #include "lldb/Core/Module.h"
14*01c3243fSZachary Turner #include "lldb/Core/ModuleList.h"
15*01c3243fSZachary Turner #include "lldb/Core/ModuleSpec.h"
16*01c3243fSZachary Turner #include "lldb/Host/File.h"
17*01c3243fSZachary Turner #include "lldb/Host/FileSystem.h"
18*01c3243fSZachary Turner #include "lldb/Host/LockFile.h"
19*01c3243fSZachary Turner #include "llvm/Support/FileSystem.h"
20*01c3243fSZachary Turner #include "llvm/Support/FileUtilities.h"
21*01c3243fSZachary Turner 
22*01c3243fSZachary Turner #include <assert.h>
23*01c3243fSZachary Turner 
24*01c3243fSZachary Turner #include <cstdio>
25*01c3243fSZachary Turner 
26*01c3243fSZachary Turner using namespace lldb;
27*01c3243fSZachary Turner using namespace lldb_private;
28*01c3243fSZachary Turner 
29*01c3243fSZachary Turner namespace {
30*01c3243fSZachary Turner 
31*01c3243fSZachary Turner const char *kModulesSubdir = ".cache";
32*01c3243fSZachary Turner const char *kLockDirName = ".lock";
33*01c3243fSZachary Turner const char *kTempFileName = ".temp";
34*01c3243fSZachary Turner const char *kTempSymFileName = ".symtemp";
35*01c3243fSZachary Turner const char *kSymFileExtension = ".sym";
36*01c3243fSZachary Turner const char *kFSIllegalChars = "\\/:*?\"<>|";
37*01c3243fSZachary Turner 
38*01c3243fSZachary Turner std::string GetEscapedHostname(const char *hostname) {
39*01c3243fSZachary Turner   if (hostname == nullptr)
40*01c3243fSZachary Turner     hostname = "unknown";
41*01c3243fSZachary Turner   std::string result(hostname);
42*01c3243fSZachary Turner   size_t size = result.size();
43*01c3243fSZachary Turner   for (size_t i = 0; i < size; ++i) {
44*01c3243fSZachary Turner     if ((result[i] >= 1 && result[i] <= 31) ||
45*01c3243fSZachary Turner         strchr(kFSIllegalChars, result[i]) != nullptr)
46*01c3243fSZachary Turner       result[i] = '_';
47*01c3243fSZachary Turner   }
48*01c3243fSZachary Turner   return result;
49*01c3243fSZachary Turner }
50*01c3243fSZachary Turner 
51*01c3243fSZachary Turner class ModuleLock {
52*01c3243fSZachary Turner private:
53*01c3243fSZachary Turner   File m_file;
54*01c3243fSZachary Turner   std::unique_ptr<lldb_private::LockFile> m_lock;
55*01c3243fSZachary Turner   FileSpec m_file_spec;
56*01c3243fSZachary Turner 
57*01c3243fSZachary Turner public:
58*01c3243fSZachary Turner   ModuleLock(const FileSpec &root_dir_spec, const UUID &uuid, Error &error);
59*01c3243fSZachary Turner   void Delete();
60*01c3243fSZachary Turner };
61*01c3243fSZachary Turner 
62*01c3243fSZachary Turner FileSpec JoinPath(const FileSpec &path1, const char *path2) {
63*01c3243fSZachary Turner   FileSpec result_spec(path1);
64*01c3243fSZachary Turner   result_spec.AppendPathComponent(path2);
65*01c3243fSZachary Turner   return result_spec;
66*01c3243fSZachary Turner }
67*01c3243fSZachary Turner 
68*01c3243fSZachary Turner Error MakeDirectory(const FileSpec &dir_path) {
69*01c3243fSZachary Turner   if (dir_path.Exists()) {
70*01c3243fSZachary Turner     if (!dir_path.IsDirectory())
71*01c3243fSZachary Turner       return Error("Invalid existing path");
72*01c3243fSZachary Turner 
73*01c3243fSZachary Turner     return Error();
74*01c3243fSZachary Turner   }
75*01c3243fSZachary Turner 
76*01c3243fSZachary Turner   return FileSystem::MakeDirectory(dir_path, eFilePermissionsDirectoryDefault);
77*01c3243fSZachary Turner }
78*01c3243fSZachary Turner 
79*01c3243fSZachary Turner FileSpec GetModuleDirectory(const FileSpec &root_dir_spec, const UUID &uuid) {
80*01c3243fSZachary Turner   const auto modules_dir_spec = JoinPath(root_dir_spec, kModulesSubdir);
81*01c3243fSZachary Turner   return JoinPath(modules_dir_spec, uuid.GetAsString().c_str());
82*01c3243fSZachary Turner }
83*01c3243fSZachary Turner 
84*01c3243fSZachary Turner FileSpec GetSymbolFileSpec(const FileSpec &module_file_spec) {
85*01c3243fSZachary Turner   return FileSpec(module_file_spec.GetPath() + kSymFileExtension, false);
86*01c3243fSZachary Turner }
87*01c3243fSZachary Turner 
88*01c3243fSZachary Turner void DeleteExistingModule(const FileSpec &root_dir_spec,
89*01c3243fSZachary Turner                           const FileSpec &sysroot_module_path_spec) {
90*01c3243fSZachary Turner   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES));
91*01c3243fSZachary Turner   UUID module_uuid;
92*01c3243fSZachary Turner   {
93*01c3243fSZachary Turner     auto module_sp =
94*01c3243fSZachary Turner         std::make_shared<Module>(ModuleSpec(sysroot_module_path_spec));
95*01c3243fSZachary Turner     module_uuid = module_sp->GetUUID();
96*01c3243fSZachary Turner   }
97*01c3243fSZachary Turner 
98*01c3243fSZachary Turner   if (!module_uuid.IsValid())
99*01c3243fSZachary Turner     return;
100*01c3243fSZachary Turner 
101*01c3243fSZachary Turner   Error error;
102*01c3243fSZachary Turner   ModuleLock lock(root_dir_spec, module_uuid, error);
103*01c3243fSZachary Turner   if (error.Fail()) {
104*01c3243fSZachary Turner     if (log)
105*01c3243fSZachary Turner       log->Printf("Failed to lock module %s: %s",
106*01c3243fSZachary Turner                   module_uuid.GetAsString().c_str(), error.AsCString());
107*01c3243fSZachary Turner   }
108*01c3243fSZachary Turner 
109*01c3243fSZachary Turner   auto link_count = FileSystem::GetHardlinkCount(sysroot_module_path_spec);
110*01c3243fSZachary Turner   if (link_count == -1)
111*01c3243fSZachary Turner     return;
112*01c3243fSZachary Turner 
113*01c3243fSZachary Turner   if (link_count > 2) // module is referred by other hosts.
114*01c3243fSZachary Turner     return;
115*01c3243fSZachary Turner 
116*01c3243fSZachary Turner   const auto module_spec_dir = GetModuleDirectory(root_dir_spec, module_uuid);
117*01c3243fSZachary Turner   FileSystem::DeleteDirectory(module_spec_dir, true);
118*01c3243fSZachary Turner   lock.Delete();
119*01c3243fSZachary Turner }
120*01c3243fSZachary Turner 
121*01c3243fSZachary Turner void DecrementRefExistingModule(const FileSpec &root_dir_spec,
122*01c3243fSZachary Turner                                 const FileSpec &sysroot_module_path_spec) {
123*01c3243fSZachary Turner   // Remove $platform/.cache/$uuid folder if nobody else references it.
124*01c3243fSZachary Turner   DeleteExistingModule(root_dir_spec, sysroot_module_path_spec);
125*01c3243fSZachary Turner 
126*01c3243fSZachary Turner   // Remove sysroot link.
127*01c3243fSZachary Turner   FileSystem::Unlink(sysroot_module_path_spec);
128*01c3243fSZachary Turner 
129*01c3243fSZachary Turner   FileSpec symfile_spec = GetSymbolFileSpec(sysroot_module_path_spec);
130*01c3243fSZachary Turner   if (symfile_spec.Exists()) // delete module's symbol file if exists.
131*01c3243fSZachary Turner     FileSystem::Unlink(symfile_spec);
132*01c3243fSZachary Turner }
133*01c3243fSZachary Turner 
134*01c3243fSZachary Turner Error CreateHostSysRootModuleLink(const FileSpec &root_dir_spec,
135*01c3243fSZachary Turner                                   const char *hostname,
136*01c3243fSZachary Turner                                   const FileSpec &platform_module_spec,
137*01c3243fSZachary Turner                                   const FileSpec &local_module_spec,
138*01c3243fSZachary Turner                                   bool delete_existing) {
139*01c3243fSZachary Turner   const auto sysroot_module_path_spec =
140*01c3243fSZachary Turner       JoinPath(JoinPath(root_dir_spec, hostname),
141*01c3243fSZachary Turner                platform_module_spec.GetPath().c_str());
142*01c3243fSZachary Turner   if (sysroot_module_path_spec.Exists()) {
143*01c3243fSZachary Turner     if (!delete_existing)
144*01c3243fSZachary Turner       return Error();
145*01c3243fSZachary Turner 
146*01c3243fSZachary Turner     DecrementRefExistingModule(root_dir_spec, sysroot_module_path_spec);
147*01c3243fSZachary Turner   }
148*01c3243fSZachary Turner 
149*01c3243fSZachary Turner   const auto error = MakeDirectory(
150*01c3243fSZachary Turner       FileSpec(sysroot_module_path_spec.GetDirectory().AsCString(), false));
151*01c3243fSZachary Turner   if (error.Fail())
152*01c3243fSZachary Turner     return error;
153*01c3243fSZachary Turner 
154*01c3243fSZachary Turner   return FileSystem::Hardlink(sysroot_module_path_spec, local_module_spec);
155*01c3243fSZachary Turner }
156*01c3243fSZachary Turner 
157*01c3243fSZachary Turner } // namespace
158*01c3243fSZachary Turner 
159*01c3243fSZachary Turner ModuleLock::ModuleLock(const FileSpec &root_dir_spec, const UUID &uuid,
160*01c3243fSZachary Turner                        Error &error) {
161*01c3243fSZachary Turner   const auto lock_dir_spec = JoinPath(root_dir_spec, kLockDirName);
162*01c3243fSZachary Turner   error = MakeDirectory(lock_dir_spec);
163*01c3243fSZachary Turner   if (error.Fail())
164*01c3243fSZachary Turner     return;
165*01c3243fSZachary Turner 
166*01c3243fSZachary Turner   m_file_spec = JoinPath(lock_dir_spec, uuid.GetAsString().c_str());
167*01c3243fSZachary Turner   m_file.Open(m_file_spec.GetCString(), File::eOpenOptionWrite |
168*01c3243fSZachary Turner                                             File::eOpenOptionCanCreate |
169*01c3243fSZachary Turner                                             File::eOpenOptionCloseOnExec);
170*01c3243fSZachary Turner   if (!m_file) {
171*01c3243fSZachary Turner     error.SetErrorToErrno();
172*01c3243fSZachary Turner     return;
173*01c3243fSZachary Turner   }
174*01c3243fSZachary Turner 
175*01c3243fSZachary Turner   m_lock.reset(new lldb_private::LockFile(m_file.GetDescriptor()));
176*01c3243fSZachary Turner   error = m_lock->WriteLock(0, 1);
177*01c3243fSZachary Turner   if (error.Fail())
178*01c3243fSZachary Turner     error.SetErrorStringWithFormat("Failed to lock file: %s",
179*01c3243fSZachary Turner                                    error.AsCString());
180*01c3243fSZachary Turner }
181*01c3243fSZachary Turner 
182*01c3243fSZachary Turner void ModuleLock::Delete() {
183*01c3243fSZachary Turner   if (!m_file)
184*01c3243fSZachary Turner     return;
185*01c3243fSZachary Turner 
186*01c3243fSZachary Turner   m_file.Close();
187*01c3243fSZachary Turner   FileSystem::Unlink(m_file_spec);
188*01c3243fSZachary Turner }
189*01c3243fSZachary Turner 
190*01c3243fSZachary Turner /////////////////////////////////////////////////////////////////////////
191*01c3243fSZachary Turner 
192*01c3243fSZachary Turner Error ModuleCache::Put(const FileSpec &root_dir_spec, const char *hostname,
193*01c3243fSZachary Turner                        const ModuleSpec &module_spec, const FileSpec &tmp_file,
194*01c3243fSZachary Turner                        const FileSpec &target_file) {
195*01c3243fSZachary Turner   const auto module_spec_dir =
196*01c3243fSZachary Turner       GetModuleDirectory(root_dir_spec, module_spec.GetUUID());
197*01c3243fSZachary Turner   const auto module_file_path =
198*01c3243fSZachary Turner       JoinPath(module_spec_dir, target_file.GetFilename().AsCString());
199*01c3243fSZachary Turner 
200*01c3243fSZachary Turner   const auto tmp_file_path = tmp_file.GetPath();
201*01c3243fSZachary Turner   const auto err_code =
202*01c3243fSZachary Turner       llvm::sys::fs::rename(tmp_file_path, module_file_path.GetPath());
203*01c3243fSZachary Turner   if (err_code)
204*01c3243fSZachary Turner     return Error("Failed to rename file %s to %s: %s", tmp_file_path.c_str(),
205*01c3243fSZachary Turner                  module_file_path.GetPath().c_str(),
206*01c3243fSZachary Turner                  err_code.message().c_str());
207*01c3243fSZachary Turner 
208*01c3243fSZachary Turner   const auto error = CreateHostSysRootModuleLink(
209*01c3243fSZachary Turner       root_dir_spec, hostname, target_file, module_file_path, true);
210*01c3243fSZachary Turner   if (error.Fail())
211*01c3243fSZachary Turner     return Error("Failed to create link to %s: %s",
212*01c3243fSZachary Turner                  module_file_path.GetPath().c_str(), error.AsCString());
213*01c3243fSZachary Turner   return Error();
214*01c3243fSZachary Turner }
215*01c3243fSZachary Turner 
216*01c3243fSZachary Turner Error ModuleCache::Get(const FileSpec &root_dir_spec, const char *hostname,
217*01c3243fSZachary Turner                        const ModuleSpec &module_spec,
218*01c3243fSZachary Turner                        ModuleSP &cached_module_sp, bool *did_create_ptr) {
219*01c3243fSZachary Turner   const auto find_it =
220*01c3243fSZachary Turner       m_loaded_modules.find(module_spec.GetUUID().GetAsString());
221*01c3243fSZachary Turner   if (find_it != m_loaded_modules.end()) {
222*01c3243fSZachary Turner     cached_module_sp = (*find_it).second.lock();
223*01c3243fSZachary Turner     if (cached_module_sp)
224*01c3243fSZachary Turner       return Error();
225*01c3243fSZachary Turner     m_loaded_modules.erase(find_it);
226*01c3243fSZachary Turner   }
227*01c3243fSZachary Turner 
228*01c3243fSZachary Turner   const auto module_spec_dir =
229*01c3243fSZachary Turner       GetModuleDirectory(root_dir_spec, module_spec.GetUUID());
230*01c3243fSZachary Turner   const auto module_file_path = JoinPath(
231*01c3243fSZachary Turner       module_spec_dir, module_spec.GetFileSpec().GetFilename().AsCString());
232*01c3243fSZachary Turner 
233*01c3243fSZachary Turner   if (!module_file_path.Exists())
234*01c3243fSZachary Turner     return Error("Module %s not found", module_file_path.GetPath().c_str());
235*01c3243fSZachary Turner   if (module_file_path.GetByteSize() != module_spec.GetObjectSize())
236*01c3243fSZachary Turner     return Error("Module %s has invalid file size",
237*01c3243fSZachary Turner                  module_file_path.GetPath().c_str());
238*01c3243fSZachary Turner 
239*01c3243fSZachary Turner   // We may have already cached module but downloaded from an another host - in
240*01c3243fSZachary Turner   // this case let's create a link to it.
241*01c3243fSZachary Turner   auto error = CreateHostSysRootModuleLink(root_dir_spec, hostname,
242*01c3243fSZachary Turner                                            module_spec.GetFileSpec(),
243*01c3243fSZachary Turner                                            module_file_path, false);
244*01c3243fSZachary Turner   if (error.Fail())
245*01c3243fSZachary Turner     return Error("Failed to create link to %s: %s",
246*01c3243fSZachary Turner                  module_file_path.GetPath().c_str(), error.AsCString());
247*01c3243fSZachary Turner 
248*01c3243fSZachary Turner   auto cached_module_spec(module_spec);
249*01c3243fSZachary Turner   cached_module_spec.GetUUID().Clear(); // Clear UUID since it may contain md5
250*01c3243fSZachary Turner                                         // content hash instead of real UUID.
251*01c3243fSZachary Turner   cached_module_spec.GetFileSpec() = module_file_path;
252*01c3243fSZachary Turner   cached_module_spec.GetPlatformFileSpec() = module_spec.GetFileSpec();
253*01c3243fSZachary Turner 
254*01c3243fSZachary Turner   error = ModuleList::GetSharedModule(cached_module_spec, cached_module_sp,
255*01c3243fSZachary Turner                                       nullptr, nullptr, did_create_ptr, false);
256*01c3243fSZachary Turner   if (error.Fail())
257*01c3243fSZachary Turner     return error;
258*01c3243fSZachary Turner 
259*01c3243fSZachary Turner   FileSpec symfile_spec = GetSymbolFileSpec(cached_module_sp->GetFileSpec());
260*01c3243fSZachary Turner   if (symfile_spec.Exists())
261*01c3243fSZachary Turner     cached_module_sp->SetSymbolFileFileSpec(symfile_spec);
262*01c3243fSZachary Turner 
263*01c3243fSZachary Turner   m_loaded_modules.insert(
264*01c3243fSZachary Turner       std::make_pair(module_spec.GetUUID().GetAsString(), cached_module_sp));
265*01c3243fSZachary Turner 
266*01c3243fSZachary Turner   return Error();
267*01c3243fSZachary Turner }
268*01c3243fSZachary Turner 
269*01c3243fSZachary Turner Error ModuleCache::GetAndPut(const FileSpec &root_dir_spec,
270*01c3243fSZachary Turner                              const char *hostname,
271*01c3243fSZachary Turner                              const ModuleSpec &module_spec,
272*01c3243fSZachary Turner                              const ModuleDownloader &module_downloader,
273*01c3243fSZachary Turner                              const SymfileDownloader &symfile_downloader,
274*01c3243fSZachary Turner                              lldb::ModuleSP &cached_module_sp,
275*01c3243fSZachary Turner                              bool *did_create_ptr) {
276*01c3243fSZachary Turner   const auto module_spec_dir =
277*01c3243fSZachary Turner       GetModuleDirectory(root_dir_spec, module_spec.GetUUID());
278*01c3243fSZachary Turner   auto error = MakeDirectory(module_spec_dir);
279*01c3243fSZachary Turner   if (error.Fail())
280*01c3243fSZachary Turner     return error;
281*01c3243fSZachary Turner 
282*01c3243fSZachary Turner   ModuleLock lock(root_dir_spec, module_spec.GetUUID(), error);
283*01c3243fSZachary Turner   if (error.Fail())
284*01c3243fSZachary Turner     return Error("Failed to lock module %s: %s",
285*01c3243fSZachary Turner                  module_spec.GetUUID().GetAsString().c_str(),
286*01c3243fSZachary Turner                  error.AsCString());
287*01c3243fSZachary Turner 
288*01c3243fSZachary Turner   const auto escaped_hostname(GetEscapedHostname(hostname));
289*01c3243fSZachary Turner   // Check local cache for a module.
290*01c3243fSZachary Turner   error = Get(root_dir_spec, escaped_hostname.c_str(), module_spec,
291*01c3243fSZachary Turner               cached_module_sp, did_create_ptr);
292*01c3243fSZachary Turner   if (error.Success())
293*01c3243fSZachary Turner     return error;
294*01c3243fSZachary Turner 
295*01c3243fSZachary Turner   const auto tmp_download_file_spec = JoinPath(module_spec_dir, kTempFileName);
296*01c3243fSZachary Turner   error = module_downloader(module_spec, tmp_download_file_spec);
297*01c3243fSZachary Turner   llvm::FileRemover tmp_file_remover(tmp_download_file_spec.GetPath());
298*01c3243fSZachary Turner   if (error.Fail())
299*01c3243fSZachary Turner     return Error("Failed to download module: %s", error.AsCString());
300*01c3243fSZachary Turner 
301*01c3243fSZachary Turner   // Put downloaded file into local module cache.
302*01c3243fSZachary Turner   error = Put(root_dir_spec, escaped_hostname.c_str(), module_spec,
303*01c3243fSZachary Turner               tmp_download_file_spec, module_spec.GetFileSpec());
304*01c3243fSZachary Turner   if (error.Fail())
305*01c3243fSZachary Turner     return Error("Failed to put module into cache: %s", error.AsCString());
306*01c3243fSZachary Turner 
307*01c3243fSZachary Turner   tmp_file_remover.releaseFile();
308*01c3243fSZachary Turner   error = Get(root_dir_spec, escaped_hostname.c_str(), module_spec,
309*01c3243fSZachary Turner               cached_module_sp, did_create_ptr);
310*01c3243fSZachary Turner   if (error.Fail())
311*01c3243fSZachary Turner     return error;
312*01c3243fSZachary Turner 
313*01c3243fSZachary Turner   // Fetching a symbol file for the module
314*01c3243fSZachary Turner   const auto tmp_download_sym_file_spec =
315*01c3243fSZachary Turner       JoinPath(module_spec_dir, kTempSymFileName);
316*01c3243fSZachary Turner   error = symfile_downloader(cached_module_sp, tmp_download_sym_file_spec);
317*01c3243fSZachary Turner   llvm::FileRemover tmp_symfile_remover(tmp_download_sym_file_spec.GetPath());
318*01c3243fSZachary Turner   if (error.Fail())
319*01c3243fSZachary Turner     // Failed to download a symfile but fetching the module was successful. The
320*01c3243fSZachary Turner     // module might
321*01c3243fSZachary Turner     // contain the necessary symbols and the debugging is also possible without
322*01c3243fSZachary Turner     // a symfile.
323*01c3243fSZachary Turner     return Error();
324*01c3243fSZachary Turner 
325*01c3243fSZachary Turner   error = Put(root_dir_spec, escaped_hostname.c_str(), module_spec,
326*01c3243fSZachary Turner               tmp_download_sym_file_spec,
327*01c3243fSZachary Turner               GetSymbolFileSpec(module_spec.GetFileSpec()));
328*01c3243fSZachary Turner   if (error.Fail())
329*01c3243fSZachary Turner     return Error("Failed to put symbol file into cache: %s", error.AsCString());
330*01c3243fSZachary Turner 
331*01c3243fSZachary Turner   tmp_symfile_remover.releaseFile();
332*01c3243fSZachary Turner 
333*01c3243fSZachary Turner   FileSpec symfile_spec = GetSymbolFileSpec(cached_module_sp->GetFileSpec());
334*01c3243fSZachary Turner   cached_module_sp->SetSymbolFileFileSpec(symfile_spec);
335*01c3243fSZachary Turner   return Error();
336*01c3243fSZachary Turner }
337