Compare commits
2 Commits
5ed5925a38
...
7a1f22eb8e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7a1f22eb8e | ||
|
|
f5cb2c8e2e |
@@ -14,7 +14,7 @@ public class JavaCompilationServiceIsolate : ICompilationService
|
||||
private readonly IsolateBoxPool _boxPool;
|
||||
|
||||
private const int CompilationTimeLimitSeconds = 30;
|
||||
private const int CompilationMemoryLimitMb = 512;
|
||||
private const int CompilationMemoryLimitMb = 1024; // JVM needs more memory
|
||||
|
||||
public JavaCompilationServiceIsolate(
|
||||
ILogger<JavaCompilationServiceIsolate> logger,
|
||||
@@ -85,6 +85,7 @@ public class JavaCompilationServiceIsolate : ICompilationService
|
||||
arguments.Add($"/box/{sourceFileName}");
|
||||
|
||||
var stderrFilePath = Path.Combine(boxDir, "compile_stderr.txt");
|
||||
var stdoutFilePath = Path.Combine(boxDir, "compile_stdout.txt");
|
||||
|
||||
var isolateResult = await _isolateService.RunAsync(new IsolateRunOptions
|
||||
{
|
||||
@@ -94,28 +95,57 @@ public class JavaCompilationServiceIsolate : ICompilationService
|
||||
TimeLimitSeconds = CompilationTimeLimitSeconds,
|
||||
WallTimeLimitSeconds = CompilationTimeLimitSeconds * 2,
|
||||
MemoryLimitKb = CompilationMemoryLimitMb * 1024,
|
||||
StackLimitKb = 256 * 1024,
|
||||
ProcessLimit = 10, // javac may spawn multiple processes
|
||||
StackLimitKb = 128 * 1024, // 128MB stack per process
|
||||
ProcessLimit = 64, // JVM needs many threads/processes
|
||||
EnableNetwork = false,
|
||||
StdoutFile = stdoutFilePath,
|
||||
StderrFile = stderrFilePath,
|
||||
WorkingDirectory = "/box",
|
||||
DirectoryBindings = new List<DirectoryBinding>
|
||||
{
|
||||
new DirectoryBinding { HostPath = "/usr/bin", SandboxPath = "/usr/bin", ReadOnly = true },
|
||||
new DirectoryBinding { HostPath = "/usr/lib", SandboxPath = "/usr/lib", ReadOnly = true },
|
||||
new DirectoryBinding { HostPath = "/lib", SandboxPath = "/lib", ReadOnly = true }
|
||||
new DirectoryBinding { HostPath = "/lib", SandboxPath = "/lib", ReadOnly = true },
|
||||
new DirectoryBinding { HostPath = "/lib64", SandboxPath = "/lib64", ReadOnly = true },
|
||||
new DirectoryBinding { HostPath = "/etc", SandboxPath = "/etc", ReadOnly = true }
|
||||
}
|
||||
});
|
||||
|
||||
var compilerOutput = string.Empty;
|
||||
|
||||
// Read stdout (javac may output errors there)
|
||||
if (File.Exists(stdoutFilePath))
|
||||
{
|
||||
var stdoutContent = await File.ReadAllTextAsync(stdoutFilePath);
|
||||
if (!string.IsNullOrWhiteSpace(stdoutContent))
|
||||
{
|
||||
compilerOutput = stdoutContent;
|
||||
_logger.LogDebug("Read stdout file: {Length} bytes", stdoutContent.Length);
|
||||
}
|
||||
}
|
||||
|
||||
// Read stderr
|
||||
if (File.Exists(stderrFilePath))
|
||||
{
|
||||
compilerOutput = await File.ReadAllTextAsync(stderrFilePath);
|
||||
var stderrContent = await File.ReadAllTextAsync(stderrFilePath);
|
||||
if (!string.IsNullOrWhiteSpace(stderrContent))
|
||||
{
|
||||
compilerOutput = string.IsNullOrEmpty(compilerOutput)
|
||||
? stderrContent
|
||||
: $"{compilerOutput}\n{stderrContent}";
|
||||
_logger.LogDebug("Read stderr file: {Length} bytes", stderrContent.Length);
|
||||
}
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(isolateResult.ErrorOutput))
|
||||
{
|
||||
compilerOutput = $"{compilerOutput}\n{isolateResult.ErrorOutput}".Trim();
|
||||
compilerOutput = string.IsNullOrEmpty(compilerOutput)
|
||||
? isolateResult.ErrorOutput
|
||||
: $"{compilerOutput}\n{isolateResult.ErrorOutput}";
|
||||
}
|
||||
|
||||
_logger.LogDebug("Final compiler output: {Output}", compilerOutput);
|
||||
|
||||
if (isolateResult.TimeLimitExceeded)
|
||||
{
|
||||
_logger.LogWarning("Java compilation time limit exceeded for box {BoxId}", boxId);
|
||||
|
||||
@@ -84,7 +84,15 @@ public class JavaExecutionServiceIsolate : IExecutionService
|
||||
EnableNetwork = false,
|
||||
StdinFile = sandboxInputPath,
|
||||
StdoutFile = outputFilePath,
|
||||
WorkingDirectory = "/box"
|
||||
WorkingDirectory = "/box",
|
||||
DirectoryBindings = new List<DirectoryBinding>
|
||||
{
|
||||
new DirectoryBinding { HostPath = "/usr/bin", SandboxPath = "/usr/bin", ReadOnly = true },
|
||||
new DirectoryBinding { HostPath = "/usr/lib", SandboxPath = "/usr/lib", ReadOnly = true },
|
||||
new DirectoryBinding { HostPath = "/lib", SandboxPath = "/lib", ReadOnly = true },
|
||||
new DirectoryBinding { HostPath = "/lib64", SandboxPath = "/lib64", ReadOnly = true },
|
||||
new DirectoryBinding { HostPath = "/etc", SandboxPath = "/etc", ReadOnly = true }
|
||||
}
|
||||
});
|
||||
|
||||
stopwatch.Stop();
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using LiquidCode.Tester.Worker.Services.Isolate;
|
||||
using LiquidCode.Tester.Worker.Models;
|
||||
|
||||
@@ -14,7 +15,7 @@ public class KotlinCompilationServiceIsolate : ICompilationService
|
||||
private readonly IsolateBoxPool _boxPool;
|
||||
|
||||
private const int CompilationTimeLimitSeconds = 30;
|
||||
private const int CompilationMemoryLimitMb = 512;
|
||||
private const int CompilationMemoryLimitMb = 1024; // Kotlin uses JVM, needs more memory
|
||||
|
||||
public KotlinCompilationServiceIsolate(
|
||||
ILogger<KotlinCompilationServiceIsolate> logger,
|
||||
@@ -38,7 +39,21 @@ public class KotlinCompilationServiceIsolate : ICompilationService
|
||||
|
||||
try
|
||||
{
|
||||
await File.WriteAllTextAsync(sourceFilePath, sourceCode);
|
||||
// Normalize line endings
|
||||
var normalizedSourceCode = sourceCode
|
||||
.Replace("\\r\\n", "\n") // Handle escaped \r\n (literal text "\\r\\n")
|
||||
.Replace("\\n", "\n") // Handle escaped \n (literal text "\\n")
|
||||
.Replace("\\r", "\n") // Handle escaped \r (literal text "\\r")
|
||||
.Replace("\r\n", "\n") // Handle actual Windows line endings
|
||||
.Replace("\r", "\n"); // Handle actual Mac line endings
|
||||
|
||||
_logger.LogDebug("Source code length: {Length}, normalized length: {NormalizedLength}, line count: {LineCount}",
|
||||
sourceCode.Length, normalizedSourceCode.Length, normalizedSourceCode.Split('\n').Length);
|
||||
|
||||
_logger.LogDebug("First 200 chars of source: {Preview}",
|
||||
normalizedSourceCode.Length > 200 ? normalizedSourceCode.Substring(0, 200) : normalizedSourceCode);
|
||||
|
||||
await File.WriteAllTextAsync(sourceFilePath, normalizedSourceCode);
|
||||
return await CompileFileInIsolateAsync(sourceFilePath, jarFilePath, version);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -88,6 +103,7 @@ public class KotlinCompilationServiceIsolate : ICompilationService
|
||||
arguments.Add($"/box/{jarFileName}");
|
||||
|
||||
var stderrFilePath = Path.Combine(boxDir, "compile_stderr.txt");
|
||||
var stdoutFilePath = Path.Combine(boxDir, "compile_stdout.txt");
|
||||
|
||||
var isolateResult = await _isolateService.RunAsync(new IsolateRunOptions
|
||||
{
|
||||
@@ -97,27 +113,57 @@ public class KotlinCompilationServiceIsolate : ICompilationService
|
||||
TimeLimitSeconds = CompilationTimeLimitSeconds,
|
||||
WallTimeLimitSeconds = CompilationTimeLimitSeconds * 2,
|
||||
MemoryLimitKb = CompilationMemoryLimitMb * 1024,
|
||||
StackLimitKb = 256 * 1024,
|
||||
ProcessLimit = 10,
|
||||
StackLimitKb = 128 * 1024, // 128MB stack per process
|
||||
ProcessLimit = 64, // Kotlin uses JVM, needs many threads/processes
|
||||
EnableNetwork = false,
|
||||
StdoutFile = stdoutFilePath,
|
||||
StderrFile = stderrFilePath,
|
||||
WorkingDirectory = "/box",
|
||||
DirectoryBindings = new List<DirectoryBinding>
|
||||
{
|
||||
new DirectoryBinding { HostPath = "/usr/lib", SandboxPath = "/usr/lib", ReadOnly = true },
|
||||
new DirectoryBinding { HostPath = "/lib", SandboxPath = "/lib", ReadOnly = true },
|
||||
new DirectoryBinding { HostPath = "/lib64", SandboxPath = "/lib64", ReadOnly = true },
|
||||
new DirectoryBinding { HostPath = "/usr/bin", SandboxPath = "/usr/bin", ReadOnly = true },
|
||||
new DirectoryBinding { HostPath = "/bin", SandboxPath = "/bin", ReadOnly = true },
|
||||
new DirectoryBinding { HostPath = "/etc", SandboxPath = "/etc", ReadOnly = true },
|
||||
new DirectoryBinding { HostPath = "/opt/kotlinc", SandboxPath = "/opt/kotlinc", ReadOnly = true }
|
||||
},
|
||||
EnvironmentVariables = new Dictionary<string, string>
|
||||
{
|
||||
["PATH"] = "/usr/local/bin:/usr/bin:/bin"
|
||||
}
|
||||
});
|
||||
|
||||
var compilerOutput = string.Empty;
|
||||
|
||||
// Read stdout (kotlinc may output errors there)
|
||||
if (File.Exists(stdoutFilePath))
|
||||
{
|
||||
var stdoutContent = await File.ReadAllTextAsync(stdoutFilePath);
|
||||
if (!string.IsNullOrWhiteSpace(stdoutContent))
|
||||
{
|
||||
compilerOutput = stdoutContent;
|
||||
}
|
||||
}
|
||||
|
||||
// Read stderr
|
||||
if (File.Exists(stderrFilePath))
|
||||
{
|
||||
compilerOutput = await File.ReadAllTextAsync(stderrFilePath);
|
||||
var stderrContent = await File.ReadAllTextAsync(stderrFilePath);
|
||||
if (!string.IsNullOrWhiteSpace(stderrContent))
|
||||
{
|
||||
compilerOutput = string.IsNullOrEmpty(compilerOutput)
|
||||
? stderrContent
|
||||
: $"{compilerOutput}\n{stderrContent}";
|
||||
}
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(isolateResult.ErrorOutput))
|
||||
{
|
||||
compilerOutput = $"{compilerOutput}\n{isolateResult.ErrorOutput}".Trim();
|
||||
compilerOutput = string.IsNullOrEmpty(compilerOutput)
|
||||
? isolateResult.ErrorOutput
|
||||
: $"{compilerOutput}\n{isolateResult.ErrorOutput}";
|
||||
}
|
||||
|
||||
if (isolateResult.TimeLimitExceeded)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Diagnostics;
|
||||
using System.Collections.Generic;
|
||||
using LiquidCode.Tester.Worker.Services.Isolate;
|
||||
|
||||
namespace LiquidCode.Tester.Worker.Services;
|
||||
@@ -78,7 +79,20 @@ public class KotlinExecutionServiceIsolate : IExecutionService
|
||||
EnableNetwork = false,
|
||||
StdinFile = sandboxInputPath,
|
||||
StdoutFile = outputFilePath,
|
||||
WorkingDirectory = "/box"
|
||||
WorkingDirectory = "/box",
|
||||
DirectoryBindings = new List<DirectoryBinding>
|
||||
{
|
||||
new DirectoryBinding { HostPath = "/usr/lib", SandboxPath = "/usr/lib", ReadOnly = true },
|
||||
new DirectoryBinding { HostPath = "/lib", SandboxPath = "/lib", ReadOnly = true },
|
||||
new DirectoryBinding { HostPath = "/lib64", SandboxPath = "/lib64", ReadOnly = true },
|
||||
new DirectoryBinding { HostPath = "/usr/bin", SandboxPath = "/usr/bin", ReadOnly = true },
|
||||
new DirectoryBinding { HostPath = "/bin", SandboxPath = "/bin", ReadOnly = true },
|
||||
new DirectoryBinding { HostPath = "/etc", SandboxPath = "/etc", ReadOnly = true }
|
||||
},
|
||||
EnvironmentVariables = new Dictionary<string, string>
|
||||
{
|
||||
["PATH"] = "/usr/local/bin:/usr/bin:/bin"
|
||||
}
|
||||
});
|
||||
|
||||
stopwatch.Stop();
|
||||
|
||||
Reference in New Issue
Block a user