News for hackers. Stuff that matters.
Re:Moving to a provenance-aware memory object model for C: p... by zik on Sunday April 14, 2019 @05:28AM (#19658366)
Can anyone summarise?
Re:Moving to a provenance-aware memory object model for C: p... by pm215 on Sunday April 14, 2019 @06:06AM (#19658475)
The "n2362+appendix" pdf is only 7 pages if you ignore the appendix which is "changes to the actual C standard to implement the proposal", so it's not that long to read, and it starts with a description of the basic idea. The short summary is that it's trying to formalize an existing idea (which has been discussed by the committee and used by compiler implementations but not really nailed down in the standards text) about when the compiler can assume two pointers don't alias. Intuitively, if you create a pointer by dubious arithmetic means (eg by adding 1 to the address of a local variable) the compiler shouldn't have to assume that that might alias with other local variables, or it would generate terrible code out of paranoia. But you do want common operations where a legitimate pointer is arithmetically manipulated to work. The paper proposes a formalization that accommodates reasonable things but rules some implausible/weird stuff as UB to accommodate both those desires.
Re:Moving to a provenance-aware memory object model for C: p... by simias on Sunday April 14, 2019 @06:59AM (#19658628)
It's interesting work but I really wonder if it's desirable. What real-world problems is this supposed to solve exactly? Making the compiler extra-clever about detecting aliasing seems like a good recipe for having broken code appear to work correctly until it doesn't or getting strange behavior that's a pain to debug. Current aliasing rules are already a significant pain point in the C standard IMO, I personally don't want them to become even more complicated.

The only concrete example given in the paper (having two contiguous objects and referencing the 2nd one through a pointer on the first one) is frankly contrived and I'm not sure how that can be considered reasonable or good code. The only time I could see myself writing code like that is when dealing with memory-mapped hardware registers at fixed addresses which would let me do funky pointer arithmetic, but in this scenario the pointer is usually tagged as volatile and sometimes accompanied by a memory barrier to make sure the compiler does the right thing.

Note that if the programmers really wanted the code given in the paper to work correctly (i.e. changing y through an offset pointer from x) they could do it by adding a memory barrier such as

    asm volatile("": : :"memory");
after the *p = 11;. Is it elegant? Arguably not, but it seems like such a niche use to me that it seems like a fair compromise. It's also not standard C but if you're making such pointer gymnastics chances are you're already making assumptions about your environment that go beyond the standard.

I much prefer Rust's approach of "mutable references never alias" and if the programmer really needs to share a reference they have to manually do the work by wrapping. Of course integrating that into C in a backward-compatible fashion doesn't seem very practical.

Re:Moving to a provenance-aware memory object model for C: p... by pm215 on Sunday April 14, 2019 @10:39AM (#19659647)
The programmers don't want the code in the paper to work correctly, though. What they want is for the C spec to rule it incorrect without also ruling a lot of existing working code to be invalid.

Rust's approach is certainly easier to understand in some ways, but C is not Rust and you can't just change its semantics without considering the existing body of code.

Re:Moving to a provenance-aware memory object model for C: p... by Animats on Sunday April 14, 2019 @03:18PM (#19661140)
C needs an ownership model, but it's almost hopeless to retrofit one. This is a very limited proposal regarding some obscure aliasing problems.

I looked at C and C++ ownership years ago. I had more limited goals than the Rust borrow checker. I was thinking in terms of permissions - read, write, delete, and keep, combined with reference counting.

Keep permission means you can take a copy of a pointer and export it to a larger scope. Like passing in a pointer to an object to be added to a collection. The stored pointer will outlive the return from the function that added it to the collection. It "kept" it.

If you pass a reference to an object without keep permission, the called function can't keep it. So the language does not have to increment the reference count. In fact, you don't even have to pass the reference count, just the raw object pointer. So objects passed by non-keep reference to a function incur no extra overhead. That's a very common case. "Keeping" is relatively rare.

So you get reference count allocation without much overhead.

Workable, but a dead end politically in C. Rust essentially makes all references "non-keep", unless you explicitly use reference counts.

Re:Moving to a provenance-aware memory object model for C: p... by fooker on Sunday April 14, 2019 @07:37AM (#19658788)
>I much prefer Rust's approach of "mutable references never alias"

This rules out a vast majority of fundamental algorithms. Jumping through hoops for getting simple logic working doesn't sound very desirable.

Re:Moving to a provenance-aware memory object model for C: p... by ekidd on Sunday April 14, 2019 @08:40AM (#19659057)
>>I much prefer Rust's approach of "mutable references never alias"

>This rules out a vast majority of fundamental algorithms.

To be more precise, in Rust:

- '&mut T' (a mutable reference to T) can never alias.

- '(asterix) mut T' (a mutable pointer to T) can alias.

The advantage is that you can write most production code with a mix of '&T' and '&mut T', which allows the compiler to assume no mutable aliases anywhere. But if you're building a doubly-linked list, then you can explicitly choose '(asterix) mut T'. (For other options, see https://rust-unofficial.github.io/too-many-lists/.)

In practice, it depends a lot on the code you're writing. I've written tens of thousands of lines of production Rust without needing shared mutability. But if I were writing a traditional game or GUI toolkit, then I might miss shared mutability a lot more.

Re:Moving to a provenance-aware memory object model for C: p... by hedora on Sunday April 14, 2019 @11:18AM (#19659886)
The & semantics sound like fortran’s semantics.

How does the rust compiler know if two references passed in from calling code can alias or not? (I.e., what if I get the type annotations wrong?).

In general, determining if two pointers can alias is equivalent to the halting problem.

The default C way is “assume aliasing unless you can prove otherwise”. This seems safer and like less work to me.

Re:Moving to a provenance-aware memory object model for C: p... by wwright on Sunday April 14, 2019 @11:56AM (#19660120)
Aliasing is checked in the calling code, not the callee. You’re not allow to create a mutable reference if another exists in the same scope. If you use “plain” safe Rust, there’s simply just no way to create alias mutable references unsafely.

(The C default you refer to is at the compiler internals level. The Rust alias prevention we’re talking about is at the language level. C compilers will try to avoid generating code that will behave incorrectly in the presence of aliasing, but do nothing to prevent aliasing and the possible logic and thread safety issues caused by aliasing. Rust makes almost all of these cases disappear for the programmer.)

Re:Moving to a provenance-aware memory object model for C: p... by Ar-Curunir on Sunday April 14, 2019 @01:04PM (#19660443)
The compiler ensures that you can only take one &mut reference at a time. You can have as many immutable `&` references as you want, because you can't change anything through those references (unless you do some unsafe trickery)
Re:Moving to a provenance-aware memory object model for C: p... by cesarb on Sunday April 14, 2019 @03:30PM (#19661194)
> you can only take one &mut reference at a time. You can have as many immutable `&` references as you want

There's another small detail: these two cases are mutually exclusive. You can either have one &mut reference, or as many & references as you want. This means that code receiving a & reference can be sure the object won't change, and that code receiving a &mut reference knows the object can be modified safely since nothing else could be looking at it. (Assuming the object doesn't contain a Cell, the rules are a bit different when a Cell is involved.)

Re:Moving to a provenance-aware memory object model for C: p... by simias on Sunday April 14, 2019 @07:58AM (#19658870)
There's always unsafe. With generics you can always implement these fundamental algorithms once and for all in an unsafe block and then reuse that. No need to reimplement quicksort for every data type.

But I'm not proposing to bring that to C (although it seems that generics are slowly creeping in by way of the macro system), I'm just saying that I think the changes proposed in this paper would make the language more complex without really making it easier to write C code 99% of the time.

Re:Moving to a provenance-aware memory object model for C: p... by FrozenVoid on Sunday April 14, 2019 @10:35AM (#19659625)
> It's interesting work but I really wonder if it's desirable

Aliasing pointers ruing performance of array/pointer access optimizations. https://en.wikipedia.org/wiki/Restrict

Re:Moving to a provenance-aware memory object model for C: p... by simias on Sunday April 14, 2019 @12:44PM (#19660373)
I'm aware of restrict and the problem it solves but that does that say about TFA? Are you proposing that restrict should be deprecated in favor of the mechanism proposed by the parent?

The example in the paper linked is specifically a situation where the compiler does not normally catch the aliasing between two pointers which is the opposite of the problem "restrict" solves (i.e. a situation where the compiler emits code that assumes that two pointers can alias when in fact they do not).

Re:Moving to a provenance-aware memory object model for C: p... by voldacar on Sunday April 14, 2019 @08:20AM (#19658964)
Isn't this exactly what __restrict is for though?