{"slug": "the-unsafe-rust-an-ai-wrote-me-last-week", "title": "The Unsafe Rust an AI Wrote Me Last Week", "summary": "A developer reported that an AI-generated lock-free SPSC ring buffer in Rust compiled and passed unit tests but contained a torn read due to incorrect memory ordering. The bug, which would surface under load or on ARM, stemmed from the model's inability to infer concurrency invariants, highlighting risks of using AI for unsafe code. The developer emphasized that human review remains essential for catching such subtle issues.", "body_md": "I told a model to write a lock-free SPSC ring buffer in Rust. The result compiled first time, passed my unit tests, and looked utterly reasonable. It also contained a torn read that would only appear at scale.\n\nHere is the interesting part, snipped down a bit:\n\n``` php\npub fn push(&self, value: T) -> bool {\n    let head = self.head.load(Ordering::Relaxed);\n    let next = (head + 1) % self.capacity;\n    if next == self.tail.load(Ordering::Relaxed) {\n        return false; // full\n    }\n    unsafe {\n        (*self.buffer.get()).as_mut_ptr().add(head).write(value);\n    }\n    self.head.store(next, Ordering::Relaxed);\n    true\n}\n```\n\nNote the orderings: all `Relaxed`\n\n.\n\nOn the producer side: write data to slot `head`\n\n, then store the updated head. On the consumer side: read the current head, then read data from that slot. The problem is that with `Relaxed`\n\nordering, nothing guarantees those two producer operations become visible in that order to the consumer.\n\nThe consumer can observe the updated head pointer before the data write actually becomes visible to it.\n\nSo the consumer reads head, sees \"yes there's data\", goes to read it, and instead reads whatever garbage is currently sitting in that memory slot. That is the torn read. Under x86 with its relatively strong memory ordering it might work for a long time. On ARM, or simply under enough load, that ordering will break.\n\nThe fix is simple:\n\n```\n// producer publishes the data with Release\nself.head.store(next, Ordering::Release);\n\n// consumer reads the index with Acquire\nlet head = self.head.load(Ordering::Acquire);\n```\n\nThe Release store and Acquire load order the data write before the index update the consumer synchronizes on, which closes the race.\n\nThe point, the actual point, is this. The bug did not exist because the model is stupid. It exists because the model does not understand what this code is for. It looked at many ring buffers in its training data, most of them using `Relaxed`\n\n, and saw `Relaxed`\n\nscattered around in contexts where it happened to be safe for other reasons.\n\nIt cannot tell the difference between \"Relaxed happened to work here because of some higher-level guarantee\" and \"Relaxed causes a data race in this pattern\". That difference lives entirely in the memory model and the actual concurrency of the program. It is not visible in the shape of the code.\n\nThis is why you should not let these things generate `unsafe`\n\n. Safe Rust is no problem, the compiler says no. But `unsafe`\n\nis exactly where invariants the compiler cannot see and the model has no concept of become critical. You get something that compiles and passes your basic tests, with the bug merely scheduled for production to unearth.\n\nI have written enough of these by hand that I tend to catch them in review. That is the real job now. Not writing the code, but knowing what questions the code has to answer, and noticing when the answers aren't there.", "url": "https://wpnews.pro/news/the-unsafe-rust-an-ai-wrote-me-last-week", "canonical_source": "https://dev.to/chronocoders/the-unsafe-rust-an-ai-wrote-me-last-week-ob0", "published_at": "2026-09-01 01:21:17+00:00", "updated_at": "2026-09-01 01:51:52.433682+00:00", "lang": "en", "topics": ["artificial-intelligence", "developer-tools", "ai-safety"], "entities": [], "alternates": {"html": "https://wpnews.pro/news/the-unsafe-rust-an-ai-wrote-me-last-week", "markdown": "https://wpnews.pro/news/the-unsafe-rust-an-ai-wrote-me-last-week.md", "text": "https://wpnews.pro/news/the-unsafe-rust-an-ai-wrote-me-last-week.txt", "jsonld": "https://wpnews.pro/news/the-unsafe-rust-an-ai-wrote-me-last-week.jsonld"}}