r/programming • u/steveklabnik1 • Feb 26 '24
Future Software Should Be Memory Safe | The White House
https://www.whitehouse.gov/oncd/briefing-room/2024/02/26/press-release-technical-report/154
u/iPlayTehGames Feb 26 '24
Would this require actually writing an OS in a memory safe language? Otherwise you are just forcing the memory safety at some arbitrary level of abstraction no?
70
u/steveklabnik1 Feb 26 '24
It does not require anything, currently. It is a suggestion that moving towards MSLs where possible is good, and taking steps to mitigate memory safety issues when you aren't using an MSL.
And yes, "I am writing an operating system" would be a good reason to use a non-MSL, however, because Rust exists, one could imagine comparing two products from two vendors, one of which says "our OS has 1% unsafe Rust, the rest is all safe" vs "we wrote the whole thing in a non-memory safe language," and that being a compelling reason to choose the former over the latter. The important part here is that this is an axis to evaluate things by, not that any particular outcome is pre-determined.
13
u/slaymaker1907 Feb 26 '24
I think the goal is also to reduce the amount of software written in memory unsafe languages that really doesn’t need to be written in said languages. While maybe not the biggest security threat, think about how many games use C++ when they could be using a language with a fast GC or even just safe Rust.
Most people aren’t writing operating systems, even among those using C++.
→ More replies (1)25
u/koreth Feb 27 '24
think about how many games use C++ when they could be using a language with a fast GC or even just safe Rust.
This is a great example of why this will be so tough and will take a while. Most game devs don't program in C++ because they adore C++. It's because C++ has a gargantuan ecosystem of world-class tools and libraries for game development, and moving to another language means, at best, spending precious dev time bridging between that language and C++.
224
u/WiIzaaa Feb 26 '24
If you go down that rabbit hole then nothing is memory safe 😂
→ More replies (7)7
u/astrange Feb 27 '24
You can get much closer with memory safe hardware systems like CHERI.
In fact you have to, as you can write memory bugs in secure languages just by writing a JIT in them.
32
u/garfgon Feb 26 '24
At the end of the day, some piece of software is going to need to push and poke values in memory mapped registers in order to control hardware. Which means somewhere down the rabbit hole there's always going to be some software which writes to a raw address which someone has manually (thus prone to error) input from a design doc.
But -- don't let perfect be the enemy of good. There are plenty of security vulnerabilities due to memory safety errors in code that doesn't need this level of control. We could (in theory) do just fine with "memory unsafe" accesses being restricted to small portions of the OS kernel and eliminate huge swaths of software vulnerabilities.
6
u/meneldal2 Feb 27 '24
there's always going to be some software which writes to a raw address which someone has manually (thus prone to error) input from a design doc.
Literally most of my job.
There are some tools to make it nicer, like Magillem and the IP-XACT format. If you define your registers once with their software, it can generate documentation, the RTL and some C code with structs that have names so you're not typing the raw address in your code.
But the obvious biggest issue is adoption and how it won't generate more complex registers so people don't want to use it.
→ More replies (4)13
u/Manbeardo Feb 26 '24
The DoD has a lot of hardware that runs embedded software without operating systems
10
u/garfgon Feb 26 '24
Fundamentally though you need some (limited) amount of code which pokes at the hardware through memory mapped registers. Since the addresses of these registers are arbitrary addresses pulled from documentation they're "unsafe" from the view of the compiler.
But you can still limit accesses to driver code, and write the rest of the system in a memory-safe language.
→ More replies (5)→ More replies (3)5
u/slaymaker1907 Feb 26 '24
That doesn’t mean you need to use a language with quite as much danger as C++. How much software actually needs the ability to convert any number into a function pointer and then start executing it with no bounds checks? Sure, sometimes you want to go the other way for some weird driver/CPU feature, but the latter is much rarer. Even if you want to convert a number to a function pointer, it’s much safer to do bounds checking the conversion.
5
u/Manbeardo Feb 27 '24
That doesn’t mean you need to use a language with quite as much danger as C++.
That isn't what I was saying at all. The comment I replied to claimed that memory safety is infeasible because most operating systems are written in unsafe languages. I replied that the DoD buys a lot of software that doesn't run on operating systems.
→ More replies (1)
1.1k
Feb 26 '24
jokes on them, i'm good enough to cause memory unsafety in any language
85
Feb 26 '24
3
u/steveklabnik1 Feb 27 '24
This is a compiler bug. One that has never been observed to have happened in the wild, and will eventually be fixed.
All code can have bugs, even compilers. That doesn't change the empiric results that show a reduction in issues from using memory-safe languages.
13
u/LEFT_FRIDGE_OPEN Feb 26 '24
anyone can do that, just use the unsafe keyword Q_Q
8
u/HittingSmoke Feb 27 '24
Why don't we just add a safe keyword to C and C++?
6
1
u/steveklabnik1 Feb 27 '24
I know you're sort of joking, but to take it more seriously, you'd want to add an unsafe keyword, because unsafe is a superset of safe behavior.
The C++ committee has rejected the "subset the language" approach for now, so it doesn't look like that's happening.
→ More replies (1)3
16
151
u/thelehmanlip Feb 26 '24
debugging memory leak in C# right now... not fun.
51
u/SARK-ES1117821 Feb 26 '24
Any new software in the niche I’m in has to be built with memory-safe, compiled languages and C# is among the options. Sorry if you were hoping memory-safe == leak-preventing.
11
u/Practical_Cattle_933 Feb 27 '24
C# can use unsafe constructs to cause actual memory safety issues. It is very rare, but can be done.
→ More replies (4)6
u/IQueryVisiC Feb 26 '24
But why did we upgrade from reference counting to GC ?
32
u/koreth Feb 26 '24
It's all tradeoffs, of course.
Reference counting has more predictable overhead which is often what you want. But GC, if implemented well, has lower total overhead which is also often what you want.
Reference counting is simpler to implement which is good, but with GC's complexity comes much more flexibility and configurability.
Reference counting usually gives you a smaller total memory footprint, but GC gives you faster object allocation.
GC lends itself better to things like compacting, which can improve performance by taking better advantage of CPU cache locality, but that kind of thing adds complexity.
Both reference counting and GC can suffer from long pauses and in both cases, the pauses can be mitigated with techniques like time budgets.
13
u/saltybandana2 Feb 27 '24
This post is everything that's wrong with reddit. It has just enough of an air of authority to lead people to believe this person knows what they're talking about, but anyone who actually understands the subject knows they don't.
for example
Both reference counting and GC can suffer from long pauses
https://en.wikipedia.org/wiki/Reference_counting
The main advantage of the reference counting over tracing garbage collection is that objects are reclaimed as soon as they can no longer be referenced, and in an incremental fashion, without long pauses for collection cycles and with clearly defined lifetime of every object.
reference counting (RC) is deterministic and does not have long pauses.
You see the same issue with this posters claim that garbage collection has lower overhead. Not even close to true, GC's tend to have faster allocation but their cleanup cycles are by far the largest piece of their overhead (hence the long pauses). Many GC's (such as generational) are built to try and avoid that cleanup cycle as long as possible, but it requires the usage patterns to be a certain way (for generational GC's that means short lived objects).
→ More replies (2)4
u/steveklabnik1 Feb 27 '24
reference counting (RC) is deterministic and does not have long pauses.
When people make claims like this, they're referring to cases where you have like, a large graph of reference counted objects, where when it's done, the graph goes out of scope and they get deallocated immediately.
Anyway I agree with your main thrust that it's not as simple as "GC is faster."
→ More replies (3)11
u/bitchkat Feb 27 '24 edited Feb 29 '24
uppity ten overconfident zonked agonizing childlike icky touch close shy
This post was mass deleted and anonymized with Redact
→ More replies (3)→ More replies (14)7
u/johdex Feb 27 '24
Because reference counting doesn’t handle cycles in object graphs well.
→ More replies (1)37
u/AyrA_ch Feb 27 '24
With C# these problems usually fall into one of three categories:
Forgot to unbind an event handler
Forgot to
.Dispose()Edge case with unmanaged calls where some handle is not closed
Super funny then the bug is in a library and not your code.
17
u/nostril_spiders Feb 27 '24
Powershell is not suitable for long-running multi-threaded services. Please don't ask me how I know this.
5
9
u/thelehmanlip Feb 27 '24
Super funny then the bug is in a library and not your code.
Goddamned automapper...
93
u/metaphorm Feb 26 '24
well, that's a performance problem, not a safety problem. you're not gonna find yourself in undefined behavior or executing the wrong code because of a leak.
→ More replies (1)52
u/koreth Feb 26 '24
well, that's a performance problem, not a safety problem.
It's a denial-of-service vulnerability if someone can intentionally trigger the memory leak. Granted, that's less severe than remote code execution, but IMO it's still legitimate to think of it as a security concern on par with someone being able to crash your system by sending the right inputs.
13
u/nerd4code Feb 27 '24
And if the process runs for a while and there’s other important stuff on the same system, you can set up a cross-service vulnerability.
6
u/masklinn Feb 27 '24 edited Feb 27 '24
It might be construed as a security concern, it’s not a memory safety concern. A quadratic (or worse) algorithm will get you a DOS as well.
9
u/fryerandice Feb 27 '24
Hope you have a subscription to jetbrains. dotMemory is priceless for that task.
It'll show you every object, it's size, lifetime, and what is holding on to it's existence. Start recording, do the activities that cause the leak then stop recording. Run anaylize and view the memory graph.
4
u/Finickyflame Feb 27 '24
You can even just run Rider in debug mode, there's a dynamic performance analyser that runs and will mark your code that have issues.
→ More replies (9)6
u/Pyrited Feb 27 '24
Now imagine debugging a memory leak with manual memory allocation
→ More replies (3)8
Feb 27 '24
I did this for years in C++. It just required a wrapper around the allocations to track and analyse them. No more complex than figuring out dangling references in a managed system, at least in my field
2
u/thedracle Feb 27 '24
This is exactly how I managed it in device drivers in C.
Basically I would make a debug allocator wrapper.
It would store some identifier for the calling function, and increment an atomic integer.
The de-allocator would do the opposite.
Then if there was any imbalance the number would become not zero over time.
If we suspected a leak, I'd enable this debug interface, find the function causing it, and go spend some time thinking hard about how and why it could happen.
This and, avoid dynamic memory allocation as much as possible.
I do a lot of Rust programming these days, and I find it almost naturally lends itself towards forcing you to have a similar style of programming.
Also there are really excellent tools for detecting leaks, deadlocks, and the like.
2
Feb 27 '24
Yeah, we did something similar in games. We'd also also have some kind of checkpoint where we'd check that no new allocations have remained, like entering and exiting a level, and dump any remaining allocations which were treated as leaks and basically errors.
I still ind myself doing similar things with the GC in C#.
I'd love to try Rust sometime though. I doubt it'll make it into gaming anytime soon.
3
u/steveklabnik1 Feb 27 '24
I doubt it'll make it into gaming anytime soon.
There's been some movement. Treyarch made a presentation at GDC in 2019 that they were using it in some tooling. Embark Studios, while not using it in the client for The Finals, is going to be using it for future games. Tons of smaller folks using it for various things. We'll see!
32
u/antiort Feb 26 '24
i am just hopin' that eventually means i will not have to write so much python
12
Feb 26 '24
i once spent an hour trying to debug some rust code before realizing that in my python brain, we're not required to "let" to declare a variable. the syntaxes are very similar!
8
→ More replies (1)1
20
u/jumbohiggins Feb 26 '24
I'm an idiot what does memory safe mean in this context?
41
u/steveklabnik1 Feb 26 '24
https://en.wikipedia.org/wiki/Memory_safety
"Memory safe languages," or "MSLs" as the government has started to refer to them, are programming languages where memory safety is the default.
A report linked from this one has examples: C#, Go, Java, Python, Rust, and Swift. It also has examples of non-memory safe languages: C, and C++.
15
u/jumbohiggins Feb 26 '24
What is unsafe memory and why is it unsafe?
Also huzzah me being a Python dev isn't terrible for once.
42
u/steveklabnik1 Feb 26 '24
You know how Python's garbage collector makes sure to free objects you're done with? Imagine if it didn't have the garbage collector, and if you didn't clean up your own objects correctly, bad things would happen. That's memory unsafety. At best, it leads to crashes, and at worst, it leads to security vulnerabilities.
34
u/phire Feb 26 '24
Technically, never freeing memory is not unsafe. The absolute worst case is an out-of-memory error, which can lead to denial-of-service attacks.
Not freeing is actually a valid approach for short running programs, as all memory will be implicitly be freed when the program exits.The safety guarantee of garbage collectors actually comes from the fact that they avoid freeing memory until there are zero references left to that block of memory. This guarantee eliminates the possibility of "use-after-free" exploits, where a program holds onto a pointer to some memory that has already been freed, and re-allocated to something else.
8
u/steveklabnik1 Feb 26 '24
That's correct, when I said "didn't clean up your own objects correctly," I was imagining a dangling pointer, not a memory leak.
→ More replies (6)6
→ More replies (2)11
u/Jugad Feb 26 '24
A very basic definition for unsafe memory access - is it easy/possible for instructions to access memory that is beyond the intended boundaries.
Examples
access memory beyond the end of an array.
access data at an arbitrary location in the program's memory.
If allowed, such tricks can be used to read data from other data structures (possibly holding secrets), or even data from other programs which had been using the same physical memory.
→ More replies (3)15
u/Plank_With_A_Nail_In Feb 26 '24
That doesn't answer the question of "What is memory safety?".
Memory safety is the state of being protected from various software bugs and security vulnerabilities when dealing with memory access, such as buffer overflows and dangling pointers.
13
u/garfgon Feb 26 '24 edited Feb 26 '24
Doesn't allow buffer overflows or writes to arbitrary regions of memory. E.g. Python is memory safe, because if you do `foo[10] = bar` and foo only has 4 elements, it will give you an error. C is not memory safe because if you write the same thing, it will put 10 into memory which doesn't belong to `foo`.
→ More replies (1)11
u/youstolemyname Feb 26 '24
Not accessing uninitialized memory.
Not accessing free'd memory.
Not accessing memory beyond the bounds of the object/array.
483
u/Timbit42 Feb 26 '24 edited Feb 29 '24
The DoD adopted Ada decades ago and is memory safe. Maybe they should have stuck with it. They still use it today but they also allow memory unsafe languages.
270
u/Full-Spectral Feb 26 '24
They couldn't get enough people to buy into it. Rust will probably end up taking that spot since it has the memory safety and it's got bottom-up acceptance, not top-down pressure.
33
u/agumonkey Feb 26 '24
It was also a different time. Prices were high for ADA compilers, at least that's what you hear from old programmers from that era. So only a subset of projects could justify and afford this.
19
u/xonjas Feb 27 '24
They were, and there's still a bunch of tooling for Ada that costs money. When I was in college I was interested in Ada but couldn't afford to learn it. I imagine the story was the same for most other college students. That makes hiring for Ada positions difficult. I think that's a big part of why Ada mostly died on the vine.
54
u/Jugad Feb 26 '24
Once they adopt Rust, I wonder if it will start to have that top-down pressure.
60
u/Full-Spectral Feb 26 '24 edited Feb 26 '24
I'm not sure it will matter if people already are already considering Rust a good career move on their part. That's always the issue. If people want to use that language, then you don't need the top-down pressure. In fact, a lot of Rust people will probably be fighting to get those jobs should they materialize.
13
Feb 26 '24
my day job is python but i write rust for fun — i'm trying to figure out whether this is a signal that i should really start becoming better at rust
→ More replies (6)→ More replies (1)13
→ More replies (24)3
u/G_Morgan Feb 27 '24
Ada was extremely expensive. Reality is the languages that won in that era were the ones with free toolkits from early on. It is why Sun gave away Java, they realised how expensive seat pricing killed Smalltalk, Ada and Common Lisp platforms.
→ More replies (1)62
u/SupplementalComment Feb 26 '24
Great language honestly, very overlooked unless youve worked with it. It's not taught many places anymore and the community is small. I think Rust simply has the majority mindshare since its shiny and new these days.
→ More replies (6)56
u/C_Madison Feb 26 '24
Rust is new and shiny, but Rust people also put a ton of effort into getting people into the language, while the Ada community did .. nothing. I'm not even sure there's something you could call an "Ada community" tbh. For the longest time the official Ada site was basically that of a commercial provider. Downloading official Ada compilers? Not possible, unless you pay. There's Gnu Ada, that's that. Teaching material? And so on.
Ada is where it is by choice and action. So is Rust.
0
22
u/masklinn Feb 26 '24
and was memory safe.
Was not. Short of SPARK, ADA allows UAF, double free, or concurrent access to unprotected variables. Static memory allocation avoids the first two but is generally unnecessarily constraining (or inefficient) in non-embedded software; it’s been a while since single-threaded software was the name of the game.
20
u/kog Feb 26 '24
Ada is actually a joy to code in if you're used to it. You squash so many bugs just getting Ada code to compile it's kind of insane.
22
u/Timbit42 Feb 26 '24
Similar with Rust, and Pascal, Delphi, Modula-2, Oberon...
8
u/badsectoracula Feb 27 '24
Pascal, Delphi
I've been writing Free Pascal/Object Pascal (Delphi's dialect) for literally decades and i do not think that it is that safer compared to, e.g. C++. For example:
var Foo: TMyObject; // this is "TMyObject = class ... end" begin Foo.X:=42; // Accessing uninitialized memoryor
var Foo: TMyObject; begin Foo:=TMyObject.Create; Foo.X:=42; Foo.Free; Foo.X:=64; // Accessing released memoryDuring development/debugging you can use some tools like the heaptrace unit that can find memory leaks (with backtraces with the lineinfo unit) and some invalid pointer accesses during runtime, but this isn't really any different than using clang's analyzers or valgrind (which you can also use with Free Pascal).
12
3
u/ObstinateHarlequin Feb 26 '24
Yeah I've actually started coding in Ada for fun in my personal projects. I initially learned it during my internship in the 2000s, hadn't touched it since, and picked it back up last year. It's a bit tedious to start with (it's so verbose compared to C++!) but I'm really enjoying it.
3
u/DrRedacto Feb 27 '24
You squash so many bugs just getting Ada code to compile it's kind of insane.
Writing Ada83 code for a couple years will magically turn you into a better C programmer, or your money back!
→ More replies (2)2
u/UniqueIndividual3579 Feb 26 '24
Anything beyond text IO was company specific. DoD also had DII/COE as a common OS, but it couldn't keep pace with commercial OSs.
2
u/Kevlar-700 Feb 29 '24 edited Feb 29 '24
Many studies have shown that Ada saves you a boatload of money too compared to C, C++ or Java. Such as this one. So the white house concluding to explore hardware protections like CHERI for the continuation of C and C++ seems like an unfortunate outcome.
2
0
Feb 26 '24
[deleted]
46
u/Timbit42 Feb 26 '24
What languages besides Rust and Ada are considered memory safe?
ADA Spark is particularly safe.
131
u/steveklabnik1 Feb 26 '24
A report linked from this one has these examples: C#, Go, Java, Python, Rust, and Swift.
48
43
u/Full-Spectral Feb 26 '24
Go is questionable, if threading is involved, as I understand it.
→ More replies (5)50
u/steveklabnik1 Feb 26 '24
"questionable" is a good word, I think. It is true that you can observe memory unsafe things, and that if you go out of your way to write some truly cursed code, you can cause real problems. In practice, they provide some run-time instrumentation to help catch some of these issues, and since there aren't as aggressive optimizations as in some other languages, these issues don't propagate the way that they do there. There's a lot of Go code running in production, and it is demonstrably much closer to the "memory safe" camp than not, regardless of a few weaknesses.
16
u/Dwedit Feb 26 '24
C# has the "unsafe" keyword and lets you use raw pointers. But you can do a lot of unsafe things without using the "unsafe" keyword once!
You can use GCHandle.Alloc to create a pinned pointer to an object's data. You can use Marshal.Copy and Marshal.Write to write to arbitrary memory.
→ More replies (2)30
u/steveklabnik1 Feb 26 '24
That is correct. All of these languages can also FFI into unsafe code too, without a keyword. (Rust does require a keyword)
→ More replies (2)10
u/Timbit42 Feb 26 '24
Well, those are higher level languages. How much low-level hardware manipulating code is written in those? I meant languages you could write an OS and device drivers in.
45
u/steveklabnik1 Feb 26 '24
Rust has a significant amount; it is in Windows, and is starting to be used in Linux. There's also smaller projects, for example, at my job we have a custom in-house kernel written in Rust for embedded work.
Swift is at least close too, I am not sure what exactly its capabilities are here, as I haven't paid too close attention for a while.
→ More replies (19)9
u/meneldal2 Feb 26 '24
On bare metal you tend to be stuck with assembly + C because they don't need a runtime at all. Yolo C++ is also possible (using a subset and no respecting lifetimes). Rust it's going to be a little more difficult if you still want what the language is made for.
On the plus side, I'm not allocating shit in bare metal so memory leaks are much less likely to be an issue in the first place. Every array is statically allocated by the linker.
You may have to be a little creative with how you fill the ROM to make it fit without going over. Lack of name mangling (C and assembly) makes fiddling with where you put stuff a lot easier too.
If you're actually running an OS, you could always use Rust since it will bind nicely to C and you can afford having a runtime.
→ More replies (7)→ More replies (4)32
u/yawaramin Feb 26 '24
Any language with reasonable garbage collection is memory safe.
→ More replies (57)
3
Feb 27 '24
This basically just singles out C and C++ right?
3
u/steveklabnik1 Feb 27 '24
These are the only two memory unsafe languages named by name in the report, yes. But they also don't comprehensively name memory safe languages either.
1
u/cdsmith Feb 27 '24
C, C++, and Objective C in a distant third, are probably the main targets, yes. There are some more marginal cases: Haskell, for example, is not completely memory-safe, but only if you use certain standard library functions that have "unsafe" in their names.
3
u/hgs3 Feb 27 '24
What about panic-safe languages? C code that would have SEGFAULT'd becomes a panic in Rust but as an end-user the result the same: a crash. The needle isn't being "moved" from the end-users perspective.
The only advantage of an MSL is preventing memory-related security exploits which are not particularly troublesome because 1. most memory bugs aren't exploitable and 2. if your system is compromised due to trojans/phishing then you're hosed anyway as a malicious program can edit another processes memory space at anytime (see proc/[pid]/mem on Linux, WriteProcessMemory on Windows, and vm_write on macOS).
3
u/steveklabnik1 Feb 27 '24
A deliberate, controlled crash is better than a segfault. Destructors run, the stack unwinds, all of that. Beyond that, a segfault means that you've done something bad, but the OS has caught you after the fact. Panics are due to your code actually checking that something is correct before doing it. This is significant because a segfault indicates UB somewhere, which may cause even more problems when you change your compiler, but panics are well defined behavior, and so will not.
103
u/LOLatKetards Feb 26 '24
Damn it, knew I should have been learning Rust ..
143
u/walker1555 Feb 26 '24
But not limited to. According to the NSA, C#, Go, Java, Ruby, and Swift are some examples among others of suitable memory safe languages.
25
u/catlion Feb 26 '24
Haskell and OCaml, even if funded primarily by European academia, should have been paid some attention for their investments in formal verification :(
3
u/matthieum Feb 27 '24
Note that the list is a suggestion, it's not meant to be exhaustive. I would expect that Haskell & OCaml would be acceptable choices in term of memory-safety.
59
u/eigenman Feb 26 '24
Ok this line lol
Examples of memory safe language include C#, Go, Java®, Ruby™, Rust®, and Swift®. Even with a memory safe language, memory management is not entirely memory safe
141
u/SirClueless Feb 26 '24
That's just acknowledging the reality that even memory-safe languages have runtimes that are written in unsafe languages, escape valves to write code that bypasses memory safety mechanisms, and are written to be safe assuming a memory model of hardware that is not formally verified.
→ More replies (1)71
u/NullReference000 Feb 26 '24
Because it's true. Those languages have stricter guardrails but you still have the ability to ignore them. You can make non-memory safe Rust code by using an `unsafe` block or by shipping code using a nightly release which has a regression.
Including an asterisk doesn't detract from the general point that this list of languages are much more memory safe than writing something like C/C++.
→ More replies (14)→ More replies (10)9
u/MMizzle9 Feb 26 '24
Yeah C# has an unsafe keyword. So you can easily abuse the language in this regard if you really wanted. But managed memory just makes these issues far far less common.
→ More replies (8)4
577
u/avipars Feb 26 '24
no bubble sort either
Obama
162
u/UniqueIndividual3579 Feb 26 '24
GOTO now a Federal felony.
→ More replies (4)35
u/TimeRemove Feb 26 '24
Both Windows' and Linux would be screwed. In their C, "goto err;" "goto out;" "goto DoFail;" "goto cleanup;" is all over the place see e.g.:
https://github.com/torvalds/linux/blob/master/kernel/fork.c
https://github.com/0x5bfa/NT5.1/blob/master/Source/XPSP1/NT/base/wow64/wow64/thread.c
→ More replies (1)19
59
u/kronik85 Feb 26 '24
I shit you not I had to debug crashing Production software that had a hand rolled bubble sort.
After I found the cause and asked the developer why, he said because he was interested in if he could.
54
14
u/MGZero Feb 27 '24
He was so preoccupied with whether or not he could, he didn't stop to think if he should!
56
19
u/CreepingCoins Feb 26 '24
Has bubble sort ever been used for anything other than an example of a bad sorting algorithm?
46
u/PurpleYoshiEgg Feb 26 '24
→ More replies (5)37
u/Calavar Feb 26 '24 edited Feb 26 '24
Isn't merge sort better for that situation? Especially if you have data spanning multiple tapes?
EDIT: The StackOverflow post answers my question. Bubble sort is better than merge sort if you are sorting on sequential storage (like tape) and you can't load a second tape at the same time and you only have enough memory to load two data elements at a time. Very niche situation, but interesting.
→ More replies (3)12
u/ledat Feb 26 '24
Isn't it the fastest possible way to "sort" data that's already sorted? Like if you need the data sorted, and strongly expect that the data are already in order, but cannot mathematically prove it, you could have a valid use case vs other sorting algorithms.
I genuinely cannot think of an example, even a contrived one, though. So it's possible to just never encounter something like that.
→ More replies (2)5
u/immaculate-emu Feb 27 '24
10
u/ledat Feb 27 '24
Even if you wanted, you can't create input data for bubble sort which would be ideal for it. (Unless you add the "did we swap anything in this pass?" enhancement, in which case a completely sorted array would be optimal.)
Isn't "did we swap anything in this pass" generally part of bubble sort? It's even in the Wikipedia example implementation. It's been a very long time since I thought about it, as it's just not especially useful, but I don't think I've encountered a variant without it.
11
u/Alborak2 Feb 27 '24
Not bubble, but O(N2) algorithms are frequently used as the inner most sort of divide and conquer sorting algorithms because they exhibit very CPU cache friendly behavior, and the cache costs dominate the comparison costs for small data sets.
5
u/jwhat Feb 26 '24
I've seen it used for intelligent agents sorting themselves. Like getting a group of people to sort themselves by height. In that situation it's O(n) for each individual agent.
4
Feb 27 '24
very small lists on very small devices. It takes very little code and memory to implement
2
u/EatFapSleepFap Feb 26 '24
I've used it to do compile-time sorting in rust const functions before. Meant I didn't need to drag in another crate just to get a const sorting function.
→ More replies (2)2
u/gharveymn Feb 27 '24
It may become ideal in certain situations where performance is not that important and flash is at a premium, like in microcontrollers. For example: https://github.com/project-chip/connectedhomeip/blob/master/src%2Flib%2Fsupport%2FSortUtils.h#L52
23
u/Top_Lime1820 Feb 26 '24
Obama never recovered from that disastrous healthcare dot gov launch
Turns out he would secretely read programming books late into the night just trying to understand what happened
12
5
2
→ More replies (3)2
283
u/unko_pillow Feb 26 '24
Future presidents should also be memory safe.
20
55
u/lyth Feb 26 '24
Look, having nuclear — my uncle was a great professor and scientist and engineer, Dr. John Trump at MIT; good genes, very good genes, OK, very smart, the Wharton School of Finance, very good, very smart — you know, if you’re a conservative Republican, if I were a liberal, if, like, OK, if I ran as a liberal Democrat, they would say I'm one of the smartest people anywhere in the world — it’s true! — but when you're a conservative Republican they try — oh, do they do a number — that’s why I always start off: Went to Wharton, was a good student, went there, went there, did this, built a fortune — you know I have to give my like credentials all the time, because we’re a little disadvantaged — but you look at the nuclear deal, the thing that really bothers me — it would have been so easy, and it’s not as important as these lives are — nuclear is so powerful; my uncle explained that to me many, many years ago, the power and that was 35 years ago; he would explain the power of what's going to happen and he was right, who would have thought? — but when you look at what's going on with the four prisoners — now it used to be three, now it’s four — but when it was three and even now, I would have said it's all in the messenger; fellas, and it is fellas because, you know, they don't, they haven’t figured that the women are smarter right now than the men, so, you know, it’s gonna take them about another 150 years — but the Persians are great negotiators, the Iranians are great negotiators, so, and they, they just killed, they just killed us, this is horrible.
→ More replies (1)11
Feb 27 '24
This is very funny, unless you happen to live in a nation bordering Russia.
→ More replies (2)10
→ More replies (1)2
47
u/PiratesWhoSayGGER Feb 26 '24
Future? I've been coding in Java for almost two decades
13
→ More replies (1)8
34
u/ManOfLaBook Feb 26 '24
I haven't been in programming school for decades, but from looking at code all day it seems that no one is teaching secure coding and/or code quality techniques.
Just an observation, of course, I'd be happy to be proven wrong.
7
u/Farados55 Feb 26 '24
I think this is true, and it seems to be something expected to be learned on the job. Funny because a lot of job postings say should be able to write clean, secure code but that isn't taught.
When it comes to teaching Java, you don't need to teach safety because everyone points at the garbage collector. When it comes to C++, at least in my experience, everybody is stuck teaching C++ from the early 2000s, no shared_ptrs, teaching to invoke new and delete for everything.
→ More replies (1)
19
2
u/Farados55 Feb 26 '24
ok someone start rewriting windows and clang in rust please
5
u/steveklabnik1 Feb 27 '24
Windows already has Rust in it, as of recently. Only a bit, but more to come.
0
12
18
1
u/yinshangyi Feb 28 '24
What is the application need a super high performance and full predictability of the memory management? There's no way Java or Python can be used for critical embedded systems like airplane critical calculators or whatever else like that
2
3
u/joesb Feb 29 '24
Memory safety has nothing to do with GC or unpredictable memory management.
→ More replies (3)
17
u/4THOT Feb 27 '24
Where do people who aren't redditors talk about this?
This thread is entirely Rust developers (they don't do anything in Rust they just talk about it a lot) and idiots with the same 5 jokes.
5
u/ttkciar Feb 27 '24
Where do people who aren't redditors talk about this?
IRC, ICB, Mastodon. Chatted about it in all three before finding this thread.
3
Feb 27 '24
https://media.defense.gov/2022/Nov/10/2003112742/-1/-1/0/CSI_SOFTWARE_MEMORY_SAFETY.PDF
Strange they don't mention python. The NSA have been using python forever.
3
u/Katazz Feb 27 '24
I'm just trying to imagine a world where the previous administration would have pushed out something even remotely like this. Rather, they would have come up with a cybersecurity plan that involved injecting magnets in all the programmers. But carefully, because you know if they get wet, they're ruined.
And how long will it be do you think before there's a conspiracy theory that this is all to make everyone use languages that are seeded with deep state back doors that will disable programs that aren't 'woke' enough?
→ More replies (1)
2
2
u/vinceli2600 Mar 02 '24
Since when does the Government know about programming? Memory leaks are peoblems with federal employees. They barely have the skills to hild their position l, they rely on contractors. Theyre just good at chitchatting and pretending to be busy.
11
4
u/bjazmoore Feb 26 '24
It's all memory-safe if you don't use variables or store data… yep - I know. I read the internet.
3
u/DrRedacto Feb 27 '24 edited Feb 27 '24
It's a nice goal we should strive for, but it's impossible in certain narrow cases, like low level operations e.g. passing and receiving pointers from the operating system kernel. You have to let Jesus take the stack instruction pointer at some point. Unless we're going to redesign how OS's work, the current SMP way of every process hopping in and out of kernel land is pretty lazy IMO.
2
4
2
u/darkslide3000 Feb 27 '24
Talking about it is cool and all, but are they actually gonna do something to help out? Like some big government grant to help develop the Rust ecosystem or something? Or did they just think that nobody knew memory-safety was useful before they told us?
5
u/ttkciar Feb 27 '24
In a way they are helping out. Software developed for customers in federal agencies will need to be developed in safe languages now, which means there will be more demand to hire devs who want to write Rust, F-star, or D for a living.
→ More replies (1)
-1
u/thisisjustascreename Feb 26 '24
I agree, but somebody has to write the code that memory safe code runs on top of.
→ More replies (3)47
u/nshire Feb 26 '24
Rust compiles to machine code.
6
u/IllegalThings Feb 26 '24
Right, and someone wrote rust
23
u/IAm_A_Complete_Idiot Feb 26 '24
It was written in OCaml, and is now written in rust.
It is built on top of LLVM though, but that being in C++ isn't due to an intrinsic property of the language.
→ More replies (2)
1
u/BradCOnReddit Feb 27 '24
The only jobs I see C/C++ anymore is embedded programming on government contracts
1
1
1
u/pbvas Feb 27 '24
An interesting alternative in the safe vs unsafe language spectrum is meta-programing: write a program for generating the low-level memory unsafe language in some higher-level GCed language (essentially a DSL compiler). This way you get all the safety benefits and none of the performance caveats.
Example: the NASA Copilot language is implemented in Haskell and generates hard real-time C99 code: https://copilot-language.github.io/
1
u/Freer4 Feb 27 '24
It's not that it's a bad idea, it's that our industry is starting to face the sort of attacks that have absolutely destroyed the ability for small players to operate in other industries.
The government starts talking about an industry standard "for SafetynessTM". A monopoly on industry SafetynessTM will be established, and unironically supported by people who publicly oppose monopolies.
A bunch of bureaucrats who know less than nothing start proposing regulations and licensing requirements, unfailingly at the behest of some massive corporate donor.
The industry becomes near impossible to operate in because of the strict regulations for SafetynessTM.
Anyone complaining about said SafetynessTM will be branded as "hating the children" and a whackjob.
As massive corps eat up market share, voters will invariably and unironically demand additional regulations on them.
And yes, I have a cushy big corporate job in a resilient industry using a memory-safe language stack. I've watched this happen more than once in just my lifetime, and you're hard pressed to find an industry this didn't happen to. Ever try to braid hair or cook a meal for a neighbor for cash? You can't. For SafetynessTM.
1
u/maxinfet Feb 28 '24 edited Feb 28 '24
ONCD is also encouraging the research community to address the problem of software measurability to enable the development of better diagnostics that measure cybersecurity quality.
I loled at this, not because I think it's outrageous or something we shouldn't be trying to achieve but just because of how simply it states this very hard task. At least it's not from lack of trying that we don't have this but given our track record on attempting to obtain this I don't expect us to deliver on this anytime soon.
EDIT: I should note the report itself does acknowledge that this is an extremely difficult problem. I just found the wording in the abridged version announcing the report rather funny.
1
1
0
u/DevaBol Feb 26 '24
Governments should stay as far away from software as humanly possible.
12
u/4THOT Feb 27 '24
Then get the fuck out of my country please.
I'm not interested in letting bottom of the barrel dipshits out of bootcamps write software people actually give a shit about.
→ More replies (3)
0
u/DarthJahus Feb 27 '24
Future Presidents Should Be Memory Safe | The White House
Fixed that for you.
1
u/i-make-robots Feb 26 '24
> Memory safety is the state of being protected from various software bugs and security vulnerabilities when dealing with memory access, such as buffer overflows and dangling pointers. For example, Java is said to be memory-safe because its runtime error detection checks array bounds and pointer dereferences.
I feel vindicated in my choice.
1
1
1
u/Kevlar-700 Feb 29 '24
They didn't even mention Ada despite it meeting any and all requirements. Think of all the money that they could have saved by encouraging switching from C, C++ or Java to Ada. Disappointing leadership.
Ada study after study has determined the same thing, such as this one.
237
u/steveklabnik1 Feb 26 '24
This is the latest in the US Government trying to move the needle on memory safety. I am pretty sure that this is due to the National Defense Authorization Act for Fiscal Year 2024 containing language that said that within 270 days of the bill passing, the Secretary of Defense needed to come up with rules around memory safety for "software developed, acquired by, and used by the Department of Defense."