-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplicationRunner.cs
209 lines (169 loc) · 7.89 KB
/
ApplicationRunner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using CosmosELFCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using XSharp.Assembler.x86;
namespace Venera {
public static class ApplicationRunner {
private class Application {
String name = null;
const String entrypoint = "main";
//FILE
byte[] mCode = null;
List<Object> args = null;
public Application(String _name, byte[] _mCode, List<Object> _args) {
//Kernel.PrintDebug("trying to create new application! name: " + _name + " args: ");
this.name = _name; this.mCode = _mCode; this.args = _args;
}
public int runEntryPoint(string entry){
unsafe
{
fixed (byte* appPtr = mCode)
{
var exe = new UnmanagedExecutable(appPtr);
exe.Load();
exe.Link();
//Kernel.PrintDebug("Executing " + entry + " @ " + name);
ArgumentWriter aw = new ArgumentWriter();
if (args != null) {
foreach (Object arg in args) {
switch (arg) {
case char _ when arg is char:
Kernel.PrintDebug("Pushing char: " + arg);
aw.Push((char)arg);
break;
case byte _ when arg is byte:
aw.Push((byte)arg);
break;
case short _ when arg is short:
aw.Push((short)arg);
break;
case int _ when arg is int:
aw.Push((int)arg);
break;
case uint _ when arg is uint:
aw.Push((uint)arg);
break;
case string _ when arg is string:
unsafe
{
fixed (byte* str = Encoding.ASCII.GetBytes((string)arg))
{
aw.Push((uint)str);
}
}
break;
default:
Console.WriteLine("Unknown argument type: " + arg.GetType());
return -1;
}
}
}
//Kernel.PrintDebug("Invoke!");
exe.Invoke(entry);
//Kernel.PrintDebug("Invoked!");
return 0;
}
}
}
public int run() {
unsafe {
fixed (byte* appPtr = mCode) {
var exe = new UnmanagedExecutable(appPtr);
exe.Load();
exe.Link();
ArgumentWriter aw = new ArgumentWriter();
if (args != null)
{
foreach (Object arg in args)
{
switch (arg)
{
case char _ when arg is char:
aw.Push((char)arg);
break;
case byte _ when arg is byte:
aw.Push((byte)arg);
break;
case short _ when arg is short:
aw.Push((short)arg);
break;
case int _ when arg is int:
aw.Push((int)arg);
break;
case uint _ when arg is uint:
aw.Push((uint)arg);
break;
default:
Console.WriteLine("Unknown argument type: " + arg.GetType());
return -1;
}
}
}
//Kernel.PrintDebug("Invoking " + entrypoint + " @ " + name);
exe.Invoke(entrypoint);
return 0;
}
}
}
}
public static int runApplication(String aName, byte[] aCode, List<Object> args, string filename){
Application cApp = new Application(aName, aCode, args);
int ret;
int pid = ProcessTable.registerProcess(aName, filename, "main");
Console.WriteLine("Executing... pid: " + pid);
ret = cApp.run();
//Console.Write("ya!");
ProcessTable.unregisterProcess(pid);
return ret;
}
public static int runApplicationEntryPoint(String aName, byte[] aCode, List<Object> args, String entryPoint, string filename) {
//Kernel.PrintDebug("received request to run " + entryPoint + " @ " + aName);
Application cApp = new Application(aName, aCode, args);
//Kernel.PrintDebug("created new Application!");
int ret = 0;
int pid = ProcessTable.registerProcess(aName, filename, entryPoint);
//Kernel.PrintDebug("registered process with pid: " + pid);
Console.WriteLine("Executing... pid: " + pid);
ret = cApp.runEntryPoint(entryPoint);
//Console.Write("ya!");
ProcessTable.unregisterProcess(pid);
return ret;
}
}
public static class ProcessTable {
private static int cPID = 0;
static List<Process> _processTable = new();
public static int registerProcess(string name, string path, string entrypoint) {
//Kernel.PrintDebug("received request to register a process " + name);
_processTable.Add(new Process(++cPID, name, path));
StreamWriter file = File.AppendText("0:\\Venera\\Sys\\PT");
file.WriteLine(cPID + " " + name + " " + entrypoint + " " + path + " " + _processTable[^1].start);
file.Close();
FileStream procFile = File.OpenWrite("0:\\Venera\\Sys\\proc\\" + cPID);
procFile.Write(Encoding.ASCII.GetBytes(cPID + " " + name + " " + entrypoint + " " + path + " " + _processTable[^1].start));
procFile.Close();
//Kernel.PrintDebug(name + " is registered with PID " + cPID);
return cPID;
}
public static void unregisterProcess(int PID) {
_processTable.RemoveAll(x => x.pid == PID);
File.Delete("0:\\Venera\\Sys\\proc\\" + PID);
}
}
public class Process {
public int pid;
public string name;
public string execPath;
public DateTime start;
public Process(int _pid, string _name, string _execPath) {
//Kernel.PrintDebug("Creating Process " + _name + " with pid " + _pid);
pid = _pid;
name = _name;
execPath = _execPath;
start = DateTime.Now;
//start = DateTime.Now;
}
}
}