Skip to content

Commit 68790e4

Browse files
authored
Unrolled build for rust-lang#128055
Rollup merge of rust-lang#128055 - workingjubilee:deny-unsafe-ops-in-sys-personality-dwarf-eh, r=Amanieu std: unsafe-wrap personality::dwarf::eh Moves the forbiddance up a little. This is another largely whitespace diff, except for hoisting some variable declarations to allow enclosing the `unsafe {}` scope fully and make it clearer where the bounds of some temporaries are.
2 parents 2e63026 + e2137a2 commit 68790e4

File tree

2 files changed

+61
-53
lines changed

2 files changed

+61
-53
lines changed

library/std/src/sys/personality/dwarf/eh.rs

+60-52
Original file line numberDiff line numberDiff line change
@@ -70,45 +70,51 @@ pub unsafe fn find_eh_action(lsda: *const u8, context: &EHContext<'_>) -> Result
7070

7171
let func_start = context.func_start;
7272
let mut reader = DwarfReader::new(lsda);
73-
74-
let start_encoding = reader.read::<u8>();
75-
// base address for landing pad offsets
76-
let lpad_base = if start_encoding != DW_EH_PE_omit {
77-
read_encoded_pointer(&mut reader, context, start_encoding)?
78-
} else {
79-
func_start
73+
let lpad_base = unsafe {
74+
let start_encoding = reader.read::<u8>();
75+
// base address for landing pad offsets
76+
if start_encoding != DW_EH_PE_omit {
77+
read_encoded_pointer(&mut reader, context, start_encoding)?
78+
} else {
79+
func_start
80+
}
8081
};
82+
let call_site_encoding = unsafe {
83+
let ttype_encoding = reader.read::<u8>();
84+
if ttype_encoding != DW_EH_PE_omit {
85+
// Rust doesn't analyze exception types, so we don't care about the type table
86+
reader.read_uleb128();
87+
}
8188

82-
let ttype_encoding = reader.read::<u8>();
83-
if ttype_encoding != DW_EH_PE_omit {
84-
// Rust doesn't analyze exception types, so we don't care about the type table
85-
reader.read_uleb128();
86-
}
87-
88-
let call_site_encoding = reader.read::<u8>();
89-
let call_site_table_length = reader.read_uleb128();
90-
let action_table = reader.ptr.add(call_site_table_length as usize);
89+
reader.read::<u8>()
90+
};
91+
let action_table = unsafe {
92+
let call_site_table_length = reader.read_uleb128();
93+
reader.ptr.add(call_site_table_length as usize)
94+
};
9195
let ip = context.ip;
9296

9397
if !USING_SJLJ_EXCEPTIONS {
9498
// read the callsite table
9599
while reader.ptr < action_table {
96-
// these are offsets rather than pointers;
97-
let cs_start = read_encoded_offset(&mut reader, call_site_encoding)?;
98-
let cs_len = read_encoded_offset(&mut reader, call_site_encoding)?;
99-
let cs_lpad = read_encoded_offset(&mut reader, call_site_encoding)?;
100-
let cs_action_entry = reader.read_uleb128();
101-
// Callsite table is sorted by cs_start, so if we've passed the ip, we
102-
// may stop searching.
103-
if ip < func_start.wrapping_add(cs_start) {
104-
break;
105-
}
106-
if ip < func_start.wrapping_add(cs_start + cs_len) {
107-
if cs_lpad == 0 {
108-
return Ok(EHAction::None);
109-
} else {
110-
let lpad = lpad_base.wrapping_add(cs_lpad);
111-
return Ok(interpret_cs_action(action_table, cs_action_entry, lpad));
100+
unsafe {
101+
// these are offsets rather than pointers;
102+
let cs_start = read_encoded_offset(&mut reader, call_site_encoding)?;
103+
let cs_len = read_encoded_offset(&mut reader, call_site_encoding)?;
104+
let cs_lpad = read_encoded_offset(&mut reader, call_site_encoding)?;
105+
let cs_action_entry = reader.read_uleb128();
106+
// Callsite table is sorted by cs_start, so if we've passed the ip, we
107+
// may stop searching.
108+
if ip < func_start.wrapping_add(cs_start) {
109+
break;
110+
}
111+
if ip < func_start.wrapping_add(cs_start + cs_len) {
112+
if cs_lpad == 0 {
113+
return Ok(EHAction::None);
114+
} else {
115+
let lpad = lpad_base.wrapping_add(cs_lpad);
116+
return Ok(interpret_cs_action(action_table, cs_action_entry, lpad));
117+
}
112118
}
113119
}
114120
}
@@ -125,15 +131,15 @@ pub unsafe fn find_eh_action(lsda: *const u8, context: &EHContext<'_>) -> Result
125131
}
126132
let mut idx = ip.addr();
127133
loop {
128-
let cs_lpad = reader.read_uleb128();
129-
let cs_action_entry = reader.read_uleb128();
134+
let cs_lpad = unsafe { reader.read_uleb128() };
135+
let cs_action_entry = unsafe { reader.read_uleb128() };
130136
idx -= 1;
131137
if idx == 0 {
132138
// Can never have null landing pad for sjlj -- that would have
133139
// been indicated by a -1 call site index.
134140
// FIXME(strict provenance)
135141
let lpad = ptr::with_exposed_provenance((cs_lpad + 1) as usize);
136-
return Ok(interpret_cs_action(action_table, cs_action_entry, lpad));
142+
return Ok(unsafe { interpret_cs_action(action_table, cs_action_entry, lpad) });
137143
}
138144
}
139145
}
@@ -151,9 +157,9 @@ unsafe fn interpret_cs_action(
151157
} else {
152158
// If lpad != 0 and cs_action_entry != 0, we have to check ttype_index.
153159
// If ttype_index == 0 under the condition, we take cleanup action.
154-
let action_record = action_table.offset(cs_action_entry as isize - 1);
160+
let action_record = unsafe { action_table.offset(cs_action_entry as isize - 1) };
155161
let mut action_reader = DwarfReader::new(action_record);
156-
let ttype_index = action_reader.read_sleb128();
162+
let ttype_index = unsafe { action_reader.read_sleb128() };
157163
if ttype_index == 0 {
158164
EHAction::Cleanup(lpad)
159165
} else if ttype_index > 0 {
@@ -186,18 +192,20 @@ unsafe fn read_encoded_offset(reader: &mut DwarfReader, encoding: u8) -> Result<
186192
if encoding == DW_EH_PE_omit || encoding & 0xF0 != 0 {
187193
return Err(());
188194
}
189-
let result = match encoding & 0x0F {
190-
// despite the name, LLVM also uses absptr for offsets instead of pointers
191-
DW_EH_PE_absptr => reader.read::<usize>(),
192-
DW_EH_PE_uleb128 => reader.read_uleb128() as usize,
193-
DW_EH_PE_udata2 => reader.read::<u16>() as usize,
194-
DW_EH_PE_udata4 => reader.read::<u32>() as usize,
195-
DW_EH_PE_udata8 => reader.read::<u64>() as usize,
196-
DW_EH_PE_sleb128 => reader.read_sleb128() as usize,
197-
DW_EH_PE_sdata2 => reader.read::<i16>() as usize,
198-
DW_EH_PE_sdata4 => reader.read::<i32>() as usize,
199-
DW_EH_PE_sdata8 => reader.read::<i64>() as usize,
200-
_ => return Err(()),
195+
let result = unsafe {
196+
match encoding & 0x0F {
197+
// despite the name, LLVM also uses absptr for offsets instead of pointers
198+
DW_EH_PE_absptr => reader.read::<usize>(),
199+
DW_EH_PE_uleb128 => reader.read_uleb128() as usize,
200+
DW_EH_PE_udata2 => reader.read::<u16>() as usize,
201+
DW_EH_PE_udata4 => reader.read::<u32>() as usize,
202+
DW_EH_PE_udata8 => reader.read::<u64>() as usize,
203+
DW_EH_PE_sleb128 => reader.read_sleb128() as usize,
204+
DW_EH_PE_sdata2 => reader.read::<i16>() as usize,
205+
DW_EH_PE_sdata4 => reader.read::<i32>() as usize,
206+
DW_EH_PE_sdata8 => reader.read::<i64>() as usize,
207+
_ => return Err(()),
208+
}
201209
};
202210
Ok(result)
203211
}
@@ -250,14 +258,14 @@ unsafe fn read_encoded_pointer(
250258
if encoding & 0x0F != DW_EH_PE_absptr {
251259
return Err(());
252260
}
253-
reader.read::<*const u8>()
261+
unsafe { reader.read::<*const u8>() }
254262
} else {
255-
let offset = read_encoded_offset(reader, encoding & 0x0F)?;
263+
let offset = unsafe { read_encoded_offset(reader, encoding & 0x0F)? };
256264
base_ptr.wrapping_add(offset)
257265
};
258266

259267
if encoding & DW_EH_PE_indirect != 0 {
260-
ptr = *(ptr.cast::<*const u8>());
268+
ptr = unsafe { *(ptr.cast::<*const u8>()) };
261269
}
262270

263271
Ok(ptr)

library/std/src/sys/personality/dwarf/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// This module is used only by x86_64-pc-windows-gnu for now, but we
66
// are compiling it everywhere to avoid regressions.
77
#![allow(unused)]
8+
#![forbid(unsafe_op_in_unsafe_fn)]
89

910
#[cfg(test)]
1011
mod tests;
@@ -17,7 +18,6 @@ pub struct DwarfReader {
1718
pub ptr: *const u8,
1819
}
1920

20-
#[forbid(unsafe_op_in_unsafe_fn)]
2121
impl DwarfReader {
2222
pub fn new(ptr: *const u8) -> DwarfReader {
2323
DwarfReader { ptr }

0 commit comments

Comments
 (0)