Skip to content

Commit

Permalink
Box<dyn Any> to sidestep Arc<[T]>provenance issues
Browse files Browse the repository at this point in the history
1 extra alloc and indirection won't kill me. probably.
Closes #18
  • Loading branch information
MaulingMonkey committed Oct 9, 2022
1 parent d2d14db commit 4d767b3
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 21 deletions.
17 changes: 1 addition & 16 deletions crates/thindx-xaudio2/src/xa28/ixaudio2sourcevoice_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,7 @@ impl<Sample, Context: Send + Sync + Sized + 'static> IXAudio2SourceVoiceTyped<Sa

b.pContext = Box::into_raw(Box::new(SourceBuffer::<Context> {
context,
audio_len: audio_data.len(),
audio_data: {
// ⚠️ WARNING: easy to muck up provenance of ArcInner<[Sample]> here.
let raw : *const [Sample] = Arc::into_raw(audio_data);
let raw : *const Sample = unsafe { (*raw).as_ptr() };
raw.cast()
},
audio_free: |data, len| {
// 🐞 BUG: core::slice::from_raw_parts here incorrectly narrows provenance from ArcInner<[Sample]> to [Sample] here.
// However, core::ptr::from_raw_parts is not yet stable: https://github.com/rust-lang/rust/issues/81513
// Local tracking issue: https://github.com/MaulingMonkey/thindx-xaudio2/issues/18
let audio_data : *const [Sample] = unsafe { core::slice::from_raw_parts(data as *const Sample, len) };

let audio_data = unsafe { Arc::from_raw(audio_data) };
drop(audio_data);
},
_audio_data: Box::new(audio_data),
})).cast();

unsafe { self.SubmitSourceBuffer(&b, null()) }.succeeded()
Expand Down
6 changes: 2 additions & 4 deletions crates/thindx-xaudio2/src/xa28/source_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[allow(unused_imports)] use super::*;
#[allow(unused_imports)] use super::xaudio2::sys::*;
use core::ffi::c_void;
use std::any::Any;



Expand All @@ -10,7 +10,5 @@ use core::ffi::c_void;
/// * [VoiceCallback::BufferContext] parameters
pub(crate) struct SourceBuffer<Context: Send + Sync + Sized + 'static> {
pub(crate) context: Context,
pub(crate) audio_data: *const c_void,
pub(crate) audio_len: usize,
pub(crate) audio_free: unsafe fn(*const c_void, usize),
pub(crate) _audio_data: Box<dyn Any>, // for keepalive only
}
1 change: 0 additions & 1 deletion crates/thindx-xaudio2/src/xa28/voice_callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ impl<VC: VoiceCallback> VoiceCallbackWrapper<VC> {
let this : &Self = unsafe { &*sptr::from_exposed_addr(sptr::Strict::addr(this)) };
let buffer_context = *unsafe { Box::from_raw(buffer_context as *mut SourceBuffer<VC::BufferContext>) };
this.callbacks.on_buffer_end(buffer_context.context);
unsafe { (buffer_context.audio_free)(buffer_context.audio_data, buffer_context.audio_len) }
})
}

Expand Down

0 comments on commit 4d767b3

Please sign in to comment.