]> git.lizzy.rs Git - rust.git/commitdiff
std: Avoid use of `libc` in portable modules
authorAlex Crichton <alex@alexcrichton.com>
Wed, 1 Nov 2017 20:04:03 +0000 (13:04 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Thu, 9 Nov 2017 15:56:44 +0000 (07:56 -0800)
This commit removes usage of the `libc` crate in "portable" modules like
those at the top level and `sys_common`. Instead common types like `*mut
u8` or `u32` are used instead of `*mut c_void` or `c_int` as well as
switching to platform-specific functions like `sys::strlen` instead of
`libc::strlen`.

16 files changed:
src/libstd/ffi/c_str.rs
src/libstd/sys/redox/backtrace/tracing.rs
src/libstd/sys/redox/mod.rs
src/libstd/sys/unix/backtrace/printing/dladdr.rs
src/libstd/sys/unix/backtrace/printing/mod.rs
src/libstd/sys/unix/backtrace/tracing/backtrace_fn.rs
src/libstd/sys/unix/backtrace/tracing/gcc_s.rs
src/libstd/sys/unix/mod.rs
src/libstd/sys/unix/thread.rs
src/libstd/sys/windows/backtrace/mod.rs
src/libstd/sys/windows/backtrace/printing/msvc.rs
src/libstd/sys/windows/mod.rs
src/libstd/sys/windows/thread.rs
src/libstd/sys_common/backtrace.rs
src/libstd/sys_common/gnu/libbacktrace.rs
src/libstd/sys_common/thread.rs

index b54603919427c33bb812fa4f56e55061cf05a001..1aa47f066b69fc7f7177f929eaee2dc93933b3c5 100644 (file)
@@ -14,7 +14,6 @@
 use error::Error;
 use fmt::{self, Write};
 use io;
-use libc;
 use mem;
 use memchr;
 use ops;
@@ -22,6 +21,7 @@
 use ptr;
 use slice;
 use str::{self, Utf8Error};
+use sys;
 
 /// A type representing an owned, C-compatible, nul-terminated string with no nul bytes in the
 /// middle.
@@ -404,7 +404,7 @@ pub unsafe fn from_vec_unchecked(mut v: Vec<u8>) -> CString {
     /// ```
     #[stable(feature = "cstr_memory", since = "1.4.0")]
     pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
-        let len = libc::strlen(ptr) + 1; // Including the NUL byte
+        let len = sys::strlen(ptr) + 1; // Including the NUL byte
         let slice = slice::from_raw_parts_mut(ptr, len as usize);
         CString { inner: Box::from_raw(slice as *mut [c_char] as *mut [u8]) }
     }
@@ -861,7 +861,7 @@ impl CStr {
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     pub unsafe fn from_ptr<'a>(ptr: *const c_char) -> &'a CStr {
-        let len = libc::strlen(ptr);
+        let len = sys::strlen(ptr);
         let ptr = ptr as *const u8;
         CStr::from_bytes_with_nul_unchecked(slice::from_raw_parts(ptr, len as usize + 1))
     }
index cfeabaddda9859fea27aa88a3da3da5d40d7d50d..0a174b3c3f58687b33d55a5769962d8f0491d823 100644 (file)
@@ -96,8 +96,8 @@ pub fn unwind_backtrace(frames: &mut [Frame])
 
     if cx.idx < cx.frames.len() {
         cx.frames[cx.idx] = Frame {
-            symbol_addr: symaddr,
-            exact_position: ip,
+            symbol_addr: symaddr as *mut u8,
+            exact_position: ip as *mut u8,
         };
         cx.idx += 1;
     }
index ca5f66375149f4d099dc58d982d0956e9b2f6663..4352b72c307735b40d55f4d8da879c706f325e89 100644 (file)
@@ -12,6 +12,7 @@
 
 use io::{self, ErrorKind};
 
+pub use libc::strlen;
 pub use self::rand::hashmap_random_keys;
 
 pub mod args;
index 21f0b3724c13066883416d8eaf41e2f7278d1f0a..bc56fd6594ea6940369ecf5e9e4c4296477c0520 100644 (file)
@@ -22,7 +22,7 @@ pub fn resolve_symname<F>(frame: Frame,
 {
     unsafe {
         let mut info: Dl_info = intrinsics::init();
-        let symname = if dladdr(frame.exact_position, &mut info) == 0 ||
+        let symname = if dladdr(frame.exact_position as *mut _, &mut info) == 0 ||
                          info.dli_sname.is_null() {
             None
         } else {
@@ -41,6 +41,5 @@ struct Dl_info {
 }
 
 extern {
-    fn dladdr(addr: *const libc::c_void,
-              info: *mut Dl_info) -> libc::c_int;
+    fn dladdr(addr: *const libc::c_void, info: *mut Dl_info) -> libc::c_int;
 }
index 8bd2d9eccd82a73db84ee6662d4df33e2af8e192..caa60712b1d585bbc59111a3852d270d3aa28ef1 100644 (file)
@@ -20,7 +20,7 @@
 #[cfg(target_os = "emscripten")]
 pub fn foreach_symbol_fileline<F>(_: Frame, _: F, _: &BacktraceContext) -> io::Result<bool>
 where
-    F: FnMut(&[u8], ::libc::c_int) -> io::Result<()>
+    F: FnMut(&[u8], u32) -> io::Result<()>
 {
     Ok(false)
 }
index ecd32aa9462a904d5dea1ca929328892ed3dcebe..400d39cd4bdcbcb93334cf73479cc11b19433050 100644 (file)
@@ -36,8 +36,8 @@ pub fn unwind_backtrace(frames: &mut [Frame])
     } as usize;
     for (from, to) in raw_frames.iter().zip(frames.iter_mut()).take(nb_frames) {
         *to = Frame {
-            exact_position: *from,
-            symbol_addr: *from,
+            exact_position: *from as *mut u8,
+            symbol_addr: *from as *mut u8,
         };
     }
     Ok((nb_frames as usize, BacktraceContext))
index e3ffbe88acd45c2492d03e9c5eefb339e66efda4..000c08d2e0d198d33c81eb47eebf6f188a63ba3c 100644 (file)
@@ -96,8 +96,8 @@ pub fn unwind_backtrace(frames: &mut [Frame])
 
     if cx.idx < cx.frames.len() {
         cx.frames[cx.idx] = Frame {
-            symbol_addr: symaddr,
-            exact_position: ip,
+            symbol_addr: symaddr as *mut u8,
+            exact_position: ip as *mut u8,
         };
         cx.idx += 1;
     }
index 47be7ab03946fa27f707c56a520b8080de94a0bc..9bdea945ea42eacf52a993f91f6912bcbb675a08 100644 (file)
@@ -30,6 +30,7 @@
 #[cfg(all(not(dox), target_os = "l4re"))]      pub use os::linux as platform;
 
 pub use self::rand::hashmap_random_keys;
+pub use libc::strlen;
 
 #[macro_use]
 pub mod weak;
index cc889454ce9d67e7ff7ec844c49e80dbdba4680f..9da33f5adac140a6db751515978e10a7dbdde9b8 100644 (file)
@@ -87,7 +87,7 @@ pub unsafe fn new<'a>(stack: usize, p: Box<FnBox() + 'a>)
         };
 
         extern fn thread_start(main: *mut libc::c_void) -> *mut libc::c_void {
-            unsafe { start_thread(main); }
+            unsafe { start_thread(main as *mut u8); }
             ptr::null_mut()
         }
     }
index 26b4cb90e0a89c8f16536667eeb6b462d2894023..176891fff23f8fbcd3fb2d8f3234a3159dca921b 100644 (file)
@@ -95,8 +95,8 @@ pub fn unwind_backtrace(frames: &mut [Frame])
                frame.AddrReturn.Offset == 0 { break }
 
             frames[i] = Frame {
-                symbol_addr: (addr - 1) as *const c_void,
-                exact_position: (addr - 1) as *const c_void,
+                symbol_addr: (addr - 1) as *const u8,
+                exact_position: (addr - 1) as *const u8,
             };
             i += 1;
         }
index 3107d78432413ff04b2fbef9dfed39c0a010bceb..5a49b77af8e75a39c92d7b27a99e02d122c94cbd 100644 (file)
@@ -10,7 +10,7 @@
 
 use ffi::CStr;
 use io;
-use libc::{c_ulong, c_int, c_char};
+use libc::{c_ulong, c_char};
 use mem;
 use sys::c;
 use sys::backtrace::BacktraceContext;
@@ -59,7 +59,7 @@ pub fn foreach_symbol_fileline<F>(frame: Frame,
                                   mut f: F,
                                   context: &BacktraceContext)
     -> io::Result<bool>
-    where F: FnMut(&[u8], c_int) -> io::Result<()>
+    where F: FnMut(&[u8], u32) -> io::Result<()>
 {
     let SymGetLineFromAddr64 = sym!(&context.dbghelp,
                                     "SymGetLineFromAddr64",
@@ -76,7 +76,7 @@ pub fn foreach_symbol_fileline<F>(frame: Frame,
                                        &mut line);
         if ret == c::TRUE {
             let name = CStr::from_ptr(line.Filename).to_bytes();
-            f(name, line.LineNumber as c_int)?;
+            f(name, line.LineNumber as u32)?;
         }
         Ok(false)
     }
index 180b55a66e3dcbedbe60f312f1846a6f1622293b..0d12ecf8fe3a1e12024088e54e202da0c2c8bd41 100644 (file)
@@ -17,6 +17,7 @@
 use path::PathBuf;
 use time::Duration;
 
+pub use libc::strlen;
 pub use self::rand::hashmap_random_keys;
 
 #[macro_use] pub mod compat;
index c47baaa2434025fe4476fd8ac4b6dc3431c31dad..74786d092855f091d8b51e9d837d2040d72d1a29 100644 (file)
@@ -52,7 +52,7 @@ pub unsafe fn new<'a>(stack: usize, p: Box<FnBox() + 'a>)
         };
 
         extern "system" fn thread_start(main: *mut c_void) -> c::DWORD {
-            unsafe { start_thread(main); }
+            unsafe { start_thread(main as *mut u8); }
             0
         }
     }
index 8f78c2e6f59d7d384a701fa0fc6c573c1aa15583..9f0214f5f0510c8cc44280305c06755166cac6a0 100644 (file)
@@ -14,7 +14,6 @@
 use env;
 use io::prelude::*;
 use io;
-use libc;
 use str;
 use sync::atomic::{self, Ordering};
 use path::{self, Path};
@@ -39,9 +38,9 @@
 #[derive(Debug, Copy, Clone)]
 pub struct Frame {
     /// Exact address of the call that failed.
-    pub exact_position: *const libc::c_void,
+    pub exact_position: *const u8,
     /// Address of the enclosing function.
-    pub symbol_addr: *const libc::c_void,
+    pub symbol_addr: *const u8,
 }
 
 /// Max number of frames to print.
@@ -201,8 +200,10 @@ fn output(w: &mut Write, idx: usize, frame: Frame,
 ///
 /// See also `output`.
 #[allow(dead_code)]
-fn output_fileline(w: &mut Write, file: &[u8], line: libc::c_int,
-                       format: PrintFormat) -> io::Result<()> {
+fn output_fileline(w: &mut Write,
+                   file: &[u8],
+                   line: u32,
+                   format: PrintFormat) -> io::Result<()> {
     // prior line: "  ##: {:2$} - func"
     w.write_all(b"")?;
     match format {
index 016c840d1541afbe4dd70d46d967c23461b23530..75c6bd5d2a2ba46e73bc213f07f2783f8352fd1c 100644 (file)
 pub fn foreach_symbol_fileline<F>(frame: Frame,
                                   mut f: F,
                                   _: &BacktraceContext) -> io::Result<bool>
-where F: FnMut(&[u8], libc::c_int) -> io::Result<()>
+where F: FnMut(&[u8], u32) -> io::Result<()>
 {
     // pcinfo may return an arbitrary number of file:line pairs,
     // in the order of stack trace (i.e. inlined calls first).
     // in order to avoid allocation, we stack-allocate a fixed size of entries.
     const FILELINE_SIZE: usize = 32;
-    let mut fileline_buf = [(ptr::null(), -1); FILELINE_SIZE];
+    let mut fileline_buf = [(ptr::null(), !0); FILELINE_SIZE];
     let ret;
     let fileline_count = {
         let state = unsafe { init_state() };
@@ -136,7 +136,7 @@ fn backtrace_pcinfo(state: *mut backtrace_state,
 // helper callbacks
 ////////////////////////////////////////////////////////////////////////
 
-type FileLine = (*const libc::c_char, libc::c_int);
+type FileLine = (*const libc::c_char, u32);
 
 extern fn error_cb(_data: *mut libc::c_void, _msg: *const libc::c_char,
                    _errnum: libc::c_int) {
@@ -162,7 +162,7 @@ fn backtrace_pcinfo(state: *mut backtrace_state,
         // if the buffer is not full, add file:line to the buffer
         // and adjust the buffer for next possible calls to pcinfo_cb.
         if !buffer.is_empty() {
-            buffer[0] = (filename, lineno);
+            buffer[0] = (filename, lineno as u32);
             unsafe { ptr::write(slot, &mut buffer[1..]); }
         }
     }
index 87fb34a9dec06459f95806377a451db8bbc27648..c19424f295226cf7e075c6e15da232a39fd1eb23 100644 (file)
 
 use env;
 use alloc::boxed::FnBox;
-use libc;
 use sync::atomic::{self, Ordering};
 use sys::stack_overflow;
 use sys::thread as imp;
 
-pub unsafe fn start_thread(main: *mut libc::c_void) {
+pub unsafe fn start_thread(main: *mut u8) {
     // Next, set up our stack overflow handler which may get triggered if we run
     // out of stack.
     let _handler = stack_overflow::Handler::new();