Skip to content

Commit c2bce54

Browse files
committed
ZJIT: Replace as usize casts in codegen.rs
The `as` casts are somewhat dangerous since when the type on either side change, it silently becomes a lossy conversion. This is why we have `IntoUsize` as well as other guaranteed lossless conversion utilities in stdlib. Use them. For pointers-to-address, `ptr::addr` is more informative. See also: https://tratt.net/laurie/blog/2021/static_integer_types.html
1 parent f33cd12 commit c2bce54

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

zjit/src/codegen.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ fn gen_entry(cb: &mut CodeBlock, iseq: IseqPtr, function_ptr: CodePtr) -> Result
188188
let (code_ptr, gc_offsets) = asm.compile(cb)?;
189189
assert!(gc_offsets.is_empty());
190190
if get_option!(perf) {
191-
let start_ptr = code_ptr.raw_ptr(cb) as usize;
192-
let end_ptr = cb.get_write_ptr().raw_ptr(cb) as usize;
191+
let start_ptr = code_ptr.raw_addr(cb);
192+
let end_ptr = cb.get_write_ptr().raw_addr(cb);
193193
let code_size = end_ptr - start_ptr;
194194
let iseq_name = iseq_get_location(iseq, 0);
195195
register_with_perf(format!("entry for {iseq_name}"), start_ptr, code_size);
@@ -298,8 +298,8 @@ fn gen_function(cb: &mut CodeBlock, iseq: IseqPtr, function: &Function) -> Resul
298298
let result = asm.compile(cb);
299299
if let Ok((start_ptr, _)) = result {
300300
if get_option!(perf) {
301-
let start_usize = start_ptr.raw_ptr(cb) as usize;
302-
let end_usize = cb.get_write_ptr().raw_ptr(cb) as usize;
301+
let start_usize = start_ptr.raw_addr(cb);
302+
let end_usize = cb.get_write_ptr().raw_addr(cb);
303303
let code_size = end_usize - start_usize;
304304
let iseq_name = iseq_get_location(iseq, 0);
305305
register_with_perf(iseq_name, start_usize, code_size);
@@ -508,7 +508,7 @@ fn gen_objtostring(jit: &mut JITState, asm: &mut Assembler, val: Opnd, cd: *cons
508508
gen_prepare_non_leaf_call(jit, asm, state);
509509
// TODO: Specialize for immediate types
510510
// Call rb_vm_objtostring(iseq, recv, cd)
511-
let ret = asm_ccall!(asm, rb_vm_objtostring, VALUE(jit.iseq as usize).into(), val, (cd as usize).into());
511+
let ret = asm_ccall!(asm, rb_vm_objtostring, VALUE::from(jit.iseq).into(), val, Opnd::const_ptr(cd));
512512

513513
// TODO: Call `to_s` on the receiver if rb_vm_objtostring returns Qundef
514514
// Need to replicate what CALL_SIMPLE_METHOD does
@@ -833,12 +833,12 @@ fn gen_setivar(jit: &mut JITState, asm: &mut Assembler, recv: Opnd, id: ID, val:
833833

834834
fn gen_getclassvar(jit: &mut JITState, asm: &mut Assembler, id: ID, ic: *const iseq_inline_cvar_cache_entry, state: &FrameState) -> Opnd {
835835
gen_prepare_non_leaf_call(jit, asm, state);
836-
asm_ccall!(asm, rb_vm_getclassvariable, VALUE(jit.iseq as usize).into(), CFP, id.0.into(), Opnd::const_ptr(ic))
836+
asm_ccall!(asm, rb_vm_getclassvariable, VALUE::from(jit.iseq).into(), CFP, id.0.into(), Opnd::const_ptr(ic))
837837
}
838838

839839
fn gen_setclassvar(jit: &mut JITState, asm: &mut Assembler, id: ID, val: Opnd, ic: *const iseq_inline_cvar_cache_entry, state: &FrameState) {
840840
gen_prepare_non_leaf_call(jit, asm, state);
841-
asm_ccall!(asm, rb_vm_setclassvariable, VALUE(jit.iseq as usize).into(), CFP, id.0.into(), val, Opnd::const_ptr(ic));
841+
asm_ccall!(asm, rb_vm_setclassvariable, VALUE::from(jit.iseq).into(), CFP, id.0.into(), val, Opnd::const_ptr(ic));
842842
}
843843

844844
/// Look up global variables
@@ -975,7 +975,7 @@ fn gen_load_ivar_embedded(asm: &mut Assembler, self_val: Opnd, id: ID, index: u1
975975
// See ROBJECT_FIELDS() from include/ruby/internal/core/robject.h
976976

977977
asm_comment!(asm, "Load embedded ivar id={} index={}", id.contents_lossy(), index);
978-
let offs = ROBJECT_OFFSET_AS_ARY as i32 + (SIZEOF_VALUE * index as usize) as i32;
978+
let offs = ROBJECT_OFFSET_AS_ARY as i32 + (SIZEOF_VALUE * index.as_usize()) as i32;
979979
let self_val = asm.load(self_val);
980980
let ivar_opnd = Opnd::mem(64, self_val, offs);
981981
asm.load(ivar_opnd)
@@ -990,7 +990,7 @@ fn gen_load_ivar_extended(asm: &mut Assembler, self_val: Opnd, id: ID, index: u1
990990
let tbl_opnd = asm.load(Opnd::mem(64, self_val, ROBJECT_OFFSET_AS_HEAP_FIELDS as i32));
991991

992992
// Read the ivar from the extended table
993-
let ivar_opnd = Opnd::mem(64, tbl_opnd, (SIZEOF_VALUE * index as usize) as i32);
993+
let ivar_opnd = Opnd::mem(64, tbl_opnd, (SIZEOF_VALUE * index.as_usize()) as i32);
994994
asm.load(ivar_opnd)
995995
}
996996

@@ -1113,7 +1113,7 @@ fn gen_send(
11131113
}
11141114
asm.ccall(
11151115
rb_vm_send as *const u8,
1116-
vec![EC, CFP, (cd as usize).into(), VALUE(blockiseq as usize).into()],
1116+
vec![EC, CFP, Opnd::const_ptr(cd), VALUE::from(blockiseq).into()],
11171117
)
11181118
}
11191119

@@ -1136,7 +1136,7 @@ fn gen_send_forward(
11361136
}
11371137
asm.ccall(
11381138
rb_vm_sendforward as *const u8,
1139-
vec![EC, CFP, (cd as usize).into(), VALUE(blockiseq as usize).into()],
1139+
vec![EC, CFP, Opnd::const_ptr(cd), VALUE::from(blockiseq).into()],
11401140
)
11411141
}
11421142

@@ -1157,7 +1157,7 @@ fn gen_send_without_block(
11571157
}
11581158
asm.ccall(
11591159
rb_vm_opt_send_without_block as *const u8,
1160-
vec![EC, CFP, (cd as usize).into()],
1160+
vec![EC, CFP, Opnd::const_ptr(cd)],
11611161
)
11621162
}
11631163

@@ -1263,7 +1263,7 @@ fn gen_invokeblock(
12631263
}
12641264
asm.ccall(
12651265
rb_vm_invokeblock as *const u8,
1266-
vec![EC, CFP, (cd as usize).into()],
1266+
vec![EC, CFP, Opnd::const_ptr(cd)],
12671267
)
12681268
}
12691269

@@ -1285,7 +1285,7 @@ fn gen_invokesuper(
12851285
}
12861286
asm.ccall(
12871287
rb_vm_invokesuper as *const u8,
1288-
vec![EC, CFP, (cd as usize).into(), VALUE(blockiseq as usize).into()],
1288+
vec![EC, CFP, Opnd::const_ptr(cd), VALUE::from(blockiseq).into()],
12891289
)
12901290
}
12911291

@@ -1544,7 +1544,7 @@ fn gen_is_method_cfunc(jit: &JITState, asm: &mut Assembler, val: lir::Opnd, cd:
15441544
unsafe extern "C" {
15451545
fn rb_vm_method_cfunc_is(iseq: IseqPtr, cd: *const rb_call_data, recv: VALUE, cfunc: *const u8) -> VALUE;
15461546
}
1547-
asm_ccall!(asm, rb_vm_method_cfunc_is, VALUE(jit.iseq as usize).into(), (cd as usize).into(), val, (cfunc as usize).into())
1547+
asm_ccall!(asm, rb_vm_method_cfunc_is, VALUE::from(jit.iseq).into(), Opnd::const_ptr(cd), val, Opnd::const_ptr(cfunc))
15481548
}
15491549

15501550
fn gen_is_bit_equal(asm: &mut Assembler, left: lir::Opnd, right: lir::Opnd) -> lir::Opnd {
@@ -1889,7 +1889,7 @@ fn param_opnd(idx: usize) -> Opnd {
18891889
/// Inverse of ep_offset_to_local_idx(). See ep_offset_to_local_idx() for details.
18901890
pub fn local_idx_to_ep_offset(iseq: IseqPtr, local_idx: usize) -> i32 {
18911891
let local_size = unsafe { get_iseq_body_local_table_size(iseq) };
1892-
local_size_and_idx_to_ep_offset(local_size as usize, local_idx)
1892+
local_size_and_idx_to_ep_offset(local_size.as_usize(), local_idx)
18931893
}
18941894

18951895
/// Convert the number of locals and a local index to an offset from the EP
@@ -2005,8 +2005,8 @@ c_callable! {
20052005
rb_set_cfp_sp(cfp, sp);
20062006

20072007
// Fill nils to uninitialized (non-argument) locals
2008-
let local_size = get_iseq_body_local_table_size(iseq) as usize;
2009-
let num_params = get_iseq_body_param_size(iseq) as usize;
2008+
let local_size = get_iseq_body_local_table_size(iseq).as_usize();
2009+
let num_params = get_iseq_body_param_size(iseq).as_usize();
20102010
let base = sp.offset(-local_size_and_idx_to_bp_offset(local_size, num_params) as isize);
20112011
slice::from_raw_parts_mut(base, local_size - num_params).fill(Qnil);
20122012
}

zjit/src/virtualmem.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl CodePtr {
8686

8787
/// Get the address of the code pointer.
8888
pub fn raw_addr(self, base: &impl CodePtrBase) -> usize {
89-
self.raw_ptr(base) as usize
89+
self.raw_ptr(base).addr()
9090
}
9191

9292
/// Get the offset component for the code pointer. Useful finding the distance between two

0 commit comments

Comments
 (0)