News for hackers. Stuff that matters.
Re:Chromium project finds that 70% of security defects are m... by nindalf on Sunday May 24, 2020 @01:54AM (#23289452)
I think reduction in the number of errors is a worthwhile goal. Switching from C++ to a language without memory safety issues while being just as fast would reduce the number of things the developer and code reviewer have to worry about. They could spend more time searching for bugs in the logic, potentially eliminating further issues.

In practice what I’ve found is that people prefer to deal in absolutes. A large reduction in a category of bugs isn’t enough, it needs to be eliminated altogether, they say. If it’s still possible in a contrived example, what’s the point of investing in switching?

Re:Chromium project finds that 70% of security defects are m... by whatshisface on Sunday May 24, 2020 @02:06AM (#23289513)
It's a miracle that people who think like that use computers at all - after all, computers only reduce the time it takes to perform a clerical task or a calculation, they do not eliminate it.
Re:Chromium project finds that 70% of security defects are m... by TeMPOraL on Sunday May 24, 2020 @02:15AM (#23289552)
No, it's a reflection of a very important heuristic used in programming (that's arguably much more fundamental on a philosophical level, but correct words to categorize it escape me): the zero-one-many principle. If there's more than one something, you have to treat it as if there may be an unbounded number of something. You can only get it out of your head if you can put reliable bounds on it; the best if you can prove there's less than two of something.
Re:Chromium project finds that 70% of security defects are m... by rictic on Sunday May 24, 2020 @03:25AM (#23289814)
So if you could make your build 70% faster, philosophically it wouldn't matter, because you would still have a build step?
Re:Chromium project finds that 70% of security defects are m... by jfoutz on Sunday May 24, 2020 @04:44AM (#23290147)
No, you sort of walked past their point.

A thing can happen zero times (never). An example might be 1+1 =0. You could get really unlucky with cosmic rays or something, but really adding two registers, both containing 1, that result isn't going to be zero unless there's some sort of hardware failure.

A thing can happen once. an example might be, you can delete a file once. There are ways to get unlucky, of corse, but once it's unlinked it's gone.

if it happens more than once, you really should probably think about an unbounded number of times. now days, that sorta means 2^64 times. There will be bugs when something overflows int64, but I hope you get the gist.

The parent comment is talking about invariants you can use in an algorithm.

I think you might be worried about python vs C or something along those lines. Really, it should all work with a pencil and paper. Which is obviously going to be slower than pushing around electrons. But if you can find those invariants, 0-1-many, you can make a better algorithm. If you're stuck with a pencil it'll still make that faster.

Re:Chromium project finds that 70% of security defects are m... by barrkel on Sunday May 24, 2020 @10:09AM (#23291619)
It's a category error to apply the intuition to a risk calculation, though.
Re:Chromium project finds that 70% of security defects are m... by TeMPOraL on Sunday May 24, 2020 @11:16AM (#23292071)
It's not a category error if you're doing an initial guess. Not to mention, particularly in information security, most of "risk calculation" is intuition and guesses, maaaybe sometimes plugged into a probabilistic framework with something resembling rigor.
Re:Chromium project finds that 70% of security defects are m... by barrkel on Sunday May 24, 2020 @02:31PM (#23293512)
I'd say binary considerations are incompatible with risk calculations because no person and no procedure is perfect nor perfectly followed, nothing is completely bug free, etc. Some small part of a calculation might appear binary, but other terms usually dominate.

Risk calculations are far broader than infosec and don't deserve the dismissiveness you seem to be casting towards them. Risk calculations are the core of business. Almost every decision a business makes is a risk calculation; every action has an opportunity cost if it isn't intrinsically risky, and actions with certainty are very rare.

(For the avoidance of doubt, I believe that use of memory-unsafe languages should be avoided if reasonably possible, but there are still plenty of reasonable reasons to use C, C++ etc. instead.)

Re:Chromium project finds that 70% of security defects are m... by TeMPOraL on Sunday May 24, 2020 @04:45PM (#23294532)
I'm not trying to dismiss risk calculations. I appreciate them and challenges involved, having worked on tools supporting risk calculations in corporate space.

I feel this thread is getting out of hand. I initially replied to say why, in general, the kind of thinking that makes you unsatisfied with reductions but not eliminations of concerns, is common among programmers - because it's a sound heuristic. Reducing is good, but eliminating is better.

Re:Chromium project finds that 70% of security defects are m... by smitty1e on Sunday May 24, 2020 @04:29AM (#23290088)
I think you're after Mathematical Induction.

https://en.m.wikipedia.org/wiki/Mathematical_induction

Re:Chromium project finds that 70% of security defects are m... by fpoling on Sunday May 24, 2020 @02:47AM (#23289676)
Also memory-safe languages with GC allow for simpler API and design. That in turn gives more time to think on errors in logic.
Re:Chromium project finds that 70% of security defects are m... by Too on Sunday May 24, 2020 @03:15AM (#23289780)
In many ways yes but not always. After writing lots of C++ and then going back to managed world I often find cases where the opposite is true. Questions like "will this be mutated?" or "will the library shallow copy, deep copy, or hold a reference to this object?" are very hard to answer in most GC languages.
Re:Chromium project finds that 70% of security defects are m... by deckard1 on Sunday May 24, 2020 @04:11AM (#23290025)
You're exceedingly lucky if the developers you work with these days are aware of the differences between shallow and deep copy, or how object references work.

This is where I make the case for having knowledge of asm/C/C++. The JavaScript developers that started on JavaScript or, maybe, Ruby have no clue what the hell is going on in the language they are working in. I see this all the time. From senior and lead developers, too.

They have no mental model for how pointers work. Which means they have no concept of what an object is, on a memory-organization level. They aren't always aware of when mutation will occur, and they are entirely too reliant on operations that are inefficient. In Lisp, this would be the developer than uses CONS everywhere. There is no awareness of the allocations behind the mechanisms. There is premature optimization and then there is not driving into the damn pothole in the first place! One of these is F1 racing and the other is basic driving skills.

I believe this may also explain why so many developers are so bad at using git. Git is entirely based on pointers. There is an elegance and simplicity to git that is lost on many people.

Re:Chromium project finds that 70% of security defects are m... by fetbaffe on Sunday May 24, 2020 @05:51AM (#23290420)
I’m not sure that is the underlying problem, the lack of knowledge about pointers, I think it is more about that the job programmer today is one of the most common jobs today, that alone lowers the general skill level.

In the old days people usually chose to become a programmer for a reason, today it is like any other job.

Re:Chromium project finds that 70% of security defects are m... by JamesBarney on Sunday May 24, 2020 @12:43PM (#23292709)
Most devs learn what the need to do their jobs. And you can be a very effective Ruby developer without needing to know much of this information.

Just like you can be a very effective C or C++ developer without knowing anything about css (which in my opinion is far more difficult to understand than when and how stack vs heap allocations are made)

Re:Chromium project finds that 70% of security defects are m... by Scramblejams on Sunday May 24, 2020 @04:39AM (#23290123)
I've never found programming so relaxing as when working in languages where almost everything is immutable. I think we're living beneath our privileges here.
Re:Chromium project finds that 70% of security defects are m... by fpoling on Sunday May 24, 2020 @03:36AM (#23289854)
I can agree with that when comparing with plain C code. But reference-counting in C++ and Objective-C leads to exactly the same problem as with managed code. What saves C is that reference-counting there leads to a lot of boilerplate and bugs. To avoid that one needs very clear ownership model.
Re:Chromium project finds that 70% of security defects are m... by Gibbon1 on Sunday May 24, 2020 @04:52AM (#23290181)
I saw an essay by someone discussing going back to C and eschewing leet pointer and decentralized memory allocation for a system of centralized memory allocation and passing handles[1] instead of pointers. Which appeared to make most of the memory safety problems go away.

[1] handles could be unboxed to expose a pointer. But unboxing had machinery to detect and trap on bad handles

Re:Chromium project finds that 70% of security defects are m... by fpoling on Sunday May 24, 2020 @05:15AM (#23290281)
That reminds me about early Fortran where everything was statically allocated including local variables and function return address. To simulate data structures one used arrays of various dimensions. Effectively array indexes where handles. This was not memory safe as there were no bounds checks. But add those when the compiler cannot eliminate them, and one gets fast and memory safe language.

Edit: that static allocation was essentially a centralized store. Everything repeats itself.

Re:Chromium project finds that 70% of security defects are m... by fetbaffe on Sunday May 24, 2020 @06:53AM (#23290678)
Re:Chromium project finds that 70% of security defects are m... by Gibbon1 on Sunday May 24, 2020 @04:53PM (#23294611)
That's the essay. I think it's interesting because he's mimicking the way I've been writing firmware. I've found most of the issues with memory safety disappear when you use static and centralized resource allocation, avoid monkeying with pointer arithmetic, and avoid making temporary copies of pointers 'for later'.
Re:Chromium project finds that 70% of security defects are m... by Too on Sunday May 24, 2020 @12:33PM (#23292630)
Sure, you can shared_ptr everything and pretend your are writing C#, which as you say circles back into the same problems of spaghetti-ownership. The difference with c++ or rust is that you have options: plain &-reference for non persistent pointers, const &-references for read only non persistent pointers, copy for true copies, move/unique_ptr for transfer of ownership and shared_ptr for shared ownership. All this information is readily accessible to the caller through the function signature.
Re:Chromium project finds that 70% of security defects are m... by wott on Sunday May 24, 2020 @05:25AM (#23290320)
> I can agree with that when comparing with plain C code. But reference-counting in C++ and Objective-C leads to exactly the same problem as with managed code. What saves C is that reference-counting there leads to a lot of boilerplate and bugs.

You've already been greyed out, but I agree 100% with what you said.

That's my main beef with this kind of bi-weekly discussion where people always put C and C++ in the same bag.

I rarely, if ever, make memory management errors in C. The model can't be more simple. If you don't explicitly allocate, you don't have to care about it (in the general case, it is on the stack and will disappear when exiting a function, anyway you also don't have to care about those implementation details, just do as if it disappeared with scope). If you allocate something (on the heap), then you free it later. Malloc(), free(), end of story. Okay, don't keep pointers to areas you will free or realloc, of course. Any function which returns pointers tomemory chunks are always to be treated as malloc'ed, those objects can never be on the stack and never have a fancy automatic management of any sort.

Now almost each piece of code I wrote in Objective-C contained memory errors/leaks. Be cause 1. I didn't grasp the more complicated, less deterministic (so to speak) models and the jargon well 2. you (well, I) never know right away which type of management was used for the object that some function returned, you have to check the doc and pray it is clearly written 2.b you have to keep that information in mind until the moment when you'd like to release the said object in order to release what should be released and not release what is/will be/has been automatically released now/later/sooner/whoknows.

And I don't even talk about the abomination that is Objective-C++, where I drowned in muddy waters.

Re:Chromium project finds that 70% of security defects are m... by kzrdude on Sunday May 24, 2020 @05:37AM (#23290372)
It's so easy to create thread safety related bugs in C# because it's easy to use parallelism and there are no checks for threading issues.
Re:Chromium project finds that 70% of security defects are m... by fetbaffe on Sunday May 24, 2020 @06:11AM (#23290498)
Correct, that is true for any language where you deal with raw threads, managed or unmanaged. That tells us that we should avoid using raw threads and instead use some other API to achieve parallelism.
Re:Chromium project finds that 70% of security defects are m... by nicoburns on Sunday May 24, 2020 @09:04AM (#23291209)
It's not true in Rust. In Rust, objects cannot be concurrently accessed from multiple threads unless they are explicitly marked as thread-safe (with the Sync trait), or wrapped in a a Mutex, which ensures that the safety variants are upheld.
Re:Chromium project finds that 70% of security defects are m... by pjmlp on Monday May 25, 2020 @02:08AM (#23298415)
Yes for multi-threaded code, although not quite for multi-processes code, which is what one should be writing when the goal is security.

Then you are back into the same synchronization issues as in every other language.

Re:Chromium project finds that 70% of security defects are m... by vardump on Monday May 25, 2020 @05:28AM (#23299466)
Yeah, it solves only the 99.9% case.
Re:Chromium project finds that 70% of security defects are m... by pjmlp on Monday May 25, 2020 @08:24AM (#23300263)
So Firefox will return to be a single executable after Rust migration is fully done? After all that is not needed it seems.
Re:Chromium project finds that 70% of security defects are m... by fetbaffe on Sunday May 24, 2020 @10:59AM (#23291946)
Rust does not compile if you don't use the techniques you described?
Re:Chromium project finds that 70% of security defects are m... by ansible on Sunday May 24, 2020 @11:19AM (#23292095)
> Rust does not compile if you don't use the techniques you described?

Yes, exactly.

https://kkimdev.github.io/posts/2019/04/22/Rust-Compile-Time...

Of course, you can bypass this by using 'unsafe' code, where you are explicitly telling the compiler that you have manually verified Rust's safety guarantees.

This is why a significant portion of Rust's community gets agitated with (possibly unnecessary) use of 'unsafe' in popular libraries.

Re:Chromium project finds that 70% of security defects are m... by fetbaffe on Sunday May 24, 2020 @11:50AM (#23292348)
Then I stand corrected and applaud the creators of Rust for haven taken these measures for writing thread safe code, because it is supereasy to get threading wrong, even for the most experienced developer.
Re:Chromium project finds that 70% of security defects are m... by littlestymaar on Sunday May 24, 2020 @01:17PM (#23292971)
Interestingly enough, this was the raison d'être of the Rust project at the beginning, while zero-cost abstraction and memory-safety without GC (and the borrow-checker) weren't part if the initial goal. When Rust was first released as a research project it had green threads and a GC (interestingly close to Go actually), but it was already designed with data-race freedom in mind.
Re:Chromium project finds that 70% of security defects are m... by thePunisher on Sunday May 24, 2020 @12:56PM (#23292812)
Both Java and C# are designed for memory-safety but not thread-safety; Rust is.

Considering many of the software problems we face have some connection to thread-safety, Rust is the way to go.

Re:Chromium project finds that 70% of security defects are m... by pjmlp on Wednesday May 27, 2020 @03:50AM (#23320509)
I would consider that the way to go is multi-processes instead, and secure every critical module on their own OS sandbox.

Threading seemed a good idea that went wrong.

Re:Chromium project finds that 70% of security defects are m... by ReactiveJelly on Sunday May 24, 2020 @10:52AM (#23291901)
And in the last quadrant, you can use non-raw threads like the Task or Parallel APIs and still have threading issues in C#.

I'm trying to learn Android and some libraries give zero indication of what thread your callbacks will be called on.

Maybe there's a convention somewhere that says "On Android, you can't assume anything about callbacks, so always assume you're on some anonymous worker thread and lock everything or dispatch to a thread you own"

But I have not come across it yet.

Re:Chromium project finds that 70% of security defects are m... by pjmlp on Monday May 25, 2020 @02:12AM (#23298445)
Yes that is the convention.

You cannot assume anything about process or thread lifetimes on Android, as the simple act of rotating the screen will restart your application, and it can be killed at any moment and restarted later, due to memory pressure, or because the use has switched applications.

So whatever was the state of your application can be completely different when the callback is supposed to be invoked later on.

Re:Chromium project finds that 70% of security defects are m... by erichocean on Sunday May 24, 2020 @02:54AM (#23289702)
They're simpler only if you could care less about performance. C and C++ are primarily used in situations where you do care about performance.
Re:Chromium project finds that 70% of security defects are m... by _ph_ on Sunday May 24, 2020 @03:51AM (#23289913)
Memory safety and performance are not a contradiction, they are widely independant. Yes, some strategies like GC induce an overhead. But in the age of multiprocessor machines, this overhead gets very much reduced by parallel collection. And practice has shown, in programs with a lot of dynamic memory allocation, garbage collected languages often perform even better. There are different approaches, like memory safety by the compiler as Rust does it. And with "performance" in mind, you shouldn't forget, that most large C/C++ programs tend to use libraries to provide some amount of safety, but those libraries are of course adding some overhead too. Finally, the less time you spend on questions of memory management, the more time you have to write an actually fast program.
Re:Chromium project finds that 70% of security defects are m... by dirtydroog on Sunday May 24, 2020 @05:08AM (#23290251)
Sorry, this is hand wavy nonsense. One counter example, we switched from Cassandra (JVM) to Scylla (C++). It was a win in terms of both query latency and infrastructure costs as we required fewer machines to handle the same load.

As for having more time to write a fast program... that's funny. If you want a fast program on something JVM based you're pretty much going to be spending the majority of your time writing things in a way where the GC plays as little role as possible.

Re:Chromium project finds that 70% of security defects are m... by _ph_ on Sunday May 24, 2020 @05:45AM (#23290400)
Sorry, this is hand wavy nonsense. One counter example, we switched from Cassandra (JVM) to Scylla (C++). It was a win in terms of both query latency and infrastructure costs as we required fewer machines to handle the same load.

Sorry, this is not hand wavy nonsense. And what you are providing is called annectodal evidence.

Also, your universe seems to consist only of the JVM as a memory-safe alternative to C++. Yes, there are a lot of bloated, badly performing programs implemented in Java. However this isn't a given. Yes, some design decisions for Java introduce the risk of bloat, but you can avoid them at much less effort (and risk) than memory corruptions and new features like the value classes are reducing the bloat quite a bit. But still, the JVM is extremely high performance, so for speed in surprisingly many cases, it often beats C++. Virtual method calls are just better optimizable at run time, the Java JIT creates excellent code and Java has some of the best garbage collectors, so at really dynamic memory loads, it beats any manual management by a wide marging.

And of course, there is a whole world beyond Java as alternatives. Rust has been explicitly designed to excel at tasks C++ traditionally shines for, while giving your full safety.

Re:Chromium project finds that 70% of security defects are m... by dirtydroog on Sunday May 24, 2020 @09:26AM (#23291342)
There is an open-source project by LMAX (a forex trading company) called Disruptor[1] that squeezes as much as possible out of the JVM. It's awesome. I ported it to C++ years ago when I wanted to learn about low-latency techniques. However, if you look at the code they need to actually break out of the JVM's safety net to get the performance they need[2]. I couldn't help but ask myself why they didn't just use C++, and when asked one of the devs did admit that their own C++ ports had an approx 10% performance increase (although this was ~7 years ago maybe)

Rust is certainly interesting and it's on my radar. I wonder though, when it comes to having it in use in anger if its guarantees turn out to be over sold, just like the JVM's safety claims were. Time will tell.

Edit: I tend to focus on comapring against the JVM because pretty much any framework you use on The Cloud is JVM based. I'm of the opinion that there are cost savings to be had if these were ported to more appropriate languages, hence the Cassandra vs Scylla comparison. The money saved was 'noticeable'.

[1] https://github.com/LMAX-Exchange/disruptor [2] http://mishadoff.com/blog/java-magic-part-4-sun-dot-misc-dot...

Re:Chromium project finds that 70% of security defects are m... by pjmlp on Monday May 25, 2020 @02:23AM (#23298513)
Which is why such projects use the JVM, they save money in developer salaries, developer pool, bug fixes due to security exploits, available set of tooling and libraries, while caring to hand optimize a tiny set of libraries for specialized use cases.

Java 15 just accepted the JEP for native memory management, yet another stepping stone for having value types support.

If the cadence continues, Java will eventually have all the features that it should have had in 1996, had Sun properly taken into consideration languages like Modula-3 and Eiffel.

Which you can get today in a language like Swift, C#, Nim or D, productivity of GC, type safe, while having the language features to do C++ like resource management.

Re:Chromium project finds that 70% of security defects are m... by murderfs on Sunday May 24, 2020 @05:56AM (#23290451)
There are notable examples where garbage collection has both better throughput and latency than manual memory collection: persistent data structures come to mind, because in the absence of garbage collection, you have giant awful cascades of reference count manipulation any time a structure would get freed.

You'll never actually see anyone tout the benefits of using GC for this, though, because the performance characteristics of persistent data structures are so horrendous compared to mutable ones, no one actually uses them in C++.

Re:Chromium project finds that 70% of security defects are m... by pkolaczk on Sunday May 24, 2020 @01:54PM (#23293220)
You can use GC to implement the persistent structure in C++/Rust just fine. But then you pay the GC cost for only that structure, not for all the other things.
Re:Chromium project finds that 70% of security defects are m... by andrepd on Sunday May 24, 2020 @07:36AM (#23290829)
Well yes, if you are comparing GC to refcounting then yes, gc wins.
Re:Chromium project finds that 70% of security defects are m... by pkolaczk on Sunday May 24, 2020 @02:00PM (#23293273)
I wouldn't be so sure. There is no easy answer. It typically wins in trivial benchmarks, where the total heap size is small. However once you have a big heap of other long lived stuff, there is a significant indirect cost caused by scanning the heap, memory barriers and evicting good data from caches. This cost is negligible only if your total heap size is much larger than the live memory set. Also modern manual memory allocators are not as slow as they used to be a few decades ago and they actually allocate in low tens of nanoseconds.
Re:Chromium project finds that 70% of security defects are m... by _ph_ on Sunday May 24, 2020 @09:41AM (#23291440)
If your problem demands many heap allocations, GC wins also vs. manual memory management. Heap allocation in a generational GC is as fast as stack allocation, you don't get fragmented heaps.
Re:Chromium project finds that 70% of security defects are m... by pkolaczk on Sunday May 24, 2020 @02:01PM (#23293284)
It is nowhere near stack allocation, please stop this nonsense. There was a paper claiming that, but the requirement was to set heap size 7x bigger than needed.

Stack is also very hot in cache. Memory that GC is handing allocations from is not.

I recently ported some of Cassandra code from Java to stack-only Rust and I got ~25x performance improvement, most from avoiding heap allocations and GC.

Re:Chromium project finds that 70% of security defects are m... by dirtydroog on Sunday May 24, 2020 @10:26AM (#23291744)
That depends on the allocator used. The default libc one works but isn't great performance-wise. It is possible to intercept calls to malloc/free by using LD_PRELOAD on Linux. That will allow you to use allocators such as jemalloc or tcmalloc instead.

Of course, repeatedly allocating and freeing is poor for performance. Cache/pre-allocate when you can. This goes for managed languages too.

Re:Chromium project finds that 70% of security defects are m... by thePunisher on Sunday May 24, 2020 @01:03PM (#23292858)
It's usually a trade-off since C++ may be more time consuming and therefore most costly to write. It's also a lot harder to get bug-free because of the manual memory management.

Both Java and C# may be somewhat slower, but the maintainability and freedom from memory management issues more than makes up for this.

Any engineer worth his salt will take this into account.

Re:Chromium project finds that 70% of security defects are m... by citrin_ru on Sunday May 24, 2020 @08:31AM (#23291053)
> Yes, some strategies like GC induce an overhead. But in the age of multiprocessor machines, this overhead gets very much reduced by parallel collection.

If you unlucky and given GC is not well optimized for given workload, then memory usage overhead can be huge. Just a few days ago had such problem with Go (HeapIdle grew until OOM).

Go GC is less mature than Java GC, but Java GC is not free too - sometimes you need to spend a lot of time time to tune GC settings or to optimize code to avoid GC problems.

In my case case it would be faster to use malloc/free than to spend time fighting with GC (looking for a workarounds).

On average GC saves development time and allows to avoid memory management bugs, but in some cases overhead is big and developers have to spend more time, not less.

Re:Chromium project finds that 70% of security defects are m... by _ph_ on Sunday May 24, 2020 @09:38AM (#23291420)
I have not claimed that a GC is always faster. Indeed, having a GC enables some to write a totally bad performing program. However this doesn't contradict that fact, that in many cases a GC does not only not mean a slower program, but sometimes a faster program. Usually, you don't have to "fight" the GC. In situations, where manual management is vastly better, you do use object pools and preallocated arrays even in a language with a GC.
Re:Chromium project finds that 70% of security defects are m... by thePunisher on Sunday May 24, 2020 @12:59PM (#23292835)
There are quite a few areas in software where determinism is a must, such as games, real-time and systems software.
Re:Chromium project finds that 70% of security defects are m... by pjmlp on Monday May 25, 2020 @02:32AM (#23298565)
Yes, for those there are real time GCs as well.

https://www.ptc.com/en/products/developer-tools/perc

https://www.aicas.com/cms/en/JamaicaVM

One of the Java vendors acquired by PTC, Aonix, used to sell real time Java implementations for military deployments, including weapons controls and targeting systems.

You don't want a couple of ms pause when playing with such "toys".

Re:Chromium project finds that 70% of security defects are m... by thePunisher on Thursday May 28, 2020 @12:13PM (#23338995)
Sounds more like snake-oil to me.
Re:Chromium project finds that 70% of security defects are m... by fetbaffe on Sunday May 24, 2020 @06:15AM (#23290516)
Both C and C++ have zero built in knowledge about parallelism, so in a modern world where most things are parallel and asynchronous I don’t agree at all with that statement.
Re:Chromium project finds that 70% of security defects are m... by adev_ on Sunday May 24, 2020 @09:36AM (#23291404)
> Both C and C++ have zero built in knowledge about parallelism, so in a modern world where most things are parallel and asynchronous I don’t agree at all with that statement.

And ?

C (even Fortran) had threads and was used to create high performance program with high degree of parallelism before any "concurrent" modern language was even born.

You can not agree if you want. But fact are there, 98% of programs running on the biggest "parallel" machines nowadays (supercomputers) are C, C++ or Fortran.

You don't need to be "designed" concurrent to be efficient at it. The same way you do not need to be designed "Cloud-native (bullshit)" to run on a virtual machine.

Re:Chromium project finds that 70% of security defects are m... by fetbaffe on Sunday May 24, 2020 @10:57AM (#23291929)
I think that it sets a different mentality if it is part of tools you use, you solve problems differently. It is of course possible to write highly concurrent code in traditional sequential languages and your examples proves that, but supercomputers are a special case with budgets for that. I'm talking about the general case, and I think the post I replied to also assumed that.

We have to give programmers, with different backgrounds & training, tools to write high performant code in their everyday job. Many of the tools we use today are not designed for that. We are stuck in a mental model 50 years old that is no longer true.

Here are some interesting stackoverflow answers. You are of course free to dismiss these answers as anecdotal.

https://stackoverflow.com/a/14637833

https://stackoverflow.com/a/2799779

Re:Chromium project finds that 70% of security defects are m... by sigotirandolas on Sunday May 24, 2020 @08:28AM (#23291024)
Both C and C++ have had a concurrent memory model and threads in the standard library for a decade now. And POSIX threads which are pretty much the same thing have been there for a quarter century on *NIX.
Re:Chromium project finds that 70% of security defects are m... by fetbaffe on Sunday May 24, 2020 @08:52AM (#23291146)
Sure, there are libraries for concurrency, but there are no language constructs like in other languages.
Re:Chromium project finds that 70% of security defects are m... by cogman10 on Sunday May 24, 2020 @11:03AM (#23291981)
What do you mean by language constructs? As in, a `synchronized` keyword? Or are you thinking along the lines of `async` `await`?

If you mean the `synchronized` keyword, then correct, they don't have that. Most languages do not have that concept. C++ does have mutexes and has had them since C++11 (nearly a decade). C++ also has as part of the language spec the concept of threads, again, there since C++11.

If you meant co-routines, then C++ just added them with C++20.

Or do you mean something different like green threads (ala go)?

C++ certainly has concurrency constructs and has been expanding them since C++11.

Re:Chromium project finds that 70% of security defects are m... by fetbaffe on Sunday May 24, 2020 @11:22AM (#23292124)
Like Gos goroutines or Erlangs processes. And more of message passing. C++ has added many of these things later on in the standard library, bolted on afterwards.
Re:Chromium project finds that 70% of security defects are m... by erichocean on Sunday May 24, 2020 @02:40PM (#23293583)
If you think that using libraries to do things in C++ means they are "bolted on", then you don't understand C++.

The entire design of C++ is to enable efficient libraries for these kinds of things to be built. And it does, and has, and will continue to for decades more.

Re:Chromium project finds that 70% of security defects are m... by SomeoneFromCA on Sunday May 24, 2020 @02:03PM (#23293293)
You forgot about OpenMP.
Re:Chromium project finds that 70% of security defects are m... by SomeoneFromCA on Sunday May 24, 2020 @02:07PM (#23293312)
The real point of using C and (less so, but still) C++ is not to have the ultimate performance, but to have ultimate control over the program execution. Garbage collection and real-time code are not compatible, because GC introduces random, unexpected stalls in the program flow; even simple reference counting can be harmful, if the last reference get eliminated in the wrong time, and creates a cascade of deallocations.
Re:Chromium project finds that 70% of security defects are m... by pjmlp on Monday May 25, 2020 @02:34AM (#23298577)
Then using C and C++ is a mistake, because there isn't an ultimate control about how memory allocator interacts with the OS.

So much that in the 90's there were vendors that could make a business out of selling specialized memory allocators.

Re:Chromium project finds that 70% of security defects are m... by SomeoneFromCA on Monday May 25, 2020 @07:39AM (#23300068)
Really? What about an OS kernel code? How does "it's allcator" interacts "with the OS"? And what these allocators were written in?
Re:Chromium project finds that 70% of security defects are m... by pjmlp on Monday May 25, 2020 @08:13AM (#23300215)
Usually in a mix of Assembly and whatever is used in OS systems implementation language, not necessarily C.

https://inf.ethz.ch/personal/wirth/ProjectOberon/Sources/Ker...

The malloc() exposed in a ISO C standard library implementation isn't necessarily related to whatever means the OS does memory allocation.

Re:Chromium project finds that 70% of security defects are m... by pjmlp on Monday May 25, 2020 @02:15AM (#23298472)
Nowadays, back in the 80's and early 90's no sane developer would used them on 8 and 16 bit home platforms if they cared about performance.

What C and C++ have going for them is 40 years of investment in optimizing compilers, to detriment of other languages, and abuse of UB in optimizers.

Now with shared compiler backends becoming a mainstream thing across most OSes, that advantage is getting thinner.

Re:Chromium project finds that 70% of security defects are m... by saagarjha on Sunday May 24, 2020 @03:14AM (#23289775)
They do well in those contexts. They’re used in other places too, but generally with poorer justification and results.
Re:Chromium project finds that 70% of security defects are m... by zetafunction on Sunday May 24, 2020 @08:01AM (#23290904)
My personal experience from reading code that uses Chromium's C++ garbage collector is that that's often not true. While there might no longer be use-after-free errors, it's also no longer possible to make assertions like "object X must outlive Y" because object Y could be referenced arbitrarily and kept alive longer than expected. To get around that, objects might have an explicit shutdown step. But shutting down a large graph of objects is often fraught with peril, especially when that work can span multiple processes.
Re:Chromium project finds that 70% of security defects are m... by johnr2 on Sunday May 24, 2020 @05:39AM (#23290377)
> Switching from C++ to a language without memory safety issues while being just as fast

Does that actually exist? (I mean in the general case, not just a few carefully chosen benchmarks).

Re:Chromium project finds that 70% of security defects are m... by devit on Sunday May 24, 2020 @08:47AM (#23291124)
Rust is that language.

Some things, namely array indexing and RefCell borrowing, have unavoidable extra runtime overhead in the default safe usage, but it's unlikely that it is significant (often array bounds checks are either essential and thus needed in C++ as well or optimized out by LLVM in Rust, and RefCell usage is generally rare), and you can use unsafe unchecked operations as well.

Re:Chromium project finds that 70% of security defects are m... by _ph_ on Sunday May 24, 2020 @06:31AM (#23290588)
Yes, they do exist. The safety of Rust comes from the compiler (which takes its time), not from run-time checks. For certain style of code, the JVM is actually faster than C++. You also have to consider, that a lot of things you do in a C++ program to make it safer add up performance costs.
Re:Chromium project finds that 70% of security defects are m... by andrepd on Sunday May 24, 2020 @07:32AM (#23290814)
>For certain style of code, the JVM is actually faster than C++.

I find this very hard to believe.

Re:Chromium project finds that 70% of security defects are m... by _ph_ on Sunday May 24, 2020 @08:13AM (#23290955)
It depends on the nature of the code. Static code is very fast in C++, but dynamic code is less so. Virtual method dispatch in C++ is comparatively slow. Hotspot can eliminate much of the cost by using runtime type information. Using runtime information generally is a way for Hotspot to perform optimizations, a static compiler cannot do, because they would be a bad tradeoff. Heap allocation in the JVM is as fast as stack allocation, this gives another boost vs. heap allocation in C++. And even GCing the youngest generation is basically cost free, as long as you don't have many surviving objects.

I don't claim that Java is always faster than C++, that would be silly and there are plenty of Java programs in the wild which proves that it isn't. But there are quite some tasks at which Java is indeed faster.

Re:Chromium project finds that 70% of security defects are m... by sigotirandolas on Sunday May 24, 2020 @09:14AM (#23291265)
In the general case, virtual dispatch has an overhead in both C++ and Java (which btw is only <5 machine instructions), but it's true that Java can sometimes eliminate it with runtime information, which C++ can not use.

However, in my experience, using virtual dispatch is a relatively rare ocurrence in C++ (compared to the vast majority of method calls). On the other hand, on Java, most of _everything else_ is indirect and has overhead: All objects are allocated on the heap, primitives (int) are often objects (Integer) where they ought not to be, all objects have 16 bytes of overhead, etc.

But the JVM will convert those heap allocations to stack allocations! And it will realize those Integers are used as int and remove the overhead! And it will realize you're not using the information on the header of every object!

Perhaps in synthetic benchmarks, but in real programs, where there are an almost infinite amount of code paths, dumb 'data transfer' objects are common and things need to be modularized, the JVM is forced to assume the worst case can happen (even if you as an human can prove that it won't happen) and inhibit those optimizations. And now you have indirect accesses everywhere, memory overhead (=cache trashing) everywhere, the runtime can't vectorize that tight due to Integers, etc.

In fact, I can't think of any domain where there is heavy competition and where high performance is a determining factor where Java has won to C/C++. In browsers, it certainly has not.

Re:Chromium project finds that 70% of security defects are m... by cogman10 on Sunday May 24, 2020 @11:21AM (#23292113)
> (which btw is only <5 machine instructions)

The number of instructions is much less important than what they are doing. In the case of virtual dispatch, it's doing a memory lookup. If that memory is in cache it could be relatively inexpensive but not guaranteed. However, if you have to hit main memory then things are much slower.

> But the JVM will convert those heap allocations to stack allocations!

I was surprised recently to learn this is not the case. (at least, not with hotspot) The JVM will try and "scalarize" things (pull the fields out of the object which may push them onto the stack) but it won't actually allocate a full object on the stack (OpenJ9 will, but I don't see people using that very often).

It is also somewhat bad at doing the Integer to int conversion. That is mainly because the Integer::valueOf method will break things (EA has a hard time realizing this is a non-escaped value). Simple code like

    Integer a = 1;
    a++;
can screw up the current analysis and end up in heap allocations.

There is current work to try and make these things better, but it hasn't landed yet (AFAIK).

> In fact, I can't think of any domain where there is heavy competition and where high performance is a determining factor where Java has won to C/C++. In browsers, it certainly has not.

I think the realm where the JVM can potentially beat C++ is, funnily, work that requires a lot of memory. The thing that the JVM memory model has going for it is that heap allocations are relatively cheap compared to heap allocations in C++. If you have a ton of tiny short lived object allocations then the JVM will do a great job at managing them for you.

Re:Chromium project finds that 70% of security defects are m... by account42 on Friday May 29, 2020 @06:00AM (#23348155)
> I think the realm where the JVM can potentially beat C++ is, funnily, work that requires a lot of memory. The thing that the JVM memory model has going for it is that heap allocations are relatively cheap compared to heap allocations in C++. If you have a ton of tiny short lived object allocations then the JVM will do a great job at managing them for you.

Except C++ also has many other options besides malloc() each of these individual objects.

Re:Chromium project finds that 70% of security defects are m... by vardump on Sunday May 24, 2020 @08:44AM (#23291114)
Allocation heavy C++ can be pretty slow. It's also true unoptimizable (pure) virtual methods performing little work have quite a bit overhead that JVM can avoid.

Of course you normally try to avoid writing C++ like that.

Re:Chromium project finds that 70% of security defects are m... by pkolaczk on Sunday May 24, 2020 @02:13PM (#23293374)
Allocation heavy Java is also very slow. But it is much easier to avoid heap allocations (and virtual calls!) in C++/Rust than in Java.
Re:Chromium project finds that 70% of security defects are m... by pjmlp on Wednesday May 27, 2020 @04:02AM (#23320586)
For the time being, I still hope that Valhalla will eventually make it.
Re:Chromium project finds that 70% of security defects are m... by fluffything on Monday May 25, 2020 @03:39AM (#23298920)
The claim is probably that idiomatic code in one case is faster than idiomatic code in the other, not that you can't write C++ code that isn't equally fast.

For example, if you use `std::vector` or `std::unique_ptr` you would be deallocating memory when those go out of scope. The JVM might actually never do that, e.g., if the program terminates before sufficient memory pressure arises.

Writing Rust code that leaks a `std::vector` is trivial, but doing the same in C++ actually requires some skill.

Re:Chromium project finds that 70% of security defects are m... by vardump on Monday May 25, 2020 @05:23AM (#23299448)
Writing C++ code that leaks a std::vector is absolutely trivial, happens all the time and requires no skill.

  auto* v = new std::vector<char>();
Re:Chromium project finds that 70% of security defects are m... by fluffything on Monday May 25, 2020 @06:16AM (#23299693)
I disagree in that it requires no skill: it requires the user to avoid doing the obvious thing:

    vector<int> foo {...};
and heap allocate a vector on the heap with `new` (without using a smart pointer), and then avoiding your linters warnings about this (e.g. clang-tidy).

If this is common in your place of work, I truly pity you.

Also, trading a call to `free` for a second call to malloc, and a second pointer indirection to the vector elements, isn't a very effective way of improving performance to beat the JVM. The whole idea behind leaking memory is doing less operations, not more :D

In Rust, leaking "the right way" is trivial (mem::forget is safe), but in C++, leaking the stack allocated vector probably requires putting it behind an union or aligned_storage or similar.