News for hackers. Stuff that matters.
Re:How to trigger races reliably in the Linux kernel by jfkebwjsbx on Tuesday May 05, 2020 @09:31AM (#23079821)
Data races don't imply memory unsafety (in the sense of having OOB accesses etc.).

It is possible (and in rare cases, desirable) to have data races while still having a perfectly safe program.

Re:How to trigger races reliably in the Linux kernel by fluffything on Tuesday May 05, 2020 @10:48AM (#23080580)
>Data races don't imply memory unsafety (in the sense of having OOB accesses etc.).

Data races are undefined behavior on all major programming languages (C, C++, Rust, Java, Go, Swift, ...) and all major compilers compile code under the assumption that they do not happen.

If your program has a data-race, neither the language specification nor the compiler make any guarantees about the behavior of such programs.

Needles to say, this is neither safe, nor desirable, nor doing so produces perfectly safe programs.

The idea that data-races are safe because the load and store memory instructions that the CPU has are atomic is completely flawed, because it ignores the fact that the CPU does not execute the program you wrote, but the machine code that the compiler generates, and the compiler can and will reorganize non-atomic loads and stores under the assumption that they don't need to be atomic.

If your program has a data-race, the compiler provides no guarantees. It does not guarantee that the binary will reach the location where the data race happens. It does not even guarantee that the binary produced will be an executable binary, or that it will produce a binary at all (e.g. it does not guarantee that compilation terminates successfully).

Re:How to trigger races reliably in the Linux kernel by ChrisSD on Tuesday May 05, 2020 @09:38AM (#23079881)
Could you explain that? Surely either it's dangerous to read and write simultaneously to a memory location or the operations are atomic (albeit perhaps unordered) and thus there is no data race?
Re:How to trigger races reliably in the Linux kernel by zozbot234 on Tuesday May 05, 2020 @09:46AM (#23079941)
Well, your memory model might fail to properly expose "atomic" access at the level you're actually relying on to prove that your 'data race' is safe. (Or using such fine-grained atomic accesses might have bad performance implications that can't be worked around using relaxed operations, etc.) But I don't think that's going to be a common case.
Re:How to trigger races reliably in the Linux kernel by ChrisSD on Tuesday May 05, 2020 @09:57AM (#23080048)
But any optimizing compiler is surely going to assume its memory model is adhered to, no? Therefore any "data races" will be undefined behaviour (even if they're atomic on physical hardware).
Re:How to trigger races reliably in the Linux kernel by jfkebwjsbx on Tuesday May 05, 2020 @10:18AM (#23080233)
"Any" and "surely" are very strong words! Compilers implement different kinds of languages, guarantees, extensions and what not.

Even if your program is not conforming to whatever standard, it may be very well be useful and working in most compilers out there.

As an example, Linux is not conforming C, including triggering UB. But at least two major compilers support the intended semantics even if they are forbidden by the standard.

Re:How to trigger races reliably in the Linux kernel by ChrisSD on Tuesday May 05, 2020 @10:52AM (#23080619)
UB is UB. It might work. It might work now but not in the future. It might work in some situation but not others. It might appear to work but introduce subtle bugs elsewhere in the program.

Linux, as Linus has said, isn't written in ISO C it's written (or was) in GNU C. The two had something of a symbiotic relationship for a long time. Which is why porting it to LLVM was such a tremendous effort.

Which is to say, Linux conforms to the memory model of the compilers it compiles on (or vice versa). Not ISO C's memory model. So I'm not sure how your example is relevant.

Re:How to trigger races reliably in the Linux kernel by gpderetta on Tuesday May 05, 2020 @12:09PM (#23081577)
There has been a significant push to get Linux to use the c11 conformant gcc builtins. There has been significant push back though, but at least some bits have been updated.
Re:How to trigger races reliably in the Linux kernel by steerablesafe on Tuesday May 05, 2020 @11:14AM (#23080886)
Linux arguably relies on compiler extensions. Compilers are allowed to define behavior that is undefined by the language standards, effectively extending the language.
Re:How to trigger races reliably in the Linux kernel by jfkebwjsbx on Tuesday May 05, 2020 @10:11AM (#23080163)
No, it is not "dangerous". CPUs are designed to deal with that all the time (it is a whole field of research) and give you different guarantees when it does happen.

If your software is designed with that in mind, then a data race is not a bug.

Re:How to trigger races reliably in the Linux kernel by ChrisSD on Tuesday May 05, 2020 @10:16AM (#23080220)
I'm not sure I understand you. If it's not dangerous and there are guarantees then there isn't a data race to begin with.
Re:How to trigger races reliably in the Linux kernel by gpderetta on Tuesday May 05, 2020 @09:51AM (#23080002)
at least in C and C++ data races are UB by defintion.