Skip to content

Maybe pre-check orderings in the atomic crate? #43

Description

@Yunzez

Problem
I was doing some dumb fuzzing on this crate, with a harness that potentially creates invalid combinations for the store function. This crate does not validate atomic ordering combinations before calling the core library functions. As a result, using invalid orderings (such as acquire for a failure ordering in store) causes a panic in the core library.

It’s not a bug in the crate or the core library. More of a suggestion to see if it would be useful to handle or pre-check orderings in the atomic crate? No worries if this is considered out of scope.


Harness

#![no_main]

extern crate libfuzzer_sys;
extern crate atomic;
use atomic::Ordering;
use atomic::Atomic;
use libfuzzer_sys::fuzz_target;
fuzz_target!(|data: (i32, i32, u8)| {
     let atomic = Atomic::new(data.0);
     let ordering = match data.2 % 5 {
         0 => Ordering::Relaxed,
         1 => Ordering::Release,
         2 => Ordering::AcqRel,
         3 => Ordering::Acquire,
         _ => Ordering::SeqCst,
     };
     atomic.store(data.1, ordering);
});

stacktrace

Running: fuzz/artifacts_cluster/atomic_Atomic_store_fuzz/crash-b11d72c1605a56162f3e1c36dba5d64a18c21329
thread '<unnamed>' panicked at /.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/sync/atomic.rs:3317:24:
there is no such thing as an acquire store
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
==52362== ERROR: libFuzzer: deadly signal
    #0 0x000104cebd44 in __sanitizer_print_stack_trace+0x28 (librustc-nightly_rt.asan.dylib:arm64+0x5bd44)
    #1 0x0001043f5674 in fuzzer::PrintStackTrace()+0x30 (atomic_Atomic_store_fuzz:arm64+0x100041674)
    #2 0x0001043e88d8 in fuzzer::Fuzzer::CrashCallback()+0x54 (atomic_Atomic_store_fuzz:arm64+0x1000348d8)
    #3 0x00019827ee00 in _sigtramp+0x34 (libsystem_platform.dylib:arm64+0x3e00)
    #4 0x000198247f6c in pthread_kill+0x11c (libsystem_pthread.dylib:arm64+0x6f6c)
    #5 0x000198154904 in abort+0x7c (libsystem_c.dylib:arm64+0x79904)
    #6 0x0001044899b4 in std::sys::pal::unix::abort_internal::h6157f00fb897029e+0x8 (atomic_Atomic_store_fuzz:arm64+0x1000d59b4)
    #7 0x0001044d2fbc in std::process::abort::h4307c511456be812+0x8 (atomic_Atomic_store_fuzz:arm64+0x10011efbc)
    #8 0x0001043e78d0 in libfuzzer_sys::initialize::_$u7b$$u7b$closure$u7d$$u7d$::h54a05afb58bc78cb+0xb8 (atomic_Atomic_store_fuzz:arm64+0x1000338d0)
    #9 0x00010448138c in std::panicking::rust_panic_with_hook::he9e7a691abc6490c+0x2a0 (atomic_Atomic_store_fuzz:arm64+0x1000cd38c)
    #10 0x000104480fdc in std::panicking::begin_panic_handler::_$u7b$$u7b$closure$u7d$$u7d$::h76e8e9b8127ee308+0x64 (atomic_Atomic_store_fuzz:arm64+0x1000ccfdc)
    #11 0x00010447eda8 in std::sys::backtrace::__rust_end_short_backtrace::h96d8ed392cde8e6b+0x8 (atomic_Atomic_store_fuzz:arm64+0x1000cada8)
    #12 0x000104480cbc in rust_begin_unwind+0x1c (atomic_Atomic_store_fuzz:arm64+0x1000cccbc)
    #13 0x0001044d4ce8 in core::panicking::panic_fmt::hf952f5ecf9ded51a+0x1c (atomic_Atomic_store_fuzz:arm64+0x100120ce8)
    #14 0x0001043c0544 in atomic_Atomic_store_fuzz::_::__libfuzzer_sys_run::h9ad112907626b016 atomic_Atomic_store_fuzz.rs:17
    #15 0x0001043bfd50 in rust_fuzzer_test_input lib.rs:359
    #16 0x0001043e142c in std::panicking::try::do_call::hd794699c51429f27+0xc4 (atomic_Atomic_store_fuzz:arm64+0x10002d42c)
    #17 0x0001043e7b38 in __rust_try+0x18 (atomic_Atomic_store_fuzz:arm64+0x100033b38)
    #18 0x0001043e6e80 in LLVMFuzzerTestOneInput+0x16c (atomic_Atomic_store_fuzz:arm64+0x100032e80)
    #19 0x0001043ea1d0 in fuzzer::Fuzzer::ExecuteCallback(unsigned char const*, unsigned long)+0x150 (atomic_Atomic_store_fuzz:arm64+0x1000361d0)
    #20 0x000104407850 in fuzzer::RunOneTest(fuzzer::Fuzzer*, char const*, unsigned long)+0xe0 (atomic_Atomic_store_fuzz:arm64+0x100053850)
    #21 0x00010440c9b4 in fuzzer::FuzzerDriver(int*, char***, int (*)(unsigned char const*, unsigned long))+0x1d18 (atomic_Atomic_store_fuzz:arm64+0x1000589b4)
    #22 0x00010441a284 in main+0x24 (atomic_Atomic_store_fuzz:arm64+0x100066284)
    #23 0x000197ec8270  (<unknown module>)

input hex dump
00000000 0e 00 00 00 00 00 00 81 f8 |.........|
00000009

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions