使用 GCC C 在 AIX 上处理空指针
我们有一段用 C 编写的代码,有时不能很好地处理零指针。
该代码最初是在 Solaris 上编写的,此类指针会导致分段错误。并不理想,但比继续耕耘要好。
我们的经验是,如果您在 AIX 上读取空指针,您会得到 0。如果您使用 xlc 编译器,您可以添加一个选项 -qcheck=all
来捕获这些指针。但我们使用 gcc(并且希望继续使用该编译器)。 gcc 提供这样的选项吗?
We have a code written in C that sometimes doesn’t handle zero pointers very well.
The code was originally written on Solaris and such pointers cause a segmentation fault. Not ideal but better than ploughing on.
Our experience is that if you read from a null pointer on AIX you get 0. If you use the xlc compiler you can add an option -qcheck=all
to trap these pointers. But we use gcc (and want to continue using that compiler). Does gcc provide such an option?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
gcc 提供这样的选项吗?
我不好意思地自愿回答不,没有。尽管我无法引用有关 gcc 和运行时 NULL 检查的信息缺失。
您要解决的问题是您试图在编写得不好的程序中更明确地定义未定义的行为。
我建议您咬紧牙关,要么切换到 xlc,要么手动向代码添加 NULL 检查,直到发现并删除不良行为。
考虑:
随着错误的删除,您可以开始删除这些检查。
Does gcc provide such an option?
I'm sheepishly volunteering the answer no, it doesn't. Although I can't cite the absence of information regarding gcc and runtime NULL checks.
The problem you're tackling is that you're trying to make undefined behavior a little more defined in a program that's poorly-written.
I recommend that you bite the bullet and either switch to xlc or manually add
NULL
checks to the code until the bad behavior has been found and removed.Consider:
As bugs are removed, you can begin to remove these checks.
请帮我们大家一个忙,并在您的代码中添加适当的
NULL
检查。仅在需要时检查NULL
,而不是让编译器在任何地方执行检查,不仅可以在性能上获得轻微的提升,而且您的代码将更易于移植到其他平台。更不用说您更有可能打印正确的错误消息,而不是让编译器删除一些无法理解的堆栈转储/源代码位置/错误代码,这对您的用户根本没有帮助。
AIX 使用 NULL 页的概念。本质上,
NULL
(即虚拟地址0x0
)被映射到包含一整串零的位置。这允许字符串操作代码等在遇到NULL
指针时继续。这与大多数其他类 Unix 系统相反,但它并不违反 C 标准,该标准将取消引用 NULL 视为未定义操作。但在我看来,这是一个严重的问题:它需要一个会剧烈崩溃的应用程序,然后将其变成一个默默地忽略编程错误的应用程序,可能会产生完全不正确的结果。
据我所知,GCC 没有办法解决根本性损坏的代码。即使是历史上支持的模式,例如可写字符串文字,也已在较新的 GCC 版本中慢慢淘汰。
使用内存调试选项(例如
-fmudflap
)时可能会有一些支持,但我真的不知道 - 无论如何,您不应该在生产系统中使用调试代码,尤其是强制损坏的情况代码工作。底线:我认为您无法避免添加显式的
NULL
检查。不幸的是,我们现在遇到了一个基本问题:应该在哪里添加 NULL 检查?。我认为让编译器不加区别地添加此类检查会有所帮助,前提是您在发现问题时添加显式检查。
不幸的是,Valgrind 不支持 AIX。如果您有足够的资金,您可能想看看 IBM Rational Purify Plus for AIX - 它可能会捕获此类错误。
也可以在测试系统上使用 xlc,并在其他方面使用 gcc,但不幸的是,它们并不完全兼容。
Please do us all a favor and add proper
NULL
checks to your code. Not only will you have a slight gain in performance by checking forNULL
only when needed, rather than having the compiler perform the check everywhere, but your code will be more portable to other platforms.And let's not mention the fact that you will be more likely to print a proper error message rather than have the compiler drop some incomprehensible stack dump/source code location/error code that will not help your users at all.
AIX uses the concept of a NULL page. Essentially,
NULL
(i.e. virtual address0x0
) is mapped to a location that contains a whole bunch of zeros. This allows string manipulation code e.t.c. to continue despite encountering aNULL
pointer.This is contrary to most other Unix-like systems, but it is not in violation of the C standard, which considers dereferencing NULL an undefined operation. In my opinion, though, this is woefully broken: it takes an application that would crash violently and turns it into one that ignores programming errors silently, potentially producing totally incorrect results.
As far as I know, GCC has no options to work around fundamentally broken code. Even historically supported patterns, such as writable string literals, have been slowly phased out in newer GCC versions.
There might be some support when using memory debugging options such as
-fmudflap
, but I don't really know - in any case you should not use debugging code in production systems, especially for forcing broken code to work.Bottom line: I don't think that you can avoid adding explicit
NULL
checks.Unfortunately we now come to the basic question: Where should the NULL checks be added?. I suppose having the compiler add such checks indiscriminately would help, provided that you add an explicit check when you discover an issue.
Unfortunately, there is no Valgrind support for AIX. If you have the cash, you might want to have a look at IBM Rational Purify Plus for AIX - it might catch such errors.
It might also be possible to use
xlc
on a testing system andgcc
for everything else, but unfortunately they are not fully compatible.