|
|
Log in / Subscribe / Register

Safety certification for open-source systems

By Daroc Alden
June 3, 2025

Linaro Connect

This year's Linaro Connect in Lisbon, Portugal featured a number of talks about the use of open-source components in safety-critical systems. Kate Stewart gave a keynote on the topic on the first day of the conference. In it, she highlighted several projects that have been working to pursue safety certification and spoke about the importance of being able to trace software's origins to safety. In a talk on the second day, Roberto Bagnara shared his experience with working on one of those projects, the Xen hypervisor, to conform to a formal set of rules for safety-critical code.

Automotive open source

Stewart is the owner of a 1996 Volvo car. Its air conditioning failed earlier this year, she explained, so with no replacement parts to be found and summer in Texas fast approaching, she recently bought a new car. Her original car is just hardware, she said, but now software is playing an increasingly important role in modern automobiles.

The difference is obvious just from comparing the dashboards: Stewart's original Volvo has physical dials and knobs, but her new Honda has displays and touchscreens. The obvious differences aren't what worry her, though. New cars also have backup cameras, radar, assistive lane following, and many more electronic features that have a real impact on the safety of the vehicle. A car is, after all, "a couple of ton piece of equipment that could easily kill someone". All of this new hardware needs to be powered by software β€” and a lot of that software is open source.

[Kate Stewart]

So how can we ensure that a complicated system like a modern car is safe? Traditional supply chains for safety-critical systems have ensured this using "needs" that are fulfilled with "requirements", Stewart explained. A component is designed out of smaller components. Assuming that the smaller components meet some standard, the engineer designing the larger component is responsible for making that component safe. The assumptions that a larger component makes about its constituents are called needs; the standards that a smaller component meets are called requirements. Every need is matched with a requirement, to produce a graph of components where, at every step, it is clear exactly what is required for the overall system to be safe.

This doesn't really work for open source. 59% of the software in Stewart's new car is open source, she said, according to a disclosure by Honda. That's great, but those components may not be designed for use in safety-critical systems. Even if they are, they're exceedingly unlikely to have reliable documentation, certified by an engineer, that lays out their safety needs. That's not something that open source has really been designed to handle.

So how can these gaps be addressed? The first step, Stewart said, is just to know what components are actually going into a system. For physical hardware, this is usually called a bill of materials (BOM); a software bill of materials (SBOM) is similar. The SBOM lists the exact version of every dependency used by a piece of software.

Safety standards for software are mostly concerned with trying to "minimize and mitigate systemic faults in the code base for an application". In order to do that, they need some way to track what the components in a system are, what faults have been discovered with them, which bugs are known, and how all of this changes over time. Today, "it's being done, quite frankly, with spreadsheets", which is "really inefficient".

Since 2009, Stewart has been involved with the System Package Data Exchange (SPDX) project, which creates standardized formats and tools for exchanging and processing SBOM information. For version 3.0 of the SPDX format, she has been specifically working to include the information needed by someone trying to comply with a safety standard. In particular, SPDX 3.0 focuses less on a static document describing a particular component, and more on relationships between components. This allows SBOM tooling to capture all kinds of information about source code, build information, licensing, datasets, AI models, provenance, and everything else that goes into understanding what is part of a software system.

The next hurdle is actually persuading open-source projects to generate SBOM information. Stewart has been trying to make this as easy as possible with SPDX tooling, and many build systems can be configured to generate SBOMs with few changes. Still, like with any change in open source, it does require someone to actually do the work. Once a project has an SBOM, there is existing tooling, from the SPDX project and elsewhere, that can process the SBOM to produce various useful reports or track changes over time.

There are more potential next steps. Several projects, especially in the embedded space, have been working to be formally certified under various safety standards, including ELISA (where Stewart serves on the technical steering committee), Zephyr, Xen, and Yocto. But adopting SBOMs is a good place to start β€” and Stewart concluded her talk with the hope that "we'll get there soon."

MISRA compliance in Xen

MISRA, the motor industry software reliability association, is perhaps best known for its extensive set of rules on how to produce high quality software, the MISRA C guidelines. Bagnara is a member of the MISRA committee, and works with bugseng, a company that produces tools to help with MISRA compliance, among other things.

The biggest benefit of following MISRA's coding guidelines comes when a project enforces them from the beginning, Bagnara said, but that is often not possible. Especially when an open-source project is started and only later incorporated into a safety-critical system.

Open-source maintainers generally take pride in their software; many projects have existing standards for their code. The problem is that these existing standards rarely match up with what a functional-safety standard mandates. In many cases, artifacts showing that these standards have been followed β€” such as tests or documentation on requirements β€” are also missing. This is understandable, since open-source projects are largely driven by volunteers, only a subset of whom will care about safety qualification.

There are also more minor problems. For example, open-source software is often highly configurable, and any given project only cares about the safety of its specific configuration. Also, development of open-source software often moves much faster than traditional safety-critical software does, which makes keeping up to date difficult.

So, faced with this dilemma, with the obvious benefit and utility of open-source software on the one hand, and its attendant challenges on the other, what can be done? Well, we could give up on certifying the software itself, Bagnara said. For example, a project could instead qualify some kind of monitor or hypervisor that ensures the correct operation of the rest of the software. Better than that would be to fork the project. Everybody wants to avoid permanent forks, but a temporary fork is probably inevitable when working on a large change like safety qualification.

The most challenging and most rewarding approach, however, is "retrofitting safety", a topic that he has been studying for some time. Projects should create or adapt a coding standard after the fact, he said, and let the subset of volunteers who care about such things do the work of getting the existing code up to snuff.

That's what the Xen hypervisor project chose to do. As a hypervisor, Xen's safety underlies the safety of many other components in products that incorporate it. In order for Xen to be used in safety-critical systems, Bagnara has been working to bring Xen's existing coding guidelines in line with the MISRA C standard.

There are a lot of misconceptions about MISRA, Bagnara said. Although he is a member of the MISRA committee, he emphasized that his personal responses to these common misconceptions should not be understood as the official position of the committee. People frequently think that the MISRA rules are too strict, he said, to the detriment of actually making code that works. That's not the case; for MISRA, code quality always comes first. If a rule makes it harder to write quality code for some reason, there is a "deviation" process that lets the developer justify departing from the MISRA guidelines.

An example that came up while working on Xen was MISRA rule 10.1 ("Operands shall not be of an inappropriate essential type"). Among other things, the rule disallows some implicit casts, which are a frequent source of subtle errors in C programs. But there are implicit casts that are safe; in Xen, this mostly appeared in the form of value-preserving conversions of integer constants. Rather than pointlessly requiring all of these places in the code to be marked with a manual cast, "rule 10.1 can be tailored by regarding those instances as safe", Bagnara explained, as long as there is a tool that can be used to check the places where the implicit casts are used.

Another common complaint he sees is that MISRA rules mix too many concerns under a single rule, and that this makes them complicated. "We have more than 200 rules", he explained. "We don't want 2,000 rules." So each rule has to be fairly general, to cover all of the relevant cases.

The general nature of the rules is also a reason some projects will need to deviate from them. Xen uses a number of bit-twiddling tricks, as might be expected from low-level software. For example, there were four places in the Xen code base that used this trick, which contravenes a MISRA rule, without explanation:

    variable &= -variable

This construction isolates the least-significant set bit of a value by using the fact that in the common two's complement representation of numbers, the least-significant set bit is the only one shared by both a number and its negation. Bagnara moved it into a macro with a meaningful name and an explanatory comment. That macro is covered by a global deviation that allows the project to use it wherever it is needed; the code is now easier to read for anyone who hasn't seen this particular construct before.

This kind of thing is generally needed in order for an upstream project to accept changes, he said. Maintainers don't want to accept lots of tiny changes in the name of MISRA compliance; they would much rather accept some documentation and additional tooling to justify and check the needed deviations from MISRA rules, instead.

In some cases, that did require making improvements to the proprietary static analyzer that Bagnara works on and with, ECLAIR. For existing high-quality code such as Xen, he said, someone working on MISRA compliance can expect to create more deviations than in a green-field project, because the code is likely to be correct already. Once those deviations have been accounted for and checked, however, the remaining problems highlighted by MISRA rules stand out even more. In the end, attempting to adopt MISRA for Xen did actually improve the code.

Today, Xen is "nearly 100% MISRA compliant". As always, the last portion of the work will be the hardest part. Bagnara is optimistic, however, that Xen will soon be completely MISRA compliant, and that this will make its inclusion in products with functional-safety requirements much easier.

[Thanks to Linaro for covering my travel expenses to Linaro Connect.]

Addendum: Video from Stewart's keynote and Bagnara's talk are now available. The slides can also be found on Linaro's website.


Index entries for this article
ConferenceLinaro Connect/2025


to post comments

Functional Safety and Linux at ER 2025

Posted Jun 3, 2025 15:35 UTC (Tue) by WallaceIT (subscriber, #160340) [Link]

On the same topic (Functional Safety and Linux), Maxime Ripard gave an excellent talk at Embedded Recipes 2025 in Nice a couple of weeks ago: https://www.youtube.com/watch?v=iJdeMzndelY

rules, exceptions and power dynamics.

Posted Jun 3, 2025 17:33 UTC (Tue) by plugwash (subscriber, #29694) [Link] (34 responses)

> That's not the case; for MISRA, code quality always comes first. If a rule makes it harder to write quality code for some reason, there is a "deviation" process that lets the developer justify departing from the MISRA guidelines.

I suspect that in many situations this is one of those ideas that works better in theory than in practice. That in practice in most "safety-critical" projects only the most execptional cases get exceptions and the developers are just forced to live with the papercuts imposed by the coding standards.

Though I also suspect the "retrofitting safety" case runs differently from a typical "safe from the start" project. In the "retrofitting safety" case the cost of a stupid rule is far easier to see, and hence an alternative approach far easier to justify and the project you are attempting to retrofit safety to has the option to say NO.

rules, exceptions and power dynamics.

Posted Jun 3, 2025 18:40 UTC (Tue) by mb (subscriber, #50428) [Link] (33 responses)

I write MISRA C code in my day job.

I think almost all of the MISRA C rules make sense and improve code readability and safety.
And I can live with the other ones. There is no rule to my knowledge that makes the code worse per se.

I guess what to do about deviations depends on the company you work for.
But I can say what I have to do. I have to explain in detail:
- What the code is supposed to do and
- Why I cannot do it in another way.

The "why" part is very strict and not all answers are accepted. "I think it looks better", "I always did it this way", "This is my coding style" are all invalid answers.
However "It's technically impossible to do it in another way" or "The alternative would be *this* much less readable code" can be working answers.
The decision whether I can deviate or not is not done by me and not done by any of my team members. It's done by a neutral third party.

That said, it's rarely required to deviate from MISRA C rules.
Almost all programs can be written in valid MISRA C. Even device drivers can be written with a surprisingly low count of deviations. That count can reach zero.

Many of the MISRA C rules make the code obviously safer and many of the rules can also be found in modern languages like Rust. For example the (almost complete) ban of implicit casts. Rust even goes one step further and completely bans then.

However, there's one catch:
Making existing code MISRA compliant is *hard* and it actually often introduces real bugs.
I have seen this so many times that I think forcefully doing MISRA warning removals on old code bases actually decreases the code quality.
It's really important to first understand the code and then maybe remove warnings. In doubt, leave the code that has been running since 2001 in everybody's car as-is.

rules, exceptions and power dynamics.

Posted Jun 3, 2025 19:15 UTC (Tue) by adobriyan (guest, #30858) [Link] (32 responses)

> There is no rule to my knowledge that makes the code worse per se.

IIRC there is a rule about single return from a function.

rules, exceptions and power dynamics.

Posted Jun 3, 2025 19:28 UTC (Tue) by mb (subscriber, #50428) [Link] (9 responses)

That's true. But if you write your code with that rule in mind it's actually not that bad.

Yes, if you want to make your existing code base which has functions with 10 returns in them MISRA compliant, this is going to be a major issue.

While this rule makes code with early error check returns harder, I think it's still better than permitting people to put returns everywhere deeply nested in 2000 line functions.

There are more rules of this kind like only one break per loop is permitted.

rules, exceptions and power dynamics.

Posted Jun 3, 2025 19:41 UTC (Tue) by adobriyan (guest, #30858) [Link] (3 responses)

No, it is just bad rule.

It is a big topic in design of programming languages: C++ exceptions are still ridiculed, Option/Result is in vogue these days, Go people had _big_ discussion on this topic (so I've heard). Lisp doubled down in the other direction with restarts.

So, conceptually, there is always happy path and error path and it is better to separate them in code.

The rule should be that each failing function must have 2(!) return statements.

rules, exceptions and power dynamics.

Posted Jun 3, 2025 19:52 UTC (Tue) by mb (subscriber, #50428) [Link]

> C++ exceptions, Option/Result, Go, Lisp

MISRA C is about C code.

> So, conceptually, there is always happy path and error path and it is better to separate them in code.

Sure. You can easily do that without returning more than once.
One can even argue that the added indentation makes them visually even more separate.

I'm not saying that this is a perfect rule. Almost no MISRA C rule is perfect and lots of them have cases that prevent me from writing perfectly valid code.

But it's a *simple* rule that gets rid of lots of misuses of C. MISRA C is only about getting rid of misuses. Not about making a modern C language alternative. If you want a modern safe language, use Rust.
MISRA C often does this at the cost of also forbidding good uses.

MISRA C rules are simple by design. But that means they sometimes "overshoot".

rules, exceptions and power dynamics.

Posted Jun 4, 2025 11:40 UTC (Wed) by cencer (subscriber, #40823) [Link] (1 responses)

Fortunately MISRA 2023 C++ dropped this bad rule

rules, exceptions and power dynamics.

Posted Jun 6, 2025 11:12 UTC (Fri) by jwakely (guest, #60262) [Link]

And I believe MISRA C 2023 did too, although I don't have a copy to check that.

IEC 61508 still says "One entry/one exit point in subroutines and functions" though, which is a problem if you interpret that as "single return statement". A more sensible interpretation is "a function must return to a single place" which is always true for C functions which return to their caller, not to some arbitrary other instruction elsewhere in the program.

Deep nesting

Posted Jun 3, 2025 22:37 UTC (Tue) by rschroev (subscriber, #4164) [Link] (4 responses)

Aren't there rules against deeply nested functions? It seems to me having lots of nested levels in a function is a problem even without returns in them.

Deep nesting

Posted Jun 4, 2025 3:17 UTC (Wed) by martinfick (subscriber, #4455) [Link] (2 responses)

Early returns prevent deep nesting in the first place, so enforcing single returns results in deeper nested code.

Deep nesting

Posted Jun 4, 2025 7:07 UTC (Wed) by taladar (subscriber, #68407) [Link] (1 responses)

I mean you could also goto the single return statement, but that would be so much worse. Wouldn't be surprised though if that was the common "solution" to complying with the single return rule.

Deep nesting

Posted Jun 6, 2025 7:29 UTC (Fri) by NYKevin (subscriber, #129325) [Link]

Doesn't the Linux kernel routinely use goto to run cleanup code on return, in basically any function that would otherwise use an early return and has cleanup code to run? I was under the impression that was considered normal and idiomatic in C.

Deep nesting

Posted Jun 4, 2025 6:35 UTC (Wed) by mb (subscriber, #50428) [Link]

To my knowledge there's nothing in MISRA C that prevents deep nesting.
But in my job there are additional rules beyond MISRA C that prevent such code by enforcing certain code metrics line nesting depth, function size, conditional complexity and so on...
MISRA C is only the base. Companies usually apply more rules.

rules, exceptions and power dynamics.

Posted Jun 3, 2025 22:56 UTC (Tue) by randomguy3 (subscriber, #71063) [Link] (18 responses)

i saw a talk unrelated to misra that strongly argued that "single return" style functions were (in general) easier to read - the primary argument was that the structure of the function was reflected in the indentation of the code (rather than being flattened out as you get with "early return" functions)

i personally prefer early returns for precondition checking, but i don't think it's at all obvious that mandating a single return is a "bad" rule that produces worse code

rules, exceptions and power dynamics.

Posted Jun 3, 2025 23:54 UTC (Tue) by Cyberax (✭ supporter ✭, #52523) [Link] (17 responses)

> i saw a talk unrelated to misra that strongly argued that "single return" style functions were (in general) easier to read

Yep, so functions end up with multiple levels of nearly-empty nesting, obscuring the code. It also makes the "goto fail" pattern impossible.

rules, exceptions and power dynamics.

Posted Jun 4, 2025 6:37 UTC (Wed) by mb (subscriber, #50428) [Link] (16 responses)

> It also makes the "goto fail" pattern impossible.

goto is straight out forbidden by MISRA C, of course.
MISRA C doesn't hand out guns to children.

rules, exceptions and power dynamics.

Posted Jun 4, 2025 7:32 UTC (Wed) by Cyberax (✭ supporter ✭, #52523) [Link]

It does not. It explicitly allows "goto fail":

> MISRA C:2023 Rule 15.2
> The goto statement shall jump to a label declared later in the same function.

There is an _optional_ rule to forbid `goto` (15.1). But it's enforced only in bad projects that don't care about reliability.

rules, exceptions and power dynamics.

Posted Jun 4, 2025 10:57 UTC (Wed) by LtWorf (subscriber, #124958) [Link] (13 responses)

Good luck handling all error paths in C without goto.

rules, exceptions and power dynamics.

Posted Jun 4, 2025 11:08 UTC (Wed) by paulj (subscriber, #341) [Link]

To be fair, the 'Result' type pattern common in more recent languages is better, and there's no reason you can't implement the essence of the idea in C.

rules, exceptions and power dynamics.

Posted Jun 4, 2025 11:09 UTC (Wed) by PengZheng (subscriber, #108006) [Link] (7 responses)

> Good luck handling all error paths in C without goto.

No problem with the help of C RAII implemented by compiler `cleanup` attribute.

rules, exceptions and power dynamics.

Posted Jun 4, 2025 14:23 UTC (Wed) by alx.manpages (subscriber, #145117) [Link] (6 responses)

That attribute is bad, IMO. It requires using weird wrappers, and also forces initialization of variables, which results in worse static analysis. But there will be 'defer' soon (C2y), which has the good things without the horrible ones.

rules, exceptions and power dynamics.

Posted Jun 4, 2025 15:02 UTC (Wed) by paulj (subscriber, #341) [Link] (5 responses)

Oh, at last, at last! An end-of-scope hook coming in C! Yay!

rules, exceptions and power dynamics.

Posted Jun 4, 2025 15:15 UTC (Wed) by alx.manpages (subscriber, #145117) [Link] (2 responses)

"defer" (was rules, exceptions and power dynamics...)

Posted Jun 4, 2025 16:55 UTC (Wed) by dskoll (subscriber, #1630) [Link]

Oh, that's wonderful news! πŸŽ‰ defer will be a welcome addition to C.

rules, exceptions and power dynamics.

Posted Jun 4, 2025 17:07 UTC (Wed) by paulj (subscriber, #341) [Link]

Indeed. See his excellent blog entry: https://thephd.dev/c2y-the-defer-technical-specification-...

rules, exceptions and power dynamics.

Posted Jun 4, 2025 19:09 UTC (Wed) by adobriyan (guest, #30858) [Link]

Yay, indeed! Kids' minecraft swords will be replaced with wooden swords!

JK, defer is likely the optimal choice for C.

rules, exceptions and power dynamics.

Posted Jun 6, 2025 13:00 UTC (Fri) by paulj (subscriber, #341) [Link]

And there's a macro based implementation of defer (as long as you enclose the desired expression(s) in their own block) for GCC: https://github.com/cmhood/c-defer/blob/master/defer.h

rules, exceptions and power dynamics.

Posted Jun 4, 2025 14:44 UTC (Wed) by mb (subscriber, #50428) [Link] (2 responses)

Well, C is a bad old and obsolete language and neither goto nor MISRA really change that.
But I don't really see the fundamental problem in error handling without goto.

I have written lots of C with goto error handling and I have written lots of MISRA C without goto error handling.
It's both equally bad compared to modern alternatives in modern languages.

None of the error path bugs I have seen so far in MISRA C programs would have been handled correctly if goto or early return had been used.
Also look at projects like the Linux kernel which excessively use goto error handling. Do you remember the discussions about making small allocations possible to fail? And the fear of incorrect error paths.

rules, exceptions and power dynamics.

Posted Jun 4, 2025 17:11 UTC (Wed) by LtWorf (subscriber, #124958) [Link] (1 responses)

> It's both equally bad compared to modern alternatives in modern languages.

It's not very different from how go does it really. In go you must assign to err and check it every time. Forgot to check? Error silently ignored.

rules, exceptions and power dynamics.

Posted Jun 6, 2025 7:26 UTC (Fri) by taladar (subscriber, #68407) [Link]

Go isn't really a modern language, just a new language that ignores virtually anything the field of language design has learned.

rules, exceptions and power dynamics.

Posted Jun 5, 2025 14:01 UTC (Thu) by Kamiccolo (subscriber, #95159) [Link]

Well, it goes nicely with single return ;)

rules, exceptions and power dynamics.

Posted Jun 20, 2025 8:33 UTC (Fri) by sammythesnake (guest, #17693) [Link]

C is a swiss army knife with all the tools out...

rules, exceptions and power dynamics.

Posted Jun 6, 2025 10:53 UTC (Fri) by jwakely (guest, #60262) [Link] (2 responses)

There's a rule about single entry / single exit, but it's widely accepted now (although maybe not universally accepted) that it's talking about not having arbitrary non-local jumps in *assembly code*, and was never supposed to apply for 'return' statements in structured languages like C. Every return from a C function restores the necessary registers and pops the stack, so that everything is left in the proper state for the caller to resume. Arguably, every 'return' in C does a jump to the single exit for that function, and so meets the rule about having a single exit.

The rule should not be interpreted to mean a function must only have a single 'return'. That interpretation forces you to write deeply nested and more error-prone code, and can have the opposite effect on safety and maintainability.

rules, exceptions and power dynamics.

Posted Jun 6, 2025 12:44 UTC (Fri) by excors (subscriber, #95769) [Link] (1 responses)

I don't have access to the MISRA C documents, but from what I've read around it, that sounds like a perverse interpretation. It's certainly not true that a "return" statement in the middle of a C function will turn into an assembly jump to the function's epilogue - compilers will happily generate multiple 'ret' instructions and multiple epilogues per function. And it seems impossible to review C code against a set of guidelines based on the assembly code that will be generated, since you can't know what every compiler is going to do. Most of the other MISRA rules are very clearly about C language constructs, so I don't see how this rule ("A function shall have a single point of exit at the end of the function") can reasonably be interpreted as anything other than forbidding early return statements in the C code.

Plenty of static analysis tools give examples where the rule is violated by early return statements (https://www.mathworks.com/help/bugfinder/ref/misrac2023ru..., https://pvs-studio.com/en/docs/warnings/v2506/, https://rules.sonarsource.com/c/RSPEC-1005/, etc).

From the discussions I've seen, what's widely accepted is that it's a bad rule for C and it makes you write worse code. But I don't think that means you should try to reinterpret it as meaning something other than the straightforward interpretation of what it actually says. (...unless you're trying to hack some bureaucracy that is forcing you to follow bad rules, in which case a deliberately perverse interpretation might be the lesser evil, but you wouldn't need to keep up that pretence in LWN comments.)

According to the summaries at https://docs.scitools.com/ the rule was "required" in MISRA C 2004 (14.7) and "advisory" in 2012/2023 (15.5); and "required" in MISRA C++ 2008 (6-6-5) but removed entirely in MISRA C++ 2023. ("Advisory" means "Recommendations to be followed as far as is reasonably practical. Violations are identified but are not required to be supported by a deviation", so MISRA C 2023 still wants you to avoid multiple returns, but you don't have to write up multiple pages of justification every time you do it anyway.)

MISRA C 2025 moved it to the new category of "disapplied", which I think effectively means the rule is disabled by default (but you can choose to reenable it) (https://www.perforce.com/blog/sca/misra-c-2025). So it seems MISRA has eventually accepted that it's a bad rule and you can now ignore it, unless your employer is particularly annoying and turns it back on again.

rules, exceptions and power dynamics.

Posted Jun 6, 2025 14:22 UTC (Fri) by mb (subscriber, #50428) [Link]

>that sounds like a perverse interpretation

There are quite a few people commenting about how bad this and that is in MISRA C who have clearly never ever seen or written a MISRA C program.

>assembly

MISRA C is about... ... wait for it ... ... C! What a surprise!

Also, as a side note, MISRA C is *by* *far* not enough to write safety applications.
MISRA C is the common base line for automotive C programming. Safety and non-safety equally.

And Rust safety and functional safety are different things that share common ideas but have very different goals.

>"Advisory" means "Recommendations to be followed as far as is reasonably practical.

Note that this does *not* mean that Joe Average programmer can decide that she doesn't like it.

It will be a company policy that will be very well documented and will be mandatory to everybody in the company and it can even be a mandatory requirement from OEM to suppliers.

>MISRA C 2025 moved it to the new category of "disapplied"

Well, so be it.
But please realize that 2025 is brand new. It doesn't have an effect on actual development during the upcoming 5 to 10 years. At least.

In the meantime we can just live with that rule just fine.
Raging about how "clearly" bad it is is just a tempest in a tea cup.
This just distracts from the rest (99%) of MISRA C that clearly is an improvement over not having it.

Alternative code instead of deviation

Posted Jun 3, 2025 19:06 UTC (Tue) by mb (subscriber, #50428) [Link] (1 responses)

> variable &= -variable

I did not run it against a checker, but isn't the following alternative code MISRA C compliant?

variable &= ~variable + 1u;

It even generates the same assembly code with gcc:

https://godbolt.org/z/KfdPjYfe6

And I don't think it decreases readability in this case.
These variants are pretty much both unreadable without a good explaining comment.

Alternative code instead of deviation

Posted Jun 3, 2025 19:10 UTC (Tue) by daroc (editor, #160859) [Link]

Based on my limited understanding of the MISRA rule in question, I agree that your modified version would be both permitted and equally unreadable.

On the other hand, I also think your modified version would remain correct on architectures that use one's complement, when the original wouldn't (and would most likely silently behave incorrectly). Xen doesn't support any of those architectures, and they're all old and obsolete, but I understand that portability between one's complement and two's complement machines is the reason for many of the rules around signedness in C.

MISRA should understand two's-complement arithmetic

Posted Jun 3, 2025 21:02 UTC (Tue) by jreiser (subscriber, #11027) [Link] (22 responses)

Please state the MISRA rule which objects to variable & -variable. Theorem provers that understand C-like code have been used for decades. No one should have to teach two's-complement arithmetic to a checker.

MISRA should understand two's-complement arithmetic

Posted Jun 3, 2025 21:22 UTC (Tue) by daroc (editor, #160859) [Link] (20 responses)

Bagnara didn't cite the rule explicitly in his talk, but some investigation of the Xen project's Git history suggests it's the same rule β€” 10.1, "Operands shall not be of an inappropriate essential type". The commit introducing the change appears to be 2f848d3cee46685ca0a9bc77dbb23417b4b35652 from November 2023.

The C standard does allow for one's complement machines; I wish it didn't, since it's the source of so many headaches.

MISRA should understand two's-complement arithmetic

Posted Jun 3, 2025 21:47 UTC (Tue) by ballombe (subscriber, #9523) [Link] (17 responses)

> The C standard does allow for one's complement machines; I wish it didn't, since it's the source of so many headaches.

Can you name one single one's complement machine ?
The C standard committee always seems to ignore how C is actually used.

MISRA should understand two's-complement arithmetic

Posted Jun 3, 2025 22:21 UTC (Tue) by khim (subscriber, #9252) [Link] (1 responses)

> The C standard committee always seems to ignore how C is actually used.

It just considers many platforms that FOSS enthusiasts don't even know exists.

Do you know when last version of MCP, that thingie that was written for B6500 which had no ints in hardware and instead it used its floating-point unit to pretend that ints actually exist… in one's complement form?

Right: version 21.0 was presented June 2023… less than two years ago. It's still supported, apparently.

MISRA should understand two's-complement arithmetic

Posted Jun 4, 2025 0:03 UTC (Wed) by iabervon (subscriber, #722) [Link]

However, the 2021 release of the C compiler for MCP still only supports C89. It seems unlikely that anyone would make a C compiler for MCP that supports a different language standard if they haven't adopted any new standard yet, and pretty much impossible that anyone would ever want a C compiler for MCP that implemented a language standard in development now.

MISRA should understand two's-complement arithmetic

Posted Jun 3, 2025 23:17 UTC (Tue) by willy (subscriber, #9762) [Link] (12 responses)

The CDC 6600 was a 1s complement integer machine! Do I win a prize?

CDC machines

Posted Jun 3, 2025 23:20 UTC (Tue) by corbet (editor, #1) [Link] (11 responses)

Not only was it ones-complement, but it had 60-bit words (in main memory and the X registers) that were divided into ten six-bit chars...that made supporting C a lot of fun there. I learned assembly-language programming on that machine. The good old days.

CDC machines

Posted Jun 4, 2025 0:50 UTC (Wed) by willy (subscriber, #9762) [Link] (7 responses)

I've never used anything that had less than 7 bit characters. I found the character set: https://en.wikipedia.org/wiki/CDC_display_code

Leaving out the lower case letters leaves space for some interesting symbols like the up-arrow.

It's interesting to speculate about how we'd do a UTF-6; we could do 5 bits in one byte (0yzzzz), 7 bits in two bytes (110yyy 10zzzz), 10 bits in 3 bytes (1110xx 10yyyy 10zzzz), 13 bits in 4 bytes (11110w 10xxxx 10yyyy 10zzzz) and 16 bits in 5 bytes (111110 10wwww 10xxxx 10yyyy 10zzzzz) and 20 bits in 6 bytes (111111 10vvvv 10wwww 10xxxx 10yyyy 10zzzz). I believe 20 bits is enough for all of modern Unicode.

CDC machines

Posted Jun 4, 2025 15:49 UTC (Wed) by geert (subscriber, #98403) [Link] (1 responses)

In followed the link, read a bit, and thought about UTF.
Then I went back to read the remainder of your comment, and was pleasantly surprised to discover a full-fledged reasoning about UTF-6 ;-)

CDC machines

Posted Jun 4, 2025 18:31 UTC (Wed) by willy (subscriber, #9762) [Link]

Well, it's not _good_ reasoning about UTF-6. In particular, the first 32 code points in Unicode aren't terribly useful except NUL, HT and LF. So I might prefer a different encoding where we do something like 00zzzz for the first 16 code points, 010001 1xxyyy 1yzzzz for 10-bit code points, 010010 1wwwxx 1xxyyy 1yzzzz for 15-bit code points, and 01010u 1vvvvw 1wwwxx 1xxyyy 1yzzzz for 21-bit code points. This preserves most of the nice advantages of UTF-8 encoding and is slightly shorter for most common characters. Also there's no meaning assigned to 011aaa or 0111bb for future expansion. We could play with the encoding a bit more to shorten more code points, but this is probably more than enough nerd sniping for now ;-)

CDC machines

Posted Jun 4, 2025 16:07 UTC (Wed) by farnz (subscriber, #17727) [Link]

You need a little over 20 bits, a little under 21 bits, to handle all of Unicode - it's defined as taking values from 0 to 0x10ffff, excluding 0xd800 to 0xdfff.

Your "UTF-6" could say that it handles 0x10_0000 to 0x10_ffff via the surrogate pairs that UTF-16 uses (which use two 16 bit codepoints to encode 0x1_0000 to 0x10_ffff), or extend to 7 bytes. You might also luck out with an offset encoding (I've not thought this through fully), where you'd say that the 5 bit encoding directly encodes the codepoint, the 7 bit encoding encodes the code point minus 32, the 3 byte encoding encodes the codepoint minus 160 and so on; if I'm doing the sums right, this, possibly with a big jump inside the 5 byte (16 bit) range to remove the surrogate pair code points would let you cover the whole range.

CDC machines

Posted Jun 4, 2025 21:44 UTC (Wed) by brunowolff (guest, #71160) [Link]

I used to use Univac machines that had 36 bit words and also used 1s complement for math. They initially used 6 6 characters per word using an encoding called fieldata https://en.wikipedia.org/wiki/Fieldata. Later models added a quarter word mode and they stored 4 9 bit characters per word using ASCII.

CDC machines

Posted Jun 4, 2025 22:19 UTC (Wed) by plugwash (subscriber, #29694) [Link] (2 responses)

What is the goal of UTF-6?

The goal of UTF-16 was to provide an upgrade path for unicode 1.x applications. The goal of UTF-8 was to provide an upgrade path for "extended ascii" systems (systems where 0-127 were ascii and 128-255 were locale-specific). UTF-EBCIDIC tried to provide a similar thing for EBCIDIC based systems. UTF-7 tried to provide an encoding that was "email-safe".

If you are trying to acheive some level of backwards compatibility with a 6-bit character set, then I'd think you would be more likely to end up with a mode-switching based encoding akin to UTF-7 rather than an encoding similar to UTF-8.

If you are on a 36-bit system and aren't constrained by backwards compatibility, a UTF-9 probablly mades more sense than a UTF-6.

CDC machines

Posted Jun 5, 2025 14:19 UTC (Thu) by willy (subscriber, #9762) [Link] (1 responses)

My goal is "pretending that 6 bit bytes are still a thing, how would one encode Unicode in a dense way". Obviously, one could just use 4 x 6-bit bytes and fit all 21 bits of Unicode in it (I guess this would be called UTF-24). But machines of this vintage have small amounts of memory and a more dense encoding of ASCII would be welcomed.

Another nice properties of UTF-8 is being able to back up and find the start byte of a character, and this encoding also achieves that. Keeping NUL as a single byte of zeroes is good too.

I don't think that maintaining compatibility with any existing 6-bit character set is a useful goal. There just aren't enough bits.

CDC machines

Posted Jun 5, 2025 16:23 UTC (Thu) by excors (subscriber, #95769) [Link]

From what I've read, the CDC 6600 didn't really have "6-bit bytes": the CPU had 60-bit words, and the ten PPUs (peripheral and control processors) had 12-bit words. The 12-bit words were occasionally (but not often) called bytes.

The PPU's display console interface packed two 6-bit characters into one 12-bit word, while the printer interface used a totally different 7-bit encoding with one character per 12-bit word. I think the punched card reader used a 12-bit Hollerith encoding (where each character is a word with no more than 3 bits set, corresponding to the hole pattern in a single column), though the Chippewa OS converted that into 6-bit display codes. And the card puncher worked row-by-row: you'd have to take your 80 columns of 12-bit character data and transpose them into 12 rows of 7 x 12-bit words.

(References: https://bitsavers.org/pdf/cdc/cyber/cyber_70/60045000_660..., https://bitsavers.org/pdf/cdc/Tom_Hunter_Scans/Codes_6600..., https://bitsavers.org/pdf/cdc/cyber/books/DesignOfAComput...)

If you wanted to encode Unicode in a dense way with relatively efficient processing, I think you'd want a format that the PPUs can easily transform to/from any of those encodings for IO, so you should base it around the PPU's 12-bit words. There's a UTF-12 proposal at https://tapemark.narod.ru/comp/utf12en.html where everything below U+07C0 is encoded as one 12-bit word, and everything else is two words, which sounds reasonable. Not as dense as the 6-bit display codes, but sounds okay for working with the printer and punched cards (you could do an in-place transformation to their native character encodings, in the common case of no two-word codepoints), and I guess those are the most relevant peripherals when you're working with large amounts of data. The CPU will need to do some bit-twiddling to pack/unpack codepoints in its 60-bit words, and some codepoints will straddle two 60-bit words, but that doesn't sound too bad - most code can probably just treat strings as an opaque 12-bit sequence (like how UTF-8 can mostly be treated as an opaque 8-bit sequence, until you need to do something clever and grapheme-based).

But I can see why trying to do any of this in C would be a pain - it doesn't match the concept of a "char" type at all.

CDC machines

Posted Jun 4, 2025 2:42 UTC (Wed) by felixfix (subscriber, #242) [Link]

Oh! My hero! I learned assembler first on an IBM 1620 Mod I, then a CDC 6400. I tell people I was a RISC programmer before there was RISC.

CDC machines

Posted Jun 13, 2025 23:45 UTC (Fri) by ras (subscriber, #33059) [Link] (1 responses)

The PDP-10 had 36 bit words, and used 6 bit characters, and presented everything in octal. It continued a pattern started with the PDP-1, which had 18 bit words, as did the PDP-4, PDP-7, PDP-9. The PDP-5 and PDP-8 used 12 bit words. Octal was a good fit for all of them.

The PDP-11 succeeded the PDP-10 (as you might assume given 10 is one bigger than 11) but broke with tradition by using 8 bite bytes. Nonetheless the debugger embedded in PDP-11/23's microcode wanted octal, much to my chagrin. Octal continued to hang around in C, equally inexplicitly to me. Only recently did it occur to me it a hang over from the PDP-1. Six bit hardware may have died with introduction of the PDP-11, but evidently it was alive and well in the minds of the programmers of it's predecessors.

As for the old days, the Uni's PDP-10 ran the TOPS-10 operating system. It regularly hosted 60 concurrent users with it's 1MB words of core memory.

CDC machines

Posted Jun 14, 2025 8:44 UTC (Sat) by jem (subscriber, #24231) [Link]

>The PDP-10 had 36 bit words, and used 6 bit characters, and presented everything in octal.

A minor nit: the PDP-10 could operate on bit fields of any size from 1 to 36 bits, which meant the size of a character was not fixed by the CPU. At least the TOPS-20 operating system used 7-bit ASCII, packing 5 characters in a 36-bit word, with one bit to spare. This spare bit was used by the EDIT editor to mark that the word contained a line number which was shown when the file was TYPEd to the terminal, but compilers etc. could ignore.

A C compiler I got to try reported that sizeof (int) == 5.

MISRA should understand two's-complement arithmetic

Posted Jun 3, 2025 23:52 UTC (Tue) by Cyberax (✭ supporter ✭, #52523) [Link]

> Can you name one single one's complement machine ?

There are some. Several DSPs use de-normalized floating points to emulate the integer arithmetic. It essentially becomes one's complement when done like this. I believe, early programmable GPUs also worked this way.

MISRA should understand two's-complement arithmetic

Posted Jun 4, 2025 13:56 UTC (Wed) by daroc (editor, #160859) [Link]

Alongside the other machines people have mentioned, the Unisys Clearpath Dorado is a one's complement machine that's still in use and available on the market.

MISRA should understand two's-complement arithmetic

Posted Jun 4, 2025 10:05 UTC (Wed) by pbonzini (subscriber, #60935) [Link] (1 responses)

Two's complement is required by C23.

MISRA should understand two's-complement arithmetic

Posted Jun 4, 2025 13:57 UTC (Wed) by daroc (editor, #160859) [Link]

Oh, I had missed that! That's excellent news.

MISRA should understand two's-complement arithmetic

Posted Jun 4, 2025 6:32 UTC (Wed) by mb (subscriber, #50428) [Link]

This is not about two's complement vs. any other form of signed representation.

MISRA is about making C code obviously correct to people reading the code.
It implements rules to make "tricks" harder and to forbid chains of operation that lead to *common* problems.
It often "overshoots" and also forbids legitimate uses.

In this case the rule(s) that hit are basically that MISRA restricts unsigned<->signed conversions and basically completely forbids unsigned<->signed<->unsigned round trip conversions.
This results from the combination of a couple of rules.

It has nothing to do with experts know how integers are encoded.
It has to do with how this prevents not-yet-experts from introducing fatal sign-casting bugs.
In automotive contexts there are many libraries that can safely be used for such conversions (AUTOSAR) and they are *usually* the correct and much safer thing to do instead of plain implicit or explicit (casts) conversions in plain old C.

did someone say 'rust'?

Posted Jun 4, 2025 15:50 UTC (Wed) by amarao (guest, #87073) [Link] (11 responses)

Do I understand that MIRSA put 'C' and 'safety' in the same sentence?

Set of rules to handle granades in a kindergarten to minimize risk of blowing things up.

did someone say 'rust'?

Posted Jun 4, 2025 18:17 UTC (Wed) by willy (subscriber, #9762) [Link] (1 responses)

Rust started in 2013. MISRA C started in 1997. It's not a wasted effort for both things to exist.

did someone say 'rust'?

Posted Jun 4, 2025 19:02 UTC (Wed) by amarao (guest, #87073) [Link]

It was then. But now switching to a sane safe language is the most natural way to improve security. Not to 'rewrite from scratch'. Just start writing new code, doing co-refactoring of C and Rust, etc.

The rust defaults make C standard looks like a a glowing radium cake icing recipe from 1930s. No matter what you do, it's still radium on top of the cake.

did someone say 'rust'?

Posted Jun 4, 2025 20:09 UTC (Wed) by pbonzini (subscriber, #60935) [Link] (8 responses)

MISRA C is about complexity as much as safety.

Rust is a much more complex language than C and it would not be trivial at all to define something like MISRA C for it. The amount of things that would have to be covered by rules (derives, generics, interior mutability,...) would be staggering.

did someone say 'rust'?

Posted Jun 4, 2025 21:02 UTC (Wed) by excors (subscriber, #95769) [Link] (1 responses)

MISRA has recently been considering how their C rules would apply to Rust, so presumably they believe there is some potential for careful use of Rust in systems that need to meet safety standards: https://misra.org.uk/app/uploads/2025/03/MISRA-C-2025-ADD...

> This document has been produced to assess the applicability of the MISRA C guidelines to the Rust programming language, and will form the basis of future work within MISRA, in collaboration with the wider Rust community.
>
> The intended purpose of this document is to highlight which areas of the Rust programming language are and are not subject to issues identified as affecting C projects.
>
> By identifying which issues enforced by a given MISRA C guideline categorically cannot apply to a Rust project, developers of a Rust project can be certain that their code is compliant to whichever parts of functional safety standards those guidelines are intended to enforce. Where an issue enforced by a MISRA C guideline does still apply or potentially apply to code written in Rust, deeper analysis may still be required to ensure compliance, but the overall list is reduced and the relevant issues are clearly identified as mapping to some known enforcement which can aid in manual review.

did someone say 'rust'?

Posted Jun 5, 2025 10:52 UTC (Thu) by farnz (subscriber, #17727) [Link]

Note that the fundamental purpose of MISRA rules, and things like it, is to ensure that an external auditor who reviews your code will definitely identify any breaches of safety requirements. That's a goal that applies to any language; it's therefore not surprising that MISRA rules also have applicability to Rust.

That MISRA is taking Rust seriously is a sign that they think it's going to be interesting to try and write safety-critical code in Rust, and therefore that it needs to be possible to audit the code for safety bugs.

did someone say 'rust'?

Posted Jun 4, 2025 22:01 UTC (Wed) by plugwash (subscriber, #29694) [Link] (4 responses)

And rust's idea of safety is probablly somewhat different from MISRA's. In particular panics and memory leaks are considered "safe" in rust, but probably wouldn't be to someone trying to design a high reliability system.

did someone say 'rust'?

Posted Jun 5, 2025 8:03 UTC (Thu) by Cyberax (✭ supporter ✭, #52523) [Link] (2 responses)

MISRA does literally nothing for memory leaks.

OK, I lied. It does, with a rule 18.4.1: "Dynamic heap memory allocation shall not be used."

See? Problem solved.

did someone say 'rust'?

Posted Jun 6, 2025 7:30 UTC (Fri) by taladar (subscriber, #68407) [Link] (1 responses)

So you would just use Rust no_std without alloc to comply with that? Seems pretty limiting for general purpose code but for tiny embedded systems it shouldn't be a problem.

did someone say 'rust'?

Posted Jun 6, 2025 17:00 UTC (Fri) by Cyberax (✭ supporter ✭, #52523) [Link]

Of course. I'm just saying that the default Rust is actually not worse for memory leak safety than MISRA C, which just ignores the issue entirely.

did someone say 'rust'?

Posted Jun 6, 2025 10:33 UTC (Fri) by jwakely (guest, #60262) [Link]

Panics should be OK when rapid termination is better than limping along in a corrupted state. You do need to be able to detect a panic and restart the system in a valid state though.

did someone say 'rust'?

Posted Jun 5, 2025 10:20 UTC (Thu) by adobriyan (guest, #30858) [Link]

I bet 1 dollar they will ban shadowing because "confusing" even if when shadowing is exactly the right tool.


Copyright © 2025, Eklektix, Inc.
This article may be redistributed under the terms of the Creative Commons CC BY-SA 4.0 license
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds