0

Run command:

emcc -fprofile-arcs -ftest-coverage demo.cpp

Error response:

error: undefined symbol: llvm_gcda_emit_arcs (referenced by top-level compiled C/C++ code)
warning: Link with `-sLLD_REPORT_UNDEFINED` to get more information on undefined symbols
warning: To disable errors for undefined symbols use `-sERROR_ON_UNDEFINED_SYMBOLS=0`
warning: _llvm_gcda_emit_arcs may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: llvm_gcda_emit_function (referenced by top-level compiled C/C++ code)
warning: _llvm_gcda_emit_function may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: llvm_gcda_end_file (referenced by top-level compiled C/C++ code)
warning: _llvm_gcda_end_file may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: llvm_gcda_start_file (referenced by top-level compiled C/C++ code)
warning: _llvm_gcda_start_file may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: llvm_gcda_summary_info (referenced by top-level compiled C/C++ code)
warning: _llvm_gcda_summary_info may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: llvm_gcov_init (referenced by top-level compiled C/C++ code)
warning: _llvm_gcov_init may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
Error: Aborting compilation due to previous errors

Does anyone know how to run emcc to support code coverage?

Azhar Khan
  • 3,829
  • 11
  • 26
  • 32

1 Answers1

0

Emscripten doesn't support it.

LLVM GCov coverage works by adding globals with data and calls to functions with special names (_llvm_gcda_emit_arcs and so on) which are expected to be implemented in compiler-rt: https://github.com/llvm/llvm-project/tree/main/compiler-rt/lib/profile

When emscripten build system pulls in a copy of compiler-rt, it strips it, pulling in only the parts of compiler-rt in its whitelist: https://github.com/emscripten-core/emscripten/blob/30f50a9335267d3bd26120f1eeeab6cf7c908430/system/lib/update_compiler_rt.py#L17

The lib/profile directory is not copied and those parts of compiler-rt aren't available to link against, leading to your link errors.

Nick Lewycky
  • 1,182
  • 6
  • 14
  • How can I run webassembly code coverage, need I recompile emscripten and llvm by myself ? Have any simple solution? – Judy Hopps Oct 17 '22 at 05:35
  • I found this https://github.com/emscripten-core/emscripten/tree/fcoverage, But it seems that it is just branch not merging to master for any release version. – Judy Hopps Oct 17 '22 at 05:41
  • There's two approaches to try. You can add `lib/profile` to the compiler-rt build and build your own emscripten. It might even work out of the box, or it might turn out to be really hard to fix. In the same vein, you could also try to write your own implementations of those llvm-gcov functions by reading what they do out of compiler-rt. The other option is "SanitizerCoverage" which IMO is better designed and probably easier to get working. I found a demo with emscripten but have not tried it myself: https://github.com/jonathanmetzman/wasm-fuzzing-demo – Nick Lewycky Oct 17 '22 at 06:14
  • Right, I totally forgot about `-fcoverage-mapping`, the third coverage format in LLVM. I bet it doesn't work with emscripten for the same reason, but maybe it's easier to fix (again by changing the build of compiler-rt). Give it a try? https://clang.llvm.org/docs/SourceBasedCodeCoverage.html – Nick Lewycky Oct 17 '22 at 06:24