Commit graph

9 commits

Author SHA1 Message Date
Scott Hutton
39a9c3b956 Improve cdiff_apply performance
* Work with data as &[u8] instead String/&str to avoid unnecessary UTF-8
  validation and reuse read buffers.
* Make error handling more concise
* Address Clippy-raised issues
2022-01-28 15:42:43 -07:00
micasnyd
527ce73449 Rust bindgen: Disable layout tests
Bindgen makes tests for struct layout by default.

We don't want these tests because pointer width won't be the same everywhere.
2022-01-14 11:50:07 -07:00
micasnyd
4d34bc10b8 Rust bindgen: Only generate if CMake MAINTAINER_MODE is ON
In testing, I found that libclang.so/clang.dll is required by bindgen
and was not found on all of our test machines. To resolve this we will
only generate sys.rs bindings when CMake MAINTAINER_MODE option is "ON".

This is unfortunate that we have to commit generated source to version
control. But as a benefit it makes rust-analyzer happier when editing a
workspace that hasn't yet been compiled. And it makes it more reasonable
that the generated sys.rs file generated to the source directory and not
the build directory (something we hadn't resolved yet).
2022-01-14 11:50:07 -07:00
micasnyd
ba0a21c31f Generate Rust-bindings for some internal C-headers
Use bindgen to generate Rust-bindings for some libclamav internal
functions and structures in an new "sys.rs" module.

"sys.rs" will be generated at build-time and unfortunately must be
dropped in the source/libclamav_rust/src directory rather than under the
build directory. As far as I know, Cargo/Rust provide no way to set an
include path to the build directory to separately place generated files.
TODO: Verify if this is true and move sys.rs to the build directory if
possible.

Using the new bindings with:
- the logging module.
- the cdiff module.

Also:
- Removed clamav_rust.h from .gitignore, because we generate it in the
  build directory now.
- Removed the hand-written bindings from the cbindgen exclusions.
  lib.rs has an annotation that prevents cbindgen from looking at sys.rs.
- Fixed a `u8` -> `c_char` type issue in cdiff in the cli_getdsig() call
  parameters.
2022-01-14 11:50:07 -07:00
Scott Hutton
0872cc2053 cdiff: Rust-ify and error handling clean-up.
Rustify cdiff_apply() and clean up error handling:
- Restore [some] safety and clean up error handling.
- Use rust-crypto sha2 instead of OpenSSL's.

Fix signedness of cli_versig2() dsig parameter.
c_char may be an i8 or u8 depending on platform:
https://doc.rust-lang.org/src/std/os/raw/mod.rs.html#91-133

Rustify cmd_close():
- Consolidate DEL/XCHG records.
- Tidy up ADD handling.
- Various error handling cleanup, etc.
2022-01-10 12:18:33 -07:00
micasnyd
5698e3285c CMake-Rust: Only execute cbindgen for build, not tests
Generate c-bindings header in build (binary) directory,
not source directory (if different).
2022-01-10 12:18:33 -07:00
Micah Snyder
b362747705 Rust: Link libclamav test executable with libstdc++ on linux
This is needed for static builds with static dependencies.
2022-01-10 12:18:33 -07:00
Scott Hutton
278ba2923e Integrate cbindgen via build.rs 2021-12-08 13:15:46 -07:00
Scott Hutton
37e5b573aa Add Rust logging module unit test & integrate with CMake / CTest
Add a basic unit test for the new libclamav_rust `logging.rs` module.
This test simply initializes logging and then prints out a message with
each of the `log` macros.

Also set the Rust edition to 2018 because the default is the 2015
edition in which using external crates is very clunky.

For the Rust test support in CMake this commit adds support for
cross-compiling the Rust tests.
Rust tests must be built for the same LLVM triple (target platform) as
the rest of the project. In particular this is needed to build both
x64 and x86 packages on a 64bit Windows host.

For Alpine, we observed that the LLVM triple for the host platform tools
may be either:
- x86_64-unknown-linux-musl, or
- x86_64-alpine-linux-musl
To support it either way, we look up the host triple with `rustc -vV`
and use that if the musl libc exists. This is a big hacky and
unfortunately means that we probably can't cross-compile to other
platforms when running on a musl libc host. There are probably
improvements to be made to improve cross compiling support.

The Rust test programs must link with libclamav, libclammspack, and
possibly libclamunrar_iface and libclamunrar plus all of the library
dependencies for those libraries.
To do this, we pass the path of each library in environment variables
when building the libclamav_rust unit test program.
Within `libclamav_rust/build.rs`, we read those environment variables.
If set, we parse each into library path and name components to use
as directives for how to build the unit test program.
See: https://doc.rust-lang.org/cargo/reference/build-scripts.html

Our `build.rs` file ignores the library path environment variables if
thye're not set, which is necessary when building the libclamav_rust
library and when libclamunrar isn't static and for when not linking with
a libiconv external to libc.

Rust test programs are built and executed in subdirectory under:
  <target>/<llvm triple>/<config>/deps
where "target" for libclamav_rust tests is set to <build>/unit_tests
For example:
  clamav/build/unit_tests/x86_64-pc-windows-msvc/debug/deps/clamav_rust-7e1343f8a2bff1cc.exe
Since this program isn't co-located with the rest of the libraries
we also have to set environment variables so the test program can find and
load the shared libraries:
- Windows: PATH
- macOS: DYLD_LIBRARY_PATH
We already set LD_LIBRARY_PATH when not Windows for similar reasons.

Note: In build.rs, we iterate references to LIB_ENV_LINK & Co because
older Rust versions do implement Iterator for [&str].
2021-11-23 10:43:14 -08:00