简体   繁体   English

MISRA-C是否适用于Linux应用程序

[英]Is MISRA-C applicable to Linux applications

I understand that MISRA-C standards are intended for embedded firmware . 据我所知,MISRA-C标准适用于嵌入式固件 When embedded Linux is your product platform, can/should your embedded applications be developed to be MISRA-C compliant? 当嵌入式Linux是您的产品平台时,您的嵌入式应用程序可以/应该开发为符合MISRA-C吗? Has anyone ever considered such an exercise? 有没有人考虑过这样的练习?

My general sense is that you must first understand all the 'rules' and then apply them in your design/coding phase. 我的一般意义是你必须先了解所有“规则”,然后在设计/编码阶段应用它们。 There may be instances like system calls (pthread_create) and void* that need to be forced into compliance - producing ugly looking code. 可能存在需要强制符合系统调用(pthread_create)和void *的情况 - 产生难看的代码。

While MISRA-C was originally created as a standard just for automotive and safety-critical applications, this is no longer true. 虽然MISRA-C最初是作为汽车和安全关键应用的标准而创建的,但现在已不再适用。 Nowadays MISRA-C is more of a general standard that can be used for any C programs where bugs, crashes and portability issues are not desired. 如今,MISRA-C更像是一种通用标准,可用于任何不希望出现错误,崩溃和可移植性问题的C程序。

You need to ask yourself why you are using MISRA-C. 您需要问问自己为什么使用MISRA-C。 Is it because you wish to use it as a coding standard to get rid of bugs, or because the application is of a mission-critical nature? 是因为您希望将其用作编码标准来摆脱错误,还是因为应用程序具有关键任务性质?

For the Linux case, the main issue will be if you'll just have your own code MISRA compliant, or if you will demand that this is true for all libraries included as well. 对于Linux案例,主要问题是如果您只有自己的代码MISRA兼容,或者如果您要求所有库也适用。 And there is just no way you'll make the Linux kernel + libraries MISRA compliant, you'd have to rewrite Linux from scratch. 你无法让Linux内核+库符合MISRA标准,你必须从头开始重写Linux。

This makes Linux unsuitable for mission-critical software. 这使Linux不适用于任务关键型软件。 But if your program is not of a mission-critical nature, you should be able to use Linux. 但是,如果您的程序不具备任务关键性,那么您应该能够使用Linux。 You might have to write a number of standing deviations from MISRA-C in advance, for things that you can tell will cause problems. 您可能需要提前写出一些与MISRA-C相关的常设偏差,因为您可以告诉的事情会导致问题。

In a word: No. 一句话:不。

MISRA does provide some good guidelines, but you'd be better off just cherry picking rules to which you want to adhere (assuming you have automated checking in your build / static analysis). MISRA确实提供了一些很好的指导方针,但您最好只选择要遵守的规则(假设您已经自动检查了构建/静态分析)。

Skimming through MISRA-2004 stuff, here are some problem areas. 浏览MISRA-2004的内容,这里有一些问题。

Having all your libraries be MISRA compliant is, in itself, MISRA rule. 使所有库符合MISRA标准本身就是MISRA规则。

Rules on goto , continue and break , function returns, and pointer arithmetic are violated in literally billions of lines of both kernel and userspace code, so good luck getting your libraries (or kernel) under compliance. 关于gotocontinuebreak ,函数返回以及指针算术的规则在内核和用户空间代码的数十亿行中都被违反,因此很好地使您的库(或内核)处于合规状态。

Pointer casting rules will be impossible to follow if using sockets, among other common APIs. 如果使用套接字以及其他常见API,则无法遵循指针强制转换规则。

MISRA-2004 11.2 Conversions shall not be performed between a pointer to object and any type other than an integral type, another pointer to object type or a pointer to void. MISRA-2004 11.2不应在指向对象的指针和除整数类型之外的任何类型,指向对象类型的另一指针或指向void的指针之间执行转换。

2004-20.x sections ban <errno.h> , <stdio.h> , <time.h> and <signal.h> . 2004-20.x部分禁止<errno.h><stdio.h><time.h><signal.h> Banning signals and error checking promotes BAD Linux programming if you're writing long-running, robust services. 如果您正在编写长期运行的强大服务,禁止信号和错误检查可以促进BAD Linux编程。

And no dynamic memory allocation is a rule in there somewhere. 在某些地方,没有动态内存分配是一个规则。

I know MISRA has rules that allow you to violate rules if you document them (is this true for required or just advisory ???), but you'll document soooo many exceptions that it's really sort of pointless. 我知道MISRA有规则允许你违反规则,如果你记录它们(这是真的是必需的还是只是建议 ???),但你会记录很多例外情况,这实际上是毫无意义的。

All that said, if you have a customer insisting on MISRA compliance (which is the only reason I've ever seen it used), you could probably document all your rule violations and make some hand-wavy attempt to call yourself MISRA compliant. 所有这一切,如果你有一个客户坚持MISRA合规(这是我见过它的唯一原因),你可能会记录你所有的规则违规行为,并做一些手动波浪尝试称自己符合MISRA。 So there might be a business case for pretending to be MISRA compliant on Linux, but I see little to no technical advantage in it. 因此可能存在假装在Linux上符合MISRA标准的商业案例,但我认为它几乎没有技术优势。

I'm afraid if you want to be truly compliant AND need a heavyweight/full-featured OS you're better off forking out the dough for QNX, GHS Integrity, VxWorks, etc. 我担心如果你想要真正顺从并且需要一个重量级/功能齐全的操作系统,你最好为QNX,GHS Integrity,VxWorks等分一杯羹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM