Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ docker/c2wasm-api
docker/wasi-sdk
docker/clangd
docker/hook-cleaner
docker/guard_checker
docker/qjsc
guard-checker/guard_checker
wasi-sdk.ts
9 changes: 6 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
[submodule "hook-cleaner-c"]
path = hook-cleaner-c
url = https://github.com/XRPLF/hook-cleaner-c
[submodule "c2wasm-api/clang/includes"]
path = c2wasm-api/clang/includes
url = https://github.com/XRPLF/hook-macros
[submodule "quickjslite"]
path = quickjslite
url = https://github.com/RichardAH/quickjslite
[submodule "c2wasm-api/clang/includes"]
path = c2wasm-api/clang/includes
url = https://github.com/Xahau/xahaud
[submodule "guard-checker"]
path = guard-checker
url = https://github.com/RichardAH/guard-checker
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ checkout:
git submodule update --init --recursive llvm-project
git submodule update --init --recursive wasi-sdk
git submodule update --init --recursive hook-cleaner-c
git submodule update --init --recursive guard-checker
git submodule update --init --recursive quickjslite
git submodule update --init --recursive c2wasm-api/clang/includes

Expand All @@ -23,6 +24,7 @@ build: bin
$(MAKE) -C clang-build
$(MAKE) -C clangd-build
$(MAKE) -C cleaner-build
$(MAKE) -C guard-checker-build
$(MAKE) -C qjsc-build
$(MAKE) -C docker

Expand All @@ -39,6 +41,8 @@ clean:
$(MAKE) -C clang-build clean
$(MAKE) -C clangd-build clean
$(MAKE) -C cleaner-build clean
$(MAKE) -C guard-checker-build clean
$(MAKE) -C qjsc-build clean
$(MAKE) -C docker clean
-rm -rf bin doc

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ unzip bin.zip
```

- CD to docker folder `cd docker`
- Run `make c2wasm-api && make clangd && make wasi-sdk && make hook-cleaner && make qjsc`
- Run `make c2wasm-api && make clangd && make wasi-sdk && make hook-cleaner && make guard-checker && make qjsc`
- Run `docker-compose build`
- Run `docker-compose up` or `docker-compose up -d`
- 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.
2 changes: 1 addition & 1 deletion c2wasm-api/clang/includes
4 changes: 2 additions & 2 deletions c2wasm-api/copyheaders.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ then
else
mkdir -p /work/c
fi
cp clang/includes/*.h /work/c
cp clang/includes/hook/*.h /work/c

if [ -d /app/clang/includes ]
then
rm /app/clang/includes/*.h
else
mkdir -p /app/clang/includes
fi
cp clang/includes/*.h /app/clang/includes
cp clang/includes/hook/*.h /app/clang/includes
125 changes: 91 additions & 34 deletions c2wasm-api/src/chooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,37 +97,43 @@ function shell_exec(cmd: string, cwd: string) {
return result;
}

function get_optimization_options(options: string) {
const optimization_options = [
/* default '-O0' not included */ '-O1', '-O2', '-O3', '-O4', '-Os'
];

let safe_options = '';
for (let o of optimization_options) {
if (options.includes(o)) {
safe_options += ' ' + o;
}
}

return safe_options;
const optimization_level = '-O3'

function get_optimization_options() {
const options = [
'--shrink-level=100000000',
'--coalesce-locals-learning',
'--vacuum',
'--merge-blocks',
'--merge-locals',
'--flatten',
'--ignore-implicit-traps',
'-ffm',
'--const-hoisting',
'--code-folding',
'--code-pushing',
'--dae-optimizing',
'--dce',
'--simplify-globals-optimizing',
'--simplify-locals-nonesting',
'--reorder-locals',
'--rereloop',
'--precompute-propagate',
'--local-cse',
'--remove-unused-brs',
'--memory-packing',
'-c',
'--avoid-reinterprets',
optimization_level
]

return options.join(' ');
}

function get_clang_options(options: string) {
function get_clang_options() {
const clang_flags = `--sysroot=${sysroot} -xc -I/app/clang/includes -fdiagnostics-print-source-range-info -Werror=implicit-function-declaration`;
const miscellaneous_options = [
'-ffast-math', '-fno-inline', '-std=c99', '-std=c89'
];

let safe_options = '';
for (let o of miscellaneous_options) {
if (options.includes(o)) {
safe_options += ' ' + o;
} else if (o.includes('-std=') && options.toLowerCase().includes(o)) {
safe_options += ' ' + o;
}
}

return clang_flags + safe_options;
return clang_flags;
}

function get_lld_options(options: string) {
Expand Down Expand Up @@ -167,10 +173,12 @@ function validate_filename(name: string) {
return parts;
}

function link_c_files(source_files: string[], compile_options: string, link_options: string, cwd: string, output: string, result_obj: Task) {
function link_c_files(source_files: string[], link_options: string, cwd: string, output: string, result_obj: Task) {
const files = source_files.join(' ');
const clang = llvmDir + '/bin/clang';
const cmd = clang + ' ' + get_clang_options(compile_options) + ' ' + get_lld_options(link_options) + ' ' + files + ' -o ' + output;

const cmd = clang + ' ' + optimization_level + ' ' + get_clang_options() + ' ' + get_lld_options(link_options) + ' ' + files + ' -o ' + output;

const out = shell_exec(cmd, cwd);
result_obj.console = sanitize_shell_output(out);
if (!existsSync(output)) {
Expand Down Expand Up @@ -225,6 +233,27 @@ function clean_wasm(cwd: string, inplace: string, result_obj: Task) {
return success;
}

function guard_check_wasm(cwd: string, inplace: string, result_obj: Task) {
const cmd = 'guard_checker ' + inplace;
const out = openSync(cwd + '/guardout.log', 'w');
let error = '';
let success = true;
try {
execSync(cmd, { cwd, stdio: [null, out, out], });
} catch (ex: unknown) {
success = false;
if (ex instanceof Error) {
error = ex?.message;
}
} finally {
closeSync(out);
}
const out_msg = readFileSync(cwd + '/guardout.log').toString() || error;
result_obj.console = sanitize_shell_output(out_msg);
result_obj.success = success;
return success;
}

export function build_project(project: RequestBody, base: string) {
const output = project.output;
const compress = project.compress;
Expand Down Expand Up @@ -292,18 +321,18 @@ export function build_project(project: RequestBody, base: string) {
name: 'building wasm'
};
build_result.tasks.push(link_result_obj);
if (!link_c_files(sources, options || '', link_options || '', dir, result, link_result_obj)) {
if (!link_c_files(sources, link_options || '', dir, result, link_result_obj)) {
return complete(false, 'Build error');
}

const opt_options = get_optimization_options(options || '');
const opt_options = get_optimization_options();
if (opt_options) {
const opt_obj = {
name: 'optimizing wasm'
};
build_result.tasks.push(opt_obj);
if (!optimize_wasm(dir, result, opt_options, opt_obj)) {
return complete(false, 'Optimization error');
return complete(false, 'Pass 1 Optimization error');
}
}

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

if (opt_options) {
const opt_obj = {
name: 'optimizing wasm'
};
build_result.tasks.push(opt_obj);
if (!optimize_wasm(dir, result, opt_options, opt_obj)) {
return complete(false, 'Pass 2 Optimization error');
}
}

// if (strip) {
// const clean_obj = {
// name: 'cleaning wasm'
// };
// build_result.tasks.push(clean_obj);
// if (!clean_wasm(dir, result, clean_obj)) {
// return complete(false, 'Pass 2 Clean error');
// }
// }

const guard_result_obj = {
name: 'guard checking wasm'
};
build_result.tasks.push(guard_result_obj);
if (!guard_check_wasm(dir, result, guard_result_obj)) {
return complete(false, 'Guard checking error');
}

build_result.output = serialize_file_data(result, compress || false);

return complete(true, 'Success');
}
// END Compile code
// END Compile code
3 changes: 2 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ MAINTAINER Vaclav Barta "vaclav@equilibrium.co"
RUN apk --no-cache add su-exec
COPY clangd /usr/bin
WORKDIR /app
COPY c2wasm-api/clang/includes ./clang/includes
COPY c2wasm-api/clang/includes/hook ./clang/includes
COPY c2wasm-api/package.json .
COPY c2wasm-api/yarn.lock .
COPY c2wasm-api/tsconfig.json .
COPY c2wasm-api/src ./src
COPY wasi-sdk ./clang/wasi-sdk
COPY hook-cleaner /usr/bin
COPY guard_checker /usr/bin
COPY wasm-opt /usr/bin
COPY qjsc /usr/bin
COPY run.sh .
Expand Down
6 changes: 6 additions & 0 deletions docker/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ clangd: ../bin/clangd

hook-cleaner: ../bin/hook-cleaner
cp $< .
chmod +x hook-cleaner

guard-checker: ../bin/guard_checker
cp $< .
chmod +x guard_checker

c2wasm-api: ../c2wasm-api
cp -LR $< .

qjsc: ../bin/qjsc
cp $< .
chmod +x qjsc

clean:
-rm -rf c2wasm-api clangd wasi-sdk hook-cleaner qjsc ../wasi-sdk.ts
1 change: 1 addition & 0 deletions guard-checker
Submodule guard-checker added at de69e8
5 changes: 5 additions & 0 deletions guard-checker-build/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM --platform=linux/amd64 node:17-alpine
MAINTAINER Vaclav Barta "vaclav@equilibrium.co"
RUN apk --no-cache add --update bash build-base
ADD build.sh .
CMD ./build.sh
17 changes: 17 additions & 0 deletions guard-checker-build/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# must be run as root

GUARD_CHECKER_BUILD_CONTAINER_TAG?=guard-checker-build

all: build1 build2 install

build1:
docker build -t $(GUARD_CHECKER_BUILD_CONTAINER_TAG) .

build2:
docker run -v $(realpath ../guard-checker):/mnt/guard-checker $(GUARD_CHECKER_BUILD_CONTAINER_TAG):latest

install:
cp ../guard-checker/guard_checker ../bin

clean:
-rm ../guard-checker/guard_checker
7 changes: 7 additions & 0 deletions guard-checker-build/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

echo running build.sh...
set -e

cd /mnt/guard-checker
make
1 change: 1 addition & 0 deletions wasi-sdk
Submodule wasi-sdk added at 628938
Loading