cmajor-lang/cmajor

[Bug] Recursing on a mutable vector crashes the host

eulervoid opened this issue · 2 comments

Hello,
i've been implementing the reverb from Signalsmiths ADC Talk and found that when i try to recurse on a vector, calling a generic function with slices of the vector, cmajor crashes. When i do the same with an array, the processor works as expected. I understand that i might be misusing the vector here, but the expected behaviour would be getting an error message, without crashing the host. As a more general question: are all crashes of cmajor considered a bug? Intuitively i'd say yes, but not sure if you are aiming for that.

Here's a more or less minimal version of the code causing the crash:

graph Crash [[ main ]] 
{
    input stream float<8> in;
    output stream float<8> out;

    node matrix = Hadamard();

    connection in -> matrix -> out;
}

processor Hadamard(using SampleType = float32, int N = 8)
{
    input  stream SampleType<N> in;
    output stream SampleType<N> out;

    let scalingFactor = sqrt(1.0f/SampleType(N));

    void recursiveUnscaled<Frame>(Frame& frame)
    {
        static_assert(frame.isVector && frame.elementType.isFloat, "input must be a float<N>");
    
        if const (frame.isVector && frame.elementType.isFloat && frame.size > 1) {
            let halfSize = frame.size / 2;

            recursiveUnscaled(frame[0:halfSize]);
            recursiveUnscaled(frame[halfSize:frame.size]);

            for (wrap<halfSize> i) {
                SampleType a = frame[i];
                SampleType b = frame[wrap<frame.size>(i + halfSize)];
                frame[i] = (a + b);
                frame[wrap<frame.size>(i + halfSize)] = (a - b);
            }
        }
    }

    void main() 
    {   
        loop {
            SampleType<N> frame = in;
            recursiveUnscaled(frame);
            out <- frame * scalingFactor;
            advance();
        }
    }
}

And here's the error from the command line:

Audio device: Built-in Output (CoreAudio)
48000Hz, 128 frames, latency: 18ms, output channels: (0, 1), input channels: (0, 1)
Loading: Crash.cmajorpatch
Assertion failed: (isValidOperands(Vec, Elt, Index) && "Invalid insertelement instruction operands!"), function InsertElementInst, file Instructions.cpp, line 1904.
[1]    55207 abort      cmaj play Crash.cmajorpatch

I'm on OSX 12.5.6, running cmajor 0.9.2051

Thanks for the bug report.

Yes, any failure in the compiler or runtime is considered an error by us. In this case, an assertion in the code has triggered so i'll debug the problem from here and let you know how we get on.

Just closing some old issues - this was fixed long ago, I think!