Live data from Hacker News

Several core problems with Rust

bykozy.me

311–320 of 341 posts

Re: Several core problems with Rust

#311

Earlier quoted context omitted.

It's not a bug until main uses it incorrectly. The lifetime is outside of the unsafe block. I can change it from static and we still have the same bug.

> It's not a bug until main uses it incorrectly. It's always a bug. The lifetime is wrong. The mere ability to "use it incorrectly" and break memory safety is evidence of a bug. Safe rust in a program without unsafe can't use anything incorrectly in that way. Safe rust in a program with appropriately careful unsafe can't use anything incorrectly in that way. > The lifetime is outside of the unsafe block. The unsafe b…

Forget lifetimes, do this. It's a bug. It's in the safe code. You fix it in the safe code, because it misused unsafe code.

  let vec: Vec = vec![1, 2, 3, 4, 5];
  let ptr = vec.as_ptr();
  let len = vec.len();
  
  let slice_ref = unsafe {slice::from_raw_parts(ptr, len)};
  
  drop(vec);
  
  println!("{:?}", slice_ref);

Re: Several core problems with Rust

#312

Earlier quoted context omitted.

> How would you fix this by only editing the code inside the unsafe block? In this case, the solution is to mark make_slice as unsafe. This should make sense if you think about it for a second, because it's a thin wrapper around an unsafe function. If it were possible to make a function which did what std::slice::from_raw_parts does safe, it wouldn't be marked as unsafe. > It's the exact opposite. From the Rust docs.…

> The actual use case for std::slice::from_raw_parts is for when you control the pointer and length and thus can guarantee that they're valid (specifically, by encapsulating them and making sure no safe functions can get them into an invalid state) Right...so you fix it from the safe code, because the safe code is in charge of using from_raw_parts correctly. > Either way, the memory safety bug is not in safe rust Thi…

> Right...so you fix it from the safe code, because the safe code is in charge of using from_raw_parts correctly.

Safe code cannot corrupt the Vec variables to make Vec do something incorrect.

> To fix this, we would not change anything in unsafe.

You're supposed to.

Some coders might implicitly count the rest of the function as trusted code that's part of enforcing the invariants of the unsafe code. So there's an implied "unsafe" block that fills up the rest of the function, as far as responsibility goes.

I won't argue one way or the other on that philosophy, but if anything outside the function with the unsafe block is part of enforcing invariants, then that is clearly wrong.

If you subscribe to that philosophy, then you could say the drop is the problem in this single-function case. But in the original the bug is still inside make_slice, because make_slice is unleashing a fragile and uncontrolled slice into the safe world of main.

(There might be some more subtleties when it comes to class API boundaries but I don't want to dig through that right now, it's not relevant to the important part of this conversation.)

Re: Several core problems with Rust

#313

Earlier quoted context omitted.

> It's not a bug until main uses it incorrectly. It's always a bug. The lifetime is wrong. The mere ability to "use it incorrectly" and break memory safety is evidence of a bug. Safe rust in a program without unsafe can't use anything incorrectly in that way. Safe rust in a program with appropriately careful unsafe can't use anything incorrectly in that way. > The lifetime is outside of the unsafe block. The unsafe b…

Forget lifetimes, do this. It's a bug. It's in the safe code. You fix it in the safe code, because it misused unsafe code. let vec: Vec = vec![1, 2, 3, 4, 5]; let ptr = vec.as_ptr(); let len = vec.len(); let slice_ref = unsafe {slice::from_raw_parts(ptr, len)}; drop(vec); println!("{:?}", slice_ref);

Okay I ended up replying to this example over here: https://news.ycombinator.com/item?id=46051496

Re: Several core problems with Rust

#314

Earlier quoted context omitted.

> The actual use case for std::slice::from_raw_parts is for when you control the pointer and length and thus can guarantee that they're valid (specifically, by encapsulating them and making sure no safe functions can get them into an invalid state) Right...so you fix it from the safe code, because the safe code is in charge of using from_raw_parts correctly. > Either way, the memory safety bug is not in safe rust Thi…

> Right...so you fix it from the safe code, because the safe code is in charge of using from_raw_parts correctly. Safe code cannot corrupt the Vec variables to make Vec do something incorrect. > To fix this, we would not change anything in unsafe. You're supposed to. Some coders might implicitly count the rest of the function as trusted code that's part of enforcing the invariants of the unsafe code. So there's an im…

>> To fix this, we would not change anything in unsafe.

> You're supposed to.

To me, it just sounds like we're disagreeing on definitions. You can view it as unsafe corrupting the vec, I don't disagree with that, but it's the way the safe code coordinated the unsafe code that caused that problem.

I'm happy to be shown a different way. Would you kindly show me how you would fix this in the unsafe block only?

> Some coders might implicitly count the rest of the function as trusted code that's part of enforcing the invariants of the unsafe code. So there's an implied "unsafe" that fills up the rest of the function, as far as responsibility goes

That's essentially what I'm arguing.

> but if anything outside the function with the unsafe block is part of enforcing invariants, then that is clearly wrong

What is clearly wrong? I'm not saying none of it is wrong, I'm just saying unsafe allows you to make bugs outside of it. And yes, it can happen at arbitrary depth, not just in the function containing unsafe code.

Re: Several core problems with Rust

#315

Earlier quoted context omitted.

> Right...so you fix it from the safe code, because the safe code is in charge of using from_raw_parts correctly. Safe code cannot corrupt the Vec variables to make Vec do something incorrect. > To fix this, we would not change anything in unsafe. You're supposed to. Some coders might implicitly count the rest of the function as trusted code that's part of enforcing the invariants of the unsafe code. So there's an im…

>> To fix this, we would not change anything in unsafe. > You're supposed to. To me, it just sounds like we're disagreeing on definitions. You can view it as unsafe corrupting the vec, I don't disagree with that, but it's the way the safe code coordinated the unsafe code that caused that problem. I'm happy to be shown a different way. Would you kindly show me how you would fix this in the unsafe block only? > Some co…

> Would you kindly show me how you would fix this in the unsafe block only?

I'd probably hold a reference to the vector or something.

> That's essentially what I'm arguing.

The thing is it's only the safe code in the same function as the unsafe block that you could call responsible in any reasonable sense.

In the original version the main() code cannot be blamed for dropping a vector 'early', because it didn't invoke this scheme that requires the vector to be kept alive, make_slice did. It was make_slice's responsibility to guarantee the underlying allocation stays alive, and it didn't do that.

> What is clearly wrong? I'm not saying none of it is wrong, I'm just saying unsafe allows you to make bugs outside of it. And yes, it can happen at arbitrary depth, not just in the function containing unsafe code.

If you're putting responsibility on code in the same function, you're not really treating it as safe code. There's an unsafe{} around the entire function body that you should have written if you were going for maximum clarity.

If the depth goes any higher, you screwed up. It should be impossible for safe code outside the function to use your code and cause a memory error. If it's not impossible then that's a bug in the unsafe blocks (or the function with the unsafe blocks).

Re: Several core problems with Rust

#316

Earlier quoted context omitted.

>> To fix this, we would not change anything in unsafe. > You're supposed to. To me, it just sounds like we're disagreeing on definitions. You can view it as unsafe corrupting the vec, I don't disagree with that, but it's the way the safe code coordinated the unsafe code that caused that problem. I'm happy to be shown a different way. Would you kindly show me how you would fix this in the unsafe block only? > Some co…

> Would you kindly show me how you would fix this in the unsafe block only? I'd probably hold a reference to the vector or something. > That's essentially what I'm arguing. The thing is it's only the safe code in the same function as the unsafe block that you could call responsible in any reasonable sense. In the original version the main() code cannot be blamed for dropping a vector 'early', because it didn't invoke…

> I'd probably hold a reference to the vector or something

In a way that only changes the unsafe block?

> make_slice's responsibility to guarantee the underlying allocation stays alive, and it didn't do that

We çan talk about the simpler code example. How can my unsafe block make sure an allocation stays alive past the unsafe block itself?

> If you're putting responsibility on code in the same function, you're not really treating it as safe code. There's an unsafe{} around the entire function body that you should have written if you were going for maximum clarity

I'm not talking about what should've been done, I'm saying this is something to be aware of on projects with multiple people who may not catch something like this. People think bugs cannot happen in safe code. I just showed it happening. The bug was not in unsafe, or in the drop, it was in println.

Re: Several core problems with Rust

#317

Earlier quoted context omitted.

> Would you kindly show me how you would fix this in the unsafe block only? I'd probably hold a reference to the vector or something. > That's essentially what I'm arguing. The thing is it's only the safe code in the same function as the unsafe block that you could call responsible in any reasonable sense. In the original version the main() code cannot be blamed for dropping a vector 'early', because it didn't invoke…

> I'd probably hold a reference to the vector or something In a way that only changes the unsafe block? > make_slice's responsibility to guarantee the underlying allocation stays alive, and it didn't do that We çan talk about the simpler code example. How can my unsafe block make sure an allocation stays alive past the unsafe block itself? > If you're putting responsibility on code in the same function, you're not re…

> How can my unsafe block make sure an allocation stays alive past the unsafe block itself?

Put it in another data structure or something?

Listen, if you can't make the invariant work, then you need to change the function. An unfixable unsafe is not an excuse to allow errors

> I'm saying this is something to be aware of on projects with multiple people who may not catch something like this.

It's good to be aware but put the blame in the right place. It's the unsafe code that's actually at fault. If you are seeing corruption, look at the unsafe code first with an adversarial mindset.

> The bug was not in unsafe, or in the drop, it was in println.

N. O.

Any unsafe code that outsources invariant enforcement that affects memory safety to safe code has bugs. There's wiggle room on "outsources" but that's the only wiggle room.

Re: Several core problems with Rust

#318

Earlier quoted context omitted.

What are those issues, if you don't mind me asking? Don't see much OCaml discussion here so I'll take interesting discussions where I can get them.

My biggest gripes: * Windows support is mediocre due to decades of not supporting it at all. * OPAM is a nightmare. Surprisingly buggy and extremely confusing. We're not talking Pip levels of badness but it's nowhere near as good as Rust, Zig or Go's equivalents. * A silly obsession with linked lists. Ok I understand why but it's still annoying. * Ocamlfmt thinks it is writing an essay, and generally makes things les…

I used OCaml extensively for a few years, around the time of OCaml 3 and OCaml 4, and I can add a few cents to this discussion.

Some of the points listed here can be considered a matter of taste or opinion, some are indeed pain points, and some are implementation details.

OCaml as a whole is hard to directly compare with most other languages you mentioned above as "better" or "worse". It both suffers and benefits from being an academic/research project not directly in control of a larger corporation.

As a language, IMHO, it is miles ahead of mostly all the languages mentioned above. It recently adopted a novel mechanism for modeling concurrency called algebraic effects, together with state-of-art multi-core support. This not only abstracts away several features that are usually hardcoded on most languages but puts it on another level as a language and abstraction capability. There are other toy languages that implement similar mechanisms or part of this, but none with the adoption level of OCaml.

However, since it does not have the same amount of resources and adoption, progress sometimes is slower that one would expect. Documentation can be sparse, community is smaller, etc.

Regarding OCaml on Windows, I myself used it exactly 20 years ago. It not only has one implementation, but three. There are some tradeoffs and support is not at the same level as Linux but it's still there, and I wouldn't call it mediocre:

https://github.com/ocaml/ocaml/blob/trunk/README.win32.adoc

https://docs.google.com/document/d/1-aTygzDsxy4mnqvSKEVhifA1...

You might find it harder to find libraries, for sure. I have not checked the situation recently, though given the smaller community that is likely still the case.

As a tongue-in-cheek comment, I could definitely say "OCaml is certainly not a good language - not as bad as most of all the others though".

Re: Several core problems with Rust

#319

Earlier quoted context omitted.

> I'd probably hold a reference to the vector or something In a way that only changes the unsafe block? > make_slice's responsibility to guarantee the underlying allocation stays alive, and it didn't do that We çan talk about the simpler code example. How can my unsafe block make sure an allocation stays alive past the unsafe block itself? > If you're putting responsibility on code in the same function, you're not re…

> How can my unsafe block make sure an allocation stays alive past the unsafe block itself? Put it in another data structure or something? Listen, if you can't make the invariant work, then you need to change the function. An unfixable unsafe is not an excuse to allow errors > I'm saying this is something to be aware of on projects with multiple people who may not catch something like this. It's good to be aware but…

> It's good to be aware but put the blame in the right place. It's the unsafe code that's actually at fault. If you are seeing corruption, look at the unsafe code first with an adversarial mindset.

Yeah, that's why my first comment in this thread was "Just having unsafe in your codebase means changing code outside the unsafe block could cause UB". You can caveat that with "that's bad code", but that's the reality.

> An unfixable unsafe is not an excuse to allow errors

Nobody is saying that, you're just moving goalposts now. Nobody is saying you should allow errors, I'm telling you that everyone, including you, will inevitably have to change the code around the unsafe block, because that's what has to enforce memory at the end of the day.

You said you don't need to look at the safe code, I'm asking how would you fix the unsafe, and you haven't. That's fine, and I'm not faulting the language for it.

>> The bug was not in unsafe, or in the drop, it was in println.

> N. O.

Of course it is. That line becomes instructions that access memory freed by the previous line, drop(vec). That's called a dangling pointer. You remove println and the slice is now dropped immediately. You remove drop and the println will work. The vector is not "corrupted" by unsafe. That's not how computers work. We just lose guarantees from Rust when using unsafe, including in safe code. Doesn't mean there's a bug. That's the whole point of unsafe, is to be trusted by the compiler.

> Any unsafe code that outsources invariant enforcement that affects memory safety to safe code has bugs.

All useful unsafe code outsources invariants to safe code. If we could verify the integrity of the memory, we wouldn't be using unsafe.

Now you have been interchangeably using unsafe to mean the literal blocks and the surrounding code. But here is my point: If you are saying that "unsafe code" means "just the unsafe blocks," then yes, unsafe fundamentally relies on safe code to do the right thing.

But if "unsafe code" means "everything that must uphold the invariant," then unsafe can span your entire codebase. Which is true. Just the presence of unsafe in your code base means you're looking at UB anywhere in the call stack if people don't pay attention. And that's been my whole point of this thread. The presence of unsafe means everyone now has to pay attention not just to the safe block, but all safe code interacting with that data, especially in multi-threaded scenarios.

Re: Several core problems with Rust

#320

Earlier quoted context omitted.

> How can my unsafe block make sure an allocation stays alive past the unsafe block itself? Put it in another data structure or something? Listen, if you can't make the invariant work, then you need to change the function. An unfixable unsafe is not an excuse to allow errors > I'm saying this is something to be aware of on projects with multiple people who may not catch something like this. It's good to be aware but…

> It's good to be aware but put the blame in the right place. It's the unsafe code that's actually at fault. If you are seeing corruption, look at the unsafe code first with an adversarial mindset. Yeah, that's why my first comment in this thread was "Just having unsafe in your codebase means changing code outside the unsafe block could cause UB". You can caveat that with "that's bad code", but that's the reality. >…

> Yeah, that's why my first comment in this thread was "Just having unsafe in your codebase means changing code outside the unsafe block could cause UB". You can caveat that with "that's bad code", but that's the reality.

I agreed with you that changing safe code could trigger the bug, but the safe code is not where the bug is.

> Nobody is saying that, you're just moving goalposts now. Nobody is saying you should allow errors, I'm telling you that everyone, including you, will inevitably have to change the code around the unsafe block, because that's what has to enforce memory at the end of the day.

When fixing a vulnerable unsafe block, you might have to redesign the unsafe API, and that might require changing some safe code.

Once you decide on an API, you will not have to change safe code. The unsafe code handles all of the enforcement. If safe code is enforcing anything, you broke the rules of unsafe.

> You said you don't need to look at the safe code, I'm asking how would you fix the unsafe, and you haven't. That's fine, and I'm not faulting the language for it.

I'm not an expert at rust. I gave a couple prose suggestions but they require redesigning the way the safe and unsafe code talk to each other. Because your original design is inherently flawed. The unsafe code cannot protect itself, so it must not be used this way. You're saying we should make the safe code protect the unsafe code, and that is not right. Unsafe code needs to protect itself.

> Of course it is. That line becomes instructions that access memory freed by the previous line, drop(vec). That's called a dangling pointer. You remove println and the slice is now dropped immediately. You remove drop and the println will work. The vector is not "corrupted" by unsafe. That's not how computers work. We just lose guarantees from Rust when using unsafe, including in safe code. Doesn't mean there's a bug. That's the whole point of unsafe, is to be trusted by the compiler.

"unsafe" means "trust me compiler, I verified this myself"

Losing the guarantee is a bug. You told the compiler it didn't need to prevent a dangling pointer via the unsafe block, that you would prevent a dangling pointer via the unsafe block, and then you didn't prevent it.

If you didn't tell the compiler to trust you, the part that wouldn't have compiled is the unsafe block. You tricked it into compiling that block, so that block is where the bug is.

> All useful unsafe code outsources invariants to safe code.

That's extremely untrue. Lots of data structures protect all their invariants in their unsafe code.

> If we could verify the integrity of the memory, we wouldn't be using unsafe.

"we" are smarter than the compiler. Unsafe is for things "we" can verify but the compiler cannot. You're not supposed to use it for unverified stuff.

> Now you have been interchangeably using unsafe to mean the literal blocks and the surrounding code. But here is my point: If you are saying that "unsafe code" means "just the unsafe blocks," then yes, unsafe fundamentally relies on safe code to do the right thing.

If you're doing things 100% properly, you will expand your unsafe blocks to include everything that verifies and upholds invariants. But even after that expansion, it's still going to be a tiny fraction of your codebase.

> But if "unsafe code" means "everything that must uphold the invariant," then unsafe can span your entire codebase.

Not if your design is competent.

> Just the presence of unsafe in your code base means you're looking at UB anywhere in the call stack if people don't pay attention. And that's been my whole point of this thread.

"if people don't pay attention" is a huge factor here. If your unsafe code is wrong then that makes it hard to write safe code. But if you go fix the unsafe code then you stop needing to worry about safe code triggering a memory error.

> The presence of unsafe means everyone now has to pay attention not just to the safe block, but all safe code interacting with that data, especially in multi-threaded scenarios.

If you did things correctly, any safe function can be ignored for memory safety. Unsafe blocks are supposed to assume that the safe code calling them is actively malicious, and make themselves impossible to misuse.

Post reply on HN