News for hackers. Stuff that matters.
Re:C for All by Dinux on Friday March 23, 2018 @10:57AM (#16658781)
This has been tried many times before, and eventually all these attempts die a lonely death. Why use extensions anyway? If one desired the luxury of modern scripting languages, switch to C++, Rust, Go or one of the other alternatives the article mentions.
Re:C for All by pjmlp on Friday March 23, 2018 @11:18AM (#16659006)
Because regardless how some of us might dislike C and its security related issues, the truth is that no one is ever going to rewrite UNIX systems in other language, nor the embedded systems where even C++ has issues gaining market share.

So if one finally manages to get a safer C variant that finally wins the hearts of UNIX kernels and embedded devs, it is a win for all, even those that don't care about C on their daily work.

Until it happens, that lower layer all IoT devices and cloud machines will be kept in C, and not all of them will be getting security updates.

Re:C for All by Dinux on Friday March 23, 2018 @11:28AM (#16659093)
I do not question the usefulness of C, I use it in my daily work. What I am saying is that most C developers that use the language day-in-day-out know quite well what they are doing, and don't need yet another non-standard way of writing the code. Safety is a good point, but the initiative doesn't even mention the word, and there is no reason to assume the C-for-All extension targets safety at all
Re:C for All by s73v3r_ on Friday March 23, 2018 @12:47PM (#16660004)
"What I am saying is that most C developers that use the language day-in-day-out know quite well what they are doing"

I'm going to strongly disagree with that statement.

Re:C for All by saagarjha on Friday March 23, 2018 @02:35PM (#16661246)
I might have agreed with you if this was C++. C is a small enough language that most programmers can understand most of it.
Re:C for All by s73v3r_ on Friday March 23, 2018 @02:36PM (#16661255)
Toyota was full of programmers that apparently "understood what they were doing."
Re:C for All by ori_b on Friday March 23, 2018 @02:50PM (#16661397)
Fair point, but changing the language won't change the amount of global state. and the associated complexity, nor will it make subsystem supervision work correctly. Changing the language will not prevent unbounded recursion or the associated stack overflows and subsystem failures. Changing the language will not fix mis-analysis of task switching overhead. And changing the language will not fix manufacturing issues with the PCBs.

As far as I'm aware, one of the very few toolchains that even try to improve on this over C are Ada/SPARK.

Re:C for All by vvanders on Friday March 23, 2018 @03:51PM (#16662015)
> but changing the language won't change the amount of global state

Global mutable state is marked as Unsafe in Rust.

> nor will it make subsystem supervision work correctly

Erlang is built specifically around this concept.

Perfect is the enemy of good here, throwing out a whole language due to one case doesn't help anyone.

Re:C for All by ori_b on Friday March 23, 2018 @04:04PM (#16662159)
> Global mutable state is marked as Unsafe in Rust.

It's also the simplest way to avoid dynamic allocation and the associated OOM issues. So, short of doing static analysis to bound heap usage at compile time, that makes things worse.

And Toyota already got the static analysis wrong for their stack usage. At least globals will fail to compile if they won't fit.

Re:C for All by cbdumas on Friday March 23, 2018 @04:41PM (#16662471)
Are you referring to the "unintended acceleration" scandal from ~10 years ago? If so the NHTSA investigated[0] that and found no flaws in electronics. The problem was essentially people pushing the gas when they thought they were on the brakes. Pedal "misapplication" I think it's called in the report.

[0] https://www.transportation.gov/briefing-room/us-department-t...

Re:C for All by abainbridge on Friday March 23, 2018 @08:12PM (#16663794)
> The problem was essentially people pushing the gas when they thought they were on the brakes

Oh dear no. Certainly not.

Read https://users.ece.cmu.edu/~koopman/pubs/koopman14_toyota_ua_...

My favourite part from is, "Watchdog kicked by a hardware timer service routine".

A watchdog timer is a piece of hardware that decrements a counter every microsecond or similar. The control system's main loop, running on the CPU, "kicks" the watchdog by setting the counter to a value like 1000 each iteration. The result is that if the CPU fails to execute the main loop often enough, the watchdog will "fire". This a) tells you that you have a bug and b) typically reboots the system so it has a chance to recover.

Toyota used a timer service routine to kick the watchdog. This defeats the purpose of the watchdog. The control software can happily get stuck or crash and the watchdog will not notice. The fact that an engineer added this "feature" tells you that the watchdog was firing in development. That should have been addressed by fixing the buggy software, not by disabling the test.

The fact that the disabled watchdog made it into the production release is unforgivable.

Re:C for All by seandougall on Friday March 23, 2018 @05:48PM (#16662874)
In that investigation they seemed to place the blame more on sticky pedals and floor mats, not operator error.

That wasn't the final word, though. I believe this is what the GP was referring to:

> When NASA software engineers evaluated parts of Toyota’s source code during their NHTSA contracted review in 2010, they checked 35 of the MISRA-C rules against the parts of the Toyota source to which they had access and found 7,134 violations. Barr checked the source code against MISRA’s 2004 edition and found 81,514 violations.

...

> Their descriptions of the incredible complexity of Toyota’s software also explain why NHTSA has reacted the way it has and why NASA never found a flaw it could connect to a Toyota’s engine going to a wide open throttle, ignoring the driver’s commands to stop and not set a diagnostic trouble code. For one, Barr testified, the NASA engineers were time limited, and did not have access to all of the source code. They relied on Toyota’s representations – and in some cases, Toyota misled NASA.

http://www.safetyresearch.net/blog/articles/toyota-unintende...

Re:C for All by saagarjha on Friday March 23, 2018 @03:11PM (#16661596)
Bad practices can carry over to any language. They’re clearly incompetent at using C, but that doesn’t mean that they won’t be incompetent in another language.
Re:C for All by pjmlp on Friday March 23, 2018 @12:51PM (#16660061)
Even ANSI/ISO C working group acknowledges that “thrust the programmer” doesn’t quite work.
Re:C for All by gmueckl on Friday March 23, 2018 @02:04PM (#16660887)
This is because trusting the programmer is fundamentally wrong, no matter the programming language. In any good development process the actual coding is the least amount of work - for a reason.
Re:C for All by pjmlp on Friday March 23, 2018 @04:07PM (#16662193)
This was the statement I was referring to.

<quote>

Spirit of C:

a. Trust the programmer.

b. Do not prevent the programmer from doing what needs to be done.

c. Keep the language small and simple.

d. Provide only one way to do an operation.

e. Make it fast, even if it is not guaranteed to be portable.

The C programming language serves a variety of markets including safety-critical systems and secure systems.

While advantageous for system level programming, facets (a) and (b) can be problematic for safety and security.

Consequently, the C11 revision added a new facet:

=> f. Make support for safety and security demonstrable.

</quote>

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2139.pdf

Re:C for All by gmueckl on Saturday March 24, 2018 @04:30AM (#16665521)
Okay, this makes me rethink my previous statement. There are two kinds of trust here: A low level programming language should not impose restrictions on the code that prevent the programmer from doing what needs to be done, even if it looks wrong. This is how I read the 'Spirit of C" that you quoted. And certain applications would be impossible to write without it. But you need a development process to make sure that your system does exactly the right thing. So the quote should read "trust the process" rather than "trust the programmer".
Re:C for All by pjc50 on Friday March 23, 2018 @12:28PM (#16659745)
The trouble with a "safer C variant" is that it must remove features, or at least more heavily constrain programs to a safer subset of the language. This makes it not backwards-compatible.

I think the only successful "subset of C" is MISRA.

Re:C for All by duneroadrunner on Friday March 23, 2018 @07:12PM (#16663469)
SaferCPlusPlus[1], for example, is a safe subset of C++ that has compatible safe substitutes for C++'s (and therefore C's) unsafe elements. So migrating existing C/C++ code generally just requires replacing variable declarations, not restructuring the code.

For C programs, one strategy is to provide a set of macros to be used as replacements for unsafe types in variable declarations. These macros will allow you, with a compile-time directive, to switch between using the original unsafe C elements, or the compatible safe substitutes (which are C++ and require a C++ compiler).

The replacement of unsafe C types with the compatible substitute macros can be largely automated, and there is actually a nascent auto-translator[2] in the works. (Well, it's being a bit neglected at the moment :)

Custom conventions using macros to improve code quality are not that uncommon in organized C projects. Right? But this one can (optionally, theoretically) deliver complete memory safety. So you might imagine, for example, a linux distribution providing two build versions, where one is a little slower but memory safe.

[1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus

[2] https://github.com/duneroadrunner/SaferCPlusPlus-AutoTransla...

Re:C for All by pjmlp on Friday March 23, 2018 @12:52PM (#16660066)
Maybe Frama-C as well.
Re:C for All by fao_ on Friday March 23, 2018 @03:04PM (#16661514)
I remember reading a paper from around 2007 that asserted that most of MISRA did not catch or significantly prevent major bugs in code, indeed it asserted that much of the standard was useless. I am failing to find it now, as I cannot remember what terms I used, and I am not at a library computer and therefore I cannot search behind paywalls beyond abstracts.
Re:C for All by zzzcpan on Friday March 23, 2018 @03:26PM (#16661764)
Cannot say that this is unexpected, but I was interested to find some papers, presumably these two: Assessing the Value of Coding Standards: An Empirical Study [1], Language subsetting in an industrial context: A comparison of misra c 1998 and misra c 2004 [2]

[1] http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.559...

[2] http://leshatton.org/Documents/MISRA_comp_1105.pdf

Re:C for All by pjmlp on Friday March 23, 2018 @03:55PM (#16662057)
Interesting, thanks for the papers.
Re:C for All by kerkeslager on Friday March 23, 2018 @11:50AM (#16659287)
What makes you think that a safer C variant would win the hearts of UNIX kernels and embedded devs any more than C++ (which started as just a C variant).
Re:C for All by pjmlp on Friday March 23, 2018 @12:47PM (#16659994)
Because most UNIX kernel devs are religiously against the idea of ever touching C++, even if their beloved C compilers are implemented in C++.
Re:C for All by pcwalton on Friday March 23, 2018 @02:17PM (#16661053)
I highly suspect it's not that Unix C diehards are against the idea of touching C++—it's that they're against the idea of using anything but C. I don't think anything can win over those kernel developers.
Re:C for All by nwmcsween on Friday March 23, 2018 @03:30PM (#16661805)
Its the complexity of the language vs the benefits it provides. C has many pitfalls (UB) but its simple. IMO a language that could migrate C programmers would have dependent types and an effect system with a nice syntax.
Re:C for All by noam87 on Friday March 23, 2018 @11:10PM (#16664585)
Idris?

It can compile small enough to run on an Arduino: https://github.com/stepcut/idris-blink

Re:C for All by pjmlp on Friday March 23, 2018 @03:10PM (#16661591)
Yep you are quite right.

Other OSes not tied to UNIX culture were always more open to reach out for C++, even if constrained to a certain subset.

Re:C for All by int0x80 on Friday March 23, 2018 @03:00PM (#16661492)
Using a compiler partially written in c++ is very different from writting and mantaining a kernel (or whatever) in c++.
Re:C for All by nickpsecurity on Friday March 23, 2018 @06:59PM (#16663395)
Maybe. The L4 people did write one of theirs in C++. L4-style kernrls are among the fastest in existence. Some even fit in the L1 cache of the CPU.

http://www.l4ka.org/projects/pistachio/pistachio-whitepaper....

I can see your point if you're saying compiler use lets them avoid a language they just dont want to use. Which they couldnt if using it for an OS.

Re:C for All by pjmlp on Friday March 23, 2018 @03:07PM (#16661562)
Other OSes, not impregnated with UNIX culture, have a different view on C++'s use on kernel space.

OS X and its descendents is the only exception.

Which goes back to NeXTSTEP using Objective-C and offering UNIX compatibility only as a path to bring software into the system, battling against SGI and Sun market space.

Re:C for All by mrpippy on Friday March 23, 2018 @04:47PM (#16662503)
Even OS X's C++ usage is kind of a historical oddity. OPENSTEP drivers were written in ObjC for an API called DriverKit, but OS X/Darwin/xnu replaced that with the C++ IOKit. The rest of the kernel is still C. I wrote a comment a few years ago explaining why I think that decision was made:

https://news.ycombinator.com/item?id=10006411

Re:C for All by pjmlp on Friday March 23, 2018 @05:48PM (#16662872)
Thanks for the hint.
Re:C for All by BoorishBears on Friday March 23, 2018 @11:54AM (#16659334)
Simplicity, no STL or templating, no OOP connotations

I don’t necessarily think it would, but if it did, those would all be reasons

Re:C for All by BruceIV on Friday March 23, 2018 @12:12PM (#16659554)
[actually on the Cforall team] This is basically our pitch -- the last 30 years of language design features applied to a language that is not only source-compatible with C (like C++), but actually maintains the procedural paradigm of C (unlike C++) -- idiomatic C code should require minimal to no change to make it idiomatic Cforall code, and skilled C programmers should be able to learn the extension features orthogonally, rather than needing to figure out complex interactions between, say, templates and class inheritance. We are working in the same space as C++, and do draw inspiration from C++ where useful, but with the benefit of decades of watching C++ try things to see where the important features are.

There's also some really neat language-level concurrency support; work is ongoing on new features and a shorter summary, but you can see one of our Master's student theses for details: https://uwspace.uwaterloo.ca/handle/10012/12888

Re:C for All by aidenn0 on Friday March 23, 2018 @02:32PM (#16661219)
the existence of exceptions seems to bely the idea of minimal changes to make idiomatic C code idiomatic Cforall code.

While C does have longjmp and friends, usage of them is hardly idiomatic, so most C code assumes no non-local tranfer of control happens when calling functions. Coding with non-local transfer of control and without require very different idioms.

Re:C for All by wolfgke on Friday March 23, 2018 @11:54AM (#16659335)
It would probably be much smaller than the gigantic C++ standard.
Re:C for All by kerkeslager on Friday March 23, 2018 @03:39PM (#16661887)
Okay, so the creators of this safer C promising that it isn't going to grow to the same size as C++? I guess if they're promising that, that makes sense.

C++ was a C variant once.

Re:C for All by emilfihlman on Friday March 23, 2018 @12:05PM (#16659489)
Because it's not C++, it's C.

It doesn't require them/us to change much, just add these flags and the compiler will warn you about things that are unsafe.

I've always found it quite sad that no one is interested in bettering C enough to push relevant changes through.

Re:C for All by pjmlp on Friday March 23, 2018 @12:45PM (#16659966)
Because there is this myth that any good programmer is able to write safe C.

Only newbies make memory corruption errors.

Yet even Dennis acknowledged correct code mattered, and Johnson created lint in 1979!

Largely ignored until clang and its analyzers came into the scene.

Re:C for All by dralley on Friday March 23, 2018 @11:55AM (#16659352)
counterexample: RedoxOS

Although it's more along the lines of Plan9 - a unix-like system that ignores the bits of POSIX that really suck.

Re:C for All by pjmlp on Friday March 23, 2018 @12:37PM (#16659881)
If it doesn’t support 100% POSIX it isn’t UNIX.

Also how would you make memcpy() safe in a POSIX implementation on Redox?

Re:C for All by IAmLiterallyAB on Friday March 23, 2018 @02:53PM (#16661430)
Almost nothing is 100% POSIX. Most Linux distros aren't. Who cares as long as the important bits are there.
Re:C for All by aidenn0 on Friday March 23, 2018 @02:27PM (#16661167)
Then most linux distributions aren't UNIX, as they do tend to deviate from POSIX in places.
Re:C for All by nine_k on Friday March 23, 2018 @03:07PM (#16661558)
It's GNU/Linux, and GNU = GNU is Not Unix.

So it says Not Unix right on th tin.

Re:C for All by boomlinde on Friday March 23, 2018 @02:52PM (#16661410)
To be perfectly fair, if it isn't UNIX it isn't UNIX.
Re:C for All by Gibbon1 on Friday March 23, 2018 @03:14PM (#16661634)
> Also how would you make memcpy() safe

Easy, don't have memcpy().

Re:C for All by zzzcpan on Friday March 23, 2018 @12:03PM (#16659443)
Which means that minimal requirements to win kernel and embedded devs is to integrate well with the rest of the C ecosystem, including myriad of C compilers and to be really well suited for low level work. This excludes pretty much all ideas, but meta languages that produce C code. Might even be necessary to promote the language itself not as a new language, but as a meta preprocessor for C to avoid alienating developers. But realistically this is not feasible nor necessary. There are much more feasible ideas to improve safety, than forcing half of the world to learn a lot of new things and change.
Re:C for All by pjmlp on Friday March 23, 2018 @12:50PM (#16660041)
Unfortunately not all systems are Solaris running on SPARC, with memory protection enabled.

Something akin to Intel MPX.

Re:C for All by s73v3r_ on Friday March 23, 2018 @12:46PM (#16659975)
They're not going to rewrite any of them in this dialect, either.
Re:C for All by snarfy on Friday March 23, 2018 @01:40PM (#16660615)
You can use c++14 on embedded devices just fine, and people do.
Re:C for All by saagarjha on Friday March 23, 2018 @02:37PM (#16661260)
You can, as long as you stay away from templates and lambdas and STL containers; in short, most of the reasons you’d use C++.
Re:C for All by aurelian15 on Friday March 23, 2018 @02:47PM (#16661358)
STL, RTTI and Exceptions should be avoided on embedded platforms (talking about 8 bit µCs here). I've extensively used both templates and lambdas on 8 bit AVRs (both the Tiny and Mega series); actually, writing templated code for µCs is a great way to avoid overhead stemming from function pointers etc. while still having well maintainable code.
Re:C for All by michaelcampbell on Friday March 23, 2018 @06:55PM (#16663371)
> Because regardless how some of us might dislike C and its security related issues, the truth is that no one is ever going to rewrite UNIX systems in other language

Now will they in this.

Re:C for All by zombieprocesses on Friday March 23, 2018 @11:55AM (#16659349)
> So if one finally manages to get a safer C variant that finally wins the hearts of UNIX kernels and embedded devs

A safer variant wouldn't be C. What makes C great for OS development is that it is just a step above assembly and you as a developer are given tremendous amount of power to do good and evil. C#/Java are programming languages with training wheels and it's great for application development. But for low level coding required for OS, network stacks, databases, etc, you really have to take the training wheels off.

I suppose you can try and make the C type system more stringent, but then it wouldn't be C. And considering they are aiming for backwards compatibility with existing C and its immense code infrastructure, they will have to keep the "flaws" in c for all.

Time would be better spent making the libraries/kernel/etc sturdier but if they can pull it off and win the hearts and minds of OS developers, then so be it.

Also, people have been trying to sideline C for decades. Each attempt has only reinforced C's standing and reminded us why C is so essential for OS development. Anyone remember the ill-fated attempt by Sun with their JVM centered JavaOS?

Re:C for All by xamuel on Friday March 23, 2018 @12:29PM (#16659764)
The only way to make C safe without losing performance would be to accompany your C code with a formal proof that it avoids undefined behavior, and use a compiler which refuses to compile the code if the formal proof doesn't validate.
Re:C for All by saagarjha on Friday March 23, 2018 @03:44PM (#16661941)
Which would be essentially impossible for any language like C.
Re:C for All by nickpsecurity on Saturday March 24, 2018 @12:49AM (#16664933)
KCC is an executable, formal semantics for C that does something like that. Runtime Verification Inc uses it for their bug-hunting tools.

https://github.com/kframework/c-semantics

http://fsl.cs.illinois.edu/pubs/ellison-rosu-2012-popl.pdf

Re:C for All by golergka on Friday March 23, 2018 @11:59AM (#16659395)
A full time C#, can confirm. It's an awesome business logic langauge, but the hot path eventually gets rewritten in a very C-like style, with all Linq, exceptions and allocations thrown out.
Re:C for All by pjmlp on Friday March 23, 2018 @12:53PM (#16660078)
The new 7.x features are going to make it easier.
Re:C for All by golergka on Friday March 23, 2018 @04:42PM (#16662483)
Some of us are still stuck in 3.5
Re:C for All by Thiez on Saturday March 24, 2018 @04:29PM (#16668738)
Can you explain why? I assume those c# programs are used for backend stuff on servers, surely it isn't hard to get a 4.6+ runtime installed?
Re:C for All by golergka on Sunday March 25, 2018 @04:51AM (#16671357)
https://docs.unity3d.com/Manual/ScriptingRuntimeUpgrade.html

I can switch to 4.6, but don't want the risk of an experimental feature yet.

Re:C for All by Thiez on Sunday March 25, 2018 @11:29AM (#16672577)
Right, forgot about Unity, that makes sense :)
Re:C for All by pjmlp on Friday March 23, 2018 @12:40PM (#16659918)
Microsoft already proven twice that languages with training wheels can be used for writing OSes.

Google is using languages with training wheels to write core components of Fucshia (TCP/IP stack and file system tools are written in Go), as well as the new Android GPU debugger (also in Go).