Skip to content

Commit f700aac

Browse files
dkoch144dkoch
andauthored
Enable building libpng on CI without using GUI (#3110)
* Fixing windows ci build in justfile. harness.cc modified for cl.exe. Additions to README. * More precise compiler check for noinline definition. --------- Co-authored-by: dkoch <dkoch@vigilantsys.com>
1 parent 58bb64f commit f700aac

File tree

3 files changed

+77
-8
lines changed

3 files changed

+77
-8
lines changed

fuzzers/binary_only/frida_libpng/Justfile

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,30 @@ harness: lib
2525
clang++ -O3 harness.o libpng-1.6.37/.libs/libpng16.a -shared -lz -o libpng-harness.so
2626

2727
[windows]
28-
harness:
29-
cl /c harness_win.cpp && link harness_win.obj /dll
28+
zlib:
29+
powershell -Command Invoke-WebRequest -OutFile zlib-1.2.11.tar.gz https://zlib.net/fossils/zlib-1.2.11.tar.gz
30+
tar -xvf zlib-1.2.11.tar.gz
31+
del /q zlib-1.2.11.tar.gz
32+
move zlib-1.2.11 zlib
33+
34+
[windows]
35+
lib: zlib
36+
cd zlib && cmake -A x64 -DCMAKE_CXX_COMPILER=cl . && cmake --build . --config Release
37+
38+
[windows]
39+
libpng:
40+
powershell -Command Invoke-WebRequest -OutFile libpng-1.6.37.tar.gz https://github.com/glennrp/libpng/archive/refs/tags/v1.6.37.tar.gz
41+
tar -xvf libpng-1.6.37.tar.gz
42+
del /q libpng-1.6.37.tar.gz
43+
44+
[windows]
45+
lib2: libpng
46+
cd libpng-1.6.37 && cmake -A x64 -DCMAKE_CXX_COMPILER=cl -DZLIB_ROOT=..\zlib -DZLIB_LIBRARY=..\zlib\Release\zlib.lib . && cmake --build . --config Release
47+
48+
[windows]
49+
harness: lib lib2
50+
copy libpng-1.6.37\Release\libpng16.lib . && copy libpng-1.6.37\Release\libpng16.dll . && copy zlib\Release\zlib.lib . && copy zlib\Release\zlib.dll . && copy target\release\frida_fuzzer.exe .
51+
cl /O2 /c /I .\libpng-1.6.37 harness.cc /Fo:harness.obj && link /DLL /OUT:libpng-harness.dll harness.obj libpng16.lib zlib.lib
3052

3153
[unix]
3254
[windows]
@@ -39,7 +61,7 @@ run: build harness
3961

4062
[windows]
4163
run: build harness
42-
{{TARGET_DIR}}\{{PROFILE}}\{{FUZZER_NAME_WIN}} -F LLVMFuzzerTestOneInput -H .\harness_win.dll -l .\harness_win.dll --cores=0
64+
{{TARGET_DIR}}\{{PROFILE}}\{{FUZZER_NAME_WIN}} -F LLVMFuzzerTestOneInput -H .\libpng-harness.dll -l .\libpng-harness.dll -l .\zlib.dll -l .\libpng16.dll --cores=0
4365

4466
[unix]
4567
test: build harness
@@ -57,7 +79,7 @@ test: build harness
5779
[windows]
5880
[script("cmd.exe", "/c")]
5981
test: build harness
60-
start "" "{{TARGET_DIR}}\{{PROFILE}}\{{FUZZER_NAME_WIN}}" -F LLVMFuzzerTestOneInput -H .\harness_win.dll -l .\harness_win.dll --cores=0
82+
start "" "{{TARGET_DIR}}\{{PROFILE}}\{{FUZZER_NAME_WIN}}" -F LLVMFuzzerTestOneInput -H .\libpng-harness.dll -l .\libpng-harness.dll -l .\zlib.dll -l .\libpng16.dll --cores=0
6183
ping -n 10 127.0.0.1>NUL && taskkill /im frida_fuzzer.exe /F
6284
dir /a-d corpus_discovered && (echo Files exist) || (exit /b 1337)
6385

fuzzers/binary_only/frida_libpng/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,44 @@ clang++ -L.\zlib.dll .\harness.o .\libpng16.lib -lzlib -shared -o .\libpng-harne
6363
./frida_fuzzer.exe ./libpng-harness.dll LLVMFuzzerTestOneInput ./libpng-harness.dll
6464
```
6565

66+
### To build it with visual studio for ci (no gui)
67+
68+
Assumes that the fuzzer is already built with `cargo build --release`
69+
70+
1. download and build zlib in this directory
71+
```
72+
powershell -Command Invoke-WebRequest -OutFile zlib-1.2.11.tar.gz https://zlib.net/fossils/zlib-1.2.11.tar.gz
73+
tar -xvf zlib-1.2.11.tar.gz
74+
del /q zlib-1.2.11.tar.gz
75+
move zlib-1.2.11 zlib
76+
cd zlib
77+
cmake -A x64 -DCMAKE_CXX_COMPILER=cl .
78+
cmake --build . --config Release
79+
```
80+
2. download and build libpng in this directory
81+
```
82+
powershell -Command Invoke-WebRequest -OutFile libpng-1.6.37.tar.gz https://github.com/glennrp/libpng/archive/refs/tags/v1.6.37.tar.gz
83+
tar -xvf libpng-1.6.37.tar.gz
84+
del /q libpng-1.6.37.tar.gz
85+
cd libpng-1.6.37
86+
cmake -A x64 -DCMAKE_CXX_COMPILER=cl -DZLIB_ROOT=..\zlib -DZLIB_LIBRARY=..\zlib\Release\zlib.lib .
87+
cmake --build . --config Release
88+
```
89+
3. copy libraries from zlib and libpng and use to build harness
90+
```
91+
copy libpng-1.6.37\Release\libpng16.lib .
92+
copy libpng-1.6.37\Release\libpng16.dll .
93+
copy zlib\Release\zlib.lib .
94+
copy zlib\Release\zlib.dll .
95+
copy target\release\frida_fuzzer.exe .
96+
cl /O2 /c /I .\libpng-1.6.37 harness.cc /Fo:harness.obj
97+
link /DLL /OUT:libpng-harness.dll harness.obj libpng16.lib zlib.lib
98+
```
99+
4. start fuzzing
100+
```
101+
.\frida_fuzzer.exe -F LLVMFuzzerTestOneInput -H .\libpng-harness.dll -l .\libpng-harness.dll -l .\zlib.dll -l .\libpng16.dll --cores 0
102+
```
103+
OR
104+
105+
1. Run `just test`
106+

fuzzers/binary_only/frida_libpng/harness.cc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,13 @@ extern "C" int afl_libfuzzer_init() {
8585

8686
static char *allocation = NULL;
8787

88-
__attribute__((noinline)) void func3(char *alloc) {
88+
#ifdef _MSC_VER
89+
#define NOINLINE __declspec(noinline)
90+
#else
91+
#define NOINLINE __attribute__((noinline))
92+
#endif
93+
94+
NOINLINE void func3(char *alloc) {
8995
// printf("func3\n");
9096
#ifdef _WIN32
9197
if ((rand() % 2) == 0) {
@@ -99,12 +105,12 @@ __attribute__((noinline)) void func3(char *alloc) {
99105
}
100106
#endif
101107
}
102-
__attribute__((noinline)) void func2() {
108+
NOINLINE void func2() {
103109
allocation = (char *)malloc(0xff);
104110
// printf("func2\n");
105111
func3(allocation);
106112
}
107-
__attribute__((noinline)) void func1() {
113+
NOINLINE void func1() {
108114
// printf("func1\n");
109115
func2();
110116
}
@@ -119,7 +125,7 @@ __attribute__((noinline)) void func1() {
119125
// Entry point for LibFuzzer.
120126
// Roughly follows the libpng book example:
121127
// http://www.libpng.org/pub/png/book/chapter13.html
122-
HARNESS_EXPORTS extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data,
128+
extern "C" HARNESS_EXPORTS int LLVMFuzzerTestOneInput(const uint8_t *data,
123129
size_t size) {
124130
if (size >= 8 && *(uint64_t *)data == 0xABCDEFAA8F1324AA) { abort(); }
125131
if (size < kPngHeaderSize) { return 0; }

0 commit comments

Comments
 (0)