5 ms·
Buffers on the Edge: Python and Rust
- dijit 4y agoNaively, going into this I assumed “channels”[0] would be the solution. All I took away was the age-old adage of: try to avoid passing objects back and forth from native code and also that I write inefficient code that copies buffers more often than I thought. If you’re interested in python performance I saw a really good talk from strangeloop this year titled "Python Performance Matters": https://youtu.be/vVUnCXKuNOg https://youtu.be/vVUnCXKuNOg ; Emery Berger has been talking about python performance for quite some time. [0]: https://doc.rust-lang.org/std/sync/mpsc/fn.channel.html https://doc.rust-lang.org/std/sync/mpsc/fn.channel.html
- macintux 4y agoI mentioned this last time I spotted a link to a Strange Loop talk, but worth reiterating so fewer people miss the opportunity: 2023 will be the last conference. Make it if you can, it’s an amazing experience. https://mailchi.mp/thestrangeloop/strange-loop-2021-cfp-5896589 https://mailchi.mp/thestrangeloop/strange-loop-2021-cfp-5896... (I’m in no way an interested party, other than someone who was lucky enough to get my job to send me once.)
- NotYourLawyer 4y ago> But Python code (even buggy Python code!) is not supposed to be able to trigger C-level undefined behavior, that’s part of the point of using a high-level language like Python. Is this really true when you’re directly modifying the underlying data structure of a python object? That can’t really be considered “pure python” anymore, and it seems like there would LOTS of ways to use it to cause shenanigans.
- macintux 4y agoI had the same reaction: clearly all bets are off when you expose memory directly to another runtime.
- choeger 4y agoNo, its not. But the general idea is, I think: If the composition of correct code can lead to incorrect behavior and the language that handles this composition does not allow to correct the error (and explicitly supports the composition), then that language is somehow lacking.
- atoav 4y agoAs an experienced python developer that dabbles in Rust (and the connection of both) I'd say that you should have a hard time to program Python in a way that makes it run into undefined behavior. However I have more confidence in the Rust code being unexploitable than the Python code. Rust's strict ownership rules, the default immutable variables, the strict type system and the good test framework all combine to code where much less can happen beyond the intended paths. Python hooks into C at some points. I have not verified every single one of those points.
- mejutoco 4y ago> you should have a hard time to program Python in a way that makes it run into undefined behavior Iterating a data structure while modifying it can get some very weird behaviour in Python. I think it qualifies as undefined behaviour. Python recommends not to do this, without any safeguards in place.
- ubercore 4y agoWouldn't that produce unexpected but not necessarily undefined behavior? To parent's point, I'm not sure there's going to be a language level _exploit_ there, but it will certainly not work the way you'd think.
- quietbritishjim 4y agoIterating a pure-Python list in one thread while modifying it in another will not give you true C-level undefined behaviour. It might throw an exception or return the same element twice, etc., but it won't crash or do any nasal daemon stuff (time travelling crashes). In part, that's due to the Python GIL. What this articles says is that the buffer protocol allows bugs that are qualitatively different.
- Etherlord87 4y agoUndefined behavior means it's not specified by the language how the code should be compiled. Just because something is weird and hard to predict doesn't mean it's UB.
- kzrdude 4y agoPython also makes no distinction between safe and unsafe code. Now, "ctypes" is maybe enough of a marker, but it just illustrates how easy it is to create danging pointers in Python just like in C, if desired. import ctypes ptr = ctypes.c_char_p(0x1234) # integer to pointer cast ptr.value # deref dangling pointer And similar operations are available through numpy and the buffer API, just a bit harder to jump into those, but once you do, the same kind of mistakes can happen.
- pjmlp 4y agoIn BASIC, a wrong POKE could also kill the machine and a reboot was required. The big difference is that most of these mistakes are opt-in, while in C every line of code is a possible source for problems.
- IshKebab 4y agoRight but his point was that they aren't explicitly opt-in. There's no `unsafe` keyword you have to use in Python before you are allowed to do unsafe things. It's just based on documentation essentially. Therefore there's not really any issue with having a Python API that you have to be a bit careful with to use safely - as long as it is documented and relatively obvious, it's no different to ctypes.
- pjmlp 4y agoI would assert that import ctypes is the opt-in somehow.
- DougBTX 4y agoYeah, that's a fair argument. You'd probably expect any public API exposed by functions in the module importing ctypes to provide a "safe" interface. The difference would be if any of that public API was unsafe, in Rust you could force the caller to use the unsafe keyword too (since unsafe functions can only be called in unsafe blocks), whereas in Python you'd rely on documentation.
- tialaramex 4y agoRust's answer here would be yes. The code which made this possible is faulty. For example, you can trivially reintroduce C-style undefined behaviour on buffer overflow by writing a Rust type which implements Index (and optionally IndexMut) using unsafe to just not bother with a bounds check. Rust says this type is wrong, it should not do this, that's a bug -- even if your program using it has no buffer mistakes, the type is wrong anyway. This is a fact of Rust's culture rather than a technical implication of the Rust language design, albeit they're closely related.
- pjmlp 4y agoThis is one thing that I appreciate in the Rust culture, back to the days of Object Pascal/Modula-2, or the managed languages, where we do a working solution first, and only if it doesn't meet the performance requirements, we go through the optimization phase, with possible somehow hacky code disabling bounds check and such ({$R-} on TP since forever). C++ culture was also a bit like that on the early days when we discussed about safer abstractions to C code on Usenet, somehow that got lost along the way as more C folks migrated into it.
- nindalf 4y agoIt’s good that Rust has this culture. But the flip side is that issues that wouldn’t even be bugs in other languages have CVEs filed for them in Rust. A layman looking at the Rust language thinks it looks very insecure. Example from a couple weeks ago - https://news.ycombinator.com/item?id=33151963 https://news.ycombinator.com/item?id=33151963
- pjmlp 4y agoThe problem, and this goes back as I can remember, is that C folks use the remaining 30% of bugs causes, to dismiss any language that is sold as improving safety on systems programming. It is Rust today, it was Ada, Object Pascal and Modula-2 in the early 80's, and anything in between since then
- 4y ago
- quietbritishjim 4y agoYou're right that a C extension could be implemented in a way that breaks the usual Python C extension contract and then have undefined behaviour. For example, if you release the GIL and then iterate over a Python list then another thread could modify that list (causing the underlying buffer to point at another location) causing undefined behaviour. But this is a case of undefined behaviour even for C extensions that do obey the usual contract. It even includes numpy for example. The two relevant elements are: * When C extension uses the buffer protocol, is should operate directly on the buffer - the whole point of the buffer protocol is to avoid copying and allow speed almost of native code. * When C extension is is doing CPU intensive work, release the Python GIL (but don't use any CPython APIs e.g. to instantiate a new object, iterate a list, etc.) A major selling point of numpy, for example, is that you can use it to do CPU intensive work without the GIL locked, allowing true parallelism. I don't think it's a deal breaker that you can use this to get proper C-level undefined behaviour but it's definitely interesting, and I don't consider it a bug in numpy or Python.
- NotYourLawyer 4y agoI’ve never used buffers, but my understanding is that you can modify the contents directly from python without even needing a C extension. Surely I could trigger UB by just scribbling into the data structure for one of python’s native types and then accessing it normally.
- quietbritishjim 4y agoI don't think that's true - you can use the buffer protcol to give one object a view into another but those objects' classes need to be written in C in order to allow that exchange to happen. But if even if you were able to modify a buffer-protocol buffer directly using native Python, that would keep the GIL locked, which would prevent simultaneous access from multiple threads.
- incomingpain 4y ago>Is this really true when you’re directly modifying the underlying data structure of a python object? That can’t really be considered “pure python” anymore, and it seems like there would LOTS of ways to use it to cause shenanigans. I've done this against my own code. I got to a point where I needed shared memory due to multiprocessing but couldn't get anything to work quite right. https://docs.python.org/3/library/multiprocessing.html#module-multiprocessing.sharedctypes https://docs.python.org/3/library/multiprocessing.html#modul... Whatever idiocy that got me there need not be mentioned but I infact was able to code a buffer overflow, and i only needed to bypass dep. aslr wasn't important even though it was win 10 with aslr on. In hindsight, not a python problem. Definitely a me problem.
- Havoc 4y agoInteresting read. Seems a bit odd to use python at all if one cares about this level of speed optimisation?
- __alexs 4y agoBecause CPython code is extremely slow there is an active cottage industry of extensions for making things very fast. This of course creates the somewhat strange outcome that writing really fast Python code is all about writing your code in anything but Python but the syntax and ecosystem are obviously worth it to people.
- FridgeSeal 4y ago> strange outcome that writing really fast Python code is all about writing your code in anything but Python I really have to wonder why people are so insistent on keeping up the “Python charade”, like, surely at some point we recognise that all the effort trying to back-port performance into something that really isn’t having it, isn’t really worth it, and we’re better off just porting the bits of Python that work out to other languages that are fast/ergonomic/correct/etc and everyone involved can get some time back.
- pjmlp 4y agoBecause Python is the new BASIC, the big difference is that at least BASIC not only has compilers for decades, it was originally designed (Dartmouth BASIC) as JIT based language. The interpreters got famous on the 8-bit machines because there wasn't space for either JIT or AOT compilers. The latest talk I saw regarding Python performance actually addresses this charade, with a profiler that helps locate which parts of Python code should be rewritten in C, some people really love their "Python". "Python Performance Matters" https://www.youtube.com/watch?v=vVUnCXKuNOg https://www.youtube.com/watch?v=vVUnCXKuNOg
- albertzeyer 4y agoThis is a slow process which takes many years, or even decades. And what language is really the alternative? There is no clear answer. Many languages might somewhat be ok but I'm not sure. Python has grown so much because: - It's very simple. - Its syntax almost reflects algorithmic pseudo code. - It's a managed language with garbage collection, you will usually not see segfaults or other memory corruption, and also not the simple kind of memleaks. - Debugging is easy. Those are features which are really nice for newcomers from other scientific fields, but also for professionals when Python is just the gluing language which orchestrates some heavy workers, e.g. TensorFlow or PyTorch. Or when it's just used as a better shell scripting language. I'm myself using Python mostly for machine learning (TensorFlow, PyTorch), and as a shell script language alternative, and for other small utilities. And I really don't know what alternative language there is, which would be a better fit for this, even ignoring the state of the tooling and library support. Some people might say Julia. But Julia is more complicated and more difficult to understand, esp for newcomers. And Julia has slow startup times. Slow startup times already disqualifies Julia for these applications, or at least it's a very huge disadvantage.
- zokier 4y ago> If we have a Python buffer, and we want to represent the data in Rust, how should we do so? The natural answer would be a &[u8], but as you may have picked up, in the face of the possibility of concurrent writes, this would be unsound. Similarly, an &mut [u8] is unacceptable because the Python buffer protocol provides no assurances that only one mutable buffer is handed out at a time. Idk, to me the natural answer for anything crossing "ffi" border would be *mut u8. Afaik rustc makes fewer assumptions about raw pointers and it might be closer to sound interface?
- Diggsey 4y agoYeah a raw pointer makes sense but is equally unhelpful for the caller, since there's still no safe way to access the data. It sounds to me like the Python buffer protocol is under-specified, which can lead to data races in native code. Here's an example of how this could occur without Rust being involved: - Two python threads with a shared buffer object - One thread instructs a C extension to write into the buffer - The other thread instructs a C extension to read from the buffer - The buffer is implemented to return the same pointer for reads and writes (not forbidden by the spec https://docs.python.org/3/c-api/buffer.html#buffer-request-types https://docs.python.org/3/c-api/buffer.html#buffer-request-t...) - The C extension releases the GIL while accessing the buffer (explicitly allowed by the spec) In this case, the C extension is reading and writing to the same buffer at the same time without the use of atomics, which is the definition of a data race, and therefore triggers UB. IMO, the best fix would be for the buffer implementation to expose a "concurrency mode", and require that users of the buffer API use the concurrency mode specified by the buffer when accessing it. In one mode, no synchronization is necessary because the buffer only ever hands out one writable pointer or multiple readable pointers but never both. In another mode, you might be required to have the GIL, in another mode there might be a buffer-specific RWLock you have to hold while accessing it.
- jerf 4y ago"Yeah a raw pointer makes sense but is equally unhelpful for the caller, since there's still no safe way to access the data." The problem is exactly that there is no safe way to access the data, according to Rust's definition of safety. The entire point of Rust is to exclude the C-esque "well, I have a pointer, I guess I have the right to do whatever I want with it, I hope, maybe?" access model, but that's what Python is operating under. No amount of Rust hackery can add Rust's promises onto the Python code. I'd argue that the "correct" answer is to hand back an opaque pointer value into the Rust, and obligate all Rust access to that opaque pointer to be through "unsafe". To put things back into Rust's safe space would require an unsafe copy, but at least the copy would be back in "true" Rust. The problem here isn't that that is incorrect, the problem is that it's uselessly unergonomic, both for the programmer, and because I may not want to pay the price of making a copy. At this point we're just down to figuring out what promise of Rust we want to break. (The only reason we're not discussing which promises of Python we want to break is that on this dimension, it doesn't make any. There are other conceivable situations where we might have compromise in both directions, e.g., Haskell's immutability.) We have the freedom of acknowledging there isn't a perfect solution, and then pursuing the "good enough"... and the "good enough" is probably to just hand Rust the buffer wrapped in whatever Rust type you feel like/is most appropriate from Rust's point of view, and then just saying "all this type can do, and therefore all it does, is constrain my Rust code; I can do nothing about the Python at all on this level". Since there is no solution, spinning about trying to find one is not terribly productive.
- pwdisswordfish9 4y ago> mutable XOR shared rule Mutable NAND shared.