CBMC 问题求解模型

本文提供了两个C语言编程问题的解决方案,涉及CBMC(C和C++自动软件验证工具)用于发现并修复代码中的错误,以及使用Frama-C进行代码注解和断言验证。学生需单独或团队协作提交修复后的代码和验证结果。
摘要由CSDN通过智能技术生成


Problem 1
This is an individual problem. Each student must work on it alone and submit her/his solution individually. Download and install CBMC is a Bounded Model Checker for C and C++
Automated Software Verification 2023/2024
Coursework 2
programs:
https://www.cprover.org/cbmc/
Question 1 [25 marks] The following C code for computing the greatest common divisor of
two positive integer numbers contains a fault:
#include <stdio.h>
//Euclidean algorithm
int gcd(int a, int b) {
return gcd(b % a, a);
}
int main() {
int a = 5, b = 2;
printf("gcd(%d, %d) = %d\n.", a, b, gcd(a, b));
a = 50, b = 6;
printf("gcd(%d, %d) = %d\n.", a, b, gcd(a, b));
return 0;
}
1. Provide the CBMC command line input/output which exposes the fault. [5 marks]
2. Explain the problem with the code. [10 marks]
3. Fix the problem, include the new version of the code into your submission. [10 marks]
Question 2 [25 marks] The following C code also contains a fault:
#include<stdio.h>
int main() {
char c;
char message[64];
int i = 0;
while( ((c = getchar()) != '.')) {
message[i] = c;
i++;
}
printf("Thank you for your feedback!");
return 0;
}
1. Provide the CBMC command line input/output which exposes the fault. [5 marks]
2. Explain the problem with the code. [10 marks]
3. Fix the problem, include the new version of the code into your submission. [10 marks]
Problem 2
Because Frama-C is only available on MacOS and Linux, the students who only have access
to a Windows computer should team up with a single other student. Although you can work
as a team on the assignment, both members of the team still should submit the solutions to
all problems individually.
Download and install Frama-C:
http://frama-c.com/
Question 1 [25 marks] Annotate the given code by specifying appropriate loop invariants
and verify the assert statement using Frama-C. The assert statement is already given in the
code.
int main(){
int a = 0;
int s = 0;
while(a != 10){
a++;
int b = 0;
while(b != 10){
b++;
s++;
}
}
//@assert s == 100;
}
1. Submit a printout of the annotated version of the code. [10 marks]
2. Submit a screenshot (with green marks) of Frama-C window showing that your code is
checkable by Frama-C. [5 marks]
3. Briefly describe the reasoning process you have gone through in order to arrive at the
solution. [10 marks]
Question 2 [25 marks] Annotate the given code by specifying appropriate loop invariants
and verify the assert statement using Frama-C. The assert statement is already given in the
code.
int f(int x){
return x+1;
}
void test(){
int s = 20;
int i = 0;
while (i<10){
s=f(s);
i++;
}
//@assert s==30;
}
1. Submit a printout of the annotated version of the code. [10 marks]
2. Submit a screenshot (with green marks) of Frama-C window showing that your code is
checkable by Frama-C. [5 marks]
3. Briefly describe the reasoning process you have gone through in order to arrive at the
solution. [10 marks]
Submit a single PDF file not exceeding 2MB in size. Change your screenshots to a lower
wechat codinghelp

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值