News for hackers. Stuff that matters.
Re:Memory Safe Languages in Android 13 by dahfizz on Thursday December 01, 2022 @02:48PM (#33821686)
I think a distinction can be made in that you never really need to use unsafe operations in python or Java. In rust, you need unsafe. Just about every data structure in the stdlib uses unsafe.

I think it's fair to call Rust a memory safe language. But I don't think it's on the same tier as a fully managed language like python.

Re:Memory Safe Languages in Android 13 by burntsushi on Thursday December 01, 2022 @02:55PM (#33821787)
I suppose reasonable people can disagree, but I don't think it's anywhere near as clear cut as you seem to be implying. You talk about data structures in std using unsafe, but you don't mention the heaps and piles of C code used to implement CPython's standard library.

It's not like you need `unsafe` in Rust to build every data structure. I build oodles of data structures on top of the fundamental primitives provided by std without using any `unsafe` explicitly whatsoever.

And it is not at all uncommon to write application code in Rust that doesn't utter `unsafe` at all. Even ripgrep has almost none of it. At the "application" level it has exactly two uses: one related to PCRE2 shenanigans and one related to the use of file backed memory maps. Both of those things are optional.

Then there's another whole perspective here, which is that if you're using Rust in the first place, there's a non-trivial chance you're working on something "low level" that might require `unsafe`. Where as with Python you probably aren't doing "low level" work and just don't care much about perf within certain contexts. That has less (albeit not "nothing") to do with the design of the languages and more to do with the problems you're trying to solve.

To be clear, I am not saying you're definitely wrong. But as someone who has written many tens of thousands of lines of both Rust and Python, I would put them on the same or very very close level in terms of memory safety personally. Certainly within the same tier.

Re:Memory Safe Languages in Android 13 by dahfizz on Thursday December 01, 2022 @03:29PM (#33822259)
You make a good point that much of pythong stdlib is implemented in C. But you could implement python's list in pure python, safely. You can't implement something like that in rust without unsafe.
Re:Memory Safe Languages in Android 13 by kibwen on Thursday December 01, 2022 @03:48PM (#33822502)
You can implement lists in Rust safely; with enums, a list is four lines of safe code. You can even implement a doubly-linked list safely. You just can't do either of these things by wielding pointers willy-nilly. If you're willing to accept a performance tradeoff by implementing a list in pure, bootstrapped, FFI-free Python, then you can do the same in Rust.
Re:Memory Safe Languages in Android 13 by alserio on Thursday December 01, 2022 @03:58PM (#33822612)
How would you implement a python list in python? I mean, what would you consider "acceptable" primitives to do so?
Re:Memory Safe Languages in Android 13 by burntsushi on Thursday December 01, 2022 @04:55PM (#33823466)
You certainly can! And that's a good example, because it exposes just how important context is to this discussion. Perf matters in certain contexts. If you implemented a list in pure Python, do you think its users would find the overall perf of Python to be acceptable?
Re:Memory Safe Languages in Android 13 by kllrnohj on Thursday December 01, 2022 @03:24PM (#33822203)
> I think a distinction can be made in that you never really need to use unsafe operations in python or Java.

You can't write any code at all in Python or Java without relying on unsafe operations. Both of them have their runtimes written in C/C++.

So based off of this unusual line of reasoning, Rust is strictly more memory safe than either of those as it's at least possible to have a Rust program without any unsafe code. That program will be of questionable value, sure, but it can at least exist at all whereas it can't for Python or Java.

Re:Memory Safe Languages in Android 13 by dahfizz on Thursday December 01, 2022 @03:33PM (#33822315)
By that logic, you can't write any safe rust at all because it relies on a compiler written in C++.

We are discussing the languages themselves, not any particular implementation.

Re:Memory Safe Languages in Android 13 by steveklabnik on Thursday December 01, 2022 @04:31PM (#33823098)
Only the codegen and many optimization parts of the compiler is in C++. The rest of it is in Rust.
Re:Memory Safe Languages in Android 13 by masklinn on Thursday December 01, 2022 @03:52PM (#33822545)
> You can't write any code at all in Python or Java without relying on unsafe operations. Both of them have their runtimes written in C/C++.

Technically, pypy is a Python runtime written in Python.

Re:Memory Safe Languages in Android 13 by fiedzia on Thursday December 01, 2022 @04:27PM (#33823045)
> You can't write any code at all in Python or Java without relying on unsafe operations.

There are Python interpreters written in other languages: there is one in Rust, and there is Jython and IronPython.

Re:Memory Safe Languages in Android 13 by pjmlp on Thursday December 01, 2022 @06:15PM (#33824412)
There are at least three Java runtimes written in Java, Jikes RVM, MaximeVM, and GraalVM.
Re:Memory Safe Languages in Android 13 by Jweb_Guru on Thursday December 01, 2022 @06:30PM (#33824588)
At least two of which use an unsafe dialect of Java for significant parts of the runtime, which I'm pretty sure you know well (maybe not Graal, but if not it's because it's bootstrapping on top of existing unsafe code).
Re:Memory Safe Languages in Android 13 by pjmlp on Friday December 02, 2022 @01:58AM (#33827708)
Easy localizable via grep, and not full of UB and memory corruption issues, which is what the 70% of unsafety issues due to memory corruption on C, C++ and Objective-C relate to.

At some level of the stack some Assembly or compiler intrisics are needed, not at every line of code.

Re:Memory Safe Languages in Android 13 by Jweb_Guru on Friday December 02, 2022 @03:33AM (#33828188)
Jikes is the one I'm most familiar with and people working on its runtime absolutely suffered from UB and memory corruption issues... obviously not throughout the whole standard library but that's not the case for other JVMs either. In fact the Jikes people found it nicer to work in Rust than in Java on components like the garbage collector, because it was a better fit for working safely with this kind of code and they didn't have to write in a restricted subset of the language to avoid triggering the GC.
Re:Memory Safe Languages in Android 13 by pjmlp on Friday December 02, 2022 @04:47AM (#33828596)
Since when Jikes uses Rust?

Also bootstrapting a language always requiring using its subset for low level layers, apparently not an issue that many parts of C, C++ cannot be implemented only with what ISO provides on the standard.

Re:Memory Safe Languages in Android 13 by Sirened on Friday December 02, 2022 @12:28AM (#33827162)
>You can't write any code at all in Python or Java without relying on unsafe operations. Both of them have their runtimes written in C/C++.

This isn't a meaningful distinction, in the end. Hardware is unsafe too. Real production CPUs have bugs in them which lead to cache lines becoming corrupted, address translations being wrong, branches going to the wrong place, etc. under extremely weird conditions. But, in the end, we don't really do much about it because we trust that it probably won't impact us since we assume the people who built the SoCs or those who wrote the standard library did a good enough job.

Re:Memory Safe Languages in Android 13 by kaba0 on Friday December 02, 2022 @02:42AM (#33827916)
I’m not sure going down this road is meaningful because as soon as we get to machine code generators you get a “reset” on safety, no matter the language you implement a compiler in, it can have logic bugs which will result in many sort of serious bugs, including memory ones. This is true of both the rust compiler and Java’s JIT compiler.

Interpreters and the rest of the VM is a different beast, while they also have to be bootstrapped from some unsafe language one way or another, they are usually written in a much more expert, security- and correctness oriented way than your average program. So while they can and do have bugs, they are exceptionally well tested and, well, I wouldn’t expect the JVM to die out under my program the same way you don’t really expect the kernel to freeze either. This is also true of Rust stdlibs, I assume, but is it true of third party libs?

Re:Memory Safe Languages in Android 13 by Ar-Curunir on Thursday December 01, 2022 @02:58PM (#33821834)
One could implement many data structures without unsafe, but with less efficiency. E.g. using an arena allocator
Re:Memory Safe Languages in Android 13 by dahfizz on Thursday December 01, 2022 @03:26PM (#33822232)
Sure, but that is kind of what I mean. Safety in rust is something you actively have to think about and work around (at least some of the time). It doesn't just come for free like in python.
Re:Memory Safe Languages in Android 13 by bogeholm on Friday December 02, 2022 @10:42AM (#33831962)
Point is, “for free like like in Python” really means “the C implementation hides that for you”
Re:Memory Safe Languages in Android 13 by estebank on Thursday December 01, 2022 @05:39PM (#33823994)
I would like to dispute the "with less efficiency" simplification, because depensing on the size and usage patterns of your code, a doubly linked list or sikilar graph datastructure, backed by an arena will be faster than the way those data structures appear in books.