HeapCrash / Challenges / Character driver read()

Character driver read()

A Linux character driver hands the latest sensor frame to userspace through read(). An AI assistant wrote it. cat works on the bench. In the field the kernel oopses now and then, and a logging script that reads it with cat never exits.

The code

Embedded Linux C. It compiles. There are 3 real bugs in it: the kind that pass a bench test and fail in the field.

static DEFINE_SPINLOCK(frame_lock);
static u8 frame[64];
static size_t frame_len;

static irqreturn_t sensor_irq(int irq, void *dev)
{
    spin_lock(&frame_lock);
    frame_len = sensor_fifo_read(frame, sizeof(frame));
    spin_unlock(&frame_lock);
    return IRQ_HANDLED;
}

static ssize_t sensor_read(struct file *f, char __user *buf,
                           size_t count, loff_t *off)
{
    spin_lock(&frame_lock);
    memcpy(buf, frame, frame_len);
    spin_unlock(&frame_lock);
    return count;
}

What an interviewer expects

The bugs, reference fixes and the follow-up question are in the practice app, after you've tried it.

Companies that test this

Matched on the skills each company's embedded roles emphasise.

Related challenges

Flag the lines, say what breaks, then fix it and build it with clang in your browser.

Find the bugs