- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
CBMC 在以下行中检测到可能存在无符号加法溢出:
l = (t + *b)&(0xffffffffL);
c += (l < t);
我同意第一行可能会溢出,但我正在处理 CBMC 无法查看的下一行中的进位。如果万一出现溢出,我将进位设置为 1。因此,由于我知道这个和这个,这就是我希望我的代码工作的方式,所以我想继续进行验证过程。那么,我如何告诉 CBMC 忽略这个错误并继续前进呢?
最佳答案
TL;DR 这取决于变量的实际类型。在所有情况下,CBMC 都会检测到可能导致未定义行为的实际错误。这意味着,您应该修复代码而不是禁用 CBMC 中的消息。
完整答案:
一般: 据我所知,CBMC 不允许排除特定属性(另一方面,您可以使用 --property 只检查一个特定属性
标志)。如果您想要官方答案/意见或提出功能请求,我建议您在 CProver Support group 中发帖.
(当然,可以使用 __CPROVER_assume
让 CBMC 排除导致错误的痕迹,但这将是一个非常非常非常糟糕的主意,因为这可能会使其他问题无法解决。)
变体 1: 我假设您的代码看起来像(与此相关:请发布自包含示例并准确解释问题所在,这很难猜测这些事情)
long nondet_long(void);
void main(void) {
long l = 0;
int c = 0;
long t = nondet_long();
long s = nondet_long();
long *b = &s;
l = (t + *b) & (0xffffffffL);
c += (l < t);
}
你正在运行
cbmc --signed-overflow-check test.c
giving an output similar to the following one?
CBMC version 5.1 64-bit x86_64 macos Parsing test.c Converting Type-checking test Generating GOTO Program Adding CPROVER library Function Pointer Removal Partial Inlining Generic Property Instrumentation Starting Bounded Model Checking size of program expression: 41 steps simple slicing removed 3 assignments Generated 2 VCC(s), 2 remaining after simplification Passing problem to propositional reduction converting SSA Running propositional reduction Post-processing Solving with MiniSAT 2.2.0 with simplifier 792 variables, 2302 clauses SAT checker: negated claim is SATISFIABLE, i.e., does not hold Runtime decision procedure: 0.006s Building error trace Counterexample: State 17 file test.c line 4 function main thread 0 ---------------------------------------------------- l=0 (0000000000000000000000000000000000000000000000000000000000000000) State 18 file test.c line 4 function main thread 0 ---------------------------------------------------- l=0 (0000000000000000000000000000000000000000000000000000000000000000) State 19 file test.c line 5 function main thread 0 ---------------------------------------------------- c=0 (00000000000000000000000000000000) State 20 file test.c line 5 function main thread 0 ---------------------------------------------------- c=0 (00000000000000000000000000000000) State 21 file test.c line 6 function main thread 0 ---------------------------------------------------- t=0 (0000000000000000000000000000000000000000000000000000000000000000) State 22 file test.c line 6 function main thread 0 ---------------------------------------------------- t=-9223372036854775808 (1000000000000000000000000000000000000000000000000000000000000000) State 23 file test.c line 7 function main thread 0 ---------------------------------------------------- s=0 (0000000000000000000000000000000000000000000000000000000000000000) State 24 file test.c line 7 function main thread 0 ---------------------------------------------------- s=-9223372036854775807 (1000000000000000000000000000000000000000000000000000000000000001) State 25 file test.c line 8 function main thread 0 ---------------------------------------------------- b=((long int *)NULL) (0000000000000000000000000000000000000000000000000000000000000000) State 26 file test.c line 8 function main thread 0 ---------------------------------------------------- b=&s!0@1 (0000001000000000000000000000000000000000000000000000000000000000) Violated property: file test.c line 10 function main arithmetic overflow on signed + in t + *b !overflow("+", signed long int, t, *b) VERIFICATION FAILED
I do not think you should disable this property check, even if you could. The reason for this is, as you say, that this addition can overflow, and, integer overflow is undefined behaviour in C, or, as this answer to the question How to check integer overflow in C? nicely puts it:
[O]nce you've executed x + y, if it overflowed, you're already hosed. It's too late to do any checking - your program could have crashed already. Think of it like checking for division by zero - if you wait until after the division has been executed to check, it's already too late.
See also Integer overflow and undefined behavior and How disastrous is integer overflow in C++?.
Thus, this is an actual bug and CBMC has a good reason for telling you about it. What you actually should do is adapt your code so that there are no potential overflows! The answer mentioned above suggests something like (remember to include limits.h
):
if ((*b > 0 && t > LONG_MAX - *b)
|| (*b < 0 && LONG_MIN < *b && t < LONG_MIN - *b)
|| (*b==LONG_MIN && t < 0))
{
/* Overflow will occur, need to do maths in a more elaborate, but safe way! */
/* ... */
}
else
{
/* No overflow, addition is safe! */
l = (t + *b) & (0xffffffffL);
/* ... */
}
变体 2:在这里,我假设您的代码类似于:
unsigned int nondet_uint(void);
void main(void) {
unsigned int l = 0;
unsigned int c = 0;
unsigned int t = nondet_uint();
unsigned int s = nondet_uint();
unsigned int *b = &s;
l = (t + *b) & (0xffffffffL);
c += (l < t);
}
你在运行
cbmc --unsigned-overflow-check test.c
giving an output similar to the following one?
CBMC version 5.1 64-bit x86_64 macosParsing test.cConvertingType-checking testGenerating GOTO ProgramAdding CPROVER libraryFunction Pointer RemovalPartial InliningGeneric Property InstrumentationStarting Bounded Model Checkingsize of program expression: 42 stepssimple slicing removed 3 assignmentsGenerated 3 VCC(s), 3 remaining after simplificationPassing problem to propositional reductionconverting SSARunning propositional reductionPost-processingSolving with MiniSAT 2.2.0 with simplifier519 variables, 1306 clausesSAT checker: negated claim is SATISFIABLE, i.e., does not holdRuntime decision procedure: 0.01sBuilding error traceCounterexample:State 17 file test.c line 4 function main thread 0---------------------------------------------------- l=0 (00000000000000000000000000000000)State 18 file test.c line 4 function main thread 0---------------------------------------------------- l=0 (00000000000000000000000000000000)State 19 file test.c line 5 function main thread 0---------------------------------------------------- c=0 (00000000000000000000000000000000)State 20 file test.c line 5 function main thread 0---------------------------------------------------- c=0 (00000000000000000000000000000000)State 21 file test.c line 6 function main thread 0---------------------------------------------------- t=0 (00000000000000000000000000000000)State 22 file test.c line 6 function main thread 0---------------------------------------------------- t=4187126263 (11111001100100100111100111110111)State 23 file test.c line 7 function main thread 0---------------------------------------------------- s=0 (00000000000000000000000000000000)State 24 file test.c line 7 function main thread 0---------------------------------------------------- s=3329066504 (11000110011011011000011000001000)State 25 file test.c line 8 function main thread 0---------------------------------------------------- b=((unsigned int *)NULL) (0000000000000000000000000000000000000000000000000000000000000000)State 26 file test.c line 8 function main thread 0---------------------------------------------------- b=&s!0@1 (0000001000000000000000000000000000000000000000000000000000000000)Violated property: file test.c line 10 function main arithmetic overflow on unsigned + in t + *b !overflow("+", unsigned int, t, *b)VERIFICATION FAILED
Again, this is an actual bug and CBMC has a good reason for telling you about it. This one could be fixed by
l = ((unsigned long)t + (unsigned long)*b) & (0xffffffffL);
c += (l < t);
给出
CBMC version 5.1 64-bit x86_64 macosParsing test.cConvertingType-checking testGenerating GOTO ProgramAdding CPROVER libraryFunction Pointer RemovalPartial InliningGeneric Property InstrumentationStarting Bounded Model Checkingsize of program expression: 42 stepssimple slicing removed 3 assignmentsGenerated 3 VCC(s), 3 remaining after simplificationPassing problem to propositional reductionconverting SSARunning propositional reductionPost-processingSolving with MiniSAT 2.2.0 with simplifier542 variables, 1561 clausesSAT checker inconsistent: negated claim is UNSATISFIABLE, i.e., holdsRuntime decision procedure: 0.002sVERIFICATION SUCCESSFUL
Variant 3: If things are as the previous one, but you have signed int
instead of unsigned int
, things get a bit more complicated. Here, assuming you use (written in a slightly more elaborate way to better see what is going on)
int nondet_int(void);
void main(void) {
int l = 0;
int c = 0;
int t = nondet_int();
int s = nondet_int();
long longt = (long)t;
long longs = (long)s;
long temp1 = longt + longs;
long temp2 = temp1 & (0xffffffffL);
l = temp2;
c += (l < t);
}
然后运行
cbmc --signed-overflow-check test.c
you will get
CBMC version 5.1 64-bit x86_64 macosParsing test.cConvertingType-checking testGenerating GOTO ProgramAdding CPROVER libraryFunction Pointer RemovalPartial InliningGeneric Property InstrumentationStarting Bounded Model Checkingsize of program expression: 48 stepssimple slicing removed 3 assignmentsGenerated 3 VCC(s), 3 remaining after simplificationPassing problem to propositional reductionconverting SSARunning propositional reductionPost-processingSolving with MiniSAT 2.2.0 with simplifier872 variables, 2430 clausesSAT checker: negated claim is SATISFIABLE, i.e., does not holdRuntime decision procedure: 0.008sBuilding error traceCounterexample:State 17 file test.c line 4 function main thread 0---------------------------------------------------- l=0 (00000000000000000000000000000000)State 18 file test.c line 4 function main thread 0---------------------------------------------------- l=0 (00000000000000000000000000000000)State 19 file test.c line 5 function main thread 0---------------------------------------------------- c=0 (00000000000000000000000000000000)State 20 file test.c line 5 function main thread 0---------------------------------------------------- c=0 (00000000000000000000000000000000)State 21 file test.c line 6 function main thread 0---------------------------------------------------- t=0 (00000000000000000000000000000000)State 22 file test.c line 6 function main thread 0---------------------------------------------------- t=-2147483648 (10000000000000000000000000000000)State 23 file test.c line 7 function main thread 0---------------------------------------------------- s=0 (00000000000000000000000000000000)State 24 file test.c line 7 function main thread 0---------------------------------------------------- s=1 (00000000000000000000000000000001)State 25 file test.c line 9 function main thread 0---------------------------------------------------- longt=0 (0000000000000000000000000000000000000000000000000000000000000000)State 26 file test.c line 9 function main thread 0---------------------------------------------------- longt=-2147483648 (1111111111111111111111111111111110000000000000000000000000000000)State 27 file test.c line 10 function main thread 0---------------------------------------------------- longs=0 (0000000000000000000000000000000000000000000000000000000000000000)State 28 file test.c line 10 function main thread 0---------------------------------------------------- longs=1 (0000000000000000000000000000000000000000000000000000000000000001)State 29 file test.c line 11 function main thread 0---------------------------------------------------- temp1=0 (0000000000000000000000000000000000000000000000000000000000000000)State 31 file test.c line 11 function main thread 0---------------------------------------------------- temp1=-2147483647 (1111111111111111111111111111111110000000000000000000000000000001)State 32 file test.c line 12 function main thread 0---------------------------------------------------- temp2=0 (0000000000000000000000000000000000000000000000000000000000000000)State 33 file test.c line 12 function main thread 0---------------------------------------------------- temp2=2147483649 (0000000000000000000000000000000010000000000000000000000000000001)Violated property: file test.c line 14 function main arithmetic overflow on signed type conversion in (signed int)temp2 temp2 = -2147483648lVERIFICATION FAILED
Or, written more concisely, if you have
t == -2147483648 (0b10000000000000000000000000000000)
s == 1 (0b00000000000000000000000000000001)
然后
temp2 == 2147483649 (0b0000000000000000000000000000000010000000000000000000000000000001)
尝试将其转换为 signed int
很麻烦,因为它超出了范围(另请参见 Does cast between signed and unsigned int maintain exact bit pattern of variable in memory?)。
如您所见,这个反例也是一个实际的错误,CBMC 再次告诉您这一点是正确的。这特别意味着,您的掩码/数学没有按预期工作(您的掩码将负数变成超出范围的正数)并且您需要修复代码,以便结果在必要的范围内。 (为了确保获得正确的结果,仔细考虑您实际想要做什么可能是值得的。)
关于c - 绕过 CBMC 检测到的无符号加法溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31065205/
这是一个与 Get OS-Version in WinRT Metro App C# 相关的问题但不是它的重复项。 是否有任何选项可以从 Metro 应用程序检测系统上是否有可用的桌面功能?据我所知,
我想在闹钟响起时做点什么。例如, toast 或设置新闹钟。我正在寻找可以检测闹钟何时响起的东西。首先,我在寻找广播 Action ,但找不到。也许是我的错? 当闹钟响起时,还有其他方法可以做些什么吗
如果某个 JS 添加了一个突变观察者,其他 JS 是否有可能检测、删除、替换或更改该观察者?我担心的是,如果某些 JS 旨在破坏某些 DOM 元素而不被发现,那么 JS 可能想要摆脱任何观察该 DOM
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想要改善这个问题吗?更新问题,以便将其作为on-topi
有没有办法在您的 Activity/应用程序中(以编程方式)知道用户已通过 USB 将您的手机连接到 PC? 最佳答案 有人建议使用 UMS_CONNECTED自最新版本的 Android 起已弃用
我正在想办法测量速度滚动事件,这将产生某种代表速度的数字(相对于所花费的时间,从滚动点 A 到点 B 的距离)。 我欢迎任何以伪代码形式提出的建议...... 我试图在网上找到有关此问题的信息,但找不
某些 JavaScript 是否可以检测 Skype 是否安装? 我问的原因是我想基于此更改链接的 href:如果未安装 Skype,则显示一个弹出窗口,解释 Skype 是什么以及如何安装它,如果已
我们正在为 OS X 制作一个使用 Quartz Events 移动光标的用户空间设备驱动程序,当游戏(尤其是在窗口模式下运行的游戏)无法正确捕获鼠标指针时,我们遇到了问题(= 将其包含/保留在其窗口
我可以在 Controller 中看到事件 $routeChangeStart,但我不知道如何告诉 Angular 留下来。我需要弹出类似“您要保存、删除还是取消吗?”的信息。如果用户选择取消,则停留
我正在解决一个问题,并且已经花了一些时间。问题陈述:给你一个正整数和负整数的数组。如果索引处的数字 n 为正,则向前移动 n 步。相反,如果为负数(-n),则向后移动 n 步。假设数组的第一个元素向前
我试图建立一个条件,其中 [i] 是 data.length 的值,问题是当有超过 1 个值时一切正常,但当只有 1 个值时,脚本不起作用。 out.href = data[i].hr
这是我的问题,我需要检测图像中的 bolt 和四分之一,我一直在搜索并找到 OpenCV,但据我所知它还没有在 Java 中。你们打算如何解决这个问题? 最佳答案 实际上有一个 OpenCV 的 Ja
是否可以检测 ping? IE。设备 1 ping 设备 2,我想要可以在设备 2 上运行的代码,该代码可以在设备 1 ping 设备时进行检测。 最佳答案 ping 实用程序使用的字面消息(“ICM
我每天多次运行构建脚本。我的感觉是我和我的同事花费了大量时间等待这个脚本执行。现在想知道:我们每天花多少时间等待脚本执行? .我可以对总体平均值感到满意,即使我真的很想拥有每天的数据(例如“上周一我们
我已经完成了对项目的编码,但是当我在客户端中提交了源代码时,就对它进行了测试,然后检测到内存泄漏。我已经在Instruments using Leaks中进行了测试。 我遇到的问题是AVPlayer和
我想我可以用 std.traits.functionAttributes 来做到这一点,但它不支持 static。对于任何类型的可调用对象(包含 opCall 的结构),我如何判断该可调用对象是否使用
我正在使用多核 R 包中的并行和收集函数来并行化简单的矩阵乘法代码。答案是正确的,但并行版本似乎与串行版本花费的时间相同。 我怀疑它仅在一个内核上运行(而不是在我的机器上可用的 8 个内核!)。有没有
我正在尝试在读取 csv 文件时编写一个这样的 if 语句: if row = [] or EOF: do stuff 我在网上搜索过,但找不到任何方法可以做到这一点。帮忙? 最佳答案 wit
我想捕捉一个 onFontSizeChange 事件然后做一些事情(比如重新渲染,因为浏览器已经改变了我的字体大小)。不幸的是,不存在这样的事件,所以我必须找到一种方法来做到这一点。 我见过有人在不可
我有一个使用 Windows 服务的 C# 应用程序,该服务并非始终打开,我希望能够在该服务启动和关闭时发送电子邮件通知。我已经编写了电子邮件脚本,但我似乎无法弄清楚如何检测服务状态更改。 我一直在阅
我是一名优秀的程序员,十分优秀!