forked from ceu-lang/ceu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_tests_os.lua
More file actions
executable file
·199 lines (165 loc) · 4.56 KB
/
Copy pathrun_tests_os.lua
File metadata and controls
executable file
·199 lines (165 loc) · 4.56 KB
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
#!/usr/bin/env lua
_RUNTESTS = true
dofile 'pak.lua'
math.randomseed(os.time())
local LIBC = '/opt/musl-0.9.15'
STATS = {
count = 0,
mem = 0,
trails = 0,
bytes = 0,
}
function main (T)
local f = assert(io.open('_ceu_main.c','w'))
f:write([[
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include "ceu_os.h"
]])
for i, _ in ipairs(T) do
f:write([[
unsigned char* f_]]..i..[[;
tceu_app* app_]]..i..[[;
]])
end
f:write([[
int dt () {
return 10000;
}
int main (void)
{
int ret;
]])
-- LOAD APPS
for i, _ in ipairs(T) do
f:write([[
{
FILE* f = fopen("_ceu_app_]]..i..[[.o", "r");
assert(f != NULL);
fseek(f, 0, SEEK_END);
int sz = ftell(f) - 0x238;
f_]]..i..[[ = malloc(sz);
fseek(f, 0x238, SEEK_SET);
fread(f_]]..i..[[, 1, sz, f);
app_]]..i..[[ = ceu_sys_load(f_]]..i..[[);
}
]])
end
-- LINKS
T.lnks = T.lnks or {}
for i, t in ipairs(T.lnks) do
t[1] = 'app_'..t[1]
t[3] = 'app_'..t[3]
f:write([[
ceu_sys_link(]]..table.concat(t,',')..[[);
]])
end
-- START APPS
for i, _ in ipairs(T) do
f:write([[
ceu_sys_start(app_]]..i..[[);
]])
end
f:write([[
ret = ceu_scheduler(dt);
]])
for i, _ in ipairs(T) do
f:write([[
free(f_]]..i..[[);
]])
end
f:write([[
printf("*** END: %d\n", ret);
return ret;
}
]])
f:close()
end
Test = function (T)
if T.todo then
print('*** TODO: '..T.todo)
return
end
STATS.count = STATS.count + 1
main(T)
for i,src in ipairs(T) do
local name = '_ceu_app_'..i
local ceu = assert(io.open(name..'.ceu', 'w'))
ceu:write(src)
ceu:close()
local cmd = './ceu --os --verbose '..
'--out-c '..name..'.c '..
'--out-h '..name..'.h '..
name..'.ceu 2>&1'
assert(os.execute(cmd) == 0)
cmd = 'gcc -Os -Wall -DCEU_DEBUG -ansi '..
'-I '..LIBC..'/include '..
'-Wa,--execstack '..
'-fpie -nostartfiles '..
--'-mcall-prologues -mshort-calls '..
--'-nostdlib '..
--'-static-libgcc -static-libstdc++ '..
--'-static '..
'-Wl,-Telf_x86_64.x '..
--'-Wl,--strip-all ' ..
'-Wl,--no-export-dynamic '..
'-Wl,--gc-sections '..
--'-Wl,--no-check-sections '..
--'-Wl,--section-start=.export=0x400000 '..
--'-Wl,--section-start=.text=0x400026 '.. -- TODO: 0x26
--'-Wl,--section-start=.interp=0x400721 '.. -- TODO: 0x26
--'-Wl,--section-start=.rodata=0x40073d '.. -- TODO: 0x26
--'-Wl,--section-start=.note.gnu.build-id=0x40078c '.. -- TODO: 0x26
--'-Wl,--section-start=.eh_frame_hdr=0x4007b0 '.. -- TODO: 0x26
--'-Wl,--section-start=.gnuhash=0x4007f4 '.. -- TODO: 0x26
'-Wl,-uCEU_EXPORT '..
' -o '..name..'.o '..name..'.c '..
LIBC..'/lib/libc.a '..
''
print(cmd)
assert(os.execute(cmd) == 0)
-- no data and no undefined symbols
assert(os.execute('objdump -h '..name..'.o | fgrep ".data"') ~= 0)
assert(os.execute('nm -u '..name..'.o | fgrep -v " U _start"') ~= 0)
-- cd /opt/musl-0.9.15
-- rm lib/libc.a
-- ar rc lib/libc.a src/string/*.o src/stdio/*.o
end
local GCC = 'gcc -g -Wall -DCEU_OS -DCEU_DEBUG -ansi -lpthread '..
'-Wa,--execstack '..
'-o ceu.exe '..
'_ceu_main.c ceu_os.c ceu_pool.c'
print(GCC)
assert(os.execute(GCC) == 0)
local EXE = ((not _VALGRIND) and './ceu.exe 2>&1')
or 'valgrind -q --leak-check=full ./ceu.exe 2>&1'
--or 'valgrind -q --tool=helgrind ./ceu.exe 2>&1'
local ret = io.popen(EXE):read'*a'
assert(not string.find(ret, '==%d+=='), 'valgrind error')
local v = tonumber( string.match(ret, 'END: (.-)\n') )
if v then
assert(v==T.run, ret..' vs '..T.run..' expected')
else
assert( string.find(ret, T.run, nil, true) )
end
local f = io.popen('du -b ceu.exe')
local n = string.match(f:read'*a', '(%d+)')
STATS.bytes = STATS.bytes + n
f:close()
end
dofile 'tests_os.lua'
print([[
=====================================
STATS = {
count = ]]..STATS.count ..[[,
bytes = ]]..STATS.bytes ..[[,
}
]])
--[[
STATS = {
count = 39,
bytes = 1307901,
}
]]
os.execute('rm -f /tmp/_ceu_*')