Skip to content

Code coverage #13046

Open
Open

Description

@3d4m-volodymyr

Is there anything done in emscripten for enabling Tests Code Coverage reports generation?

I found this very interesting article on how to do it manually.
Requires to recompile compiler-rt.

Activity

  1. sbc100 commented on Dec 15, 2020

    @sbc100
    Collaborator

    I don't think we have any direct support for that today, but its sounds like it would be fairly easy to add.

    Sounds like it would involve a new option and new compiler-rt variant added to tools/system_libs.py

  2. 3d4m-volodymyr commented on Dec 15, 2020

    @3d4m-volodymyr
    Author

    Is this the place to start looking at https://github.com/emscripten-core/emscripten/blob/master/tools/system_libs.py#L642 ?

    I never dealt with the internals of emscripten. Probably will have lots of questions.

  3. sbc100 commented on Dec 18, 2020

    @sbc100
    Collaborator

    I took an initial stab at getting this to work: #13072

    It works up until llvm-cov fails to parse the data sections in the wasm executable.

  4. nokotan commented on Oct 27, 2021

    @nokotan
    Contributor

    It seems that the gcc-like coverage tool will get worked with #13072.

    Adding compile option -fprofile-arcs -ftest-coverage generates vaild *.gcdo into physical file system and *.gcda into virtual file system.
    llvm-cov gcov is also working completely using these *.gcdo, *gcda files.

    Therefore, I think that we have already a good reason to merge #13072.

  5. emaxx-google commented on May 8, 2022

    @emaxx-google

    Hello, is there any update on this effort?
    (We're looking into ways to set up coverage analysis for Google Smart Card Connector, and IIUC currently the only workaround is to switch to the regular clang/gcc, but this doesn't let test the code in the WebAssembly environment.)

  6. AnnieXia commented on Oct 17, 2022

    @AnnieXia

    emcc -fprofile-instr-generate -fcoverage-mapping demo.cpp

    wasm-ld: error: /var/folders/61/x35v93n50_5g1dfjrtprr2dw0000gn/T/emscripten_temp_xjrzk63_/demo_0.o: undefined symbol: __llvm_profile_runtime

    have emcc supported code coverage? need help :)

  7. sbc100 commented on Oct 17, 2022

    @sbc100
    Collaborator

    No, emcc does not yet support this method of doing code coverage. #13072 is a start, but that has yet to land.

  8. junyuecao commented on Mar 2, 2023

    @junyuecao
    Contributor

    It looks like SanitizerCoverage works. I've tried the demo by replacing clang++ to em++

    em++ -g  -fsanitize-coverage=trace-pc-guard trace-pc-guard-example.cc -c
    em++ trace-pc-guard-cb.cc trace-pc-guard-example.o -fsanitize=address -sNO_EXIT_RUNTIME -o trace-pc-guard-cb.html
    

    And the result can be printed as expected.
    image

  9. 3d4m-volodymyr commented on Mar 9, 2023

    @3d4m-volodymyr
    Author

    @junyuecao this is not the same.

  10. bbarwik commented on Oct 19, 2023

    @bbarwik

    Hey. I've created a tutorial how to generate code coverage for webassembly projects. This one is dedicated for blockchains using WASM VM, however I think it may be useful to you too - https://github.com/hknio/code-coverage-for-webassembly

  11. nihui commented on Feb 13, 2025

    @nihui

    Hi

    I implemented the required routines for wasm code coverage, see Tencent/ncnn#5903

    I can use node to run the wasm program, collect gcda data, and combine it with the llvm-cov gcov wrapper to get a reliable coverage report.

    It does not require compiler-rt library, you could just add my simplegcov.cpp into your project build, and compile with some special flags for coverage working

    sample usage guide
    Tencent/ncnn#5902 (comment)

    wasm code coverage

    # compile with --coverage -fprofile-arcs -ftest-coverage -g
    emcc test.cpp -c -o test.o --coverage -fprofile-arcs -ftest-coverage -g
    emcc simplegcov.cpp -c -o simplegcov.o --coverage -fprofile-arcs -ftest-coverage -g
    
    # link with --coverage -g -s FORCE_FILESYSTEM=1 -sEXIT_RUNTIME=1
    # link with -lnodefs.js -lnoderawfs.js
    emcc test.o simplegcov.o -o test.js -s EXPORTED_FUNCTIONS='["_main"]' --coverage -g -s FORCE_FILESYSTEM=1 -sEXIT_RUNTIME=1 -lnodefs.js -lnoderawfs.js
    
    # run wasm
    node test.js
    
    # collect gcda and generate output html
    lcov -d ./ -c --gcov-tool `pwd`/llvm-gcov -o lcov.info --ignore-errors inconsistent
    genhtml lcov.info -o ./output --ignore-errors inconsistent

    Tencent/ncnn#5903

    the llvm-gcov wrapper

    #!/bin/sh
    exec llvm-cov gcov "$@"
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions