|
|
Log in / Subscribe / Register

MISRA C is not provably safe.

MISRA C is not provably safe.

Posted Nov 19, 2025 14:22 UTC (Wed) by tialaramex (subscriber, #21167)
In reply to: MISRA C might be provably safe. by gmatht
Parent article: Rust in Android: move fast and fix things (Google Security Blog)

MISRA-C is basically a glorified style guide so, no, it's definitely not "provably safe" in a useful sense.

MISRA-C;2012 R17.5 describes an ambition C cannot fulfil. It might almost as well say "Programs shall not have bugs". For rule 17.5 it wants to specify that the array size is part of the parameter type, but in C that is nonsense, the array types always decay to pointers here. You can write this out in C but C compilers will ignore your intent.

You can actually do what MISRA-C calls for here in Rust, and I have, exactly once, because it's very rarely actually useful. The case where I did took memory pages, they're always exactly 4096 bytes long (no, I don't handle huge pages), and I specified that the function takes only (references to) arrays of precisely 4096 bytes, so for Rust passing some other size won't compile for the same reason you can't pass a boolean to a function which takes a string - they're different types. C doesn't provide any such thing and MISRA can't imagine it into existence.

Obeying MISRA's style is a sign of compliance, which is a useful trait for safety systems, but to a considerable extent it's functioning as a Brown M&M check, it's not a proof of anything.


to post comments

MISRA C is not provably safe.

Posted Nov 20, 2025 12:10 UTC (Thu) by khim (subscriber, #9252) [Link] (2 responses)

> You can write this out in C but C compilers will ignore your intent.

C compiler would, but there are other programs that wouldn't. It's the same story as Ada and SPARK, Rust and mrustc, etc.

The big question is whether full-blown checker for MISRA C exists or may even theoretically exist. PVS-Studio claims 80% coverage.

> they're always exactly 4096 bytes long (no, I don't handle huge pages)

What about 16KiB pages? MacOS uses them and Android moves there, too.

> but to a considerable extent it's functioning as a Brown M&M check, it's not a proof of anything

You can as well only use Rust with mrustc and claim that all these lifetime markups are just a wishful thinking. The big question about MISRA C is how much enforcement can be done automatically with existing tools, not whether C decays pointers. Sure, it decays them but for the MISRA C it's not a problem if you have an external checker.

MISRA C is not provably safe.

Posted Nov 21, 2025 10:10 UTC (Fri) by taladar (subscriber, #68407) [Link] (1 responses)

Are you seriously comparing the situation in C where everyone is using compilers that do not do these checks along with rumored full blown checkers that basically no C programmer ever tried with people on Rust using the standard compiler that everyone uses and a strawman user who uses mrustc, a compiler only used for bootstrapping?

MISRA C is not provably safe.

Posted Nov 21, 2025 11:52 UTC (Fri) by khim (subscriber, #9252) [Link]

We are talking about MISRA C here, isn't it?

All companies that I knew about with MISRA C aspirations have verifiers that they use to ensure [partial] compliance. Yes, these checkers do have bugs and don't catch 100% of violations, but so does Rust.

Thus I have no idea what are you talking about here, sorry: MISRA C and Rust approaches are very comparable.

Why not?

Posted Nov 21, 2025 14:17 UTC (Fri) by gmatht (guest, #58961) [Link] (1 responses)

The major innovation in Rust is the borrow checker. This does not accept all valid programs but (should) only accept valid programs.

Writing a general borrow checker for C is hard because C lacks the lifetime annotations and valid standards compliant C code may not conform to the borrow checkers expectations.

However MISRA-C is much more pedantic than the Rust borrow-checker. Since dynamic allocations are banned, unless all lifetimes are static, the program is not valid MISRA-C. And if all lifetimes are static, lifetime annotations are redundant, and lifetime checks are redundant.

There are lots of edge cases to consider, for example, if race conditions could result in memory unsafety. However, it seems that if a compiler is allowed to reject non-MISRA-C this would make it vastly easier to only accept provably safe programs.

Why not?

Posted Nov 21, 2025 15:21 UTC (Fri) by excors (subscriber, #95769) [Link]

> However MISRA-C is much more pedantic than the Rust borrow-checker. Since dynamic allocations are banned, unless all lifetimes are static, the program is not valid MISRA-C. And if all lifetimes are static, lifetime annotations are redundant, and lifetime checks are redundant.

MISRA C still allows automatic storage duration (i.e. stack variables), so not all lifetimes are static, and you can get bugs when a stack pointer escapes the function's scope.

MISRA C has the rule "The address of an object with automatic or thread-local storage shall not be copied to another object that persists after the first object has ceased to exist", but that's an explicitly undecidable rule, so it's basically saying "your program must not be buggy".

Some static checkers apparently detect simple violations of that rule (e.g. assigning the address to a global variable or returning it from the function that allocated it: https://www.mathworks.com/help/bugfinder/ref/misrac2023ru...), but I don't believe it's possible to detect all violations in otherwise-compliant code without rejecting plenty of reasonable and fully-compliant code.


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds