Skip to content

Commit df2b725

Browse files
Hardened native timers and V8 background tasks, potentially fixing reported intermittent V8 crashes. Tested with V8 5.1.281.65.
1 parent a453fe9 commit df2b725

27 files changed

Lines changed: 603 additions & 40 deletions

ClearScript/ClearScript.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@
9494
<Compile Include="Util\CoTaskMemBlock.cs" />
9595
<Compile Include="Util\DisposedFlag.cs" />
9696
<Compile Include="HostList.cs" />
97+
<Compile Include="Util\INativeCallback.cs" />
9798
<Compile Include="Util\IHostInvokeContext.cs" />
99+
<Compile Include="Util\NativeCallbackTimer.cs" />
98100
<Compile Include="Util\NativeMethods.cs" />
99101
<Compile Include="Util\COMDispatch.cs" />
100102
<Compile Include="Util\IScriptMarshalWrapper.cs" />
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
//
4+
// Microsoft Public License (MS-PL)
5+
//
6+
// This license governs use of the accompanying software. If you use the
7+
// software, you accept this license. If you do not accept the license, do not
8+
// use the software.
9+
//
10+
// 1. Definitions
11+
//
12+
// The terms "reproduce," "reproduction," "derivative works," and
13+
// "distribution" have the same meaning here as under U.S. copyright law. A
14+
// "contribution" is the original software, or any additions or changes to
15+
// the software. A "contributor" is any person that distributes its
16+
// contribution under this license. "Licensed patents" are a contributor's
17+
// patent claims that read directly on its contribution.
18+
//
19+
// 2. Grant of Rights
20+
//
21+
// (A) Copyright Grant- Subject to the terms of this license, including the
22+
// license conditions and limitations in section 3, each contributor
23+
// grants you a non-exclusive, worldwide, royalty-free copyright license
24+
// to reproduce its contribution, prepare derivative works of its
25+
// contribution, and distribute its contribution or any derivative works
26+
// that you create.
27+
//
28+
// (B) Patent Grant- Subject to the terms of this license, including the
29+
// license conditions and limitations in section 3, each contributor
30+
// grants you a non-exclusive, worldwide, royalty-free license under its
31+
// licensed patents to make, have made, use, sell, offer for sale,
32+
// import, and/or otherwise dispose of its contribution in the software
33+
// or derivative works of the contribution in the software.
34+
//
35+
// 3. Conditions and Limitations
36+
//
37+
// (A) No Trademark License- This license does not grant you rights to use
38+
// any contributors' name, logo, or trademarks.
39+
//
40+
// (B) If you bring a patent claim against any contributor over patents that
41+
// you claim are infringed by the software, your patent license from such
42+
// contributor to the software ends automatically.
43+
//
44+
// (C) If you distribute any portion of the software, you must retain all
45+
// copyright, patent, trademark, and attribution notices that are present
46+
// in the software.
47+
//
48+
// (D) If you distribute any portion of the software in source code form, you
49+
// may do so only under this license by including a complete copy of this
50+
// license with your distribution. If you distribute any portion of the
51+
// software in compiled or object code form, you may only do so under a
52+
// license that complies with this license.
53+
//
54+
// (E) The software is licensed "as-is." You bear the risk of using it. The
55+
// contributors give no express warranties, guarantees or conditions. You
56+
// may have additional consumer rights under your local laws which this
57+
// license cannot change. To the extent permitted under your local laws,
58+
// the contributors exclude the implied warranties of merchantability,
59+
// fitness for a particular purpose and non-infringement.
60+
//
61+
62+
using System;
63+
64+
namespace Microsoft.ClearScript.Util
65+
{
66+
internal interface INativeCallback : IDisposable
67+
{
68+
void Invoke();
69+
}
70+
}

ClearScript/Util/MiscHelpers.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
using System.Globalization;
6565
using System.Linq;
6666
using System.Text;
67+
using System.Threading;
6768

6869
namespace Microsoft.ClearScript.Util
6970
{
@@ -377,6 +378,17 @@ public static bool IsX86InstructionSet()
377378
(info.ProcessorArchitecture == 9 /*PROCESSOR_ARCHITECTURE_AMD64*/));
378379
}
379380

381+
public static void QueueNativeCallback(INativeCallback callback)
382+
{
383+
ThreadPool.QueueUserWorkItem(state =>
384+
{
385+
using (callback)
386+
{
387+
Try(callback.Invoke);
388+
}
389+
});
390+
}
391+
380392
#region Nested type: EmptyArray<T>
381393

382394
private static class EmptyArray<T>
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
//
4+
// Microsoft Public License (MS-PL)
5+
//
6+
// This license governs use of the accompanying software. If you use the
7+
// software, you accept this license. If you do not accept the license, do not
8+
// use the software.
9+
//
10+
// 1. Definitions
11+
//
12+
// The terms "reproduce," "reproduction," "derivative works," and
13+
// "distribution" have the same meaning here as under U.S. copyright law. A
14+
// "contribution" is the original software, or any additions or changes to
15+
// the software. A "contributor" is any person that distributes its
16+
// contribution under this license. "Licensed patents" are a contributor's
17+
// patent claims that read directly on its contribution.
18+
//
19+
// 2. Grant of Rights
20+
//
21+
// (A) Copyright Grant- Subject to the terms of this license, including the
22+
// license conditions and limitations in section 3, each contributor
23+
// grants you a non-exclusive, worldwide, royalty-free copyright license
24+
// to reproduce its contribution, prepare derivative works of its
25+
// contribution, and distribute its contribution or any derivative works
26+
// that you create.
27+
//
28+
// (B) Patent Grant- Subject to the terms of this license, including the
29+
// license conditions and limitations in section 3, each contributor
30+
// grants you a non-exclusive, worldwide, royalty-free license under its
31+
// licensed patents to make, have made, use, sell, offer for sale,
32+
// import, and/or otherwise dispose of its contribution in the software
33+
// or derivative works of the contribution in the software.
34+
//
35+
// 3. Conditions and Limitations
36+
//
37+
// (A) No Trademark License- This license does not grant you rights to use
38+
// any contributors' name, logo, or trademarks.
39+
//
40+
// (B) If you bring a patent claim against any contributor over patents that
41+
// you claim are infringed by the software, your patent license from such
42+
// contributor to the software ends automatically.
43+
//
44+
// (C) If you distribute any portion of the software, you must retain all
45+
// copyright, patent, trademark, and attribution notices that are present
46+
// in the software.
47+
//
48+
// (D) If you distribute any portion of the software in source code form, you
49+
// may do so only under this license by including a complete copy of this
50+
// license with your distribution. If you distribute any portion of the
51+
// software in compiled or object code form, you may only do so under a
52+
// license that complies with this license.
53+
//
54+
// (E) The software is licensed "as-is." You bear the risk of using it. The
55+
// contributors give no express warranties, guarantees or conditions. You
56+
// may have additional consumer rights under your local laws which this
57+
// license cannot change. To the extent permitted under your local laws,
58+
// the contributors exclude the implied warranties of merchantability,
59+
// fitness for a particular purpose and non-infringement.
60+
//
61+
62+
using System;
63+
using System.Threading;
64+
65+
namespace Microsoft.ClearScript.Util
66+
{
67+
internal sealed class NativeCallbackTimer : IDisposable
68+
{
69+
private readonly Timer timer;
70+
private readonly INativeCallback callback;
71+
private readonly InterlockedDisposedFlag disposedFlag = new InterlockedDisposedFlag();
72+
73+
public NativeCallbackTimer(int dueTime, int period, INativeCallback callback)
74+
{
75+
this.callback = callback;
76+
timer = new Timer(OnTimer, null, Timeout.Infinite, Timeout.Infinite);
77+
78+
if ((dueTime != Timeout.Infinite) || (period != Timeout.Infinite))
79+
{
80+
timer.Change(dueTime, period);
81+
}
82+
}
83+
84+
public bool Change(int dueTime, int period)
85+
{
86+
if (!disposedFlag.IsSet())
87+
{
88+
bool result;
89+
if (MiscHelpers.Try(out result, () => timer.Change(dueTime, period)))
90+
{
91+
return result;
92+
}
93+
}
94+
95+
return false;
96+
}
97+
98+
private void OnTimer(object state)
99+
{
100+
if (!disposedFlag.IsSet())
101+
{
102+
MiscHelpers.Try(callback.Invoke);
103+
}
104+
}
105+
106+
public void Dispose()
107+
{
108+
if (disposedFlag.Set())
109+
{
110+
timer.Dispose();
111+
callback.Dispose();
112+
}
113+
}
114+
}
115+
}

ClearScript/V8/ClearScriptV8/32/ClearScriptV8-32.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
<CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</CompileAsManaged>
132132
<CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</CompileAsManaged>
133133
</ClCompile>
134+
<ClCompile Include="..\NativeCallbackImpl.cpp" />
134135
<ClCompile Include="..\RefCount.cpp">
135136
<CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</CompileAsManaged>
136137
<CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</CompileAsManaged>
@@ -219,6 +220,7 @@
219220
<ClInclude Include="..\HostObjectHolderImpl.h" />
220221
<ClInclude Include="..\ManagedPlatform.h" />
221222
<ClInclude Include="..\Mutex.h" />
223+
<ClInclude Include="..\NativeCallbackImpl.h" />
222224
<ClInclude Include="..\NativePlatform.h" />
223225
<ClInclude Include="..\RefCount.h" />
224226
<ClInclude Include="..\SharedPtr.h" />

ClearScript/V8/ClearScriptV8/32/ClearScriptV8-32.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
<ClCompile Include="..\V8TestProxyImpl.cpp">
6262
<Filter>Source Files</Filter>
6363
</ClCompile>
64+
<ClCompile Include="..\NativeCallbackImpl.cpp">
65+
<Filter>Source Files</Filter>
66+
</ClCompile>
6467
</ItemGroup>
6568
<ItemGroup>
6669
<Filter Include="Header Files">
@@ -193,5 +196,8 @@
193196
<ClInclude Include="..\V8TestProxyImpl.h">
194197
<Filter>Header Files</Filter>
195198
</ClInclude>
199+
<ClInclude Include="..\NativeCallbackImpl.h">
200+
<Filter>Header Files</Filter>
201+
</ClInclude>
196202
</ItemGroup>
197203
</Project>

ClearScript/V8/ClearScriptV8/64/ClearScriptV8-64.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
<CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</CompileAsManaged>
133133
<CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</CompileAsManaged>
134134
</ClCompile>
135+
<ClCompile Include="..\NativeCallbackImpl.cpp" />
135136
<ClCompile Include="..\RefCount.cpp">
136137
<CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</CompileAsManaged>
137138
<CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</CompileAsManaged>
@@ -220,6 +221,7 @@
220221
<ClInclude Include="..\HostObjectHolderImpl.h" />
221222
<ClInclude Include="..\ManagedPlatform.h" />
222223
<ClInclude Include="..\Mutex.h" />
224+
<ClInclude Include="..\NativeCallbackImpl.h" />
223225
<ClInclude Include="..\NativePlatform.h" />
224226
<ClInclude Include="..\RefCount.h" />
225227
<ClInclude Include="..\SharedPtr.h" />

ClearScript/V8/ClearScriptV8/64/ClearScriptV8-64.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
<ClCompile Include="..\V8TestProxyImpl.cpp">
6262
<Filter>Source Files</Filter>
6363
</ClCompile>
64+
<ClCompile Include="..\NativeCallbackImpl.cpp">
65+
<Filter>Source Files</Filter>
66+
</ClCompile>
6467
</ItemGroup>
6568
<ItemGroup>
6669
<Filter Include="Header Files">
@@ -193,5 +196,8 @@
193196
<ClInclude Include="..\V8TestProxyImpl.h">
194197
<Filter>Header Files</Filter>
195198
</ClInclude>
199+
<ClInclude Include="..\NativeCallbackImpl.h">
200+
<Filter>Header Files</Filter>
201+
</ClInclude>
196202
</ItemGroup>
197203
</Project>

ClearScript/V8/ClearScriptV8/ClearScriptV8Managed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,5 @@
8585
#include "V8ObjectImpl.h"
8686
#include "V8ScriptImpl.h"
8787
#include "V8DebugListenerImpl.h"
88+
#include "NativeCallbackImpl.h"
8889
#include "V8TestProxyImpl.h"

ClearScript/V8/ClearScriptV8/ClearScriptV8Native.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
#include "RefCount.h"
7070
#include "SharedPtr.h"
7171
#include "WeakRef.h"
72-
#include "Timer.h"
7372
#include "V8ObjectHolder.h"
7473
#include "V8ScriptHolder.h"
7574
#include "HostObjectHolder.h"
@@ -83,6 +82,7 @@
8382
#include "HostObjectHolderImpl.h"
8483
#include "V8ObjectHelpers.h"
8584
#include "HostObjectHelpers.h"
85+
#include "Timer.h"
8686
#include "V8IsolateImpl.h"
8787
#include "V8ContextImpl.h"
8888
#include "V8WeakContextBinding.h"

0 commit comments

Comments
 (0)