Skip to content

Commit a4a51e3

Browse files
authored
Check against long.MaxValue instead of int in memory limit configuration (#2068)
1 parent 8750190 commit a4a51e3

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

Jint/Constraints/ConstraintsOptionsExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static Options LimitMemory(this Options options, long memoryLimit)
2727
{
2828
options.WithoutConstraint(x => x is MemoryLimitConstraint);
2929

30-
if (memoryLimit > 0 && memoryLimit < int.MaxValue)
30+
if (memoryLimit > 0 && memoryLimit < long.MaxValue)
3131
{
3232
options.Constraint(new MemoryLimitConstraint(memoryLimit));
3333
}

Jint/Constraints/MemoryLimitConstraint.cs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@ namespace Jint.Constraints;
44

55
public sealed class MemoryLimitConstraint : Constraint
66
{
7-
private static readonly Func<long>? GetAllocatedBytesForCurrentThread;
87
private readonly long _memoryLimit;
98
private long _initialMemoryUsage;
109

10+
#if !NET8_0_OR_GREATER
11+
private static readonly Func<long>? _getAllocatedBytesForCurrentThread;
12+
1113
static MemoryLimitConstraint()
1214
{
1315
var methodInfo = typeof(GC).GetMethod("GetAllocatedBytesForCurrentThread");
1416

1517
if (methodInfo != null)
1618
{
17-
GetAllocatedBytesForCurrentThread = (Func<long>)Delegate.CreateDelegate(typeof(Func<long>), null, methodInfo);
19+
_getAllocatedBytesForCurrentThread = (Func<long>) Delegate.CreateDelegate(typeof(Func<long>), null, methodInfo);
1820
}
1921
}
22+
#endif
2023

2124
internal MemoryLimitConstraint(long memoryLimit)
2225
{
@@ -25,28 +28,33 @@ internal MemoryLimitConstraint(long memoryLimit)
2528

2629
public override void Check()
2730
{
28-
if (_memoryLimit > 0)
31+
if (_memoryLimit <= 0)
2932
{
30-
if (GetAllocatedBytesForCurrentThread != null)
31-
{
32-
var memoryUsage = GetAllocatedBytesForCurrentThread() - _initialMemoryUsage;
33-
if (memoryUsage > _memoryLimit)
34-
{
35-
ExceptionHelper.ThrowMemoryLimitExceededException($"Script has allocated {memoryUsage} but is limited to {_memoryLimit}");
36-
}
37-
}
38-
else
39-
{
40-
ExceptionHelper.ThrowPlatformNotSupportedException("The current platform doesn't support MemoryLimit.");
41-
}
33+
return;
34+
}
35+
36+
#if NET8_0_OR_GREATER
37+
var usage = GC.GetAllocatedBytesForCurrentThread();
38+
#else
39+
if (_getAllocatedBytesForCurrentThread == null)
40+
{
41+
ExceptionHelper.ThrowPlatformNotSupportedException("The current platform doesn't support MemoryLimit.");
42+
}
43+
44+
var usage = _getAllocatedBytesForCurrentThread();
45+
#endif
46+
if (usage - _initialMemoryUsage > _memoryLimit)
47+
{
48+
ExceptionHelper.ThrowMemoryLimitExceededException($"Script has allocated {usage - _initialMemoryUsage} but is limited to {_memoryLimit}");
4249
}
4350
}
4451

4552
public override void Reset()
4653
{
47-
if (GetAllocatedBytesForCurrentThread != null)
48-
{
49-
_initialMemoryUsage = GetAllocatedBytesForCurrentThread();
50-
}
54+
#if NET8_0_OR_GREATER
55+
_initialMemoryUsage = GC.GetAllocatedBytesForCurrentThread();
56+
#else
57+
_initialMemoryUsage = _getAllocatedBytesForCurrentThread?.Invoke() ?? 0;
58+
#endif
5159
}
5260
}

0 commit comments

Comments
 (0)