Skip to content

Commit 4922551

Browse files
committed
Fix clippy warnings and formatting.
We also enable `#![warn(unsafe_op_in_unsafe_fn)]` in the whole mmtk_ruby crate.
1 parent 3c66928 commit 4922551

File tree

7 files changed

+100
-74
lines changed

7 files changed

+100
-74
lines changed

gc/mmtk/src/abi.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl GCThreadTLS {
230230
/// Has undefined behavior if `ptr` is invalid.
231231
pub unsafe fn check_cast(ptr: *mut GCThreadTLS) -> &'static mut GCThreadTLS {
232232
assert!(!ptr.is_null());
233-
let result = &mut *ptr;
233+
let result = unsafe { &mut *ptr };
234234
debug_assert!({
235235
let kind = result.kind;
236236
kind == GC_THREAD_KIND_WORKER
@@ -245,7 +245,7 @@ impl GCThreadTLS {
245245
/// Has undefined behavior if `ptr` is invalid.
246246
pub unsafe fn from_vwt_check(vwt: VMWorkerThread) -> &'static mut GCThreadTLS {
247247
let ptr = Self::from_vwt(vwt);
248-
Self::check_cast(ptr)
248+
unsafe { Self::check_cast(ptr) }
249249
}
250250

251251
#[allow(clippy::not_unsafe_ptr_arg_deref)] // `transmute` does not dereference pointer
@@ -281,7 +281,7 @@ impl RawVecOfObjRef {
281281
///
282282
/// This function turns raw pointer into a Vec without check.
283283
pub unsafe fn into_vec(self) -> Vec<ObjectReference> {
284-
Vec::from_raw_parts(self.ptr, self.len, self.capa)
284+
unsafe { Vec::from_raw_parts(self.ptr, self.len, self.capa) }
285285
}
286286
}
287287

gc/mmtk/src/api.rs

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
use std::sync::atomic::Ordering;
1+
// Functions in this module are unsafe for one reason:
2+
// They are called by C functions and they need to pass raw pointers to Rust.
3+
#![allow(clippy::missing_safety_doc)]
4+
25
use mmtk::util::options::PlanSelector;
6+
use std::sync::atomic::Ordering;
37

48
use crate::abi::RawVecOfObjRef;
59
use crate::abi::RubyBindingOptions;
610
use crate::abi::RubyUpcalls;
711
use crate::binding;
812
use crate::binding::RubyBinding;
913
use crate::mmtk;
10-
use crate::Ruby;
11-
use crate::RubySlot;
1214
use crate::utils::default_heap_max;
1315
use crate::utils::parse_capacity;
16+
use crate::Ruby;
17+
use crate::RubySlot;
1418
use mmtk::memory_manager;
1519
use mmtk::memory_manager::mmtk_init;
1620
use mmtk::util::constants::MIN_OBJECT_SIZE;
@@ -38,22 +42,18 @@ pub extern "C" fn mmtk_is_reachable(object: ObjectReference) -> bool {
3842
// =============== Bootup ===============
3943

4044
fn mmtk_builder_default_parse_threads() -> usize {
41-
let threads_str = std::env::var("MMTK_THREADS")
42-
.unwrap_or("0".to_string());
45+
let threads_str = std::env::var("MMTK_THREADS").unwrap_or("0".to_string());
4346

44-
threads_str
45-
.parse::<usize>()
46-
.unwrap_or_else(|_err| {
47-
eprintln!("[FATAL] Invalid MMTK_THREADS {}", threads_str);
48-
std::process::exit(1);
49-
})
47+
threads_str.parse::<usize>().unwrap_or_else(|_err| {
48+
eprintln!("[FATAL] Invalid MMTK_THREADS {}", threads_str);
49+
std::process::exit(1);
50+
})
5051
}
5152

5253
fn mmtk_builder_default_parse_heap_min() -> usize {
5354
const DEFAULT_HEAP_MIN: usize = 1 << 20;
5455

55-
let heap_min_str = std::env::var("MMTK_HEAP_MIN")
56-
.unwrap_or(DEFAULT_HEAP_MIN.to_string());
56+
let heap_min_str = std::env::var("MMTK_HEAP_MIN").unwrap_or(DEFAULT_HEAP_MIN.to_string());
5757

5858
let size = parse_capacity(&heap_min_str, 0);
5959
if size == 0 {
@@ -65,8 +65,7 @@ fn mmtk_builder_default_parse_heap_min() -> usize {
6565
}
6666

6767
fn mmtk_builder_default_parse_heap_max() -> usize {
68-
let heap_max_str = std::env::var("MMTK_HEAP_MAX")
69-
.unwrap_or(default_heap_max().to_string());
68+
let heap_max_str = std::env::var("MMTK_HEAP_MAX").unwrap_or(default_heap_max().to_string());
7069

7170
let size = parse_capacity(&heap_max_str, 0);
7271
if size == 0 {
@@ -78,8 +77,7 @@ fn mmtk_builder_default_parse_heap_max() -> usize {
7877
}
7978

8079
fn mmtk_builder_default_parse_heap_mode(heap_min: usize, heap_max: usize) -> GCTriggerSelector {
81-
let heap_mode_str = std::env::var("MMTK_HEAP_MODE")
82-
.unwrap_or("dynamic".to_string());
80+
let heap_mode_str = std::env::var("MMTK_HEAP_MODE").unwrap_or("dynamic".to_string());
8381

8482
match heap_mode_str.as_str() {
8583
"fixed" => GCTriggerSelector::FixedHeapSize(heap_max),
@@ -92,8 +90,7 @@ fn mmtk_builder_default_parse_heap_mode(heap_min: usize, heap_max: usize) -> GCT
9290
}
9391

9492
fn mmtk_builder_default_parse_plan() -> PlanSelector {
95-
let plan_str = std::env::var("MMTK_PLAN")
96-
.unwrap_or("Immix".to_string());
93+
let plan_str = std::env::var("MMTK_PLAN").unwrap_or("Immix".to_string());
9794

9895
match plan_str.as_str() {
9996
"NoGC" => PlanSelector::NoGC,
@@ -121,19 +118,25 @@ pub extern "C" fn mmtk_builder_default() -> *mut MMTKBuilder {
121118
let heap_max = mmtk_builder_default_parse_heap_max();
122119

123120
if heap_min >= heap_max {
124-
eprintln!("[FATAL] MMTK_HEAP_MIN({}) >= MMTK_HEAP_MAX({})", heap_min, heap_max);
121+
eprintln!(
122+
"[FATAL] MMTK_HEAP_MIN({}) >= MMTK_HEAP_MAX({})",
123+
heap_min, heap_max
124+
);
125125
std::process::exit(1);
126126
}
127127

128-
builder.options.gc_trigger.set(mmtk_builder_default_parse_heap_mode(heap_min, heap_max));
128+
builder
129+
.options
130+
.gc_trigger
131+
.set(mmtk_builder_default_parse_heap_mode(heap_min, heap_max));
129132

130133
builder.options.plan.set(mmtk_builder_default_parse_plan());
131134

132135
Box::into_raw(Box::new(builder))
133136
}
134137

135138
#[no_mangle]
136-
pub extern "C" fn mmtk_init_binding(
139+
pub unsafe extern "C" fn mmtk_init_binding(
137140
builder: *mut MMTKBuilder,
138141
_binding_options: *const RubyBindingOptions,
139142
upcalls: *const RubyUpcalls,
@@ -142,11 +145,19 @@ pub extern "C" fn mmtk_init_binding(
142145
crate::set_panic_hook();
143146

144147
let builder = unsafe { Box::from_raw(builder) };
145-
let binding_options = RubyBindingOptions {ractor_check_mode: false, suffix_size: 0};
148+
let binding_options = RubyBindingOptions {
149+
ractor_check_mode: false,
150+
suffix_size: 0,
151+
};
146152
let mmtk_boxed = mmtk_init(&builder);
147153
let mmtk_static = Box::leak(Box::new(mmtk_boxed));
148154

149-
let binding = RubyBinding::new(mmtk_static, &binding_options, upcalls, weak_reference_dead_value);
155+
let binding = RubyBinding::new(
156+
mmtk_static,
157+
&binding_options,
158+
upcalls,
159+
weak_reference_dead_value,
160+
);
150161

151162
crate::BINDING
152163
.set(binding)
@@ -164,7 +175,7 @@ pub extern "C" fn mmtk_bind_mutator(tls: VMMutatorThread) -> *mut RubyMutator {
164175
}
165176

166177
#[no_mangle]
167-
pub extern "C" fn mmtk_destroy_mutator(mutator: *mut RubyMutator) {
178+
pub unsafe extern "C" fn mmtk_destroy_mutator(mutator: *mut RubyMutator) {
168179
// notify mmtk-core about destroyed mutator
169180
memory_manager::destroy_mutator(unsafe { &mut *mutator });
170181
// turn the ptr back to a box, and let Rust properly reclaim it
@@ -184,7 +195,9 @@ pub extern "C" fn mmtk_handle_user_collection_request(
184195

185196
#[no_mangle]
186197
pub extern "C" fn mmtk_set_gc_enabled(enable: bool) {
187-
crate::CONFIGURATION.gc_enabled.store(enable, Ordering::Relaxed);
198+
crate::CONFIGURATION
199+
.gc_enabled
200+
.store(enable, Ordering::Relaxed);
188201
}
189202

190203
#[no_mangle]
@@ -195,7 +208,7 @@ pub extern "C" fn mmtk_gc_enabled_p() -> bool {
195208
// =============== Object allocation ===============
196209

197210
#[no_mangle]
198-
pub extern "C" fn mmtk_alloc(
211+
pub unsafe extern "C" fn mmtk_alloc(
199212
mutator: *mut RubyMutator,
200213
size: usize,
201214
align: usize,
@@ -213,7 +226,7 @@ pub extern "C" fn mmtk_alloc(
213226
}
214227

215228
#[no_mangle]
216-
pub extern "C" fn mmtk_post_alloc(
229+
pub unsafe extern "C" fn mmtk_post_alloc(
217230
mutator: *mut RubyMutator,
218231
refer: ObjectReference,
219232
bytes: usize,
@@ -243,7 +256,7 @@ pub extern "C" fn mmtk_remove_weak(ptr: &ObjectReference) {
243256
// =============== Write barriers ===============
244257

245258
#[no_mangle]
246-
pub extern "C" fn mmtk_object_reference_write_post(
259+
pub unsafe extern "C" fn mmtk_object_reference_write_post(
247260
mutator: *mut RubyMutator,
248261
object: ObjectReference,
249262
) {
@@ -347,7 +360,7 @@ pub extern "C" fn mmtk_plan() -> *const u8 {
347360
PlanSelector::NoGC => NO_GC.as_ptr(),
348361
PlanSelector::MarkSweep => MARK_SWEEP.as_ptr(),
349362
PlanSelector::Immix => IMMIX.as_ptr(),
350-
_ => panic!("Unknown plan")
363+
_ => panic!("Unknown plan"),
351364
}
352365
}
353366

@@ -359,7 +372,7 @@ pub extern "C" fn mmtk_heap_mode() -> *const u8 {
359372
match *crate::BINDING.get().unwrap().mmtk.get_options().gc_trigger {
360373
GCTriggerSelector::FixedHeapSize(_) => FIXED_HEAP.as_ptr(),
361374
GCTriggerSelector::DynamicHeapSize(_, _) => DYNAMIC_HEAP.as_ptr(),
362-
_ => panic!("Unknown heap mode")
375+
_ => panic!("Unknown heap mode"),
363376
}
364377
}
365378

@@ -368,7 +381,7 @@ pub extern "C" fn mmtk_heap_min() -> usize {
368381
match *crate::BINDING.get().unwrap().mmtk.get_options().gc_trigger {
369382
GCTriggerSelector::FixedHeapSize(_) => 0,
370383
GCTriggerSelector::DynamicHeapSize(min_size, _) => min_size,
371-
_ => panic!("Unknown heap mode")
384+
_ => panic!("Unknown heap mode"),
372385
}
373386
}
374387

@@ -377,7 +390,7 @@ pub extern "C" fn mmtk_heap_max() -> usize {
377390
match *crate::BINDING.get().unwrap().mmtk.get_options().gc_trigger {
378391
GCTriggerSelector::FixedHeapSize(max_size) => max_size,
379392
GCTriggerSelector::DynamicHeapSize(_, max_size) => max_size,
380-
_ => panic!("Unknown heap mode")
393+
_ => panic!("Unknown heap mode"),
381394
}
382395
}
383396

gc/mmtk/src/binding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl RubyBinding {
8383
gc_thread_join_handles: Default::default(),
8484
wb_unprotected_objects: Default::default(),
8585

86-
weak_reference_dead_value
86+
weak_reference_dead_value,
8787
}
8888
}
8989

gc/mmtk/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Warn about unsafe operations in functions that are already marked as unsafe.
2+
// This will become default in Rust 2024 edition.
3+
#![warn(unsafe_op_in_unsafe_fn)]
4+
15
extern crate libc;
26
extern crate mmtk;
37
#[macro_use]
@@ -141,4 +145,4 @@ macro_rules! extra_assert {
141145
std::assert!($($arg)*);
142146
}
143147
};
144-
}
148+
}

gc/mmtk/src/object_model.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ impl ObjectModel<Ruby> for VMObjectModel {
4040
_semantics: CopySemantics,
4141
_copy_context: &mut GCWorkerCopyContext<Ruby>,
4242
) -> ObjectReference {
43-
unimplemented!(
44-
"Copying GC not currently supported"
45-
)
43+
unimplemented!("Copying GC not currently supported")
4644
}
4745

4846
fn copy_to(_from: ObjectReference, _to: ObjectReference, _region: Address) -> Address {

gc/mmtk/src/utils.rs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::sync::atomic::{AtomicUsize, Ordering};
33
use atomic_refcell::AtomicRefCell;
44
use mmtk::scheduler::{GCWork, GCWorker, WorkBucketStage};
55

6-
use sysinfo::System;
76
use crate::Ruby;
7+
use sysinfo::System;
88

99
pub struct ChunkedVecCollector<T> {
1010
vecs: Vec<Vec<T>>,
@@ -97,7 +97,7 @@ pub fn default_heap_max() -> usize {
9797
.expect("Invalid Memory size") as usize
9898
}
9999

100-
pub fn parse_capacity(input: &String, default: usize) -> usize {
100+
pub fn parse_capacity(input: &str, default: usize) -> usize {
101101
let trimmed = input.trim();
102102

103103
const KIBIBYTE: usize = 1024;
@@ -112,17 +112,20 @@ pub fn parse_capacity(input: &String, default: usize) -> usize {
112112

113113
// 1MiB is the default heap size
114114
match (val, suffix) {
115-
(number, "GiB") => number.parse::<usize>()
116-
.and_then(|v| Ok(v * GIBIBYTE))
115+
(number, "GiB") => number
116+
.parse::<usize>()
117+
.map(|v| v * GIBIBYTE)
117118
.unwrap_or(default),
118-
(number, "MiB") => number.parse::<usize>()
119-
.and_then(|v| Ok(v * MEBIBYTE))
119+
(number, "MiB") => number
120+
.parse::<usize>()
121+
.map(|v| v * MEBIBYTE)
120122
.unwrap_or(default),
121-
(number, "KiB") => number.parse::<usize>()
122-
.and_then(|v| Ok(v * KIBIBYTE))
123+
(number, "KiB") => number
124+
.parse::<usize>()
125+
.map(|v| v * KIBIBYTE)
123126
.unwrap_or(default),
124-
(number, suffix) if suffix.is_empty() => number.parse::<usize>().unwrap_or(default),
125-
(_, _) => default
127+
(number, "") => number.parse::<usize>().unwrap_or(default),
128+
(_, _) => default,
126129
}
127130
}
128131

@@ -154,10 +157,25 @@ mod tests {
154157
fn test_parses_nonsense_value_as_default_max() {
155158
let default = 100;
156159

157-
assert_eq!(default, parse_capacity(&String::from("notanumber"), default));
158-
assert_eq!(default, parse_capacity(&String::from("5tartswithanumber"), default));
159-
assert_eq!(default, parse_capacity(&String::from("number1nthemiddle"), default));
160-
assert_eq!(default, parse_capacity(&String::from("numberattheend111"), default));
161-
assert_eq!(default, parse_capacity(&String::from("mult1pl3numb3r5"), default));
160+
assert_eq!(
161+
default,
162+
parse_capacity(&String::from("notanumber"), default)
163+
);
164+
assert_eq!(
165+
default,
166+
parse_capacity(&String::from("5tartswithanumber"), default)
167+
);
168+
assert_eq!(
169+
default,
170+
parse_capacity(&String::from("number1nthemiddle"), default)
171+
);
172+
assert_eq!(
173+
default,
174+
parse_capacity(&String::from("numberattheend111"), default)
175+
);
176+
assert_eq!(
177+
default,
178+
parse_capacity(&String::from("mult1pl3numb3r5"), default)
179+
);
162180
}
163181
}

0 commit comments

Comments
 (0)