Skip to content

test: call the library API from C++ and build a consumer project - #22

Merged
SizzleUnrlsd merged 2 commits into
mainfrom
test/api-consumer
Sep 27, 2026
Merged

SizzleUnrlsd merged 2 commits into
mainfrom
test/api-consumer

Conversation

@SizzleUnrlsd

Copy link
Copy Markdown
Contributor

Item 1 of #21: the library API called from C++, and a project consuming the library the way ctrace will (CoreTrace/coretrace#112).

Change

  1. api_unit (unittests/api_test.cpp): Run and RunBatch on real programs, compiled with coretrace-compiler and executed. The heap overflow fixture gives one finding with its rule, CWE, severity, message, faulting line and column, and allocation site in the same source. A compile-only run succeeds without executing and leaves the binary at output_path. A hanging program is killed after the 1 s timeout, reported timed_out and not a success, within seconds. A batch of three sources (hang, overflow, clean) reports them in path order with each one's result, timeouts=1, no compile failure, success and verdict 2. Compile-only flags with a run are refused with a diagnostic naming --no-run. An empty directory is not analyzed and says so.
  2. consumer: unittests/consumer/ is a complete consumer, FetchContent_Declare on the release tag and coretrace::runtime-analyzer_lib, whose main analyzes a program and prints its findings. The test configures it with the same generator, compilers and LLVM as this build, points FETCHCONTENT_SOURCE_DIR_RUNTIME_ANALYZER at the checkout under test and the CC and CORETRACE_LOGGER ones at the sources already fetched here (nothing is cloned), builds it and expects heap-buffer-overflow CWE-122 on heap_overflow_write.c. It lives under unittests/, not test/, which the sweeps run in full.
  3. build: require C++20 of the library's consumers: the first run of the consumer test failed to include runtime_analyzer.hpp (no template named 'optional' in namespace 'std'), because the C++ standard was set for this project's targets only. cxx_std_20 is now a public compile feature of the library. ctrace would have hit this on its first build.
  4. README: the two tests in the table, and the consumer named in "Library Use".

Tests (written first)

Both tests are the deliverable. api_unit failed on its first run on my prediction of the location's path (the runtime names the source as coretrace-compiler recorded it, which can be relative; the test checks the tail of the path and that the allocation site is in the same source). consumer failed on the C++20 requirement, item 3 fixed it. With ParseFindings disabled in Run, both fail again: 2 checks in api_unit, the grep in consumer.

Verification

  • macOS arm64: ctest --test-dir build 14/14, consumer in 10 to 18 s; ./scripts/format-check.sh clean.
  • Linux: this pull request's Build workflow.

The public header uses std::optional and a nested namespace, but the C++
standard was only set for this project's own targets. A consumer linking
coretrace::runtime-analyzer_lib with its compiler's default standard failed
to include runtime_analyzer.hpp ("no template named 'optional'"). The
library now carries cxx_std_20 as a public compile feature.

Found by the consumer test of the next commit. Part of #21.
Run and RunBatch were only reached through the CLI, while ctrace will link
the library (CoreTrace/coretrace#112). api_unit drives them on real
programs: the heap overflow fixture gives one finding with its rule, CWE,
severity, faulting line and allocation site; a compile-only run succeeds
without executing; a hanging program is killed at the timeout and reported
as timed out; a batch of three sources reports each one in path order with
its counters and verdict 2; compile-only flags with a run are refused; an
empty directory is not analyzed.

The consumer test configures and builds unittests/consumer, a project that
fetches this repository on a release tag the way ctrace does, links
coretrace::runtime-analyzer_lib and analyzes a program. The test points
FetchContent at the checkout under test and at the already fetched
coretrace-compiler and logger sources, so nothing is cloned. Its first run
failed on the missing C++20 requirement fixed by the previous commit.

Part of #21.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant