Skip to content

Commit

Permalink
feat: add sanitize feature (#162)
Browse files Browse the repository at this point in the history
* feat: add sanitize feature

* Update README.md

Co-authored-by: Pure White <wudi.daniel@bytedance.com>

---------

Co-authored-by: Pure White <wudi.daniel@bytedance.com>
  • Loading branch information
liuq19 and PureWhiteWu authored Feb 7, 2025
1 parent 2ede40b commit 2276c06
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,6 @@ use_raw = []

# Allow to parse JSON with invalid UTF-8 and UTF-16 characters. Will replace them with `\uFFFD` (displayed as �).
utf8_lossy = []

# Enable sanitize, maybe cause 30% performance-loss in serialize.
sanitize = []
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ A fast Rust JSON library based on SIMD. It has some references to other open-sou

3. Please add the compile options `-C target-cpu=native`

4. Should enable `sanitize` feature to avoid false-positive if you are using LLVM-sanitizer in your program. Don't enable this feature in production, since it will cause 30% performance loss in serialize.

## Quick to use sonic-rs

To ensure that SIMD instruction is used in sonic-rs, you need to add rustflags `-C target-cpu=native` and compile on the host machine. For example, Rust flags can be configured in Cargo [config](.cargo/config.toml).
Expand Down
8 changes: 3 additions & 5 deletions scripts/sanitize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ set -ex
export ASAN_OPTIONS="disable_coredump=0:unmap_shadow_on_exit=1:abort_on_error=1"

run_tests() {
local san="$1"
local features="$2"
cargo +nightly test --target x86_64-unknown-linux-gnu --features "$features" -- --test-threads=1 --nocapture
cargo +nightly test --doc --package sonic-rs --target x86_64-unknown-linux-gnu --features "$features" -- --show-output --test-threads=1
cargo +nightly test --release --target x86_64-unknown-linux-gnu --features sanitize "$2"
cargo +nightly test --doc --package sonic-rs --target x86_64-unknown-linux-gnu --features sanitize "$2"
}

echo "Running tests with $1 and $2"
RUSTFLAGS="-Zsanitizer=$1" RUSTDOCFLAGS="-Zsanitizer=$1" run_tests $1 $2
RUSTFLAGS="-Zsanitizer=$1" RUSTDOCFLAGS="-Zsanitizer=$1" run_tests "$2"
4 changes: 2 additions & 2 deletions src/util/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,12 +585,12 @@ pub fn format_string(value: &str, dst: &mut [MaybeUninit<u8>], need_quote: bool)
std::ptr::copy_nonoverlapping(sptr, temp[..].as_mut_ptr(), nb);
load(temp[..].as_ptr())
} else {
#[cfg(not(debug_assertions))]
#[cfg(not(any(debug_assertions, feature = "sanitize")))]
{
// disable memory sanitizer here
load(sptr)
}
#[cfg(debug_assertions)]
#[cfg(any(debug_assertions, feature = "sanitize"))]
{
std::ptr::copy_nonoverlapping(sptr, temp[..].as_mut_ptr(), nb);
load(temp[..].as_ptr())
Expand Down

0 comments on commit 2276c06

Please # to comment.