rust-secure-code / safety-dance

Auditing crates for unsafe code which can be safely replaced
Apache License 2.0
536 stars 10 forks source link

Audit log #69

Open jyn514 opened 4 years ago

jyn514 commented 4 years ago

log: GitHub, crates.io

Reverse dependencies: 4456 many, including rand, env_logger, and tokio.

I was surprised to see so many usages of unsafe in a logging crate:

Functions  Expressions  Impls  Traits  Methods  Dependency

1/1        37/49        0/0    0/0     0/2      !  log 0.4.10
0/0        0/0          0/0    0/0     0/0      ?  ├── cfg-if 0.1.10
0/0        0/15         0/0    0/0     0/2      ?  └── sval 0.5.1

1/1        37/64        0/0    0/0     0/4 
danielhenrymantilla commented 4 years ago

Okay, I have skimmed their code, and there are two reasons for the unsafe:


it's mainly a specialized Any

This actually makes me think that it should be possible to remove all the Erased-related unsafe by replacing:

jyn514 commented 4 years ago

They don't want to remove the transmute for performance reasons :/ https://github.com/rust-lang/log/pull/392#issuecomment-611991970

Shnatsel commented 4 years ago

Perhaps the "new-typed const" idea is worth pursuing then.

@danielhenrymantilla could you open a PR to convert the type erasure to safe code? I admit this is a bit over my head, so I cannot follow up on this myself.

jyn514 commented 4 years ago

e.g., using a module-namespaced set of new-typed constants rather than an enum: the behavior when matching on that enum is then never-exhaustive, and so forces users to handle it with default-branches, that on the current design, is as if they had unsafe { ::std::hint::unrechable_unchecked() }.

This has the same performance penalty as an unreachable in the match would, I'm not sure I understand the benefit.