Skip to content

Commit

Permalink
use safer pointer casts
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik authored and gquintard committed Sep 4, 2024
1 parent 8263b6a commit 70f990e
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 74 deletions.
30 changes: 16 additions & 14 deletions src/vcl/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,20 +295,20 @@ unsafe extern "C" fn vfp_pull<T: Transfer>(
let vfe = vfep.as_mut().unwrap();
assert_eq!(vfe.magic, varnish_sys::VFP_ENTRY_MAGIC);

let buf = std::slice::from_raw_parts_mut(ptr as *mut u8, *len as usize);
let buf = std::slice::from_raw_parts_mut(ptr.cast::<u8>(), *len as usize);
if buf.is_empty() {
*len = 0;
return varnish_sys::vfp_status_VFP_OK;
}

let reader = (vfe.priv1 as *mut T).as_mut().unwrap();
let reader = vfe.priv1.cast::<T>().as_mut().unwrap();
match reader.read(buf) {
Err(e) => {
let msg = e.to_string();
// TODO: we should grow a VSL object
let t = varnish_sys::txt {
b: msg.as_ptr() as *const c_char,
e: msg.as_ptr().add(msg.len()) as *const c_char,
b: msg.as_ptr().cast::<c_char>(),
e: msg.as_ptr().add(msg.len()).cast::<c_char>(),
};
varnish_sys::VSLbt((*(*ctxp).req).vsl, varnish_sys::VSL_tag_e_SLT_Error, t);

Expand Down Expand Up @@ -344,7 +344,7 @@ unsafe extern "C" fn wrap_list<S: Serve<T>, T: Transfer>(
detailed: i32,
json: i32,
) {
let mut ctx = Ctx::new(ctxp as *mut varnish_sys::vrt_ctx);
let mut ctx = Ctx::new(ctxp.cast_mut());
let mut vsb = Vsb::new(vsbp);
assert!(!be.is_null());
assert_eq!((*be).magic, varnish_sys::DIRECTOR_MAGIC);
Expand Down Expand Up @@ -372,7 +372,7 @@ unsafe extern "C" fn wrap_pipe<S: Serve<T>, T: Transfer>(
ctxp: *const varnish_sys::vrt_ctx,
be: VCLBackendPtr,
) -> varnish_sys::stream_close_t {
let mut ctx = Ctx::new(ctxp as *mut varnish_sys::vrt_ctx);
let mut ctx = Ctx::new(ctxp.cast_mut());
assert!(!(*ctxp).req.is_null());
assert_eq!((*(*ctxp).req).magic, varnish_sys::REQ_MAGIC);
assert!(!(*(*ctxp).req).sp.is_null());
Expand All @@ -393,7 +393,7 @@ unsafe extern "C" fn wrap_gethdrs<S: Serve<T>, T: Transfer>(
ctxp: *const varnish_sys::vrt_ctx,
be: VCLBackendPtr,
) -> ::std::os::raw::c_int {
let mut ctx = Ctx::new(ctxp as *mut varnish_sys::vrt_ctx);
let mut ctx = Ctx::new(ctxp.cast_mut());
assert!(!be.is_null());
assert_eq!((*be).magic, varnish_sys::DIRECTOR_MAGIC);
assert!(!(*be).vcl_name.is_null());
Expand All @@ -418,8 +418,9 @@ unsafe extern "C" fn wrap_gethdrs<S: Serve<T>, T: Transfer>(

let htc = varnish_sys::WS_Alloc(
(*ctx.raw.bo).ws.as_mut_ptr(),
std::mem::size_of::<varnish_sys::http_conn>() as u32,
) as *mut varnish_sys::http_conn;
size_of::<varnish_sys::http_conn>() as u32,
)
.cast::<varnish_sys::http_conn>();
if htc.is_null() {
ctx.fail(&format!("{}: insuficient workspace", (*backend).get_type()));
return -1;
Expand All @@ -445,13 +446,14 @@ unsafe extern "C" fn wrap_gethdrs<S: Serve<T>, T: Transfer>(
(*htc).content_length = l as isize;
}
};
(*htc).priv_ = Box::into_raw(Box::new(transfer)) as *mut std::ffi::c_void;
(*htc).priv_ = Box::into_raw(Box::new(transfer)).cast::<std::ffi::c_void>();
// build a vfp to wrap the Transfer object if there's something to push
if (*htc).body_status != varnish_sys::BS_NONE.as_ptr() {
let vfp = varnish_sys::WS_Alloc(
(*ctx.raw.bo).ws.as_mut_ptr(),
std::mem::size_of::<varnish_sys::vfp>() as u32,
) as *mut varnish_sys::vfp;
size_of::<varnish_sys::vfp>() as u32,
)
.cast::<varnish_sys::vfp>();
if vfp.is_null() {
ctx.fail(&format!("{}: insuficient workspace", (*backend).get_type()));
return -1;
Expand All @@ -468,7 +470,7 @@ unsafe extern "C" fn wrap_gethdrs<S: Serve<T>, T: Transfer>(
}
Ok(s) => s,
};
(*vfp).name = t.as_ptr() as *const c_char;
(*vfp).name = t.as_ptr().cast::<c_char>();
(*vfp).init = None;
(*vfp).pull = Some(vfp_pull::<T>);
(*vfp).fini = None;
Expand Down Expand Up @@ -562,7 +564,7 @@ unsafe extern "C" fn wrap_finish<S: Serve<T>, T: Transfer>(
// drop the Transfer
let htc = (*(*ctxp).bo).htc;
if !(*htc).priv_.is_null() {
drop(Box::from_raw((*htc).priv_ as *mut T));
drop(Box::from_raw((*htc).priv_.cast::<T>()));
}
(*(*ctxp).bo).htc = ptr::null_mut();

Expand Down
52 changes: 27 additions & 25 deletions src/vcl/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,16 @@ impl IntoVCL<VCL_STRING> for &[u8] {
// try to save some work if the buffer is already in the workspace
// and if it's followed by a null byten
if unsafe {
varnish_sys::WS_Allocated(
WS_Allocated(
ws.raw,
self.as_ptr() as *const c_void,
self.as_ptr().cast::<c_void>(),
self.len() as isize + 1,
) == 1
&& *self.as_ptr().add(self.len()) == b'\0'
} {
Ok(self.as_ptr() as *const c_char)
Ok(self.as_ptr().cast::<c_char>())
} else {
Ok(ws.copy_bytes_with_null(&self)?.as_ptr() as *const c_char)
Ok(ws.copy_bytes_with_null(&self)?.as_ptr().cast::<c_char>())
}
}
}
Expand Down Expand Up @@ -186,8 +186,9 @@ impl<T: IntoVCL<VCL_STRING> + AsRef<[u8]>> IntoVCL<VCL_STRING> for Option<T> {
impl<'a> IntoVCL<VCL_PROBE> for COWProbe<'a> {
fn into_vcl(self, ws: &mut WS) -> Result<VCL_PROBE, String> {
let p = ws
.alloc(std::mem::size_of::<varnish_sys::vrt_backend_probe>())?
.as_mut_ptr() as *mut vrt_backend_probe;
.alloc(size_of::<vrt_backend_probe>())?
.as_mut_ptr()
.cast::<vrt_backend_probe>();
let probe = unsafe { p.as_mut().unwrap() };
probe.magic = varnish_sys::VRT_BACKEND_PROBE_MAGIC;
match self.request {
Expand All @@ -210,8 +211,9 @@ impl<'a> IntoVCL<VCL_PROBE> for COWProbe<'a> {
impl IntoVCL<VCL_PROBE> for Probe {
fn into_vcl(self, ws: &mut WS) -> Result<VCL_PROBE, String> {
let p = ws
.alloc(std::mem::size_of::<varnish_sys::vrt_backend_probe>())?
.as_mut_ptr() as *mut vrt_backend_probe;
.alloc(size_of::<vrt_backend_probe>())?
.as_mut_ptr()
.cast::<vrt_backend_probe>();
let probe = unsafe { p.as_mut().unwrap() };
probe.magic = varnish_sys::VRT_BACKEND_PROBE_MAGIC;
match self.request {
Expand All @@ -235,26 +237,26 @@ impl IntoVCL<VCL_IP> for SocketAddr {
fn into_vcl(self, ws: &mut WS) -> Result<VCL_IP, String> {
unsafe {
let p =
ws.alloc(varnish_sys::vsa_suckaddr_len)?.as_mut_ptr() as *mut varnish_sys::suckaddr;
ws.alloc(vsa_suckaddr_len)?.as_mut_ptr().cast::<suckaddr>();
match self {
std::net::SocketAddr::V4(sa) => {
assert!(!varnish_sys::VSA_BuildFAP(
p as *mut std::ffi::c_void,
varnish_sys::PF_INET as varnish_sys::sa_family_t,
sa.ip().octets().as_slice().as_ptr() as *const std::os::raw::c_void,
SocketAddr::V4(sa) => {
assert!(!VSA_BuildFAP(
p.cast::<std::ffi::c_void>(),
PF_INET as sa_family_t,
sa.ip().octets().as_slice().as_ptr().cast::<c_void>(),
4,
&sa.port().to_be() as *const u16 as *const std::os::raw::c_void,
(&sa.port().to_be() as *const u16).cast::<c_void>(),
2
)
.is_null());
}
std::net::SocketAddr::V6(sa) => {
assert!(!varnish_sys::VSA_BuildFAP(
p as *mut std::ffi::c_void,
varnish_sys::PF_INET6 as varnish_sys::sa_family_t,
sa.ip().octets().as_slice().as_ptr() as *const std::os::raw::c_void,
SocketAddr::V6(sa) => {
assert!(!VSA_BuildFAP(
p.cast::<std::ffi::c_void>(),
PF_INET6 as sa_family_t,
sa.ip().octets().as_slice().as_ptr().cast::<c_void>(),
16,
&sa.port().to_be() as *const u16 as *const std::os::raw::c_void,
(&sa.port().to_be() as *const u16).cast::<c_void>(),
2
)
.is_null());
Expand Down Expand Up @@ -515,14 +517,14 @@ impl IntoRust<Option<SocketAddr>> for VCL_IP {
let port = VSA_Port(self) as u16;

match fam {
varnish_sys::PF_INET => {
let buf: &[u8; 4] = std::slice::from_raw_parts(ptr as *const u8, 4)
PF_INET => {
let buf: &[u8; 4] = std::slice::from_raw_parts(ptr.cast::<u8>(), 4)
.try_into()
.unwrap();
Some(SocketAddr::new(IpAddr::V4(Ipv4Addr::from(*buf)), port))
}
varnish_sys::PF_INET6 => {
let buf: &[u8; 16] = std::slice::from_raw_parts(ptr as *const u8, 16)
PF_INET6 => {
let buf: &[u8; 16] = std::slice::from_raw_parts(ptr.cast::<u8>(), 16)
.try_into()
.unwrap();
Some(SocketAddr::new(IpAddr::V6(Ipv6Addr::from(*buf)), port))
Expand Down
12 changes: 6 additions & 6 deletions src/vcl/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ impl<'a> Ctx<'a> {
log(logtag, msg);
} else {
let t = varnish_sys::txt {
b: msg.as_ptr() as *const c_char,
e: msg.as_ptr().add(msg.len()) as *const c_char,
b: msg.as_ptr().cast::<c_char>(),
e: msg.as_ptr().add(msg.len()).cast::<c_char>(),
};
varnish_sys::VSLbt(p.vsl, logtag.into_u32(), t);
}
Expand All @@ -134,8 +134,8 @@ impl<'a> Ctx<'a> {
ptr: *const c_void,
len: isize,
) -> std::os::raw::c_int {
let v = (priv_ as *mut Vec<&'a [u8]>).as_mut().unwrap();
let buf = std::slice::from_raw_parts(ptr as *const u8, len as usize);
let v = priv_.cast::<Vec<&'a [u8]>>().as_mut().unwrap();
let buf = std::slice::from_raw_parts(ptr.cast::<u8>(), len as usize);
v.push(buf);
0
}
Expand All @@ -154,7 +154,7 @@ impl<'a> Ctx<'a> {
req.vsl.as_mut_ptr(),
req,
Some(chunk_collector),
p as *mut c_void,
p.cast::<c_void>(),
)
} {
0 => Ok(*v),
Expand Down Expand Up @@ -246,7 +246,7 @@ pub fn log(logtag: LogTag, msg: &str) {
logtag.into_u32(),
varnish_sys::vxids { vxid: 0 },
c"%s".as_ptr(),
c_cstring.as_ptr() as *const u8,
c_cstring.as_ptr().cast::<u8>(),
);
}
}
8 changes: 4 additions & 4 deletions src/vcl/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ impl<'a> HTTP<'a> {
let hdr_buf = ws.copy_bytes_with_null(&value)?;
unsafe {
let hd = self.raw.hd.offset(idx as isize);
(*hd).b = hdr_buf.as_ptr() as *const c_char;
(*hd).b = hdr_buf.as_ptr().cast::<c_char>();
/* -1 accounts for the null character */
(*hd).e = hdr_buf.as_ptr().add(hdr_buf.len() - 1) as *const c_char;
(*hd).e = hdr_buf.as_ptr().add(hdr_buf.len() - 1).cast::<c_char>();
let hdf = self.raw.hdf.offset(idx as isize);
*hdf = 0;
}
Expand Down Expand Up @@ -136,7 +136,7 @@ impl<'a> HTTP<'a> {
None
} else {
let e = (*self.raw.hd.offset(idx as isize)).e;
let buf = from_raw_parts(b as *const u8, e.offset_from(b) as usize);
let buf = from_raw_parts(b.cast::<u8>(), e.offset_from(b) as usize);
Some(from_utf8(buf).unwrap())
}
}
Expand Down Expand Up @@ -262,7 +262,7 @@ fn header_from_hd<'a>(txt: *const varnish_sys::txt) -> Option<(&'a str, &'a str)
return None;
}
let e = (*txt).e;
let buf = from_raw_parts(b as *const u8, e.offset_from(b) as usize);
let buf = from_raw_parts(b.cast::<u8>(), e.offset_from(b) as usize);

let colon = buf.iter().position(|x| *x == b':').unwrap();

Expand Down
22 changes: 11 additions & 11 deletions src/vcl/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub unsafe extern "C" fn gen_vdp_init<T: VDP>(
oc,
) {
InitResult::Ok(proc) => {
*priv_ = Box::into_raw(Box::new(proc)) as *mut c_void;
*priv_ = Box::into_raw(Box::new(proc)).cast::<c_void>();
0
}
InitResult::Err(_) => -1, // TODO: log error
Expand All @@ -104,7 +104,7 @@ pub unsafe extern "C" fn gen_vdp_fini<T: VDP>(
return 0;
}
assert_ne!(*priv_, ptr::null_mut());
drop(Box::from_raw(*priv_ as *mut T));
drop(Box::from_raw((*priv_).cast::<T>()));
*priv_ = ptr::null_mut();
0
}
Expand All @@ -124,8 +124,8 @@ pub unsafe extern "C" fn gen_vdp_push<T: VDP>(
varnish_sys::vdp_action_VDP_END => PushAction::End,
_ => return 1, /* TODO: log */
};
let buf = std::slice::from_raw_parts(ptr as *const u8, len as usize);
match (*(*priv_ as *mut T)).push(&mut VDPCtx::new(ctx_raw), out_action, buf) {
let buf = std::slice::from_raw_parts(ptr.cast::<u8>(), len as usize);
match (*(*priv_).cast::<T>()).push(&mut VDPCtx::new(ctx_raw), out_action, buf) {
PushResult::Err => -1, // TODO: log error
PushResult::Ok => 0,
PushResult::End => 1,
Expand All @@ -135,7 +135,7 @@ pub unsafe extern "C" fn gen_vdp_push<T: VDP>(
/// Create a `varnish_sys::vdp` that can be fed to `varnish_sys::VRT_AddVDP`
pub fn new_vdp<T: VDP>() -> varnish_sys::vdp {
varnish_sys::vdp {
name: T::name().as_ptr() as *const c_char,
name: T::name().as_ptr().cast::<c_char>(),
init: Some(gen_vdp_init::<T>),
bytes: Some(gen_vdp_push::<T>),
fini: Some(gen_vdp_fini::<T>),
Expand Down Expand Up @@ -166,7 +166,7 @@ impl<'a> VDPCtx<'a> {
varnish_sys::VDP_bytes(
self.raw,
act as std::os::raw::c_uint,
buf.as_ptr() as *const c_void,
buf.as_ptr().cast::<c_void>(),
buf.len() as isize,
)
} {
Expand Down Expand Up @@ -213,7 +213,7 @@ unsafe extern "C" fn wrap_vfp_init<T: VFP>(
&mut VFPCtx::new(ctx),
) {
InitResult::Ok(proc) => {
vfe.priv1 = Box::into_raw(Box::new(proc)) as *mut c_void;
vfe.priv1 = Box::into_raw(Box::new(proc)).cast::<c_void>();
0
}
InitResult::Err(_) => -1, // TODO: log the error,
Expand All @@ -232,8 +232,8 @@ pub unsafe extern "C" fn wrap_vfp_pull<T: VFP>(
let vfe = vfep.as_mut().unwrap();
assert_eq!(vfe.magic, varnish_sys::VFP_ENTRY_MAGIC);

let buf = std::slice::from_raw_parts_mut(ptr as *mut u8, *len as usize);
let obj = (vfe.priv1 as *mut T).as_mut().unwrap();
let buf = std::slice::from_raw_parts_mut(ptr.cast::<u8>(), *len as usize);
let obj = vfe.priv1.cast::<T>().as_mut().unwrap();
match obj.pull(&mut VFPCtx::new(ctx), buf) {
PullResult::Err => varnish_sys::vfp_status_VFP_ERROR, // TODO: log error
PullResult::Ok(l) => {
Expand All @@ -257,14 +257,14 @@ pub unsafe extern "C" fn wrap_vfp_fini<T: VFP>(ctxp: *mut vfp_ctx, vfep: *mut vf
return;
}

drop(Box::from_raw(vfe.priv1 as *mut T));
drop(Box::from_raw(vfe.priv1.cast::<T>()));
vfe.priv1 = ptr::null_mut();
}

/// Create a `varnish_sys::vfp` that can be fed to `varnish_sys::VRT_AddVFP`
pub fn new_vfp<T: VFP>() -> varnish_sys::vfp {
varnish_sys::vfp {
name: T::name().as_ptr() as *const c_char,
name: T::name().as_ptr().cast::<c_char>(),
init: Some(wrap_vfp_init::<T>),
pull: Some(wrap_vfp_pull::<T>),
fini: Some(wrap_vfp_fini::<T>),
Expand Down
8 changes: 4 additions & 4 deletions src/vcl/vpriv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<T> VPriv<T> {
}

fn get_inner(&mut self) -> Option<&mut InnerVPriv<T>> {
unsafe { (self.ptr.as_mut()?.priv_ as *mut InnerVPriv<T>).as_mut() }
unsafe { self.ptr.as_mut()?.priv_.cast::<InnerVPriv<T>>().as_mut() }
}

pub fn store(&mut self, obj: T) {
Expand All @@ -56,15 +56,15 @@ impl<T> VPriv<T> {
obj: None,
};
(*self.ptr).methods = methods_ptr;
(*self.ptr).priv_ = Box::into_raw(Box::new(inner_priv)) as *mut c_void;
(*self.ptr).priv_ = Box::into_raw(Box::new(inner_priv)).cast::<c_void>();
}
}
let inner_priv = self.get_inner().unwrap();
inner_priv.obj = Some(obj);
}

pub fn as_ref(&self) -> Option<&T> {
let inner = unsafe { ((*self.ptr).priv_ as *mut InnerVPriv<T>).as_ref()? };
let inner = unsafe { (*self.ptr).priv_.cast::<InnerVPriv<T>>().as_ref()? };
inner.obj.as_ref()
}

Expand All @@ -85,7 +85,7 @@ impl<T> VPriv<T> {
}

unsafe extern "C" fn vpriv_free<T>(_: *const varnish_sys::vrt_ctx, ptr: *mut c_void) {
let inner_priv = Box::from_raw(ptr as *mut InnerVPriv<T>);
let inner_priv = Box::from_raw(ptr.cast::<InnerVPriv<T>>());
drop(Box::from_raw(inner_priv.name));
drop(Box::from_raw(inner_priv.methods));
}
Expand Down
Loading

0 comments on commit 70f990e

Please sign in to comment.