]> git.lizzy.rs Git - rust.git/blob - src/librustc/metadata/loader.rs
auto merge of #14906 : P1start/rust/rustdoc-lifetimes, r=huonw
[rust.git] / src / librustc / metadata / loader.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Finds crate binaries and loads their metadata
12
13 use back::archive::{ArchiveRO, METADATA_FILENAME};
14 use back::svh::Svh;
15 use driver::session::Session;
16 use lib::llvm::{False, llvm, ObjectFile, mk_section_iter};
17 use metadata::cstore::{MetadataBlob, MetadataVec, MetadataArchive};
18 use metadata::decoder;
19 use metadata::encoder;
20 use metadata::filesearch::{FileSearch, FileMatches, FileDoesntMatch};
21 use syntax::abi;
22 use syntax::codemap::Span;
23 use syntax::diagnostic::SpanHandler;
24 use syntax::crateid::CrateId;
25 use syntax::attr::AttrMetaMethods;
26 use util::fs;
27
28 use std::c_str::ToCStr;
29 use std::cmp;
30 use std::io;
31 use std::mem;
32 use std::ptr;
33 use std::slice;
34 use std::str;
35
36 use std::collections::{HashMap, HashSet};
37 use flate;
38 use time;
39
40 pub static MACOS_DLL_PREFIX: &'static str = "lib";
41 pub static MACOS_DLL_SUFFIX: &'static str = ".dylib";
42
43 pub static WIN32_DLL_PREFIX: &'static str = "";
44 pub static WIN32_DLL_SUFFIX: &'static str = ".dll";
45
46 pub static LINUX_DLL_PREFIX: &'static str = "lib";
47 pub static LINUX_DLL_SUFFIX: &'static str = ".so";
48
49 pub static FREEBSD_DLL_PREFIX: &'static str = "lib";
50 pub static FREEBSD_DLL_SUFFIX: &'static str = ".so";
51
52 pub static ANDROID_DLL_PREFIX: &'static str = "lib";
53 pub static ANDROID_DLL_SUFFIX: &'static str = ".so";
54
55 pub struct CrateMismatch {
56     path: Path,
57     got: String,
58 }
59
60 pub struct Context<'a> {
61     pub sess: &'a Session,
62     pub span: Span,
63     pub ident: &'a str,
64     pub crate_id: &'a CrateId,
65     pub id_hash: &'a str,
66     pub hash: Option<&'a Svh>,
67     pub triple: &'a str,
68     pub os: abi::Os,
69     pub filesearch: FileSearch<'a>,
70     pub root: &'a Option<CratePaths>,
71     pub rejected_via_hash: Vec<CrateMismatch>,
72     pub rejected_via_triple: Vec<CrateMismatch>,
73 }
74
75 pub struct Library {
76     pub dylib: Option<Path>,
77     pub rlib: Option<Path>,
78     pub metadata: MetadataBlob,
79 }
80
81 pub struct ArchiveMetadata {
82     _archive: ArchiveRO,
83     // See comments in ArchiveMetadata::new for why this is static
84     data: &'static [u8],
85 }
86
87 pub struct CratePaths {
88     pub ident: String,
89     pub dylib: Option<Path>,
90     pub rlib: Option<Path>
91 }
92
93 impl CratePaths {
94     fn paths(&self) -> Vec<Path> {
95         match (&self.dylib, &self.rlib) {
96             (&None,    &None)              => vec!(),
97             (&Some(ref p), &None) |
98             (&None, &Some(ref p))          => vec!(p.clone()),
99             (&Some(ref p1), &Some(ref p2)) => vec!(p1.clone(), p2.clone()),
100         }
101     }
102 }
103
104 impl<'a> Context<'a> {
105     pub fn maybe_load_library_crate(&mut self) -> Option<Library> {
106         self.find_library_crate()
107     }
108
109     pub fn load_library_crate(&mut self) -> Library {
110         match self.find_library_crate() {
111             Some(t) => t,
112             None => {
113                 self.report_load_errs();
114                 unreachable!()
115             }
116         }
117     }
118
119     pub fn report_load_errs(&mut self) {
120         let message = if self.rejected_via_hash.len() > 0 {
121             format!("found possibly newer version of crate `{}`",
122                     self.ident)
123         } else if self.rejected_via_triple.len() > 0 {
124             format!("found incorrect triple for crate `{}`", self.ident)
125         } else {
126             format!("can't find crate for `{}`", self.ident)
127         };
128         let message = match self.root {
129             &None => message,
130             &Some(ref r) => format!("{} which `{}` depends on",
131                                     message, r.ident)
132         };
133         self.sess.span_err(self.span, message.as_slice());
134
135         let mismatches = self.rejected_via_triple.iter();
136         if self.rejected_via_triple.len() > 0 {
137             self.sess.span_note(self.span,
138                                 format!("expected triple of {}",
139                                         self.triple).as_slice());
140             for (i, &CrateMismatch{ ref path, ref got }) in mismatches.enumerate() {
141                 self.sess.fileline_note(self.span,
142                     format!("crate `{}` path {}{}, triple {}: {}",
143                             self.ident, "#", i+1, got, path.display()).as_slice());
144             }
145         }
146         if self.rejected_via_hash.len() > 0 {
147             self.sess.span_note(self.span, "perhaps this crate needs \
148                                             to be recompiled?");
149             let mismatches = self.rejected_via_hash.iter();
150             for (i, &CrateMismatch{ ref path, .. }) in mismatches.enumerate() {
151                 self.sess.fileline_note(self.span,
152                     format!("crate `{}` path {}{}: {}",
153                             self.ident, "#", i+1, path.display()).as_slice());
154             }
155             match self.root {
156                 &None => {}
157                 &Some(ref r) => {
158                     for (i, path) in r.paths().iter().enumerate() {
159                         self.sess.fileline_note(self.span,
160                             format!("crate `{}` path #{}: {}",
161                                     r.ident, i+1, path.display()).as_slice());
162                     }
163                 }
164             }
165         }
166         self.sess.abort_if_errors();
167     }
168
169     fn find_library_crate(&mut self) -> Option<Library> {
170         let dypair = self.dylibname();
171
172         // want: crate_name.dir_part() + prefix + crate_name.file_part + "-"
173         let dylib_prefix = dypair.map(|(prefix, _)| {
174             format!("{}{}-", prefix, self.crate_id.name)
175         });
176         let rlib_prefix = format!("lib{}-", self.crate_id.name);
177
178         let mut candidates = HashMap::new();
179
180         // First, find all possible candidate rlibs and dylibs purely based on
181         // the name of the files themselves. We're trying to match against an
182         // exact crate_id and a possibly an exact hash.
183         //
184         // During this step, we can filter all found libraries based on the
185         // name and id found in the crate id (we ignore the path portion for
186         // filename matching), as well as the exact hash (if specified). If we
187         // end up having many candidates, we must look at the metadata to
188         // perform exact matches against hashes/crate ids. Note that opening up
189         // the metadata is where we do an exact match against the full contents
190         // of the crate id (path/name/id).
191         //
192         // The goal of this step is to look at as little metadata as possible.
193         self.filesearch.search(|path| {
194             let file = match path.filename_str() {
195                 None => return FileDoesntMatch,
196                 Some(file) => file,
197             };
198             if file.starts_with(rlib_prefix.as_slice()) &&
199                     file.ends_with(".rlib") {
200                 info!("rlib candidate: {}", path.display());
201                 match self.try_match(file, rlib_prefix.as_slice(), ".rlib") {
202                     Some(hash) => {
203                         info!("rlib accepted, hash: {}", hash);
204                         let slot = candidates.find_or_insert_with(hash, |_| {
205                             (HashSet::new(), HashSet::new())
206                         });
207                         let (ref mut rlibs, _) = *slot;
208                         rlibs.insert(fs::realpath(path).unwrap());
209                         FileMatches
210                     }
211                     None => {
212                         info!("rlib rejected");
213                         FileDoesntMatch
214                     }
215                 }
216             } else if dypair.map_or(false, |(_, suffix)| {
217                 file.starts_with(dylib_prefix.get_ref().as_slice()) &&
218                 file.ends_with(suffix)
219             }) {
220                 let (_, suffix) = dypair.unwrap();
221                 let dylib_prefix = dylib_prefix.get_ref().as_slice();
222                 info!("dylib candidate: {}", path.display());
223                 match self.try_match(file, dylib_prefix, suffix) {
224                     Some(hash) => {
225                         info!("dylib accepted, hash: {}", hash);
226                         let slot = candidates.find_or_insert_with(hash, |_| {
227                             (HashSet::new(), HashSet::new())
228                         });
229                         let (_, ref mut dylibs) = *slot;
230                         dylibs.insert(fs::realpath(path).unwrap());
231                         FileMatches
232                     }
233                     None => {
234                         info!("dylib rejected");
235                         FileDoesntMatch
236                     }
237                 }
238             } else {
239                 FileDoesntMatch
240             }
241         });
242
243         // We have now collected all known libraries into a set of candidates
244         // keyed of the filename hash listed. For each filename, we also have a
245         // list of rlibs/dylibs that apply. Here, we map each of these lists
246         // (per hash), to a Library candidate for returning.
247         //
248         // A Library candidate is created if the metadata for the set of
249         // libraries corresponds to the crate id and hash criteria that this
250         // search is being performed for.
251         let mut libraries = Vec::new();
252         for (_hash, (rlibs, dylibs)) in candidates.move_iter() {
253             let mut metadata = None;
254             let rlib = self.extract_one(rlibs, "rlib", &mut metadata);
255             let dylib = self.extract_one(dylibs, "dylib", &mut metadata);
256             match metadata {
257                 Some(metadata) => {
258                     libraries.push(Library {
259                         dylib: dylib,
260                         rlib: rlib,
261                         metadata: metadata,
262                     })
263                 }
264                 None => {}
265             }
266         }
267
268         // Having now translated all relevant found hashes into libraries, see
269         // what we've got and figure out if we found multiple candidates for
270         // libraries or not.
271         match libraries.len() {
272             0 => None,
273             1 => Some(libraries.move_iter().next().unwrap()),
274             _ => {
275                 self.sess.span_err(self.span,
276                     format!("multiple matching crates for `{}`",
277                             self.crate_id.name).as_slice());
278                 self.sess.note("candidates:");
279                 for lib in libraries.iter() {
280                     match lib.dylib {
281                         Some(ref p) => {
282                             self.sess.note(format!("path: {}",
283                                                    p.display()).as_slice());
284                         }
285                         None => {}
286                     }
287                     match lib.rlib {
288                         Some(ref p) => {
289                             self.sess.note(format!("path: {}",
290                                                    p.display()).as_slice());
291                         }
292                         None => {}
293                     }
294                     let data = lib.metadata.as_slice();
295                     let crate_id = decoder::get_crate_id(data);
296                     note_crateid_attr(self.sess.diagnostic(), &crate_id);
297                 }
298                 None
299             }
300         }
301     }
302
303     // Attempts to match the requested version of a library against the file
304     // specified. The prefix/suffix are specified (disambiguates between
305     // rlib/dylib).
306     //
307     // The return value is `None` if `file` doesn't look like a rust-generated
308     // library, or if a specific version was requested and it doesn't match the
309     // apparent file's version.
310     //
311     // If everything checks out, then `Some(hash)` is returned where `hash` is
312     // the listed hash in the filename itself.
313     fn try_match(&self, file: &str, prefix: &str, suffix: &str) -> Option<String>{
314         let middle = file.slice(prefix.len(), file.len() - suffix.len());
315         debug!("matching -- {}, middle: {}", file, middle);
316         let mut parts = middle.splitn('-', 1);
317         let hash = match parts.next() { Some(h) => h, None => return None };
318         debug!("matching -- {}, hash: {} (want {})", file, hash, self.id_hash);
319         let vers = match parts.next() { Some(v) => v, None => return None };
320         debug!("matching -- {}, vers: {} (want {})", file, vers,
321                self.crate_id.version);
322         match self.crate_id.version {
323             Some(ref version) if version.as_slice() != vers => return None,
324             Some(..) => {} // check the hash
325
326             // hash is irrelevant, no version specified
327             None => return Some(hash.to_string())
328         }
329         debug!("matching -- {}, vers ok", file);
330         // hashes in filenames are prefixes of the "true hash"
331         if self.id_hash == hash.as_slice() {
332             debug!("matching -- {}, hash ok", file);
333             Some(hash.to_string())
334         } else {
335             None
336         }
337     }
338
339     // Attempts to extract *one* library from the set `m`. If the set has no
340     // elements, `None` is returned. If the set has more than one element, then
341     // the errors and notes are emitted about the set of libraries.
342     //
343     // With only one library in the set, this function will extract it, and then
344     // read the metadata from it if `*slot` is `None`. If the metadata couldn't
345     // be read, it is assumed that the file isn't a valid rust library (no
346     // errors are emitted).
347     fn extract_one(&mut self, m: HashSet<Path>, flavor: &str,
348                    slot: &mut Option<MetadataBlob>) -> Option<Path> {
349         let mut ret = None::<Path>;
350         let mut error = 0;
351
352         if slot.is_some() {
353             // FIXME(#10786): for an optimization, we only read one of the
354             //                library's metadata sections. In theory we should
355             //                read both, but reading dylib metadata is quite
356             //                slow.
357             if m.len() == 0 {
358                 return None
359             } else if m.len() == 1 {
360                 return Some(m.move_iter().next().unwrap())
361             }
362         }
363
364         for lib in m.move_iter() {
365             info!("{} reading metadata from: {}", flavor, lib.display());
366             let metadata = match get_metadata_section(self.os, &lib) {
367                 Ok(blob) => {
368                     if self.crate_matches(blob.as_slice(), &lib) {
369                         blob
370                     } else {
371                         info!("metadata mismatch");
372                         continue
373                     }
374                 }
375                 Err(_) => {
376                     info!("no metadata found");
377                     continue
378                 }
379             };
380             if ret.is_some() {
381                 self.sess.span_err(self.span,
382                                    format!("multiple {} candidates for `{}` \
383                                             found",
384                                            flavor,
385                                            self.crate_id.name).as_slice());
386                 self.sess.span_note(self.span,
387                                     format!(r"candidate #1: {}",
388                                             ret.get_ref()
389                                                .display()).as_slice());
390                 error = 1;
391                 ret = None;
392             }
393             if error > 0 {
394                 error += 1;
395                 self.sess.span_note(self.span,
396                                     format!(r"candidate #{}: {}", error,
397                                             lib.display()).as_slice());
398                 continue
399             }
400             *slot = Some(metadata);
401             ret = Some(lib);
402         }
403         return if error > 0 {None} else {ret}
404     }
405
406     fn crate_matches(&mut self, crate_data: &[u8], libpath: &Path) -> bool {
407         match decoder::maybe_get_crate_id(crate_data) {
408             Some(ref id) if self.crate_id.matches(id) => {}
409             _ => { info!("Rejecting via crate_id"); return false }
410         }
411         let hash = match decoder::maybe_get_crate_hash(crate_data) {
412             Some(hash) => hash, None => {
413                 info!("Rejecting via lack of crate hash");
414                 return false;
415             }
416         };
417
418         let triple = decoder::get_crate_triple(crate_data);
419         if triple.as_slice() != self.triple {
420             info!("Rejecting via crate triple: expected {} got {}", self.triple, triple);
421             self.rejected_via_triple.push(CrateMismatch {
422                 path: libpath.clone(),
423                 got: triple.to_string()
424             });
425             return false;
426         }
427
428         match self.hash {
429             None => true,
430             Some(myhash) => {
431                 if *myhash != hash {
432                     info!("Rejecting via hash: expected {} got {}", *myhash, hash);
433                     self.rejected_via_hash.push(CrateMismatch {
434                         path: libpath.clone(),
435                         got: myhash.as_str().to_string()
436                     });
437                     false
438                 } else {
439                     true
440                 }
441             }
442         }
443     }
444
445
446     // Returns the corresponding (prefix, suffix) that files need to have for
447     // dynamic libraries
448     fn dylibname(&self) -> Option<(&'static str, &'static str)> {
449         match self.os {
450             abi::OsWin32 => Some((WIN32_DLL_PREFIX, WIN32_DLL_SUFFIX)),
451             abi::OsMacos => Some((MACOS_DLL_PREFIX, MACOS_DLL_SUFFIX)),
452             abi::OsLinux => Some((LINUX_DLL_PREFIX, LINUX_DLL_SUFFIX)),
453             abi::OsAndroid => Some((ANDROID_DLL_PREFIX, ANDROID_DLL_SUFFIX)),
454             abi::OsFreebsd => Some((FREEBSD_DLL_PREFIX, FREEBSD_DLL_SUFFIX)),
455             abi::OsiOS => None,
456         }
457     }
458
459 }
460
461 pub fn note_crateid_attr(diag: &SpanHandler, crateid: &CrateId) {
462     diag.handler().note(format!("crate_id: {}", crateid.to_str()).as_slice());
463 }
464
465 impl ArchiveMetadata {
466     fn new(ar: ArchiveRO) -> Option<ArchiveMetadata> {
467         let data: &'static [u8] = {
468             let data = match ar.read(METADATA_FILENAME) {
469                 Some(data) => data,
470                 None => {
471                     debug!("didn't find '{}' in the archive", METADATA_FILENAME);
472                     return None;
473                 }
474             };
475             // This data is actually a pointer inside of the archive itself, but
476             // we essentially want to cache it because the lookup inside the
477             // archive is a fairly expensive operation (and it's queried for
478             // *very* frequently). For this reason, we transmute it to the
479             // static lifetime to put into the struct. Note that the buffer is
480             // never actually handed out with a static lifetime, but rather the
481             // buffer is loaned with the lifetime of this containing object.
482             // Hence, we're guaranteed that the buffer will never be used after
483             // this object is dead, so this is a safe operation to transmute and
484             // store the data as a static buffer.
485             unsafe { mem::transmute(data) }
486         };
487         Some(ArchiveMetadata {
488             _archive: ar,
489             data: data,
490         })
491     }
492
493     pub fn as_slice<'a>(&'a self) -> &'a [u8] { self.data }
494 }
495
496 // Just a small wrapper to time how long reading metadata takes.
497 fn get_metadata_section(os: abi::Os, filename: &Path) -> Result<MetadataBlob, String> {
498     let start = time::precise_time_ns();
499     let ret = get_metadata_section_imp(os, filename);
500     info!("reading {} => {}ms", filename.filename_display(),
501            (time::precise_time_ns() - start) / 1000000);
502     return ret;
503 }
504
505 fn get_metadata_section_imp(os: abi::Os, filename: &Path) -> Result<MetadataBlob, String> {
506     if !filename.exists() {
507         return Err(format!("no such file: '{}'", filename.display()));
508     }
509     if filename.filename_str().unwrap().ends_with(".rlib") {
510         // Use ArchiveRO for speed here, it's backed by LLVM and uses mmap
511         // internally to read the file. We also avoid even using a memcpy by
512         // just keeping the archive along while the metadata is in use.
513         let archive = match ArchiveRO::open(filename) {
514             Some(ar) => ar,
515             None => {
516                 debug!("llvm didn't like `{}`", filename.display());
517                 return Err(format!("failed to read rlib metadata: '{}'",
518                                    filename.display()));
519             }
520         };
521         return match ArchiveMetadata::new(archive).map(|ar| MetadataArchive(ar)) {
522             None => {
523                 return Err((format!("failed to read rlib metadata: '{}'",
524                                     filename.display())))
525             }
526             Some(blob) => return Ok(blob)
527         }
528     }
529     unsafe {
530         let mb = filename.with_c_str(|buf| {
531             llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf)
532         });
533         if mb as int == 0 {
534             return Err(format!("error reading library: '{}'",
535                                filename.display()))
536         }
537         let of = match ObjectFile::new(mb) {
538             Some(of) => of,
539             _ => {
540                 return Err((format!("provided path not an object file: '{}'",
541                                     filename.display())))
542             }
543         };
544         let si = mk_section_iter(of.llof);
545         while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
546             let mut name_buf = ptr::null();
547             let name_len = llvm::LLVMRustGetSectionName(si.llsi, &mut name_buf);
548             let name = str::raw::from_buf_len(name_buf as *u8, name_len as uint);
549             debug!("get_metadata_section: name {}", name);
550             if read_meta_section_name(os).as_slice() == name.as_slice() {
551                 let cbuf = llvm::LLVMGetSectionContents(si.llsi);
552                 let csz = llvm::LLVMGetSectionSize(si.llsi) as uint;
553                 let mut found =
554                     Err(format!("metadata not found: '{}'", filename.display()));
555                 let cvbuf: *u8 = mem::transmute(cbuf);
556                 let vlen = encoder::metadata_encoding_version.len();
557                 debug!("checking {} bytes of metadata-version stamp",
558                        vlen);
559                 let minsz = cmp::min(vlen, csz);
560                 let version_ok = slice::raw::buf_as_slice(cvbuf, minsz,
561                     |buf0| buf0 == encoder::metadata_encoding_version);
562                 if !version_ok {
563                     return Err((format!("incompatible metadata version found: '{}'",
564                                         filename.display())));
565                 }
566
567                 let cvbuf1 = cvbuf.offset(vlen as int);
568                 debug!("inflating {} bytes of compressed metadata",
569                        csz - vlen);
570                 slice::raw::buf_as_slice(cvbuf1, csz-vlen, |bytes| {
571                     match flate::inflate_bytes(bytes) {
572                         Some(inflated) => found = Ok(MetadataVec(inflated)),
573                         None => {
574                             found =
575                                 Err(format!("failed to decompress \
576                                              metadata for: '{}'",
577                                             filename.display()))
578                         }
579                     }
580                 });
581                 if found.is_ok() {
582                     return found;
583                 }
584             }
585             llvm::LLVMMoveToNextSection(si.llsi);
586         }
587         return Err(format!("metadata not found: '{}'", filename.display()));
588     }
589 }
590
591 pub fn meta_section_name(os: abi::Os) -> Option<&'static str> {
592     match os {
593         abi::OsMacos => Some("__DATA,__note.rustc"),
594         abi::OsiOS => Some("__DATA,__note.rustc"),
595         abi::OsWin32 => Some(".note.rustc"),
596         abi::OsLinux => Some(".note.rustc"),
597         abi::OsAndroid => Some(".note.rustc"),
598         abi::OsFreebsd => Some(".note.rustc")
599     }
600 }
601
602 pub fn read_meta_section_name(os: abi::Os) -> &'static str {
603     match os {
604         abi::OsMacos => "__note.rustc",
605         abi::OsiOS => unreachable!(),
606         abi::OsWin32 => ".note.rustc",
607         abi::OsLinux => ".note.rustc",
608         abi::OsAndroid => ".note.rustc",
609         abi::OsFreebsd => ".note.rustc"
610     }
611 }
612
613 // A diagnostic function for dumping crate metadata to an output stream
614 pub fn list_file_metadata(os: abi::Os, path: &Path,
615                           out: &mut io::Writer) -> io::IoResult<()> {
616     match get_metadata_section(os, path) {
617         Ok(bytes) => decoder::list_crate_metadata(bytes.as_slice(), out),
618         Err(msg) => {
619             write!(out, "{}\n", msg)
620         }
621     }
622 }