Coder Social home page Coder Social logo

Comments (6)

jmbarbier avatar jmbarbier commented on June 7, 2024

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.

Boddlnagg avatar Boddlnagg commented on June 7, 2024

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.

Boddlnagg avatar Boddlnagg commented on June 7, 2024

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 MIDIPackets are constructed that are not aligned.

from coremidi.

Boddlnagg avatar Boddlnagg commented on June 7, 2024

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.

Boddlnagg avatar Boddlnagg commented on June 7, 2024

@jmbarbier @arteme @oilcake Can you confirm that you're seeing this on Intel processors?

from coremidi.

oilcake avatar oilcake commented on June 7, 2024

@jmbarbier @arteme @oilcake Can you confirm that you're seeing this on Intel processors?

Yep, my one is intel.

from coremidi.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.