Comments (6)
Same bug here, fixed it by adding read_unaligned
for those lines
#[inline]
pub unsafe fn MIDIPacketNext(pkt: *const MIDIPacket) -> *const MIDIPacket {
// Get pointer to potentially unaligned data without triggering undefined behavior
// addr_of does not require creating an intermediate reference to unaligned data.
let ptrx = ptr::addr_of!((*pkt).data) as *const u8;
let rawl = ptr::addr_of!((*pkt).length) as *const u16;
let offset = rawl.read_unaligned() as isize;
if cfg!(any(target_arch = "arm", target_arch = "aarch64")) {
// MIDIPacket must be 4-byte aligned on ARM
((ptrx.offset(offset + 3) as usize) & !(3usize)) as *const MIDIPacket
} else {
ptrx.offset(offset) as *const MIDIPacket
}
}
#[inline]
pub unsafe fn MIDIEventPacketNext(pkt: *const MIDIEventPacket) -> *const MIDIEventPacket {
// Get pointer to potentially unaligned data without triggering undefined behavi
// or
// addr_of does not require creating an intermediate reference to unaligned data.
let ptrx = ptr::addr_of!((*pkt).words) as *const u8;
let rawwc = ptr::addr_of!((*pkt).wordCount) as *const u32;
let wc = rawwc.read_unaligned() as usize;
let offset = (wc * mem::size_of::<u32>()) as isize;
if cfg!(any(target_arch = "arm", target_arch = "aarch64")) {
// MIDIEventPacket must be 4-byte aligned on ARM
((ptrx.offset(offset + 3) as usize) & !(3usize)) as *const MIDIEventPacket
} else {
ptrx.offset(offset) as *const MIDIEventPacket
}
}
Take this as a hack without any warranty ("it works for me"), i have no knowledge of the internals used here...
from coremidi.
This has repeatedly hit users of midir
. I think the proposed fix by @jmbarbier is correct, but that code is actually part of https://github.com/jonas-k/coremidi-sys.
@jmbarbier Maybe you can open a PR against coremidi-sys?
from coremidi.
Actually I'm not sure whether that fix is correct. MIDIPacket
should be 4-byte aligned on ARM (see also #9), so read_unaligned
shouldn't be necessary. The error must be somewhere else where MIDIPacket
s are constructed that are not aligned.
from coremidi.
Okay, I think the problem is this: "The alignment requirements of MIDIPacket may differ between CPU architectures. On Intel and PowerPC, MIDIPacket is unaligned. On ARM, MIDIPacket must be 4-byte aligned."
(https://github.com/phracker/MacOSX-SDKs/blob/041600eda65c6a668f66cb7d56b7d1da3e8bcc93/MacOSX10.15.sdk/System/Library/Frameworks/CoreMIDI.framework/Versions/A/Headers/MIDIServices.h#L420-L423)
So I'm assuming that this issue occurs on Intel processors? That would make sense because then Rust sees an unaligned MIDIPacket
that should be 4-byte aligned but isn't, so the read_unaligned
is needed to not trigger Rust's UB check. On ARM this would not be needed and would actually make performance worse, because on ARM the packets are aligned and explicit unaligned reads would generate worse machine code.
Furthermore, this can only affect MIDIPacket
, not MIDIEvent
(its length will always be a multiple of 4 bytes), so the fix does not need to be applied to MIDIEventPacketNext
.
from coremidi.
@jmbarbier @arteme @oilcake Can you confirm that you're seeing this on Intel processors?
from coremidi.
@jmbarbier @arteme @oilcake Can you confirm that you're seeing this on Intel processors?
Yep, my one is intel.
from coremidi.
Related Issues (18)
- Serious safety problems with callbacks HOT 1
- Handle MIDIGetDestination/MIDIGetSource failure
- Why not use MIDIPacketListAdd? HOT 5
- Client can be dropped while port is open HOT 1
- Cloning Destination/Source? HOT 4
- Client is not Send HOT 9
- Notifications callback not called when devices are added or removed
- SIGILL in Tests on macOS 10.14
- CoreFoundation objects over-released in Properties
- Undefined Behavior in `Client` and `Properties`
- Use the block crate and functions that take blocks instead of boxing callbacks. HOT 4
- Client::new() returns error -50 after 6 calls HOT 1
- Is Human Readable midi data support HOT 1
- Issue receiving midi packets when using CoreMIDI with Tauri HOT 5
- MIDIClientDispose causes trouble on iOS HOT 1
- Report a vulnerability in chris-zen/coremidi HOT 2
- PacketBuffer mutates self HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coremidi.