Skip to content

Commit 5c3fe11

Browse files
committed
std: Avoid use of libc in portable modules
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`.
1 parent 348930e commit 5c3fe11

File tree

16 files changed

+33
-31
lines changed

16 files changed

+33
-31
lines changed

src/libstd/ffi/c_str.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ use cmp::Ordering;
1414
use error::Error;
1515
use fmt::{self, Write};
1616
use io;
17-
use libc;
1817
use mem;
1918
use memchr;
2019
use ops;
2120
use os::raw::c_char;
2221
use ptr;
2322
use slice;
2423
use str::{self, Utf8Error};
24+
use sys;
2525

2626
/// A type representing an owned, C-compatible, nul-terminated string with no nul bytes in the
2727
/// middle.
@@ -404,7 +404,7 @@ impl CString {
404404
/// ```
405405
#[stable(feature = "cstr_memory", since = "1.4.0")]
406406
pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
407-
let len = libc::strlen(ptr) + 1; // Including the NUL byte
407+
let len = sys::strlen(ptr) + 1; // Including the NUL byte
408408
let slice = slice::from_raw_parts_mut(ptr, len as usize);
409409
CString { inner: Box::from_raw(slice as *mut [c_char] as *mut [u8]) }
410410
}
@@ -861,7 +861,7 @@ impl CStr {
861861
/// ```
862862
#[stable(feature = "rust1", since = "1.0.0")]
863863
pub unsafe fn from_ptr<'a>(ptr: *const c_char) -> &'a CStr {
864-
let len = libc::strlen(ptr);
864+
let len = sys::strlen(ptr);
865865
let ptr = ptr as *const u8;
866866
CStr::from_bytes_with_nul_unchecked(slice::from_raw_parts(ptr, len as usize + 1))
867867
}

src/libstd/sys/redox/backtrace/tracing.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ extern fn trace_fn(ctx: *mut uw::_Unwind_Context,
9696

9797
if cx.idx < cx.frames.len() {
9898
cx.frames[cx.idx] = Frame {
99-
symbol_addr: symaddr,
100-
exact_position: ip,
99+
symbol_addr: symaddr as *mut u8,
100+
exact_position: ip as *mut u8,
101101
};
102102
cx.idx += 1;
103103
}

src/libstd/sys/redox/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use io::{self, ErrorKind};
1414

15+
pub use libc::strlen;
1516
pub use self::rand::hashmap_random_keys;
1617

1718
pub mod args;

src/libstd/sys/unix/backtrace/printing/dladdr.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn resolve_symname<F>(frame: Frame,
2222
{
2323
unsafe {
2424
let mut info: Dl_info = intrinsics::init();
25-
let symname = if dladdr(frame.exact_position, &mut info) == 0 ||
25+
let symname = if dladdr(frame.exact_position as *mut _, &mut info) == 0 ||
2626
info.dli_sname.is_null() {
2727
None
2828
} else {
@@ -41,6 +41,5 @@ struct Dl_info {
4141
}
4242

4343
extern {
44-
fn dladdr(addr: *const libc::c_void,
45-
info: *mut Dl_info) -> libc::c_int;
44+
fn dladdr(addr: *const libc::c_void, info: *mut Dl_info) -> libc::c_int;
4645
}

src/libstd/sys/unix/backtrace/printing/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub use self::dladdr::resolve_symname;
2020
#[cfg(target_os = "emscripten")]
2121
pub fn foreach_symbol_fileline<F>(_: Frame, _: F, _: &BacktraceContext) -> io::Result<bool>
2222
where
23-
F: FnMut(&[u8], ::libc::c_int) -> io::Result<()>
23+
F: FnMut(&[u8], u32) -> io::Result<()>
2424
{
2525
Ok(false)
2626
}

src/libstd/sys/unix/backtrace/tracing/backtrace_fn.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ pub fn unwind_backtrace(frames: &mut [Frame])
3636
} as usize;
3737
for (from, to) in raw_frames.iter().zip(frames.iter_mut()).take(nb_frames) {
3838
*to = Frame {
39-
exact_position: *from,
40-
symbol_addr: *from,
39+
exact_position: *from as *mut u8,
40+
symbol_addr: *from as *mut u8,
4141
};
4242
}
4343
Ok((nb_frames as usize, BacktraceContext))

src/libstd/sys/unix/backtrace/tracing/gcc_s.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ extern fn trace_fn(ctx: *mut uw::_Unwind_Context,
9696

9797
if cx.idx < cx.frames.len() {
9898
cx.frames[cx.idx] = Frame {
99-
symbol_addr: symaddr,
100-
exact_position: ip,
99+
symbol_addr: symaddr as *mut u8,
100+
exact_position: ip as *mut u8,
101101
};
102102
cx.idx += 1;
103103
}

src/libstd/sys/unix/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use libc;
3030
#[cfg(all(not(dox), target_os = "l4re"))] pub use os::linux as platform;
3131

3232
pub use self::rand::hashmap_random_keys;
33+
pub use libc::strlen;
3334

3435
#[macro_use]
3536
pub mod weak;

src/libstd/sys/unix/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl Thread {
8787
};
8888

8989
extern fn thread_start(main: *mut libc::c_void) -> *mut libc::c_void {
90-
unsafe { start_thread(main); }
90+
unsafe { start_thread(main as *mut u8); }
9191
ptr::null_mut()
9292
}
9393
}

src/libstd/sys/windows/backtrace/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ pub fn unwind_backtrace(frames: &mut [Frame])
9595
frame.AddrReturn.Offset == 0 { break }
9696

9797
frames[i] = Frame {
98-
symbol_addr: (addr - 1) as *const c_void,
99-
exact_position: (addr - 1) as *const c_void,
98+
symbol_addr: (addr - 1) as *const u8,
99+
exact_position: (addr - 1) as *const u8,
100100
};
101101
i += 1;
102102
}

src/libstd/sys/windows/backtrace/printing/msvc.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use ffi::CStr;
1212
use io;
13-
use libc::{c_ulong, c_int, c_char};
13+
use libc::{c_ulong, c_char};
1414
use mem;
1515
use sys::c;
1616
use sys::backtrace::BacktraceContext;
@@ -59,7 +59,7 @@ pub fn foreach_symbol_fileline<F>(frame: Frame,
5959
mut f: F,
6060
context: &BacktraceContext)
6161
-> io::Result<bool>
62-
where F: FnMut(&[u8], c_int) -> io::Result<()>
62+
where F: FnMut(&[u8], u32) -> io::Result<()>
6363
{
6464
let SymGetLineFromAddr64 = sym!(&context.dbghelp,
6565
"SymGetLineFromAddr64",
@@ -76,7 +76,7 @@ pub fn foreach_symbol_fileline<F>(frame: Frame,
7676
&mut line);
7777
if ret == c::TRUE {
7878
let name = CStr::from_ptr(line.Filename).to_bytes();
79-
f(name, line.LineNumber as c_int)?;
79+
f(name, line.LineNumber as u32)?;
8080
}
8181
Ok(false)
8282
}

src/libstd/sys/windows/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use os::windows::ffi::{OsStrExt, OsStringExt};
1717
use path::PathBuf;
1818
use time::Duration;
1919

20+
pub use libc::strlen;
2021
pub use self::rand::hashmap_random_keys;
2122

2223
#[macro_use] pub mod compat;

src/libstd/sys/windows/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl Thread {
5252
};
5353

5454
extern "system" fn thread_start(main: *mut c_void) -> c::DWORD {
55-
unsafe { start_thread(main); }
55+
unsafe { start_thread(main as *mut u8); }
5656
0
5757
}
5858
}

src/libstd/sys_common/backtrace.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use env;
1515
use io::prelude::*;
1616
use io;
17-
use libc;
1817
use str;
1918
use sync::atomic::{self, Ordering};
2019
use path::{self, Path};
@@ -39,9 +38,9 @@ pub const HEX_WIDTH: usize = 10;
3938
#[derive(Debug, Copy, Clone)]
4039
pub struct Frame {
4140
/// Exact address of the call that failed.
42-
pub exact_position: *const libc::c_void,
41+
pub exact_position: *const u8,
4342
/// Address of the enclosing function.
44-
pub symbol_addr: *const libc::c_void,
43+
pub symbol_addr: *const u8,
4544
}
4645

4746
/// Max number of frames to print.
@@ -201,8 +200,10 @@ fn output(w: &mut Write, idx: usize, frame: Frame,
201200
///
202201
/// See also `output`.
203202
#[allow(dead_code)]
204-
fn output_fileline(w: &mut Write, file: &[u8], line: libc::c_int,
205-
format: PrintFormat) -> io::Result<()> {
203+
fn output_fileline(w: &mut Write,
204+
file: &[u8],
205+
line: u32,
206+
format: PrintFormat) -> io::Result<()> {
206207
// prior line: " ##: {:2$} - func"
207208
w.write_all(b"")?;
208209
match format {

src/libstd/sys_common/gnu/libbacktrace.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ use sys_common::backtrace::Frame;
2020
pub fn foreach_symbol_fileline<F>(frame: Frame,
2121
mut f: F,
2222
_: &BacktraceContext) -> io::Result<bool>
23-
where F: FnMut(&[u8], libc::c_int) -> io::Result<()>
23+
where F: FnMut(&[u8], u32) -> io::Result<()>
2424
{
2525
// pcinfo may return an arbitrary number of file:line pairs,
2626
// in the order of stack trace (i.e. inlined calls first).
2727
// in order to avoid allocation, we stack-allocate a fixed size of entries.
2828
const FILELINE_SIZE: usize = 32;
29-
let mut fileline_buf = [(ptr::null(), -1); FILELINE_SIZE];
29+
let mut fileline_buf = [(ptr::null(), !0); FILELINE_SIZE];
3030
let ret;
3131
let fileline_count = {
3232
let state = unsafe { init_state() };
@@ -136,7 +136,7 @@ extern {
136136
// helper callbacks
137137
////////////////////////////////////////////////////////////////////////
138138

139-
type FileLine = (*const libc::c_char, libc::c_int);
139+
type FileLine = (*const libc::c_char, u32);
140140

141141
extern fn error_cb(_data: *mut libc::c_void, _msg: *const libc::c_char,
142142
_errnum: libc::c_int) {
@@ -162,7 +162,7 @@ extern fn pcinfo_cb(data: *mut libc::c_void,
162162
// if the buffer is not full, add file:line to the buffer
163163
// and adjust the buffer for next possible calls to pcinfo_cb.
164164
if !buffer.is_empty() {
165-
buffer[0] = (filename, lineno);
165+
buffer[0] = (filename, lineno as u32);
166166
unsafe { ptr::write(slot, &mut buffer[1..]); }
167167
}
168168
}

src/libstd/sys_common/thread.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010

1111
use env;
1212
use alloc::boxed::FnBox;
13-
use libc;
1413
use sync::atomic::{self, Ordering};
1514
use sys::stack_overflow;
1615
use sys::thread as imp;
1716

18-
pub unsafe fn start_thread(main: *mut libc::c_void) {
17+
pub unsafe fn start_thread(main: *mut u8) {
1918
// Next, set up our stack overflow handler which may get triggered if we run
2019
// out of stack.
2120
let _handler = stack_overflow::Handler::new();

0 commit comments

Comments
 (0)