Skip to content

Commit a2f3c72

Browse files
authored
Add wasm-mutator-fuzz test (#3420)
1 parent 33aada2 commit a2f3c72

34 files changed

Lines changed: 6621 additions & 0 deletions

tests/fuzz/wasm-mutator-fuzz/.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
portal_port=9999
2+
server_port=16667
3+
proxy=""
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build/
2+
workspace/build_*
3+
error_restart_build_*
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Copyright (C) 2019 Intel Corporation. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
cmake_minimum_required (VERSION 2.8)
5+
6+
project(wasm_mutator)
7+
8+
add_definitions(-DUNIT_TEST)
9+
10+
set (CMAKE_BUILD_TYPE Debug)
11+
12+
set (CMAKE_C_COMPILER "clang")
13+
set (CMAKE_CXX_COMPILER "clang++")
14+
15+
set (WAMR_BUILD_PLATFORM "linux")
16+
17+
# Reset default linker flags
18+
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
19+
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
20+
21+
set (CMAKE_C_STANDARD 99)
22+
23+
# Set WAMR_BUILD_TARGET, currently values supported:
24+
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
25+
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
26+
if (NOT DEFINED WAMR_BUILD_TARGET)
27+
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
28+
set (WAMR_BUILD_TARGET "AARCH64")
29+
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
30+
set (WAMR_BUILD_TARGET "RISCV64")
31+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
32+
# Build as X86_64 by default in 64-bit platform
33+
set (WAMR_BUILD_TARGET "X86_64")
34+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
35+
# Build as X86_32 by default in 32-bit platform
36+
set (WAMR_BUILD_TARGET "X86_32")
37+
else ()
38+
message(SEND_ERROR "Unsupported build target platform!")
39+
endif ()
40+
endif ()
41+
42+
if(CUSTOM_MUTATOR EQUAL 1)
43+
add_compile_definitions(CUSTOM_MUTATOR)
44+
endif()
45+
46+
if (NOT CMAKE_BUILD_TYPE)
47+
set(CMAKE_BUILD_TYPE Release)
48+
endif ()
49+
50+
if (NOT DEFINED WAMR_BUILD_INTERP)
51+
# Enable Interpreter by default
52+
set (WAMR_BUILD_INTERP 1)
53+
endif ()
54+
55+
if (NOT DEFINED WAMR_BUILD_AOT)
56+
# Enable AOT by default.
57+
set (WAMR_BUILD_AOT 1)
58+
endif ()
59+
60+
if (NOT DEFINED WAMR_BUILD_JIT)
61+
# Disable JIT by default.
62+
set (WAMR_BUILD_JIT 0)
63+
endif ()
64+
65+
if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN)
66+
# Enable libc builtin support by default
67+
set (WAMR_BUILD_LIBC_BUILTIN 1)
68+
endif ()
69+
70+
if (NOT DEFINED WAMR_BUILD_LIBC_WASI)
71+
# Enable libc wasi support by default
72+
set (WAMR_BUILD_LIBC_WASI 1)
73+
endif ()
74+
75+
if (NOT DEFINED WAMR_BUILD_FAST_INTERP)
76+
# Enable fast interpreter
77+
set (WAMR_BUILD_FAST_INTERP 1)
78+
endif ()
79+
80+
if (NOT DEFINED WAMR_BUILD_MULTI_MODULE)
81+
# Enable multiple modules
82+
set (WAMR_BUILD_MULTI_MODULE 0)
83+
endif ()
84+
85+
if (NOT DEFINED WAMR_BUILD_LIB_PTHREAD)
86+
# Disable pthread library by default
87+
set (WAMR_BUILD_LIB_PTHREAD 0)
88+
endif ()
89+
90+
if (NOT DEFINED WAMR_BUILD_MINI_LOADER)
91+
# Disable wasm mini loader by default
92+
set (WAMR_BUILD_MINI_LOADER 0)
93+
endif ()
94+
95+
if (NOT DEFINED WAMR_BUILD_SIMD)
96+
# Enable SIMD by default
97+
set (WAMR_BUILD_SIMD 1)
98+
endif ()
99+
100+
if (NOT DEFINED WAMR_BUILD_REF_TYPES)
101+
# Disable reference types by default
102+
set (WAMR_BUILD_REF_TYPES 0)
103+
endif ()
104+
105+
if (NOT DEFINED WAMR_BUILD_DEBUG_INTERP)
106+
# Disable Debug feature by default
107+
set (WAMR_BUILD_DEBUG_INTERP 0)
108+
endif ()
109+
110+
if (WAMR_BUILD_DEBUG_INTERP EQUAL 1)
111+
set (WAMR_BUILD_FAST_INTERP 0)
112+
set (WAMR_BUILD_MINI_LOADER 0)
113+
set (WAMR_BUILD_SIMD 0)
114+
endif ()
115+
116+
set (REPO_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../../..)
117+
message([ceith]:REPO_ROOT_DIR, ${REPO_ROOT_DIR})
118+
119+
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
120+
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
121+
122+
add_definitions(-DWAMR_USE_MEM_POOL=0)
123+
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -fsanitize=signed-integer-overflow \
124+
-fprofile-instr-generate -fcoverage-mapping \
125+
-fsanitize=address,undefined,fuzzer")
126+
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=signed-integer-overflow \
127+
-fprofile-instr-generate -fcoverage-mapping \
128+
-fsanitize=address,undefined,fuzzer")
129+
130+
include(${REPO_ROOT_DIR}/core/shared/utils/uncommon/shared_uncommon.cmake)
131+
include(${REPO_ROOT_DIR}/build-scripts/runtime_lib.cmake)
132+
133+
add_library(vmlib
134+
${WAMR_RUNTIME_LIB_SOURCE}
135+
)
136+
137+
add_executable(wasm_mutator_fuzz wasm_mutator_fuzz.cc)
138+
target_link_libraries(wasm_mutator_fuzz vmlib -lm)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# WAMR fuzz test framework
2+
3+
## install wasm-tools
4+
5+
```bash
6+
1.git clone https://github.com/bytecodealliance/wasm-tools
7+
$ cd wasm-tools
8+
2.This project can be installed and compiled from source with this Cargo command:
9+
$ cargo install wasm-tools
10+
3.Installation can be confirmed with:
11+
$ wasm-tools --version
12+
4.Subcommands can be explored with:
13+
$ wasm-tools help
14+
```
15+
16+
## Build
17+
18+
```bash
19+
mkdir build && cd build
20+
# Without custom mutator (libfuzzer modify the buffer randomly)
21+
cmake ..
22+
# With custom mutator (wasm-tools mutate)
23+
cmake .. -DCUSTOM_MUTATOR=1
24+
make -j$(nproc)
25+
```
26+
27+
## Manually generate wasm file in build
28+
29+
```bash
30+
# wasm-tools smith generate some valid wasm file
31+
# The generated wasm file is in corpus_dir under build
32+
# N - Number of files to be generated
33+
./smith_wasm.sh N
34+
35+
# running
36+
``` bash
37+
cd build
38+
./wasm-mutate-fuzz CORPUS_DIR
39+
40+
```
41+
42+
## Fuzzing Server
43+
44+
```shell
45+
1. Installation Dependent Environment
46+
$ cd server
47+
$ pip install -r requirements.txt
48+
49+
2. Database Migration
50+
$ python3 app/manager.py db init
51+
$ python3 app/manager.py db migrate
52+
$ python3 app/manager.py db upgrade
53+
54+
3. Change localhost to your machine's IP address
55+
$ cd ../portal
56+
$ vim .env # Change localhost to your machine's IP address # http://<ip>:16667
57+
58+
4. Run Server and Portal
59+
$ cd .. # Switch to the original directory
60+
If you want to customize the front-end deployment port: # defaut 9999
61+
$ vim .env # Please change the portal_port to the port you want to use
62+
63+
The server is deployed on port 16667 by default, If you want to change the server deployment port:
64+
$ vim .env # Please change the server_port to the port you want to use
65+
$ vim portal/.env # Please change the VITE_SERVER_URL to the port you want to use # http://ip:<port>
66+
67+
68+
If your network needs to set up a proxy
69+
$ vim .env # Change proxy to your proxy address
70+
71+
$ docker-compose up --build -d
72+
Wait for completion, Access the port set by env
73+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# yaml configuration
2+
services:
3+
web:
4+
platform: linux/amd64
5+
container_name: fuzz_web
6+
build:
7+
context: ./portal
8+
dockerfile: Dockerfile
9+
args:
10+
- proxy=${proxy}
11+
volumes:
12+
- "./portal:/portal"
13+
ports:
14+
- "${portal_port}:80"
15+
server:
16+
build:
17+
context: ../../..
18+
dockerfile: ./tests/fuzz/wasm-mutator-fuzz/server/Dockerfile
19+
args:
20+
- proxy=${proxy}
21+
ports:
22+
- "${server_port}:16667"
23+
container_name: fuzz_server
24+
volumes:
25+
- "./server/app/data.db:/wamr-test/tests/fuzz/wasm-mutator-fuzz/server/app/data.db"
26+
- "./workspace:/wamr-test/tests/fuzz/wasm-mutator-fuzz/workspace"
27+
environment:
28+
- "TZ=Asia/Shanghai"
29+
restart: on-failure
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VITE_SERVER_URL=http://localhost:16667
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
FROM node:16 as builder
2+
3+
WORKDIR /portal
4+
COPY . .
5+
6+
ARG proxy=""
7+
8+
RUN if [ "$proxy" != "" ]; \
9+
then npm config set proxy "$proxy" && npm config set https-proxy "$proxy"; \
10+
else echo Do not set proxy; \
11+
fi
12+
RUN npm install && chmod +x node_modules/.bin/tsc \
13+
&& chmod +x node_modules/.bin/vite \
14+
&& npm run build
15+
16+
FROM nginx:alpine
17+
WORKDIR /portal
18+
COPY --from=builder /portal/dist/ /usr/share/nginx/html/
19+
RUN rm /etc/nginx/conf.d/default.conf
20+
COPY nginx.conf /etc/nginx/nginx.conf
21+
COPY default.conf.template /etc/nginx/conf.d
22+
23+
# hadolint ignore=DL3025
24+
CMD /bin/sh -c "envsubst '80' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf" && nginx -g 'daemon off;'
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
server {
2+
3+
listen 80 default_server;
4+
5+
location ^~ / {
6+
root /usr/share/nginx/html;
7+
index index.html index.htm;
8+
try_files $uri $uri/ /index.html;
9+
}
10+
11+
location @router {
12+
rewrite ^.*$ /index.html last; # important!
13+
}
14+
15+
16+
location ~* \.(?:manifest|appcache|html?|xml|json)$ {
17+
18+
root /usr/share/nginx/html;
19+
20+
if ($request_uri ~* .*[.](manifest|appcache|xml|json)$) {
21+
add_header Cache-Control "public, max-age=2592000";
22+
}
23+
24+
if ($request_filename ~* ^.*[.](html|htm)$) {
25+
add_header Cache-Control "public, no-cache";
26+
}
27+
28+
expires -1;
29+
}
30+
31+
location ~* \.(?:js|css|map|jpg|png|svg|ico)$ {
32+
root /usr/share/nginx/html;
33+
try_files $uri =404;
34+
35+
expires 1y;
36+
access_log off;
37+
38+
add_header Cache-Control "public";
39+
}
40+
41+
location ~ ^.+\..+$ {
42+
root /usr/share/nginx/html;
43+
try_files $uri =404;
44+
45+
include /etc/nginx/mime.types;
46+
}
47+
48+
error_page 500 502 503 504 /50x.html;
49+
50+
location = /50x.html {
51+
root /usr/share/nginx/html;
52+
}
53+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>WAMR fuzzing test system</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.tsx"></script>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)