Skip to content

Commit 6a954d4

Browse files
authored
Use Xahau Submodule (#38)
1 parent 2e0dc48 commit 6a954d4

File tree

14 files changed

+146
-42
lines changed

14 files changed

+146
-42
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ docker/c2wasm-api
55
docker/wasi-sdk
66
docker/clangd
77
docker/hook-cleaner
8+
docker/guard_checker
89
docker/qjsc
10+
guard-checker/guard_checker
911
wasi-sdk.ts

.gitmodules

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
[submodule "hook-cleaner-c"]
88
path = hook-cleaner-c
99
url = https://github.com/XRPLF/hook-cleaner-c
10-
[submodule "c2wasm-api/clang/includes"]
11-
path = c2wasm-api/clang/includes
12-
url = https://github.com/XRPLF/hook-macros
1310
[submodule "quickjslite"]
1411
path = quickjslite
1512
url = https://github.com/RichardAH/quickjslite
13+
[submodule "c2wasm-api/clang/includes"]
14+
path = c2wasm-api/clang/includes
15+
url = https://github.com/Xahau/xahaud
16+
[submodule "guard-checker"]
17+
path = guard-checker
18+
url = https://github.com/RichardAH/guard-checker

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ checkout:
88
git submodule update --init --recursive llvm-project
99
git submodule update --init --recursive wasi-sdk
1010
git submodule update --init --recursive hook-cleaner-c
11+
git submodule update --init --recursive guard-checker
1112
git submodule update --init --recursive quickjslite
1213
git submodule update --init --recursive c2wasm-api/clang/includes
1314

@@ -23,6 +24,7 @@ build: bin
2324
$(MAKE) -C clang-build
2425
$(MAKE) -C clangd-build
2526
$(MAKE) -C cleaner-build
27+
$(MAKE) -C guard-checker-build
2628
$(MAKE) -C qjsc-build
2729
$(MAKE) -C docker
2830

@@ -39,6 +41,8 @@ clean:
3941
$(MAKE) -C clang-build clean
4042
$(MAKE) -C clangd-build clean
4143
$(MAKE) -C cleaner-build clean
44+
$(MAKE) -C guard-checker-build clean
4245
$(MAKE) -C qjsc-build clean
4346
$(MAKE) -C docker clean
4447
-rm -rf bin doc
48+

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ unzip bin.zip
5656
```
5757

5858
- CD to docker folder `cd docker`
59-
- Run `make c2wasm-api && make clangd && make wasi-sdk && make hook-cleaner && make qjsc`
59+
- Run `make c2wasm-api && make clangd && make wasi-sdk && make hook-cleaner && make guard-checker && make qjsc`
6060
- Run `docker-compose build`
6161
- Run `docker-compose up` or `docker-compose up -d`
6262
- This should start server at port `:9000`, the actual compiling endpoint is this: [http://localhost:9000/api/build](localhost:9000/api/build). Note that it takes a while to start.

c2wasm-api/clang/includes

c2wasm-api/copyheaders.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ then
99
else
1010
mkdir -p /work/c
1111
fi
12-
cp clang/includes/*.h /work/c
12+
cp clang/includes/hook/*.h /work/c
1313

1414
if [ -d /app/clang/includes ]
1515
then
1616
rm /app/clang/includes/*.h
1717
else
1818
mkdir -p /app/clang/includes
1919
fi
20-
cp clang/includes/*.h /app/clang/includes
20+
cp clang/includes/hook/*.h /app/clang/includes

c2wasm-api/src/chooks.ts

Lines changed: 91 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -97,37 +97,43 @@ function shell_exec(cmd: string, cwd: string) {
9797
return result;
9898
}
9999

100-
function get_optimization_options(options: string) {
101-
const optimization_options = [
102-
/* default '-O0' not included */ '-O1', '-O2', '-O3', '-O4', '-Os'
103-
];
104-
105-
let safe_options = '';
106-
for (let o of optimization_options) {
107-
if (options.includes(o)) {
108-
safe_options += ' ' + o;
109-
}
110-
}
111-
112-
return safe_options;
100+
const optimization_level = '-O3'
101+
102+
function get_optimization_options() {
103+
const options = [
104+
'--shrink-level=100000000',
105+
'--coalesce-locals-learning',
106+
'--vacuum',
107+
'--merge-blocks',
108+
'--merge-locals',
109+
'--flatten',
110+
'--ignore-implicit-traps',
111+
'-ffm',
112+
'--const-hoisting',
113+
'--code-folding',
114+
'--code-pushing',
115+
'--dae-optimizing',
116+
'--dce',
117+
'--simplify-globals-optimizing',
118+
'--simplify-locals-nonesting',
119+
'--reorder-locals',
120+
'--rereloop',
121+
'--precompute-propagate',
122+
'--local-cse',
123+
'--remove-unused-brs',
124+
'--memory-packing',
125+
'-c',
126+
'--avoid-reinterprets',
127+
optimization_level
128+
]
129+
130+
return options.join(' ');
113131
}
114132

115-
function get_clang_options(options: string) {
133+
function get_clang_options() {
116134
const clang_flags = `--sysroot=${sysroot} -xc -I/app/clang/includes -fdiagnostics-print-source-range-info -Werror=implicit-function-declaration`;
117-
const miscellaneous_options = [
118-
'-ffast-math', '-fno-inline', '-std=c99', '-std=c89'
119-
];
120-
121-
let safe_options = '';
122-
for (let o of miscellaneous_options) {
123-
if (options.includes(o)) {
124-
safe_options += ' ' + o;
125-
} else if (o.includes('-std=') && options.toLowerCase().includes(o)) {
126-
safe_options += ' ' + o;
127-
}
128-
}
129135

130-
return clang_flags + safe_options;
136+
return clang_flags;
131137
}
132138

133139
function get_lld_options(options: string) {
@@ -167,10 +173,12 @@ function validate_filename(name: string) {
167173
return parts;
168174
}
169175

170-
function link_c_files(source_files: string[], compile_options: string, link_options: string, cwd: string, output: string, result_obj: Task) {
176+
function link_c_files(source_files: string[], link_options: string, cwd: string, output: string, result_obj: Task) {
171177
const files = source_files.join(' ');
172178
const clang = llvmDir + '/bin/clang';
173-
const cmd = clang + ' ' + get_clang_options(compile_options) + ' ' + get_lld_options(link_options) + ' ' + files + ' -o ' + output;
179+
180+
const cmd = clang + ' ' + optimization_level + ' ' + get_clang_options() + ' ' + get_lld_options(link_options) + ' ' + files + ' -o ' + output;
181+
174182
const out = shell_exec(cmd, cwd);
175183
result_obj.console = sanitize_shell_output(out);
176184
if (!existsSync(output)) {
@@ -225,6 +233,27 @@ function clean_wasm(cwd: string, inplace: string, result_obj: Task) {
225233
return success;
226234
}
227235

236+
function guard_check_wasm(cwd: string, inplace: string, result_obj: Task) {
237+
const cmd = 'guard_checker ' + inplace;
238+
const out = openSync(cwd + '/guardout.log', 'w');
239+
let error = '';
240+
let success = true;
241+
try {
242+
execSync(cmd, { cwd, stdio: [null, out, out], });
243+
} catch (ex: unknown) {
244+
success = false;
245+
if (ex instanceof Error) {
246+
error = ex?.message;
247+
}
248+
} finally {
249+
closeSync(out);
250+
}
251+
const out_msg = readFileSync(cwd + '/guardout.log').toString() || error;
252+
result_obj.console = sanitize_shell_output(out_msg);
253+
result_obj.success = success;
254+
return success;
255+
}
256+
228257
export function build_project(project: RequestBody, base: string) {
229258
const output = project.output;
230259
const compress = project.compress;
@@ -292,18 +321,18 @@ export function build_project(project: RequestBody, base: string) {
292321
name: 'building wasm'
293322
};
294323
build_result.tasks.push(link_result_obj);
295-
if (!link_c_files(sources, options || '', link_options || '', dir, result, link_result_obj)) {
324+
if (!link_c_files(sources, link_options || '', dir, result, link_result_obj)) {
296325
return complete(false, 'Build error');
297326
}
298327

299-
const opt_options = get_optimization_options(options || '');
328+
const opt_options = get_optimization_options();
300329
if (opt_options) {
301330
const opt_obj = {
302331
name: 'optimizing wasm'
303332
};
304333
build_result.tasks.push(opt_obj);
305334
if (!optimize_wasm(dir, result, opt_options, opt_obj)) {
306-
return complete(false, 'Optimization error');
335+
return complete(false, 'Pass 1 Optimization error');
307336
}
308337
}
309338

@@ -313,12 +342,40 @@ export function build_project(project: RequestBody, base: string) {
313342
};
314343
build_result.tasks.push(clean_obj);
315344
if (!clean_wasm(dir, result, clean_obj)) {
316-
return complete(false, 'Post-build error');
345+
return complete(false, 'Pass 1 Clean error');
317346
}
318347
}
319348

349+
if (opt_options) {
350+
const opt_obj = {
351+
name: 'optimizing wasm'
352+
};
353+
build_result.tasks.push(opt_obj);
354+
if (!optimize_wasm(dir, result, opt_options, opt_obj)) {
355+
return complete(false, 'Pass 2 Optimization error');
356+
}
357+
}
358+
359+
// if (strip) {
360+
// const clean_obj = {
361+
// name: 'cleaning wasm'
362+
// };
363+
// build_result.tasks.push(clean_obj);
364+
// if (!clean_wasm(dir, result, clean_obj)) {
365+
// return complete(false, 'Pass 2 Clean error');
366+
// }
367+
// }
368+
369+
const guard_result_obj = {
370+
name: 'guard checking wasm'
371+
};
372+
build_result.tasks.push(guard_result_obj);
373+
if (!guard_check_wasm(dir, result, guard_result_obj)) {
374+
return complete(false, 'Guard checking error');
375+
}
376+
320377
build_result.output = serialize_file_data(result, compress || false);
321378

322379
return complete(true, 'Success');
323380
}
324-
// END Compile code
381+
// END Compile code

docker/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ MAINTAINER Vaclav Barta "vaclav@equilibrium.co"
44
RUN apk --no-cache add su-exec
55
COPY clangd /usr/bin
66
WORKDIR /app
7-
COPY c2wasm-api/clang/includes ./clang/includes
7+
COPY c2wasm-api/clang/includes/hook ./clang/includes
88
COPY c2wasm-api/package.json .
99
COPY c2wasm-api/yarn.lock .
1010
COPY c2wasm-api/tsconfig.json .
1111
COPY c2wasm-api/src ./src
1212
COPY wasi-sdk ./clang/wasi-sdk
1313
COPY hook-cleaner /usr/bin
14+
COPY guard_checker /usr/bin
1415
COPY wasm-opt /usr/bin
1516
COPY qjsc /usr/bin
1617
COPY run.sh .

docker/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,18 @@ clangd: ../bin/clangd
2525

2626
hook-cleaner: ../bin/hook-cleaner
2727
cp $< .
28+
chmod +x hook-cleaner
29+
30+
guard-checker: ../bin/guard_checker
31+
cp $< .
32+
chmod +x guard_checker
2833

2934
c2wasm-api: ../c2wasm-api
3035
cp -LR $< .
3136

3237
qjsc: ../bin/qjsc
3338
cp $< .
39+
chmod +x qjsc
3440

3541
clean:
3642
-rm -rf c2wasm-api clangd wasi-sdk hook-cleaner qjsc ../wasi-sdk.ts

guard-checker

Submodule guard-checker added at de69e8a

0 commit comments

Comments
 (0)