问题 为什么在Webkit运行时pow()计算错误?


我有一个Qt C ++应用程序,其中有一个GUI线程,其中发生一些浮点计算。它也打开了 QWebView 哪里有一个带有一些视频的flash播放器。

很明显,关闭QWebView会干扰新的下一个浮点运算。 所以 pow(double, double) 返回明确但不正确的值。

在一种情况下,它返回值 1000 比正确的多一倍。另一次它返回1。#inf 与参数一起使用时 pow(10.0, 2.0)

我必须提到它是在不同的计算机上测试的,并不是特定于特定的CPU。

您是否有任何关于如何在Webkit中找到协处理器出错的地方以及如何防止它的建议?

样本(仅限x64)

环境:Qt 4.7.4,C ++,HTML和flowplayer

CPP

wrongpow::wrongpow(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
 QVBoxLayout* layout = new QVBoxLayout(0);  
 m_view = new QWebView(this);  
 m_view->setMinimumSize(400, 400);
 m_view->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
 m_view->settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, true);
 layout->addWidget(m_view);
 QDir dir(QApplication::applicationDirPath());
 dir.cd("media");
 m_view->load(QUrl(QFileInfo(dir, "index.html").absoluteFilePath()));

 QPushButton* button = new QPushButton(QLatin1String("Click on video start"), this);
 layout->addWidget(button);
 Q_ASSERT(connect(button, SIGNAL(clicked()), this, SLOT(closeView()))); 

 setLayout(layout);
 adjustSize();
}  
Q_SLOT void wrongpow::closeView()
{
 delete m_view;
 m_view = NULL;

 double wrongResult = pow(10.0, 2.0);
 Q_ASSERT(wrongResult == 100.0);
}

HTML

<div id='player' style='width:100%; height:100%;'>
    <object width='100%' height='100%' id='_494187117' name='_494187117' data='js/plugins/flowplayer-3.2.18.swf' type='application/x-shockwave-flash'>                      
        <param name='wmode' value='opaque'>         
        <param name='flashvars' value='config={&quot;clip&quot;:{&quot;url&quot;:&quot;mp4:vod/demo.flowplayer/buffalo_soldiers.mp4&quot;,&quot;scaling&quot;:&quot;fit&quot;,&quot;provider&quot;:&quot;hddn&quot;,&quot;live&quot;:true},&quot;plugins&quot;:{&quot;hddn&quot;:{&quot;url&quot;:&quot;js/plugins/flowplayer.rtmp-3.2.13.swf&quot;,&quot;netConnectionUrl&quot;:&quot;rtmp://r.demo.flowplayer.netdna-cdn.com/play&quot;}},&quot;canvas&quot;:{&quot;backgroundGradient&quot;:&quot;none&quot;}}'>
    </object>
</div>  

这是一个包含来源的完整工作计划: 下载15MB


3621
2017-07-17 06:02


起源

请添加用于重现问题的代码。你认为webkit如何干扰pow() - 函数? - Sebastian Lange
@ sebastian-lange它确实会以某种方式干扰。看这里: 截图 我发现只有第一次调用pow会返回错误的结果。此外,我会尽力制作一个工作样本,因为实际应用程序中有数千个代码行。 - Ezee
@Ezee您重现的步骤是XCode中两行代码的屏幕截图?你完全误解了这些是什么。我们相信你。我们并没有要求提供它发生的证明。我们需要重现这个问题才能给你正确的解释。我们应该如何从带有两行代码的屏幕截图到那里? (注意“我们”是StackOverflow,特别是我。) - Pascal Cuoq
@Ezee这是一个很棒的问题,如果在48小时内没有回答,我会给它一个赏金。一般说法是浮点数学在具有状态(特别是当前舍入模式)的FPU内实现。状态以命令式方式改变(计算 2.0 + 3.0四舍五入,指令序列是“更改为'向上''模式,然后计算 2.0 + 3.0,然后再次改变舍入模式 - 或者不是“)。一些基本的浮点函数(例如 sin 和 pow)使用精细的浮点运算实现,如果舍入模式可以完全错误... - Pascal Cuoq
MSDN声明两者都是为了 的x87 和 SSE 控制词, “修改FPCSR中任何字段的被调用者必须在返回其调用者之前恢复它们。此外,修改任何这些字段的调用者必须在调用被调用者之前将它们恢复为标准值,除非通过协议被调用者期望修改值“。。进入/退出任何功能的标准精度和舍入模式是Double和Round-to-Nearest。你能核实一下吗? - Iwillnotexist Idonotexist


答案:


结果不正确的原因 pow 是x87寄存器ST0-ST3成了 1#SNAN 所以TAGS = 0xFF。它发生在包含视频flash对象的QWebView的破坏期间。 x87和SSE的控制字包含正确的值。 经过测试的Adove Flash库:NPSWF64_14_0_0_125.dll

它发生在 WebCore::PluginView::stop method调用Adobe Flash插件对象的析构函数。

NPError npErr = m_plugin->pluginFuncs()->destroy(m_instance, &savedData);

这是破坏寄存器的程序(NPSWF64.dll)(实际上它使用与x87寄存器相关的MMX寄存器):

mov         qword ptr [rsp+8],rcx 
mov         qword ptr [rsp+10h],rdx 
mov         qword ptr [rsp+18h],r8 
push        rsi  
push        rdi  
push        rbx  
mov         rdi,qword ptr [rsp+20h] 
mov         rsi,qword ptr [rsp+28h] 
mov         rcx,qword ptr [rsp+30h] 
cmp         rcx,20h 
jle         000000002F9D8A2D 
sub         rcx,20h 

// writes wrong values to the registers:
movq        mm0,mmword ptr [rsi] 
movq        mm1,mmword ptr [rsi+8] 
movq        mm2,mmword ptr [rsi+10h] 
movq        mm3,mmword ptr [rsi+18h] 

add         rsi,20h 
movq        mmword ptr [rdi],mm0
movq        mmword ptr [rdi+8],mm1 
movq        mmword ptr [rdi+10h],mm2 
movq        mmword ptr [rdi+18h],mm3 
add         rdi,20h 
sub         rcx,20h 
jge         000000002F9D89F1 
add         rcx,20h 
rep movs    byte ptr [rdi],byte ptr [rsi] 
pop         rbx  
pop         rdi  
pop         rsi  
ret         

防止错误的计算 pow 由此错误导致需要恢复寄存器值。我用最简单的方法来做到这一点。当插件被破坏时我打电话 pow 使用一些参数并恢复寄存器。下一个电话会是正确的。
通过使用方法将新值写入寄存器,有一种更复杂(但可能是正确的)方法 float.h 图书馆。


6
2017-07-26 13:25



@ pascal-cuoq我很感激您对调查结果的评论。 - Ezee


只是稍微补充一点,我想我遇到了同样的问题(在64位C#应用程序中调用Math.Cos,并且恰好在.NET Windows窗体WebBrowser控件中显示Flash视频)。

同样,在我的情况下,似乎64位Flash OCX(16.0.0.305)中的以下代码在MM0,MM1,MM2,MM3寄存器中留下了无效值(此代码似乎是某种快速内存复制) :

C:\Windows\System32\Macromed\Flash\Flash64_16_0_0_305.ocx
00000000`2f9833c0 48894c2408      mov     qword ptr [rsp+8],rcx
00000000`2f9833c5 4889542410      mov     qword ptr [rsp+10h],rdx
00000000`2f9833ca 4c89442418      mov     qword ptr [rsp+18h],r8
00000000`2f9833cf 56              push    rsi
00000000`2f9833d0 57              push    rdi
00000000`2f9833d1 53              push    rbx
00000000`2f9833d2 488b7c2420      mov     rdi,qword ptr [rsp+20h]
00000000`2f9833d7 488b742428      mov     rsi,qword ptr [rsp+28h]
00000000`2f9833dc 488b4c2430      mov     rcx,qword ptr [rsp+30h]
00000000`2f9833e1 4881f920000000  cmp     rcx,20h
00000000`2f9833e8 7e43            jle     Flash64_16_0_0_305!DllUnregisterServer+0x3c2a2d (00000000`2f98342d)
00000000`2f9833ea 4881e920000000  sub     rcx,20h
00000000`2f9833f1 0f6f06          movq    mm0,mmword ptr [rsi]
00000000`2f9833f4 0f6f4e08        movq    mm1,mmword ptr [rsi+8]
00000000`2f9833f8 0f6f5610        movq    mm2,mmword ptr [rsi+10h]
00000000`2f9833fc 0f6f5e18        movq    mm3,mmword ptr [rsi+18h]
00000000`2f983400 4881c620000000  add     rsi,20h
00000000`2f983407 0f7f07          movq    mmword ptr [rdi],mm0
00000000`2f98340a 0f7f4f08        movq    mmword ptr [rdi+8],mm1
00000000`2f98340e 0f7f5710        movq    mmword ptr [rdi+10h],mm2
00000000`2f983412 0f7f5f18        movq    mmword ptr [rdi+18h],mm3
00000000`2f983416 4881c720000000  add     rdi,20h
00000000`2f98341d 4881e920000000  sub     rcx,20h
00000000`2f983424 7dcb            jge     Flash64_16_0_0_305!DllUnregisterServer+0x3c29f1 (00000000`2f9833f1)
00000000`2f983426 4881c120000000  add     rcx,20h
00000000`2f98342d f3a4            rep movs byte ptr [rdi],byte ptr [rsi]
00000000`2f98342f 5b              pop     rbx
00000000`2f983430 5f              pop     rdi
00000000`2f983431 5e              pop     rsi
00000000`2f983432 c3              ret

Flash OCX中的上述代码在Web浏览器离开显示Flash视频的页面时执行。

在此代码执行之前,浮点寄存器如下(使用WinDbg.exe检查):

fpcw=027f fpsw=3820 fptw=0080
st0= 6.00000000000000000000000...0e+0001 (0:4004:f000000000000000)
st1= 0.00000000000000000000000...0e+0000 (0:0000:0000000000000000)
st2= 0.00000000000000000000000...0e+0000 (0:0000:0000000000000000)
st3= 0.00000000000000000000000...0e+0000 (0:0000:0000000000000000)
st4= 0.00000000000000000000000...0e+0000 (0:0000:0000000000000000)
st5= 1.00000000000000000000000...0e+0000 (0:3fff:8000000000000000)
st6= 8.94231504669236176852000...0e-0001 (0:3ffe:e4ec5b1b9b742000)
st7= 0.00000000000000000000000...0e+0000 (0:0000:0000000000000000)
mxcsr=00001fa4

执行上述代码后,浮点寄存器如下:

fpcw=027f fpsw=0020 fptw=00ff
st0=-1.#SNAN000000000000000000...0e+0000 (1:7fff:0000000000000000)
st1=-1.#SNAN000000000000000000...0e+0000 (1:7fff:0000000000000000)
st2=-1.#SNAN000000000000000000...0e+0000 (1:7fff:0000000000000000)
st3=-1.#SNAN000000000000000000...0e+0000 (1:7fff:0000000000000000)
st4= 1.00000000000000000000000...0e+0000 (0:3fff:8000000000000000)
st5= 8.94231504669236176852000...0e-0001 (0:3ffe:e4ec5b1b9b742000)
st6= 0.00000000000000000000000...0e+0000 (0:0000:0000000000000000)
st7= 6.00000000000000000000000...0e+0001 (0:4004:f000000000000000)
mxcsr=00001fa4

此时浮点寄存器似乎处于“损坏”状态。

所以稍后,回到C#程序,当执行Math.Cos时,fld操作码失败,因为它试图将值2.0推送到浮点堆栈上(即,当它试图准备计算2.0的余弦时):

COMDouble::Cos:
000007FE`E1D01570  movsd   mmword ptr [rsp+8],xmm0
000007FE`E1D01576  fld     qword ptr [rsp+8]
000007FE`E1D0157A  fcos
000007FE`E1D0157C  fstp    qword ptr [rsp+8]
000007FE`E1D01580  movsd   xmm0,mmword ptr [rsp+8]
000007FE`E1D01586  ret

在执行fld操作码之前,浮点寄存器就像它们由Flash OCX保留的那样(即如上所述):

fpcw=027f fpsw=0020 fptw=00ff
st0=-1.#SNAN000000000000000000...0e+0000 (1:7fff:0000000000000000)
st1=-1.#SNAN000000000000000000...0e+0000 (1:7fff:0000000000000000)
st2=-1.#SNAN000000000000000000...0e+0000 (1:7fff:0000000000000000)
st3=-1.#SNAN000000000000000000...0e+0000 (1:7fff:0000000000000000)
st4= 1.00000000000000000000000...0e+0000 (0:3fff:8000000000000000)
st5= 8.94231504669236176852000...0e-0001 (0:3ffe:e4ec5b1b9b742000)
st6= 0.00000000000000000000000...0e+0000 (0:0000:0000000000000000)
st7= 6.00000000000000000000000...0e+0001 (0:4004:f000000000000000)
mxcsr=00001fa4

执行fld操作码后,浮点寄存器立即如下(注意st0是#IND而不是预期值2.00000000000000000000000 ... 0e + 0000):

fpcw=027f fpsw=3a61 fptw=00ff
st0=-1.#IND0000000000000000000...0e+0000 (1:7fff:c000000000000000)
st1=-1.#SNAN000000000000000000...0e+0000 (1:7fff:0000000000000000)
st2=-1.#SNAN000000000000000000...0e+0000 (1:7fff:0000000000000000)
st3=-1.#SNAN000000000000000000...0e+0000 (1:7fff:0000000000000000)
st4=-1.#SNAN000000000000000000...0e+0000 (1:7fff:0000000000000000)
st5= 1.00000000000000000000000...0e+0000 (0:3fff:8000000000000000)
st6= 8.94231504669236176852000...0e-0001 (0:3ffe:e4ec5b1b9b742000)
st7= 0.00000000000000000000000...0e+0000 (0:0000:0000000000000000)
mxcsr=00001fa4

这意味着fcos操作码尝试计算#IND的余弦值。因此,最终C#中Math.Cos的返回值为Double.NaN而不是预期的-0.416146836547142。

解析上面的浮点状态字(FPSW)值0x3a61表示问题是“尝试将值加载到非空闲的寄存器中”:

3a61: 0011 1010 0110 0001

    TOP (13,12,11):          111
    C3,C2,C1,C0 (14,10,9,8): 0 010  (ie. C1 is 1)  <-- loading a value into a register which is not free
    IR (7):                  0  Interrupt Request
    SF (6):                  1  Stack Fault  <-- loading a value into a register which is not free
    P (5):                   1  Precision
    U (4):                   0  Underflow
    O (3):                   0  Overflow
    Z (2):                   0  Zero Divide
    D (1):                   0  Denormalised
    I (0):                   1  Invalid Operation

请注意,问题仅在第一次尝试Math.Cos(2)时发生(即,在远离Flash视频页面导航后不久)。第二次和以后的计算Math.Cos(2)的尝试都成功了。

如果在Flash视频正在播放(或暂停)时处理WebBrowser控件,也会出现此问题。

所以一些潜在的解决方法是:

  1. 执行一个虚拟数学计算并忽略结果。

  2. 写一个64位DLL,它导出一个执行finit或emms操作码的函数(以重置浮点寄存器的状态)。从C#调用该函数。

  3. 以32位进程运行。

注意:在C#中抛出一个虚拟异常然后捕获该异常没有帮助(建议其他浮点损坏问题)。

下面是一些重现问题的C#代码(确保编译并运行为64位;在视频开始播放后单击“计算”按钮)。

using System;
using System.IO;
using System.Windows.Forms;

namespace FlashTest
{
    public partial class TestForm : Form
    {
        public TestForm()
        {
            // Windows 7 SP1 64 bit
            // Internet Explorer 11 (11.0.9600.17633)
            // Flash Player 16.0.0.305
            // Visual Studio 2013 (12.0.31101.00)
            // .NET 4.5 (4.0.30319.34209)

            InitializeComponent();
            addressTextBox.Text = "http://www.youtube.com/v/JVGdyC9CvFQ?autoplay=1";
            GoButtonClickHandler(this, EventArgs.Empty);
        }

        private void GoButtonClickHandler(object sender, EventArgs e)
        {
            string path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".html");
            File.WriteAllText(path, string.Format(@"<html><body>
                <object classid=""clsid:D27CDB6E-AE6D-11CF-96B8-444553540000"" width=""100%"" height=""100%"" id=""youtubeviewer"">
                    <param name=""movie"" value=""{0}"">
                </object></body></html>", addressTextBox.Text));
            webBrowser.Navigate(path);
        }

        private void CalculateButtonClickHandler(object sender, EventArgs e)
        {
            webBrowser.DocumentCompleted += DocumentCompletedHandler;
            webBrowser.Navigate("about:blank");
        }

        private void DocumentCompletedHandler(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            webBrowser.DocumentCompleted -= DocumentCompletedHandler;
            MessageBox.Show("Math.Cos(2) returned " + Math.Cos(2));
        }
    }
}

namespace FlashTest
{
    partial class TestForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.webBrowser = new System.Windows.Forms.WebBrowser();
            this.addressTextBox = new System.Windows.Forms.TextBox();
            this.goButton = new System.Windows.Forms.Button();
            this.calculateButton = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // webBrowser
            // 
            this.webBrowser.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.webBrowser.Location = new System.Drawing.Point(12, 41);
            this.webBrowser.MinimumSize = new System.Drawing.Size(20, 20);
            this.webBrowser.Name = "webBrowser";
            this.webBrowser.Size = new System.Drawing.Size(560, 309);
            this.webBrowser.TabIndex = 3;
            // 
            // addressTextBox
            // 
            this.addressTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.addressTextBox.Location = new System.Drawing.Point(12, 14);
            this.addressTextBox.Name = "addressTextBox";
            this.addressTextBox.Size = new System.Drawing.Size(398, 20);
            this.addressTextBox.TabIndex = 0;
            // 
            // goButton
            // 
            this.goButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.goButton.Location = new System.Drawing.Point(416, 12);
            this.goButton.Name = "goButton";
            this.goButton.Size = new System.Drawing.Size(75, 23);
            this.goButton.TabIndex = 1;
            this.goButton.Text = "&Go";
            this.goButton.UseVisualStyleBackColor = true;
            this.goButton.Click += new System.EventHandler(this.GoButtonClickHandler);
            // 
            // calculateButton
            // 
            this.calculateButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.calculateButton.Location = new System.Drawing.Point(497, 12);
            this.calculateButton.Name = "calculateButton";
            this.calculateButton.Size = new System.Drawing.Size(75, 23);
            this.calculateButton.TabIndex = 2;
            this.calculateButton.Text = "&Calculate";
            this.calculateButton.UseVisualStyleBackColor = true;
            this.calculateButton.Click += new System.EventHandler(this.CalculateButtonClickHandler);
            // 
            // TestForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(584, 362);
            this.Controls.Add(this.webBrowser);
            this.Controls.Add(this.goButton);
            this.Controls.Add(this.addressTextBox);
            this.Controls.Add(this.calculateButton);
            this.Name = "TestForm";
            this.Text = "Adobe Flash Test";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.WebBrowser webBrowser;
        private System.Windows.Forms.TextBox addressTextBox;
        private System.Windows.Forms.Button goButton;
        private System.Windows.Forms.Button calculateButton;
    }
}

2
2018-02-13 13:55





显然你遇到了你的Qt的webkit版本中的一个错误。我无法在QT 5.3中使用QVCreator版本在版本和调试中使用MSVC 13 x64进行重现。

已经为QtWebKit报告了一些浮点数的错误:


1
2017-07-17 16:06



*此错误与此问题无关。 *我在Qt 4.7和4.8中进行了测试。 - Ezee
我知道,我只是想说你不是第一个打到这个的人 类 webkit的bug。 :) - fjardon
实际上它变成了Adobe Flash Player插件中的一个错误。 - Ezee