News for hackers. Stuff that matters.
Re:The Lost Art of Structure Packing (2018) by scatters on Monday April 27, 2020 @11:55AM (#22997015)
memset, sure, then you copy the struct (return it or pass it by value) and the compiler won't bother copying the padding bytes. Or worse, an optimizing compiler will see that you're writing to padding bytes and helpfully no-op it.
Re:The Lost Art of Structure Packing (2018) by shawxe on Monday April 27, 2020 @12:09PM (#22997153)
First of all, there is no way the compiler is going to optimize a call to memset(&foo, 0, sizeof(foo)) when &foo is being interpreted as a void pointer. That doesn't even make sense.

Second of all, in a generic C interface keys are likely to be treated as void pointer and almost certainly are going to be moved around with memcpy etc. rather than returned/passed by value since doing so would make the interface non-generic.

Re:The Lost Art of Structure Packing (2018) by jcelerier on Monday April 27, 2020 @12:52PM (#22997555)
> First of all, there is no way the compiler is going to optimize a call to memset(&foo, 0, sizeof(foo)) when &foo is being interpreted as a void pointer. That doesn't even make sense.

that is really dangerous to assume. memset is a compiler built-in in every relevant C++ compiler and the compiler definitely knows the type of the object that is behind your void* and knows if you're being nasty.

e.g. look at this code : https://gcc.godbolt.org/z/xh9BXs

it's UB, and the compiler knows it and inserts an "invalid opcode" instruction even if you try to hide a memset behind a void*-taking function

Re:The Lost Art of Structure Packing (2018) by saagarjha on Monday April 27, 2020 @01:32PM (#22997948)
-Ofast makes non-compliant programs anyways, so it's not the best example.
Re:The Lost Art of Structure Packing (2018) by jcelerier on Monday April 27, 2020 @01:56PM (#22998192)
I had -Ofast in there but you can check that it's the same with -O2
Re:The Lost Art of Structure Packing (2018) by scatters on Monday April 27, 2020 @12:48PM (#22997517)
The compiler knows what memset does. One of the earliest steps of optimization is replacing calls to well-known functions with intrinsics. Try it! https://godbolt.org/z/QUREQi

Yes, it's true that generic C code will type-erase the key type. However it just takes a little refactoring in specific code to move the struct initialization across a call boundary from where it is passed to the generic code.

Re:The Lost Art of Structure Packing (2018) by kazinator on Tuesday April 28, 2020 @12:33PM (#23008688)
The compiler knows that memset clobbers an object, and can classify that as a dead store.

I'm skeptical about compilers optimizing memset not to cover padding between structure members.

Firstly, that would introduce security holes into a heck of a lot more existing code compared to code that uses a dead-store memset to wipe sensitive crypto.

Secondly, it wouldn't run any faster. Gaps in a structure and at the end exist in order to eliminate misalignment. Before most padding, there is a member that ends on a misaligned address. It's slower to update just that member, and leave the padding alone, than to clobber the padding.

For instance if we have a { char a; int b; char c; } structure, we gain nothing by zeroing just one byte of a, b and c.

In some compiler for an 8 bit system, this reasoning is likely false; I will worry about it when porting to that. Very little existing code will fit; you're coding from scratch for such things.

Re:The Lost Art of Structure Packing (2018) by dependenttypes on Monday April 27, 2020 @12:16PM (#22997205)
The HN markdown ate your stars.
Re:The Lost Art of Structure Packing (2018) by shawxe on Monday April 27, 2020 @12:17PM (#22997227)
Thanks for letting me know, should be better now! :-)
Re:The Lost Art of Structure Packing (2018) by kazinator on Monday April 27, 2020 @12:20PM (#22997254)
Basically, put a space after the star that you want to render verbatim, and don't try to do that in a region of text that is already italicized with stars.

E.g. "... being interpreted as a void * type."

Re:The Lost Art of Structure Packing (2018) by kazinator on Monday April 27, 2020 @12:06PM (#22997116)
A compiler that optimizes away memset is not one that could be used for targeting anything that sits on a network.
Re:The Lost Art of Structure Packing (2018) by saagarjha on Monday April 27, 2020 @12:57PM (#22997603)
That immediately rules out GCC and Clang…
Re:The Lost Art of Structure Packing (2018) by kazinator on Monday April 27, 2020 @03:39PM (#22999179)
I don't think so, unless we're mistaking memset for __builtin_memset.
Re:The Lost Art of Structure Packing (2018) by scatters on Monday April 27, 2020 @05:04PM (#23000011)
Re:The Lost Art of Structure Packing (2018) by kazinator on Monday April 27, 2020 @08:11PM (#23001815)
I see; because the sixth argument (BOTH_P) in that macro call is true.