栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言常见的程序崩溃问题分析

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言常见的程序崩溃问题分析

文章目录
  • 1. 常见的程序崩溃示例
  • 2. 执行结果分析

1. 常见的程序崩溃示例

常见的崩溃类型有以下几种:

  • 对空指针指向的内存非法写操作
  • 对空指针指向的内存非法读操作
  • 除0操作
  • 大的临时变量或者递归等导致栈溢出
  • 对只读内存进行写操作
  • 堆越界写操作
  • 对已经释放的内存进行写操作

代码示例:

//corruption.c
#include 
#include 

void make_corruption(int type)
{
    printf("case %d:n", type);
    switch (type)
    {
	    case 0:
	    {
	        //对空指针指向的内存非法写操作
	        char *str = NULL;
	        str[0] = 0;
	        break;
	    }
	    case 1:
	    {
	        //对空指针指向的内存非法读操作
	        char *str = NULL;
	        int i = str[0];
	        printf("i=%d", i);
	        break;
	    }
	    case 2:
	    {
	        //除0操作
	        int i = 10;
	        int j = 0;
	        int ret = i / j;
	        printf("ret=%d", ret);
	        break;
	    }
	    case 3:
	    {
	        //大的临时变量导致栈溢出
	        //char str[8 * 1024 * 1024] = {0}; //需要验证时再放开该行,否则栈越界必现死机导致其他case无法验证
	        break;
	    }
	    case 4:
	    {
	        //对只读内存进行写操作
	        char *str = "abc";
	        str[0] = 10;
	        break;
	    }
	    case 5:
	    {
	        //堆越界写操作
	        int *a = (int *)malloc(sizeof(int) * 4);
	        int *b = (int *)malloc(sizeof(int) * 4);
	        printf("a:%p, b:%pn", a, b);
	        memset(a, 0, sizeof(int) * 8);
	        free(a);
	        free(b);
	        break;
	    }
	    case 6:
	    {
	        //对已经释放的内存进行写操作。
	        #if 0
	        	//没有死机
				char *str = (char *)malloc(SMALL_MEMORY);
				free(str);
				memset(str, 0, SMALL_MEMORY);
			#else
				//coredump
				char *str = (char *)malloc(LARGE_MEMORY);
				free(str);
				memset(str, 0, LARGE_MEMORY);
			#endif
	        break;
	    }
	    default:
	    {
	        printf("to do.n");
	    }
    }

    return;
}

int main()
{
    int type = 0;
    scanf("%d", &type);
    //制造程序崩溃
    make_corruption(type);

    return 0;
}
2. 执行结果分析
[root@localhost all_kinds_corrupt]# gcc -g corruption.c -o corrupt
[root@localhost all_kinds_corrupt]# ./corrupt
0
case 0:
Segmentation fault (core dumped)
[root@localhost all_kinds_corrupt]# ./corrupt
1
case 1:
Segmentation fault (core dumped)
[root@localhost all_kinds_corrupt]# ./corrupt
2
case 2:
Floating point exception (core dumped)
[root@localhost all_kinds_corrupt]# ./corrupt
4
case 4:
Segmentation fault (core dumped)
[root@localhost all_kinds_corrupt]# ./corrupt
5
case 5:
a:0x1ca9ac0, b:0x1ca9ae0
free(): invalid pointer
Segmentation fault (core dumped)
[root@localhost all_kinds_corrupt]# ./corrupt
6
case 6:

我们可以看到,case 6在两种不同内存大小情况下表现一同:
(1)小内存情况下没有死机,这是因为Glibc会将该内存放到unsorted bin,此时内存没有进行合并或者重新分配出去,可以进行写操作。
(2)对于大内存情况,Glibc会通过调用mmap(大于128K)从系统申请内存,在free之后会直接归还给操作系统,此时进行写操作是非法的。
至于Glibc对于不同大小的内存管理,则需要另外一篇文章来进行详细讲解了。

我们把case 3的代码放开,重新编译执行:

[root@localhost all_kinds_corrupt]# ./corrupt
100
段错误 (核心已转储)

type=100时应该是走进default分支,但实际上程序产生了段错误,其原因就是栈溢出。我们可以通过ulimit -s查看栈的默认大小,通常为8M。

[root@localhost all_kinds_corrupt]# ulimit -s
8192
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/938249.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号