Skip to content

Commit 0a2d3c5

Browse files
committed
auto merge of #9096 : huonw/rust/linenoise, r=brson
- Wrap calls into linenoise in a mutex so that the functions don't have to be `unsafe` any more (fixes #3921) - Stop leaking every line that linenoise reads. - Handle the situation of `rl::complete(some_function); do spawn { rl::read(""); }` which would crash (`fail!` that turned into an abort, possibly due to failing with the lock locked) when the user attempted to tab-complete anything. - Add a test for the various functions; it has to be run by hand to verify anything works, but it won't bitrot.
2 parents 4825db4 + a918497 commit 0a2d3c5

File tree

5 files changed

+170
-41
lines changed

5 files changed

+170
-41
lines changed

src/libextra/rl.rs

Lines changed: 73 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// FIXME #3921. This is unsafe because linenoise uses global mutable
12-
// state without mutexes.
13-
1411
use std::c_str::ToCStr;
1512
use std::libc::{c_char, c_int};
16-
use std::local_data;
17-
use std::str;
13+
use std::{local_data, str, rt};
14+
use std::unstable::finally::Finally;
1815

1916
#[cfg(stage0)]
2017
pub mod rustrt {
@@ -28,6 +25,9 @@ pub mod rustrt {
2825
fn linenoiseHistoryLoad(file: *c_char) -> c_int;
2926
fn linenoiseSetCompletionCallback(callback: *u8);
3027
fn linenoiseAddCompletion(completions: *(), line: *c_char);
28+
29+
fn rust_take_linenoise_lock();
30+
fn rust_drop_linenoise_lock();
3131
}
3232
}
3333

@@ -42,65 +42,107 @@ pub mod rustrt {
4242
externfn!(fn linenoiseHistoryLoad(file: *c_char) -> c_int)
4343
externfn!(fn linenoiseSetCompletionCallback(callback: extern "C" fn(*i8, *())))
4444
externfn!(fn linenoiseAddCompletion(completions: *(), line: *c_char))
45+
46+
externfn!(fn rust_take_linenoise_lock())
47+
externfn!(fn rust_drop_linenoise_lock())
48+
}
49+
50+
macro_rules! locked {
51+
($expr:expr) => {
52+
unsafe {
53+
// FIXME #9105: can't use a static mutex in pure Rust yet.
54+
rustrt::rust_take_linenoise_lock();
55+
let x = $expr;
56+
rustrt::rust_drop_linenoise_lock();
57+
x
58+
}
59+
}
4560
}
4661

4762
/// Add a line to history
48-
pub unsafe fn add_history(line: &str) -> bool {
63+
pub fn add_history(line: &str) -> bool {
4964
do line.with_c_str |buf| {
50-
rustrt::linenoiseHistoryAdd(buf) == 1 as c_int
65+
(locked!(rustrt::linenoiseHistoryAdd(buf))) == 1 as c_int
5166
}
5267
}
5368

5469
/// Set the maximum amount of lines stored
55-
pub unsafe fn set_history_max_len(len: int) -> bool {
56-
rustrt::linenoiseHistorySetMaxLen(len as c_int) == 1 as c_int
70+
pub fn set_history_max_len(len: int) -> bool {
71+
(locked!(rustrt::linenoiseHistorySetMaxLen(len as c_int))) == 1 as c_int
5772
}
5873

5974
/// Save line history to a file
60-
pub unsafe fn save_history(file: &str) -> bool {
75+
pub fn save_history(file: &str) -> bool {
6176
do file.with_c_str |buf| {
62-
rustrt::linenoiseHistorySave(buf) == 1 as c_int
77+
// 0 on success, -1 on failure
78+
(locked!(rustrt::linenoiseHistorySave(buf))) == 0 as c_int
6379
}
6480
}
6581

6682
/// Load line history from a file
67-
pub unsafe fn load_history(file: &str) -> bool {
83+
pub fn load_history(file: &str) -> bool {
6884
do file.with_c_str |buf| {
69-
rustrt::linenoiseHistoryLoad(buf) == 1 as c_int
85+
// 0 on success, -1 on failure
86+
(locked!(rustrt::linenoiseHistoryLoad(buf))) == 0 as c_int
7087
}
7188
}
7289

7390
/// Print out a prompt and then wait for input and return it
74-
pub unsafe fn read(prompt: &str) -> Option<~str> {
91+
pub fn read(prompt: &str) -> Option<~str> {
7592
do prompt.with_c_str |buf| {
76-
let line = rustrt::linenoise(buf);
93+
let line = locked!(rustrt::linenoise(buf));
7794

7895
if line.is_null() { None }
79-
else { Some(str::raw::from_c_str(line)) }
96+
else {
97+
unsafe {
98+
do (|| {
99+
Some(str::raw::from_c_str(line))
100+
}).finally {
101+
// linenoise's return value is from strdup, so we
102+
// better not leak it.
103+
rt::global_heap::exchange_free(line);
104+
}
105+
}
106+
}
80107
}
81108
}
82109

83110
pub type CompletionCb = @fn(~str, @fn(~str));
84111

85-
static complete_key: local_data::Key<@CompletionCb> = &local_data::Key;
86-
87-
/// Bind to the main completion callback
88-
pub unsafe fn complete(cb: CompletionCb) {
89-
local_data::set(complete_key, @cb);
90-
91-
extern fn callback(line: *c_char, completions: *()) {
92-
do local_data::get(complete_key) |cb| {
93-
let cb = **cb.unwrap();
94-
95-
unsafe {
96-
do cb(str::raw::from_c_str(line)) |suggestion| {
97-
do suggestion.with_c_str |buf| {
98-
rustrt::linenoiseAddCompletion(completions, buf);
112+
static complete_key: local_data::Key<CompletionCb> = &local_data::Key;
113+
114+
/// Bind to the main completion callback in the current task.
115+
///
116+
/// The completion callback should not call any `extra::rl` functions
117+
/// other than the closure that it receives as its second
118+
/// argument. Calling such a function will deadlock on the mutex used
119+
/// to ensure that the calls are thread-safe.
120+
pub fn complete(cb: CompletionCb) {
121+
local_data::set(complete_key, cb);
122+
123+
extern fn callback(c_line: *c_char, completions: *()) {
124+
do local_data::get(complete_key) |opt_cb| {
125+
// only fetch completions if a completion handler has been
126+
// registered in the current task.
127+
match opt_cb {
128+
None => {},
129+
Some(cb) => {
130+
let line = unsafe { str::raw::from_c_str(c_line) };
131+
do (*cb)(line) |suggestion| {
132+
do suggestion.with_c_str |buf| {
133+
// This isn't locked, because `callback` gets
134+
// called inside `rustrt::linenoise`, which
135+
// *is* already inside the mutex, so
136+
// re-locking would be a deadlock.
137+
unsafe {
138+
rustrt::linenoiseAddCompletion(completions, buf);
139+
}
140+
}
99141
}
100142
}
101143
}
102144
}
103145
}
104146

105-
rustrt::linenoiseSetCompletionCallback(callback);
147+
locked!(rustrt::linenoiseSetCompletionCallback(callback));
106148
}

src/librusti/rusti.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,12 @@ fn compile_crate(src_filename: ~str, binary: ~str) -> Option<bool> {
355355
/// None if no input was read (e.g. EOF was reached).
356356
fn get_line(use_rl: bool, prompt: &str) -> Option<~str> {
357357
if use_rl {
358-
let result = unsafe { rl::read(prompt) };
358+
let result = rl::read(prompt);
359359

360360
match result {
361361
None => None,
362362
Some(line) => {
363-
unsafe { rl::add_history(line) };
363+
rl::add_history(line);
364364
Some(line)
365365
}
366366
}
@@ -525,14 +525,12 @@ pub fn main_args(args: &[~str]) {
525525
println("unstable. If you encounter problems, please use the");
526526
println("compiler instead. Type :help for help.");
527527

528-
unsafe {
529-
do rl::complete |line, suggest| {
530-
if line.starts_with(":") {
531-
suggest(~":clear");
532-
suggest(~":exit");
533-
suggest(~":help");
534-
suggest(~":load");
535-
}
528+
do rl::complete |line, suggest| {
529+
if line.starts_with(":") {
530+
suggest(~":clear");
531+
suggest(~":exit");
532+
suggest(~":help");
533+
suggest(~":load");
536534
}
537535
}
538536
}

src/rt/rust_builtin.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,18 @@ rust_drop_env_lock() {
633633
env_lock.unlock();
634634
}
635635

636+
static lock_and_signal linenoise_lock;
637+
638+
extern "C" CDECL void
639+
rust_take_linenoise_lock() {
640+
linenoise_lock.lock();
641+
}
642+
643+
extern "C" CDECL void
644+
rust_drop_linenoise_lock() {
645+
linenoise_lock.unlock();
646+
}
647+
636648
extern "C" CDECL unsigned int
637649
rust_valgrind_stack_register(void *start, void *end) {
638650
return VALGRIND_STACK_REGISTER(start, end);

src/rt/rustrt.def.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ rust_take_global_args_lock
189189
rust_drop_global_args_lock
190190
rust_take_change_dir_lock
191191
rust_drop_change_dir_lock
192+
rust_take_linenoise_lock
193+
rust_drop_linenoise_lock
192194
rust_get_test_int
193195
rust_get_task
194196
rust_uv_get_loop_from_getaddrinfo_req

src/test/run-pass/rl-human-test.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2013 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+
// xfail-fast no compile flags for check-fast
12+
13+
// we want this to be compiled to avoid bitrot, but the actual test
14+
//has to be conducted by a human, i.e. someone (you?) compiling this
15+
//file with a plain rustc invocation and running it and checking it
16+
//works.
17+
18+
// compile-flags: --cfg robot_mode
19+
20+
extern mod extra;
21+
use extra::rl;
22+
23+
static HISTORY_FILE: &'static str = "rl-human-test-history.txt";
24+
25+
fn main() {
26+
// don't run this in robot mode, but still typecheck it.
27+
if !cfg!(robot_mode) {
28+
println("~~ Welcome to the rl test \"suite\". ~~");
29+
println!("Operations:
30+
- restrict the history to 2 lines,
31+
- set the tab-completion to suggest three copies of each of the last 3 letters (or 'empty'),
32+
- add 'one' and 'two' to the history,
33+
- save it to `{0}`,
34+
- add 'three',
35+
- prompt & save input (check the history & completion work and contains only 'two', 'three'),
36+
- load from `{0}`
37+
- prompt & save input (history should be 'one', 'two' again),
38+
- prompt once more.
39+
40+
The bool return values of each step are printed.",
41+
HISTORY_FILE);
42+
43+
println!("restricting history length: {}", rl::set_history_max_len(3));
44+
45+
do rl::complete |line, suggest| {
46+
if line.is_empty() {
47+
suggest(~"empty")
48+
} else {
49+
for c in line.rev_iter().take(3) {
50+
suggest(format!("{0}{1}{1}{1}", line, c))
51+
}
52+
}
53+
}
54+
55+
println!("adding 'one': {}", rl::add_history("one"));
56+
println!("adding 'two': {}", rl::add_history("two"));
57+
58+
println!("saving history: {}", rl::save_history(HISTORY_FILE));
59+
60+
println!("adding 'three': {}", rl::add_history("three"));
61+
62+
match rl::read("> ") {
63+
Some(s) => println!("saving input: {}", rl::add_history(s)),
64+
None => return
65+
}
66+
println!("loading history: {}", rl::load_history(HISTORY_FILE));
67+
68+
match rl::read("> ") {
69+
Some(s) => println!("saving input: {}", rl::add_history(s)),
70+
None => return
71+
}
72+
73+
rl::read("> ");
74+
}
75+
}

0 commit comments

Comments
 (0)