简体   繁体   English

我如何找出正在改变C ++中函数的返回地址的内容

[英]How can I find out what's changing the return address of a function in c++

I have a program that behaves weirdly and probably has undefined behaviour. 我有一个程序,行为异常,并且可能具有未定义的行为。 Sometimes, the return address of a function seems to be changed, and I don't know what's causing it. 有时,函数的返回地址似乎已更改,但我不知道是什么原因引起的。

The return address is always changed to the same address, an assertion inside a function the control shouldn't be able to reach. 返回地址始终更改为相同的地址,而该控件内部的断言不应该到达。 I've been able to stop the program with a debugger to see that when it's supposed to execute a return statement, it jumps straight to the line with the assertion instead. 我已经能够使用调试器停止该程序,以查看应该执行return语句时,它直接跳到带有断言的行。

This code approximates how my function works. 此代码近似于我的函数的工作方式。

int foo(Vector t)
   double sum = 0;
   for(unsgined int i=0; i<t.size();++i){
        sum += t[i]; 
   }
   double limit = bar(); // bar returns a value between 0 and 1
   double a=0;
   for(double i=0; i<10; i++){
       a += f(i)/sum; // f(1)/sum + ... + f(10)/sum = 1.0f
       if(a>3)return a;
   }
   //shoudn'get here
   assert(false); // ... then this line is executed
}

This is what I've tried so far: 到目前为止,这是我尝试过的:

  • Switching all std::vector [] operators with .at to prevent accidentily writing into memory 使用.at切换所有std :: vector []运算符,以防止意外写入内存
  • Made sure all return-by-value values are const. 确保所有按值返回的值都是const。
  • Switched on -Wall and -Werror and -pedantic-errors in gcc 在gcc中打开-Wall-Werror以及-pedantic-errors
  • Ran the program with valgrind 用valgrind跑程序

I get a couple of invalid read of size 8 , but they seem to originate from qt, so I'm not sure what to make of it. 我收到了一些invalid read of size 8invalid read of size 8 ,但它们似乎源自qt,所以我不确定该怎么做。 Could this be the problem? 这可能是问题吗?

The error happens only occasionally when I have run the program for a while and give it certain input values, and more often in a release build than in a debug build. 仅当我运行程序一段时间并为其提供某些输入值时,才会偶尔发生该错误,并且在发行版本中而不是在调试版本中更常见。

EDIT: So I managed to reproduce the problem in a console application (no qt loaded) I then manages to simulate events that caused the problem. 编辑:因此,我设法在控制台应用程序(未加载qt)中重现该问题,然后设法模拟导致该问题的事件。

Like some of you suggested, it turns out I misjudged what was actually causing it to reach the assertion, probably due to my lack of experience with qt's debugger. 就像你们中的一些建议一样,事实证明我错误地判断了导致它到达断言的真正原因,这可能是由于我对qt调试器缺乏经验所致。 The actual problem was a floating point error in the double i used as a loop condition. 实际的问题是用作循环条件的double中的浮点错误。

I was implementing softmax, but exp(x) got rounded to zero with particular inputs. 我正在实现softmax,但是使用特定的输入将exp(x)舍入为零。

Now, as I have solved the problem, I might rephrase it. 现在,当我解决了问题时,我可以改写一下。 Is there a method for checking problems like rounding errors automatically. 有没有一种方法可以自动检查诸如舍入错误之类的问题。 Ie breaking on 0/0 for instance? 即打破0/0例如?

The short answer is: 简短的答案是:

The most portable way of determining if a floating-point exceptional condition has occurred is to use the floating-point exception facilities provided by C in fenv.h. 确定是否发生浮点异常情况的最可移植方法是使用fenv.h中C提供的浮点异常功能。

although, unfortunately, this is far from being perfect. 尽管不幸的是,这远非完美。

I suggest you to read both https://www.securecoding.cert.org/confluence/display/seccode/FLP04-C.+Check+floating-point+inputs+for+exceptional+values and https://www.securecoding.cert.org/confluence/display/seccode/FLP03-C.+Detect+and+handle+floating-point+errors which concisely address the exact question you are posing: 我建议您同时阅读https://www.securecoding.cert.org/confluence/display/seccode/FLP04-C.+Check+floating-point+inputs+for+exception+valueshttps://www.securecoding .cert.org / confluence / display / seccode / FLP03-C。+ Detect + and + handle + floating-point +错误可以简明地解决您所提出的确切问题:

Is there a method for checking problems like rounding errors automatically. 有没有一种方法可以自动检查诸如舍入错误之类的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM