3 ms·
Why would you expect the above to work? It is a pretty clear violation of the type rules on any sane language. For what is worth, I would love to be able to wr
by gpderetta 10y ago
Why would you expect the above to work? It is a pretty clear violation of the type rules on any sane language.
For what is worth, I would love to be able to wrap logically different types of float and ints in distinct structures so that I can benefit from TBAA more, but I believe that GCC, for many reasons, internally uses a structural type system to track TBAA.
- nitrogen 10y agoBecause C's type rules are often thought of as syntactic sugar for pointer arithmetic, not sacrosanct definitions of what is or is not an X or a Y.
- kazinator 10y ago> Why would you expect the above to work? Because 1. What it is trying to do is perfectly clear. 2. What it is trying to do is not only perfectly clear, but has a completely machine independent interpretation. 3. "Work" is better than "not work", all round, for everyone involved.
- gpderetta 10y ago1. It is so perfectly clear that no two C programmers can agree to a common set of rules. 2. A machine independent interpretation would prevent any memory safety santiizer to do its job, which it seems to me much more useful than allowing such shenanigans. 3. The best resolution for everyone involved is for the program to abort as soon as it detect a rule violation. "Working" for some ill defined definition of working is not useful to anyone.
- kazinator 10y agoNo two C programmers can possibly disagree about what that code should do if it were to be defined. There are many ways of adding requirements to make it defined (and those ways will differ in what else they make defined). > The best resolution for everyone involved is for the program to abort as soon as it detect a rule violation. The pointer cast in the code makes it clear that the aliasing between the identically structured struct is intentional. (If we remove the cast, we have a diagnosable constraint violation: no need to whip out the memory sanitizers). At least we seem to agree as far as that optimizing based on the assumption that aliasing does not take place is one of the worst things that could happen. The rule violation is not diagnosed, and the obvious meaning doesn't occur either, but rather some weird result. For instance, garbage is accessed because the underlying struct is not yet initialized (on grounds that it's not necessary to initialize something before an improperly typed access to it).
- gpderetta 10y ago"No two C programmers can possibly disagree about what that code should do if it were to be defined." The whole point is that there is no agreement on how they should be defined. "At least we seem to agree as far as that optimizing based on the assumption that aliasing does not take place is one of the worst things that could happen" I do want the compiler to optimize assuming no aliasing but first I want: a) the rules to be clarified beyond the current mess; b) have a strict compilation mode (possibly on by default) that will enforce the rules so that mistakes can be caught. Such mode would necessarily be very slow as the C language is very unfriendly to such an analysis (in the worst case you need a shadow type map for every memory location), but given the nature of aliasing violations, even a debug only mode should catch the large majority of mistakes.
- icedchai 10y agoOne would expect it to work because the memory layout is the same for the two structures.
- naasking 10y ago> Why would you expect the above to work? It is a pretty clear violation of the type rules on any sane language. It doesn't violate structural typing rules, so I disagree.
- gpderetta 10y agoYou are actually right that it will work on a structurally typed language. Have an upvote.
- kazinator 10y agoBy the way, in the original C language by Dennis R. struct member names were in a kind of global namespace of all such names. So for instance if we had a struct stat { ... int st_ino; ... } struct dirent { ... int d_ino; ... } d; It was possible to access: d.st_ino; The compiler would just use the st_ino offset from struct stat, and generate an access into the struct bar b. This is the main reason members have funny prefixes in Unix structures. Once upon a time they had to, to prevent clashes.
- naasking 10y agoIndeed, it depends how one should interpret struct declarations. If you consider all structs structurally typed in C, then that should pass. If you only consider anonymous structs to be structurally typed, and named structs are the equivalent of newtype in Haskell (which is how C currently behaves I think), then the sample as written should fail, though using an anonymous struct for the alias would pass. I think the latter semantics makes for a better language because you can exploit the type checker to greater effect, but the former is certainly not unequivocally wrong.
- gpderetta 10y agoI think that at least for standard C and certainly for C++, the type system is meant to be nominal. Unnamed structures are better understood as having a unique but unnameable type. Anonymous structures are just weird and probably better understood as being just a syntactic aggregation without any type level connotation (C++ doesn't have anonymous structures; it does have anonymous unions though).
- kazinator 10y ago> It is a pretty clear violation of the type rules on any sane language. Is it? What is "sane"? This is the TXR Lisp interactive listener of TXR 141. Use the :quit command or type Ctrl-D on empty line to exit. 1> (defstruct foo nil x) #<struct-type foo> 2> (defstruct bar nil x) #<struct-type bar> 3> (defun access-x (obj) obj.x) access-x 4> (access-x (new foo x 42)) 42 5> (access-x (new bar x 42)) 42 This is so insane that it will work even if x is at different offsets in foo and bar. We can whip up a similar thing statically with, say, C++ templates. Note that C does explicitly allow this: { struct foo { int x; char other; }; struct bar { int x; double other; }; union combined { struct foo f; struct bar b; } c; c.f.x = 42; return c.b.x; } If structs which have some common sequence of leading members (same names and types) are combined via a union, then those common members can be accessed through any member of the that union.
- lmm 10y ago> This is so insane that it will work even if x is at different offsets in foo and bar. You're accessing foo.x or bar.x. You're just doing it in a generic way. That's fine and should work, in any language. > If structs which have some common sequence of leading members (same names and types) are combined via a union, then those common members can be accessed through any member of the that union. Unions have defined semantics in all cases, no? union combined { float f; int i; } c; c.f = 1.0f return c.i; will return some (possibly implementation-defined) value rather than being undefined behaviour, because that's the whole point of a union - access to different fields of a union shouldn't be an aliasing violation.
- kazinator 10y agoUnions have well-defined semantics in that special case, spelled out in ISO C. I have a copy of C99 handy, where it is paragraph 5 in 6.5.2.3 (Structure and union members): "One special guarantee is made in order to simplify the use of unions: if a union contains several structures that share a common initial sequence (see below), and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declaration of the complete type of the union is visible. Two structures share a common initial sequence if corresponding members have compatible types (and, for bit-fields, the same widths) for a sequence of one or more initial members." The part about "anywhere that a declaration of the complete type of the union is visible" isn't superfluous verbiage: it means that the aliasing doesn't have to go through the union; but it has to be in a region of code where the union is declared. So for instance, it is no longer well-defined if you do this: // external fun has no idea about the union external_fun(&c.f); // exernal_fun is this: void external_fun(struct foo *pf) { struct bar *pb = (struct bar *) pf; // access through pb } Here, c.f is a member of a union where and has a "common initial part" with c.b of the same union object. But this is not known in external_fun which has no declaration of the union and so the translation of external_fun doesn't have to honor this aliasing if there is no completely declared union type in scope which aliases struct foo and struct bar. (See, C goes out of its way to make it clear that aliasing between different struct types is bad: even when an exception is made, there is weasel wording to carefully contain it!)