about summary refs log tree commit diff
path: root/src/librustc_codegen_llvm
diff options
context:
space:
mode:
authorIrina Popa <irinagpopa@gmail.com>2018-07-10 19:19:17 +0300
committerIrina Popa <irinagpopa@gmail.com>2018-07-30 20:10:28 +0300
commit2f73cef5a3bff0da510bfc35f88bb42009af6035 (patch)
tree0bdcdb2012c991f47b07fb614577145bfecb1f0b /src/librustc_codegen_llvm
parentebec156abfc25ce695227ed2c12c420244a433f8 (diff)
downloadrust-2f73cef5a3bff0da510bfc35f88bb42009af6035.tar.gz
rust-2f73cef5a3bff0da510bfc35f88bb42009af6035.zip
rustc_codegen_llvm: use safe references for MemoryBuffer and ObjectFile.
Diffstat (limited to 'src/librustc_codegen_llvm')
-rw-r--r--src/librustc_codegen_llvm/llvm/ffi.rs16
-rw-r--r--src/librustc_codegen_llvm/llvm/mod.rs15
-rw-r--r--src/librustc_codegen_llvm/metadata.rs6
3 files changed, 16 insertions, 21 deletions
diff --git a/src/librustc_codegen_llvm/llvm/ffi.rs b/src/librustc_codegen_llvm/llvm/ffi.rs
index d67cc773bbc..422eb32bab8 100644
--- a/src/librustc_codegen_llvm/llvm/ffi.rs
+++ b/src/librustc_codegen_llvm/llvm/ffi.rs
@@ -390,13 +390,11 @@ extern { pub type Metadata; }
 extern { pub type BasicBlock; }
 extern { pub type Builder; }
 extern { pub type MemoryBuffer; }
-pub type MemoryBufferRef = *mut MemoryBuffer;
 extern { pub type PassManager; }
 pub type PassManagerRef = *mut PassManager;
 extern { pub type PassManagerBuilder; }
 pub type PassManagerBuilderRef = *mut PassManagerBuilder;
 extern { pub type ObjectFile; }
-pub type ObjectFileRef = *mut ObjectFile;
 extern { pub type SectionIterator; }
 pub type SectionIteratorRef = *mut SectionIterator;
 extern { pub type Pass; }
@@ -1143,17 +1141,19 @@ extern "C" {
     // Stuff that's in rustllvm/ because it's not upstream yet.
 
     /// Opens an object file.
-    pub fn LLVMCreateObjectFile(MemBuf: MemoryBufferRef) -> ObjectFileRef;
+    pub fn LLVMCreateObjectFile(
+        MemBuf: &'static mut MemoryBuffer,
+    ) -> Option<&'static mut ObjectFile>;
     /// Closes an object file.
-    pub fn LLVMDisposeObjectFile(ObjFile: ObjectFileRef);
+    pub fn LLVMDisposeObjectFile(ObjFile: &'static mut ObjectFile);
 
     /// Enumerates the sections in an object file.
-    pub fn LLVMGetSections(ObjFile: ObjectFileRef) -> SectionIteratorRef;
+    pub fn LLVMGetSections(ObjFile: &ObjectFile) -> SectionIteratorRef;
     /// Destroys a section iterator.
     pub fn LLVMDisposeSectionIterator(SI: SectionIteratorRef);
     /// Returns true if the section iterator is at the end of the section
     /// list:
-    pub fn LLVMIsSectionIteratorAtEnd(ObjFile: ObjectFileRef, SI: SectionIteratorRef) -> Bool;
+    pub fn LLVMIsSectionIteratorAtEnd(ObjFile: &ObjectFile, SI: SectionIteratorRef) -> Bool;
     /// Moves the section iterator to point to the next section.
     pub fn LLVMMoveToNextSection(SI: SectionIteratorRef);
     /// Returns the current section size.
@@ -1163,7 +1163,9 @@ extern "C" {
 
     /// Reads the given file and returns it as a memory buffer. Use
     /// LLVMDisposeMemoryBuffer() to get rid of it.
-    pub fn LLVMRustCreateMemoryBufferWithContentsOfFile(Path: *const c_char) -> MemoryBufferRef;
+    pub fn LLVMRustCreateMemoryBufferWithContentsOfFile(
+        Path: *const c_char,
+    ) -> Option<&'static mut MemoryBuffer>;
 
     pub fn LLVMStartMultithreaded() -> Bool;
 
diff --git a/src/librustc_codegen_llvm/llvm/mod.rs b/src/librustc_codegen_llvm/llvm/mod.rs
index b4b5ae42d02..6bca2a16221 100644
--- a/src/librustc_codegen_llvm/llvm/mod.rs
+++ b/src/librustc_codegen_llvm/llvm/mod.rs
@@ -179,21 +179,16 @@ impl Attribute {
 // Memory-managed interface to object files.
 
 pub struct ObjectFile {
-    pub llof: ObjectFileRef,
+    pub llof: &'static mut ffi::ObjectFile,
 }
 
 unsafe impl Send for ObjectFile {}
 
 impl ObjectFile {
     // This will take ownership of llmb
-    pub fn new(llmb: MemoryBufferRef) -> Option<ObjectFile> {
+    pub fn new(llmb: &'static mut MemoryBuffer) -> Option<ObjectFile> {
         unsafe {
-            let llof = LLVMCreateObjectFile(llmb);
-            if llof as isize == 0 {
-                // LLVMCreateObjectFile took ownership of llmb
-                return None;
-            }
-
+            let llof = LLVMCreateObjectFile(llmb)?;
             Some(ObjectFile { llof: llof })
         }
     }
@@ -202,7 +197,7 @@ impl ObjectFile {
 impl Drop for ObjectFile {
     fn drop(&mut self) {
         unsafe {
-            LLVMDisposeObjectFile(self.llof);
+            LLVMDisposeObjectFile(&mut *(self.llof as *mut _));
         }
     }
 }
@@ -221,7 +216,7 @@ impl Drop for SectionIter {
     }
 }
 
-pub fn mk_section_iter(llof: ObjectFileRef) -> SectionIter {
+pub fn mk_section_iter(llof: &ffi::ObjectFile) -> SectionIter {
     unsafe { SectionIter { llsi: LLVMGetSections(llof) } }
 }
 
diff --git a/src/librustc_codegen_llvm/metadata.rs b/src/librustc_codegen_llvm/metadata.rs
index 144baa65c1b..fcb704413ef 100644
--- a/src/librustc_codegen_llvm/metadata.rs
+++ b/src/librustc_codegen_llvm/metadata.rs
@@ -58,10 +58,8 @@ impl MetadataLoader for LlvmMetadataLoader {
                           -> Result<MetadataRef, String> {
         unsafe {
             let buf = common::path2cstr(filename);
-            let mb = llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf.as_ptr());
-            if mb as isize == 0 {
-                return Err(format!("error reading library: '{}'", filename.display()));
-            }
+            let mb = llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf.as_ptr())
+                .ok_or_else(|| format!("error reading library: '{}'", filename.display()))?;
             let of = ObjectFile::new(mb)
                 .map(|of| OwningRef::new(box of))
                 .ok_or_else(|| format!("provided path not an object file: '{}'",