Optimization of different gcc versions based on STACK layout

Source: Internet
Author: User

Problem Introduction: The same problematic code runs well in 4u gcc3.4.6, but fails to run in 5u gcc4.1.2!

The simplified problem is as follows:

# Include <iostream> using namespace std; int test (void * null1, void * I, void * j, void * k, void * null2) {* (long *) i) = 1; * (long *) j) = 2; * (long *) k) = 3;} int main () {int null_1 = 0; int myvalue_ I = 3; int myvalue_j = 5; int myvalue_k = 5; int null_2 = 0; test (& null_1, & myvalue_ I, & myvalue_j, & myvalue_k, & null_2 ); cout <"I =" <myvalue_ I <", j =" <myvalue_j <", k =" <myvalue_k <endl; return 0 ;} // test the same code on a 4U machine: (gcc 3.4.6) [root @ machine1 test] $ g ++ t. cpp [root @ machine1 test] $. /. out I = 0, j = 0, k = 3 [root @ machine1 test] $ g ++ t. cpp-O2 [root @ machine1 test] $. /. out I = 1, j = 2, k = 3 // same code, test on 5U machine :( gcc 4.1.2) [root @ machine2 test] $ g ++ t. cpp [root @ machine2 test] $. /. out I = 0, j = 0, k = 3 [root @ machine2 test] $ g ++ t. cpp-O2 [root @ machine2 test] $. /. out I = 0, j = 0, k = 3

This code has a significant memory overwrite problem, that is, in 64-Bit mode, long is 8 bytes, and its own valid memory is only 4 bytes, which leads to overwrite between values! However, please note that after gcc3.4.6 uses-O2 optimization, the value is correct !!!

Find the problem through disassembly --

The diagram is as follows:

We can see that

 Gcc3.4.6 on a 4u machine uses the-O2 optimization scheme, and the variable sequence in the stack has changed. In this way, when assigning values to overwrite values, it goes from bottom to top (low address to high address) overwrite, so that the correct value can be read.

Gcc4.1.2 on a 5u machine is no longer optimized!

Therefore, this explains why gcc3.4.6 on 4u has no problem, but can read the value normally if there is a coverage problem. However, gcc4.1.2 on 5u has a problem and there is a coverage problem, the read value is incorrect.

Due to the coverage problem, whether it is gcc3.4.6 on 4u or gcc4.1.2 on 5u, if there is no null_2, it will overwrite other content in the stack, causing the program core when the stack frame is destroyed! So I added a null_1/2 parameter to prevent program Core

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.