Skip to contentSkip to conversation

Build errors with fuzz/x86_instruction_emulator harness

DISCUSSION Andrew Cooper 2 participants 5 messages Aug 14, 2023

lore
Share & tools
lore
#0 lore raw

Hello,

See the attached log snippet.  This is current staging, with a GCC 13 toolchain.

First of all, a minor issue.  Counting the number of -D__XEN_INTERFACE_VERSION__=__XEN_LATEST_INTERFACE_VERSION__'s, we have included CFLAGS on the CC line 4 times.  This is something that appears to be in common with all of tools/ and is probably the primary contributor to exceeding the Gitlab CI 4M log limit...

Next the error.  This doesn't build when CFLAGS coming in from the packaging environment include -flto=auto.

Clearly our wrapping trick doesn't work with LTO, but it's not obvious why.

readelf -Wa tools/fuzz/x86_instruction_emulator/wrappers.o | grep emul_
  [223] .gnu.lto_emul_fwrite.38.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01c2ec 000194 00   E  0   0  1
  [224] .gnu.lto_emul_memcmp.39.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01c480 0001a7 00   E  0   0  1
  [225] .gnu.lto_emul_memcpy.40.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01c627 000175 00   E  0   0  1
  [226] .gnu.lto_emul_memset.41.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01c79c 000186 00   E  0   0  1
  [227] .gnu.lto_emul_printf.42.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01c922 0002cd 00   E  0   0  1
  [228] .gnu.lto_emul_putchar.43.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01cbef 000216 00   E  0   0  1
  [229] .gnu.lto_emul_puts.44.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01ce05 000180 00   E  0   0  1

shows that there's something relevant in the object file.

Manual poking about in the build environment indicates that tools/tests/x86_instruction_emulator is similarly impacted.

Any ideas?

Obviously we can inhibit LTO for the x86_emul userspace, but that ought to be a last resort.

~Andrew

make[5]: Entering directory '/builddir/build/BUILD/xen-4.18.0/tools/fuzz/x86_instruction_emulator'
gcc -O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/xenserver/xenserver-hardened-cc1 -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -m64 -DBUILD_ID -fno-strict-aliasing -std=gnu99 -Wall -Wstrict-prototypes -Wdeclaration-after-statement -Wno-unused-but-set-variable -Wno-unused-local-typedefs -m64 -DBUILD_ID -fno-strict-aliasing -std=gnu99 -Wall -Wstrict-prototypes -Wdeclaration-after-statement -Wno-unused-but-set-variable -Wno-unused-local-typedefs -g3 -Werror -Og -fno-omit-frame-pointer -D__XEN_INTERFACE_VERSION__=__XEN_LATEST_INTERFACE_VERSION__ -MMD -MP -MF .subdirs-all.d -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -m64 -DBUILD_ID -fno-strict-aliasing -std=gnu99 -Wall -Wstrict-prototypes -Wdeclaration-after-statement -Wno-unused-but-set-variable -Wno-unused-local-typedefs -g3 -Werror -Og -fno-omit-frame-pointer -D__XEN_INTERFACE_VERSION__=__XEN_LATEST_INTERFACE_VERSION__ -MMD -MP -MF .subdir-all-fuzz.d -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -m64 -DBUILD_ID -fno-strict-aliasing -std=gnu99 -Wall -Wstrict-prototypes -Wdeclaration-after-statement -Wno-unused-but-set-variable -Wno-unused-local-typedefs -g3 -Werror -Og -fno-omit-frame-pointer -D__XEN_INTERFACE_VERSION__=__XEN_LATEST_INTERFACE_VERSION__ -MMD -MP -MF .subdirs-all.d -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -m64 -DBUILD_ID -fno-strict-aliasing -std=gnu99 -Wall -Wstrict-prototypes -Wdeclaration-after-statement -Wno-unused-but-set-variable -Wno-unused-local-typedefs -g3 -Werror -Og -fno-omit-frame-pointer -D__XEN_INTERFACE_VERSION__=__XEN_LATEST_INTERFACE_VERSION__ -MMD -MP -MF .subdir-all-x86_instruction_emulator.d -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -m64 -DBUILD_ID -fno-strict-aliasing -std=gnu99 -Wall -Wstrict-prototypes -Wdeclaration-after-statement -Wno-unused-but-set-variable -Wno-unused-local-typedefs -g3 -Werror -Og -fno-omit-frame-pointer -D__XEN_INTERFACE_VERSION__=__XEN_LATEST_INTERFACE_VERSION__ -MMD -MP -MF .afl-harness.d -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -I/builddir/build/BUILD/xen-4.18.0/tools/fuzz/x86_instruction_emulator/../../../tools/include -D__XEN_TOOLS__ -iquote . afl-harness.o fuzz-emul.o x86-emulate.o x86_emulate/0f01.o x86_emulate/0fae.o x86_emulate/0fc7.o x86_emulate/decode.o x86_emulate/fpu.o cpuid.o wrappers.o -o afl-harness
make[5]: Leaving directory '/builddir/build/BUILD/xen-4.18.0/tools/fuzz/x86_instruction_emulator' /usr/bin/ld: /tmp/ccphSQcS.ltrans0.ltrans.o: in function `disable_hooks':
/usr/include/bits/stdio2.h:86: undefined reference to `emul_puts'
/usr/bin/ld: /usr/include/bits/stdio2.h:86: undefined reference to `emul_puts'
/usr/bin/ld: /usr/include/bits/stdio2.h:86: undefined reference to `emul_puts'
/usr/bin/ld: /usr/include/bits/stdio2.h:86: undefined reference to `emul_puts'
/usr/bin/ld: /usr/include/bits/stdio2.h:86: undefined reference to `emul_puts'
/usr/bin/ld: /tmp/ccphSQcS.ltrans0.ltrans.o:/usr/include/bits/stdio2.h:86: more undefined references to emul_puts' follow /usr/bin/ld: /tmp/ccphSQcS.ltrans0.ltrans.o: in function input_read':
/usr/include/bits/string_fortified.h:29: undefined reference to `emul_memcpy'
/usr/bin/ld: /tmp/ccphSQcS.ltrans0.ltrans.o: in function `data_read':
/usr/include/bits/stdio2.h:86: undefined reference to `emul_putchar'
/usr/bin/ld: /tmp/ccphSQcS.ltrans0.ltrans.o: in function `fuzz_read_msr':
/usr/include/bits/stdio2.h:86: undefined reference to `emul_puts'
/usr/bin/ld: /tmp/ccphSQcS.ltrans0.ltrans.o: in function `dump_state':
/usr/include/bits/stdio2.h:86: undefined reference to `emul_puts'
/usr/bin/ld: /tmp/ccphSQcS.ltrans0.ltrans.o: in function `LLVMFuzzerTestOneInput':
/usr/include/bits/stdio2.h:86: undefined reference to `emul_puts'
/usr/bin/ld: /usr/include/bits/stdio2.h:86: undefined reference to `emul_puts'
/usr/bin/ld: /tmp/ccphSQcS.ltrans0.ltrans.o: in function `main':
/usr/include/bits/stdio2.h:86: undefined reference to `emul_puts'
/usr/bin/ld: /tmp/ccphSQcS.ltrans0.ltrans.o: in function `x86_emulate':
/usr/include/bits/string_fortified.h:29: undefined reference to `emul_memcpy'
/usr/bin/ld: /usr/include/bits/string_fortified.h:59: undefined reference to `emul_memset'
/usr/bin/ld: /usr/include/bits/string_fortified.h:59: undefined reference to `emul_memset'
/usr/bin/ld: /usr/include/bits/string_fortified.h:59: undefined reference to `emul_memset'
/usr/bin/ld: /usr/include/bits/string_fortified.h:29: undefined reference to `emul_memcpy'
/usr/bin/ld: /usr/include/bits/string_fortified.h:29: undefined reference to `emul_memcpy'
collect2: error: ld returned 1 exit status
make[5]: *** [Makefile:54: afl-harness] Error 1
make[4]: *** [/builddir/build/BUILD/xen-4.18.0/tools/fuzz/../../tools/Rules.mk:204: subdir-all-x86_instruction_emulator] Error 2

#1 lore raw
Quoted 30 lines · Andrew Cooper

See the attached log snippet.  This is current staging, with a GCC 13 toolchain.

First of all, a minor issue.  Counting the number of -D__XEN_INTERFACE_VERSION__=__XEN_LATEST_INTERFACE_VERSION__'s, we have included CFLAGS on the CC line 4 times.  This is something that appears to be in common with all of tools/ and is probably the primary contributor to exceeding the Gitlab CI 4M log limit...

Next the error.  This doesn't build when CFLAGS coming in from the packaging environment include -flto=auto.

Clearly our wrapping trick doesn't work with LTO, but it's not obvious why.

readelf -Wa tools/fuzz/x86_instruction_emulator/wrappers.o | grep emul_
  [223] .gnu.lto_emul_fwrite.38.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01c2ec 000194 00   E  0   0  1
  [224] .gnu.lto_emul_memcmp.39.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01c480 0001a7 00   E  0   0  1
  [225] .gnu.lto_emul_memcpy.40.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01c627 000175 00   E  0   0  1
  [226] .gnu.lto_emul_memset.41.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01c79c 000186 00   E  0   0  1
  [227] .gnu.lto_emul_printf.42.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01c922 0002cd 00   E  0   0  1
  [228] .gnu.lto_emul_putchar.43.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01cbef 000216 00   E  0   0  1
  [229] .gnu.lto_emul_puts.44.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01ce05 000180 00   E  0   0  1

Quoted 1 line

shows that there's something relevant in the object file.

But only sections, no symbols. Doing a simple test with a trivial source file, I observe that no actual code is emitted at all, and hence also no symbols. Which means our trick - working entirely behind the back of the compiler by inserting .equ in the assembly output - takes no effect on our own sources. And really the errors in the log fragment you provided all point back to standard library headers, where (just a guess) something may be done that cause _some_ code to be emitted, for which our overrides then take effect.

Quoted 7 lines

Manual poking about in the build environment indicates that tools/tests/x86_instruction_emulator is similarly impacted.

Any ideas?

Obviously we can inhibit LTO for the x86_emul userspace, but that ought to be a last resort.

To get the build issue addressed, merely suppressing LTO for wrappers.o may suffice (provided such mixing is permitted in the first place). However, due to our overrides not taking effect (as per above) I can't see how the resulting binaries then would work correctly.

Question therefore is whether we can find a way of effecting the needed overrides (including for compiler generated calls) without resorting to emitting .equ (or alike), and hence without doing it fully behind the compiler's back.

Jan

#2 lore raw
Quoted 32 lines · Jan Beulich
Quoted 30 lines · Andrew Cooper

See the attached log snippet.  This is current staging, with a GCC 13 toolchain.

First of all, a minor issue.  Counting the number of -D__XEN_INTERFACE_VERSION__=__XEN_LATEST_INTERFACE_VERSION__'s, we have included CFLAGS on the CC line 4 times.  This is something that appears to be in common with all of tools/ and is probably the primary contributor to exceeding the Gitlab CI 4M log limit...

Next the error.  This doesn't build when CFLAGS coming in from the packaging environment include -flto=auto.

Clearly our wrapping trick doesn't work with LTO, but it's not obvious why.

readelf -Wa tools/fuzz/x86_instruction_emulator/wrappers.o | grep emul_
  [223] .gnu.lto_emul_fwrite.38.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01c2ec 000194 00   E  0   0  1
  [224] .gnu.lto_emul_memcmp.39.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01c480 0001a7 00   E  0   0  1
  [225] .gnu.lto_emul_memcpy.40.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01c627 000175 00   E  0   0  1
  [226] .gnu.lto_emul_memset.41.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01c79c 000186 00   E  0   0  1
  [227] .gnu.lto_emul_printf.42.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01c922 0002cd 00   E  0   0  1
  [228] .gnu.lto_emul_putchar.43.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01cbef 000216 00   E  0   0  1
  [229] .gnu.lto_emul_puts.44.43da3a7fd30cc0a1 PROGBITS 0000000000000000 01ce05 000180 00   E  0   0  1

Quoted 1 line

shows that there's something relevant in the object file.

Quoted 8 lines

But only sections, no symbols. Doing a simple test with a trivial source file, I observe that no actual code is emitted at all, and hence also no symbols. Which means our trick - working entirely behind the back of the compiler by inserting .equ in the assembly output - takes no effect on our own sources. And really the errors in the log fragment you provided all point back to standard library headers, where (just a guess) something may be done that cause _some_ code to be emitted, for which our overrides then take effect.

It turns out it was GCC 12 not GCC 13.  Things build cleanly with no LTO.

Quoted 11 lines
Quoted 7 lines

Manual poking about in the build environment indicates that tools/tests/x86_instruction_emulator is similarly impacted.

Any ideas?

Obviously we can inhibit LTO for the x86_emul userspace, but that ought to be a last resort.

To get the build issue addressed, merely suppressing LTO for wrappers.o may suffice (provided such mixing is permitted in the first place). However, due to our overrides not taking effect (as per above) I can't see how the resulting binaries then would work correctly.

Indeed.  While I don't particularly care for the fuzzing harness (It's a little dubious that it builds by default but doesn't install anything), the main emulator harness does need to function properly.  That gets widespred use in testing.

Quoted 4 lines

Question therefore is whether we can find a way of effecting the needed overrides (including for compiler generated calls) without resorting to emitting .equ (or alike), and hence without doing it fully behind the compiler's back.

--wrap= each symbol, except when compiling wrappers.c ?

This seems to be the normal way to mock out functions including malloc() and friends.

~Andrew

#3 lore raw
Quoted 10 lines · Andrew Cooper
Quoted 4 lines · Jan Beulich

Question therefore is whether we can find a way of effecting the needed overrides (including for compiler generated calls) without resorting to emitting .equ (or alike), and hence without doing it fully behind the compiler's back.

--wrap= each symbol, except when compiling wrappers.c ?

This seems to be the normal way to mock out functions including malloc() and friends.

What is --wrap= ?

I was thinking along the lines of

void*memcpy(void*, const void*, size_t) __asm__("emul_memcpy"); void*memset(void*, int, size_t) __asm__("emul_memset");

This looks to work at least for explicit __builtin_mem*() invocations, without LTO. Inspecting the LTO object, though, doesn't show any sign of "memcpy" in any of the strings it embeds (custom functions are recorded with strings one can recognize), so I fear this wouldn't work there either. (But if indeed so, I'd wonder if this wasn't a compiler bug, as then we don't do things behind the compiler's back anymore.)

Jan

#4 lore raw
Quoted 11 lines · Jan Beulich
Quoted 9 lines · Andrew Cooper
Quoted 4 lines · Jan Beulich

Question therefore is whether we can find a way of effecting the needed overrides (including for compiler generated calls) without resorting to emitting .equ (or alike), and hence without doing it fully behind the compiler's back.

--wrap= each symbol, except when compiling wrappers.c ?

This seems to be the normal way to mock out functions including malloc() and friends.

What is --wrap= ?

-Wl,--wrap=foo

It's a staple of C unit-testing frameworks.

From the LD manual,

--wrap=symbol
    Use a wrapper function for symbol.  Any undefined reference to symbol will be resolved to "__wrap_symbol".  Any undefined reference to "__real_symbol" will be resolved to symbol.

~Andrew