Using gcc stack debug skill
The stack error is hard to debug, but we can debug it assisted by the tool provided by GCC. As we known, there are three options to debug the stack in gcc.
- -fstack-protector
- -fstack-protector-strong
- -fstack-protector-all To ALL functions
Here, we only check the –fstack-protector-all, see code example below.
#include <stdio.h>
#include <string.h> void bar(char* str)
{
char buf[4];
strcpy(buf, str);
} void foo()
{
printf("It survived!");
} int main(void)
{
bar("Longer than 4.");
foo();
return 0;
}
The code is wrong implementation obviously, it assigns more data to 4 byte memory in bar(). Compile it by gcc -ggdb -fstack-protector-all stack.c -o stack.
Debug it, you will find out the stack error.
gdb stack
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.3) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from stack...done.
(gdb) run
Starting program: /home/zjb/Documents/test/stack
*** stack smashing detected ***: /home/zjb/Documents/test/stack terminated
Program received signal SIGABRT, Aborted.
0x00007ffff7a47c37 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56 ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt full
#0 0x00007ffff7a47c37 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
resultvar = 0
pid = 25535
selftid = 25535
#1 0x00007ffff7a4b028 in __GI_abort () at abort.c:89
save_stage = 2
act = {__sigaction_handler = {sa_handler = 0x7ffff7a21ca8, sa_sigaction = 0x7ffff7a21ca8}, sa_mask = {__val = {140737353977856, 140737488346240, 140737351925564,
140733193388032, 524302793, 1, 3, 140737353979144, 140737351927990, 0, 140737488345920, 140737347931440, 140737488346192, 140737347942264, 8192231,
140737488346176}}, sa_flags = -136470528, sa_restorer = 0x0}
sigs = {__val = {32, 0 <repeats 15 times>}}
#2 0x00007ffff7a842a4 in __libc_message (do_abort=do_abort@entry=1, fmt=fmt@entry=0x7ffff7b93db0 "*** %s ***: %s terminated\n") at ../sysdeps/posix/libc_fatal.c:175
ap = {{gp_offset = 32, fp_offset = 0, overflow_arg_area = 0x7fffffffdd50, reg_save_area = 0x7fffffffdce0}}
fd = 3
on_2 = <optimized out>
list = <optimized out>
nlist = <optimized out>
cp = <optimized out>
written = <optimized out>
#3 0x00007ffff7b1f87c in __GI___fortify_fail (msg=<optimized out>, msg@entry=0x7ffff7b93d98 "stack smashing detected") at fortify_fail.c:38
do_abort = 1
#4 0x00007ffff7b1f820 in __stack_chk_fail () at stack_chk_fail.c:28
No locals.
#5 0x000000000040062f in bar (str=0x400751 "Longer than 4.") at stack.c:8
buf = "Long"
#6 0x000000000040068e in main () at stack.c:17
No locals.
(gdb) help bt
Print backtrace of all stack frames, or innermost COUNT frames.
With a negative argument, print outermost -COUNT frames.
Use of the 'full' qualifier also prints the values of the local variables.
Use of the 'no-filters' qualifier prohibits frame filters from executing
on this backtrace.
Using gcc stack debug skill的更多相关文章
- Stack Overflow Skill IQ Testing All In One
Stack Overflow Skill IQ Testing All In One Pluralsight IQ | Stack Overflow https://www.pluralsight.c ...
- GCC: compilation process..
gcc -Iproj/src myfile.c -o myfile gcc -c myfile.c "compile without linking gcc -D DEBUG myfile. ...
- golang debug调试
1. debug by gdb: office doc download the runtime-gdb file. $ wget -q -O - https://golang.org/src/run ...
- link options and how g++ is invoked gcc g++
yum install gcc yum install gcc-c++ yum reinstall gcc gcc-c++ Downloading packages:(1/2): gcc-c++-4. ...
- gcc栈溢出保护机制:stack-protector
关键词:stack-protector.stack-protector-strong.stack-protector-all等等. 1. gcc栈保护机制stack-protector简介 gcc提供 ...
- 结合python版本安装python-devel gcc和g++的区别 安装前做yum搜索
[test@ecs autocloudservices]# yum install python-develLoaded plugins: fastestmirrorLoading mirror sp ...
- 清华大学OS操作系统实验lab1练习知识点汇总
lab1知识点汇总 还是有很多问题,但是我觉得我需要在查看更多资料后回来再理解,学这个也学了一周了,看了大量的资料...还是它们自己的80386手册和lab的指导手册觉得最准确,现在我就把这部分知识做 ...
- C++成员函数指针错误用法警示(成员函数指针与高性能的C++委托,三篇),附好多评论
今天做一个成绩管理系统的并发引擎,用Qt做的,仿照QtConcurrent搞了个模板基类.这里为了隐藏细节,隔离变化,把并发的东西全部包含在模板基类中.子类只需注册需要并发执行的入口函数即可在单独线程 ...
- linux c 链接详解1-多目标文件链接
1. 多目标文件的链接 摘自:linux c编程一站式学习 http://learn.akae.cn/media/index.html 可以学会在linux下将多个c语言文件一起编译. 现在我们把例 ...
随机推荐
- core net 实现post 跟get
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using Syst ...
- Wrapper
开放封闭原则: 开放对扩展 封闭修改源代码 改变了人家调用方式 装饰器结构 """ 默认结构为三层!!!每层返回下一层内存地址就可以进行执行函数, 传参:语法糖中的传参可 ...
- nwjs 解决手指可滑动问题
package.json 中添加: "chromium-args": "--touch-events --enable-touch-drag-drop",
- FZU 2273 Triangles 第八届福建省赛 (三角形面积交 有重边算相交)
Problem Description This is a simple problem. Given two triangles A and B, you should determine they ...
- lintcode 刷题 by python 部分链表题总结(2)
本篇博客对最近做的链表的算法题做个简单的小结,主要描述题目和提供解题思路,具体代码见我的 github:https://github.com/MUSK1881/lintcode-by-python 3 ...
- Python学习笔记第七周
目录: 1.静态方法 @staticmethod 2.类方法 @classmethod 3.属性方法 @property 4.类的特殊成员方法 a) __doc__表示类的描述信息 b) __ ...
- Python学习笔记第六周
目录 一.基础概念 面向对象编程 1.面向对象的几个核心特点 1.class类 2.object对象 3.encapsulation封装 4.inheritance继承 5.polymorphism多 ...
- HttpClient官方sample代码的深入分析(连接池)
前言 之前一直使用apache的httpclient(4.5.x), 进行http的交互处理. 而httpclient实例则使用了http连接池, 而一旦涉及到连接池, 那会不会在使用上有些隐藏很 ...
- python之路,正则表达式
python3 正则表达式 前言: (1). 处理文本称为计算机主要工作之一(2)根据文本内容进行固定搜索是文本处理的常见工作(3)为了快速方便的处理上述问题,正则表达式技术诞生,逐渐发展为一个单独技 ...
- Angular 手动解析表达式
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...