Live data from Hacker News

How we found that the Linux nios2 memset() implementation had a bug

free-electrons.com

11–17 of 17 posts

Re: How we found that the Linux nios2 memset() implementation had a bug

#11
post #4

Here's another horrid gcc inline asm bug: http://robertoconcerto.blogspot.co.uk/2013/03/my-hardest-bug... As a general rule, to which the usual caveats for general rules apply, when it comes to gcc-style inline asm, just say no. Demand more from your tools.

To be fair, that's a horrid gcc inline asm bug coupled with a crazy CPU design (for the PS2). Still a good story though.

Re: How we found that the Linux nios2 memset() implementation had a bug

#12

We were quite surprised to find a bug in some common code for the NIOS II architecture: we were assuming it would have already been tested on enough platforms and with enough compilers/situations to not have such issues. I'm betting on the vast majority of memset() uses being to zero memory, so it might've not been tested much with other fill patterns. Compared to MSVC, I've found GCC's inline-assembler feature a hug…

The only problem is if one uses UNIX instead of Windows to do development.

Fortunately there are still some people doing development on UNIX and sharing their software tools.

http://cr.yp.to/highspeed/fall2006.html

Re: How we found that the Linux nios2 memset() implementation had a bug

#13
post #4

Here's another horrid gcc inline asm bug: http://robertoconcerto.blogspot.co.uk/2013/03/my-hardest-bug... As a general rule, to which the usual caveats for general rules apply, when it comes to gcc-style inline asm, just say no. Demand more from your tools.

Personally I don't find inline-asm with gcc to be all that bad. It is definitely a very nice way of accessing other-wise inaccessible CPU instructions - and those tend to be generally trivial to implement (IE. Single asm instruction, along with some input and output operands). I admit that on the rare occasion that I do use it, I almost always go reread the documentation. But still, the compiled end result is tons better then having to call a completely separate function in your code every time you just want to use one single CPU instruction.

That said, for longer pieces of code, I definitely avoid inline-asm and just use a separate assembly file. There's no debating it's a cleaner solution when you have more then one or a few lines of assembly. And really, besides those obscure CPU instructions, your written assembly is usually not much different, then the stuff generated from some C code. It's worth using C wherever it's viable.

Re: How we found that the Linux nios2 memset() implementation had a bug

#14
post #5
post #3

It's been my experience that it's easy to write GCC inline asm that works the first few times you use it, but subtly breaks depending on what the optimizer does with the code around it. Those input/output/clobber arguments can be tricky to get right and wrongness can be hard to spot.

Yeah, and as one of the comments to that blog post says, for bit twiddling operations like this, modern compilers are good enough to generate the optimal assembler the vast majority of the time: If you write the equivalent C code uint32 fill16 = (fill (copied from the comment, so hopefully correct!) you get the benefit of a) not having to fiddle with abstruse register allocation semantics but b) you’re letting gcc us…

No compiler (MSVC, gcc, icc) outputs the "bts" instruction for operations like: bitfield[val / 32] = val % 32; which could implicitly perform the modulo.

Using the intrinsic provided significant performance improvements, and we got still more when the rest of the inner loop was rewritten purely in assembly.

Re: How we found that the Linux nios2 memset() implementation had a bug

#16
post #8

We were quite surprised to find a bug in some common code for the NIOS II architecture: we were assuming it would have already been tested on enough platforms and with enough compilers/situations to not have such issues. I'm betting on the vast majority of memset() uses being to zero memory, so it might've not been tested much with other fill patterns. Compared to MSVC, I've found GCC's inline-assembler feature a hug…

There was exactly this bug in Android for a long time - memset always cleared the memory to zero. I can't find a good link to this, but try maybe https://review.source.android.com/#patch,sidebyside,14699,1,... , not that Firefox will let me connect to check. As for the rules for VC++, they are conservative - see https://msdn.microsoft.com/en-us/library/k1a8ss06.aspx . In general, you're right that it's much nicer to…

I don't have any need to type inline Assembly in the type of work I do, but used to back in the MS-DOS days.

The inline Assembly syntax of PC compilers always felt natural and easy to remember.

Every time I look at gcc inline Assembly, I get this feeling it is impossible to get right without having the manual page always open.

Post reply on HN