Skip to content

MOD-9985 Bindings of C types to allow Rust Unit tests for RSSortingVector#6342

Closed
DarthB wants to merge 38 commits intomasterfrom
rp-rssortingvector-bindings-to-rust
Closed

MOD-9985 Bindings of C types to allow Rust Unit tests for RSSortingVector#6342
DarthB wants to merge 38 commits intomasterfrom
rp-rssortingvector-bindings-to-rust

Conversation

@DarthB
Copy link
Copy Markdown
Contributor

@DarthB DarthB commented Jun 18, 2025

Closed

It was stopped because the rust coverage unit tests require many (more than 400) symbols to be defined. It’s not clear why the linker behavior changes. A speculation is that it is related to changes due to command specifics:

cargo +nightly llvm-cov test --doctests --codecov --workspace ...

Initial Goal

Link RSValue and dependencies, which is a variant data type that contains:

  • Constructor Functions for each type
    • Four different pathways for allocations (malloc, mempool_t, sds and strdup)
  • Set Functions for each type
  • Helper Functions (ToString, RSValue_StringPtrLen, compare_arrays, convert..., …)
  • RSValue_SendReply

Problem

There was a dependency on mempool_t, which required parts of the RedisModule API.

We started by mocking those RedisModule API parts, but more unresolved symbols appeared.

The C Code is not very modular, which means that we need many symbols quickly. There are two possible solutions:

  1. Modularize the C Code, which includes many headers if not needed.
  2. Do the work and mock all the open symbols.

Solved Challenges

Open Challenges

The CI job that generates the test coverage report uses different linker settings. However, the output is not easily reproducible.

The command ./build.sh FORCE COV=1 RUN_UNIT_TESTS RUN_RUST_TESTS acts like the CI job. The problem occurs in Rust, so running ./build.sh FORCE DEBUG COV=1 RUN_RUST_TESTS should be enough.

  • macOS: Not reproducible on macOS
  • The Ubuntu-based development container generates unrelated errors when running coverage. The container does not have the right tools or the correct version of the tools installed.
  • CI and Pieters system: Fail to link redis_mock

When we get rid of the error-limit of the linker, we still receive over 400 symbols that need to be defined for the PR. The following code shows how to get rid of the linker error:

if target_os != "macos" {
        //println!("cargo:rustc-link-arg=-Wl,--unresolved-symbols=ignore-all");
        println!("cargo:rustc-link-arg=-Wl,--error-limit=0");
    }

The line linker argument --unresolved-symbols=ignore-all shifts the problem from build time to runtime and breaks on the first symbol that cannot be resolved.

Describe the changes in the pull request

We need these changes to run Rust unit tests that use RSValue internally; more precisely, RSValue's reference-counted methods require linking to the Rust test binaries. The tests with coverage require part of the RedisModule API mocked out.

  1. Generate Bindings for value.c module (RSValue)
  2. Generate Bindings for mempool.c module (mempool_t) used by RSValue for allocations
  3. Generate Bindings for global variables setup in module initialization (RSGlobalConfig and RSDummyContext)
  4. Be less restrictive in Rust's allocator shims (no panics if size=0 is given as parameter anymore) --> mempool_t relies on allocations with size zero. And it's safe as long as the pointers don't get dereferenced.
  5. Mock/Impl more methods for allocation, e.g. RedisModule_TryAlloc, the reply functions, the Command API.

Extracted from: #6311

Mark if applicable

  • This PR introduces API changes
  • This PR introduces serialization changes

@DarthB DarthB marked this pull request as draft June 18, 2025 10:59
@DarthB DarthB changed the title MOD-9985 Bindings of C types to allow Rust Unit tests for RSSortingVecotr MOD-9985 Bindings of C types to allow Rust Unit tests for RSSortingVector Jun 18, 2025
@DarthB DarthB force-pushed the rp-rssortingvector-bindings-to-rust branch 2 times, most recently from fba6674 to c3843f6 Compare June 18, 2025 15:48
@DarthB DarthB marked this pull request as ready for review June 18, 2025 15:48
@DarthB DarthB force-pushed the rp-rssortingvector-bindings-to-rust branch from d3ad5e8 to 9a140bb Compare June 19, 2025 07:33
@DarthB DarthB requested a review from JonasKruckenberg June 19, 2025 08:00
Copy link
Copy Markdown
Collaborator

@JonasKruckenberg JonasKruckenberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great improvement, maybe we can find a way to simplify this even more? Cut down the size of the PR?

As a sidenote, the past tense of bind would be bound 😉 but I think we should rename the crate to something more helpful anyway 🤔

let platform = format!("{}-{}-{}", os, arch, get_build_profile_name());

let root = git_root();
let lib_path = root
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we depend on the rust binding artifact in the build script we need to add it as a dependency to Cargo.toml too.

.join("rust-binded");

// check if the static library exists and quit with an error if it does not
let full_path = lib_path.join("librust_binded.a");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will not work on windows I think?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should be modifying the redis headers for this PR if at all possible.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only a rename: For the c file it's necessary to not generate duplicate symbols (root CMakeLists used "*.c" in the src folder.

The pattern of keeping headers and c files together was used before, e.g. in wildcard. Let's stick to it.

@DarthB DarthB force-pushed the rp-rssortingvector-bindings-to-rust branch 3 times, most recently from 0d9c8e3 to 2fc0440 Compare June 19, 2025 13:29
@codecov
Copy link
Copy Markdown

codecov bot commented Jun 19, 2025

Codecov Report

Attention: Patch coverage is 73.17073% with 22 lines in your changes missing coverage. Please review.

Project coverage is 88.82%. Comparing base (f0ecb9e) to head (2fc0440).
Report is 5 commits behind head on master.

Current head 2fc0440 differs from pull request most recent head 9a47c73

Please upload reports for the commit 9a47c73 to get more accurate results.

Files with missing lines Patch % Lines
src/c2rust/value.h 75.00% 18 Missing ⚠️
src/redisearch_rs/redis_mock/src/allocator.rs 0.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6342      +/-   ##
==========================================
+ Coverage   88.77%   88.82%   +0.05%     
==========================================
  Files         250      247       -3     
  Lines       41497    41212     -285     
  Branches     3632     3482     -150     
==========================================
- Hits        36837    36605     -232     
+ Misses       4611     4564      -47     
+ Partials       49       43       -6     
Flag Coverage Δ
flow 81.50% <76.92%> (+0.03%) ⬆️
unit 46.68% <40.24%> (-0.27%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DarthB DarthB force-pushed the rp-rssortingvector-bindings-to-rust branch 3 times, most recently from 15f2dc0 to 26a98cf Compare June 23, 2025 10:46
@DarthB DarthB force-pushed the rp-rssortingvector-bindings-to-rust branch 10 times, most recently from bf7f303 to a923381 Compare June 24, 2025 09:33
DarthB added 2 commits June 24, 2025 12:17
… e.g. Config and Context

some free methods are using leaking variants  for rust test suite
@DarthB DarthB force-pushed the rp-rssortingvector-bindings-to-rust branch from 0940230 to ca72d9c Compare June 24, 2025 10:21
@DarthB DarthB force-pushed the rp-rssortingvector-bindings-to-rust branch from ca72d9c to f318a67 Compare June 24, 2025 10:34
@DarthB DarthB added the enforce:coverage Run coverage flow even on draft pull request label Jun 24, 2025
@DarthB DarthB marked this pull request as draft June 24, 2025 11:42
@DarthB DarthB closed this Jun 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enforce:coverage Run coverage flow even on draft pull request size:L size:XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants