C Memory Object and Value Semantics: the Space of De Facto and ISO Standards
Total Page:16
File Type:pdf, Size:1020Kb
DRAFT – WORK IN PROGRESS C memory object and value semantics: the space of de facto and ISO standards [This revises and extends WG14 N2013] Revision: 1571 2016-03-17 David Chisnall Justus Matthiesen Kayvan Memarian Peter Sewell Robert N. M. Watson University of Cambridge http://www.cl.cam.ac.uk/~pes20/cerberus/ Contents 2.3.1 Q8. Should intra-object pointer sub- traction give provenance-free inte- 1 Introduction5 ger results?............. 15 1.1 Experimental Testing............6 2.3.2 Q9. Can one make a usable off- 1.2 Summary of answers............7 set between two separately allocated objects by inter-object subtraction 2 Abstract Pointers7 (using either pointer or integer arith- 2.1 Pointer Provenance.............7 metic), to make a usable pointer to the second by adding the offset to 2.1.1 Q1. Must the pointer used for the first?............... 16 a memory access have the right provenance, i.e. be derived from 2.3.3 Q10. Presuming that one can the pointer to the original alloca- have valid pointers with multiple tion (with undefined behaviour oth- provenances, does an inter-object erwise)? (This lets compilers do pointer subtraction give a value with provenance-based alias analysis)..7 explicitly-unknown provenance or 2.1.2 Q2. Can equality testing on pointers something more specific?...... 18 be affected by pointer provenance 2.3.4 Q11. Is the XOR linked list idiom information?............9 supported?............. 18 2.1.3 GCC and ISO C11 differ on the re- 2.3.5 Q12. For arithmetic over prove- sult of a == comparison on a one- nanced integer values, is the prove- past pointer............. 10 nance of the result invariant under 2.2 Pointer provenance via integer types.... 10 plus/minus associativity?...... 19 2.2.1 Q3. Can one make a usable pointer 2.3.6 Multiple provenance semantics via casts to intptr t and back?.. 10 summarised............. 19 2.2.2 Q4. Can one make a usable pointer 2.4 Pointer provenance via pointer representa- via casts to unsigned long and tion copying................. 19 back?................ 10 2.4.1 Q13. Can one make a usable copy of 2.2.3 Q5. Must provenance information a pointer by copying its representa- be tracked via casts to integer types tion bytes using the library memcpy? 19 and integer arithmetic?....... 12 2.4.2 Q14. Can one make a usable copy of 2.2.4 Q6. Can one use bit manipulation a pointer by copying its representa- and integer casts to store informa- tion bytes (unchanged) in user code? 20 tion in unused bits of pointers?... 14 2.4.3 Q15. Can one make a usable copy 2.2.5 Q7. Can equality testing on integers of a pointer by copying its repre- that are derived from pointer values sentation bytes by user code that in- be affected by their provenance?.. 15 directly computes the identity func- 2.3 Pointers involving multiple provenances.. 15 tion on those bytes?......... 20 1 2016/3/17 2.4.4 Q16. Can one carry provenance 2.13.1 Q31. Can one construct out-of- through dataflow alone or also bounds (by more than one) pointer through control flow?........ 21 values by pointer arithmetic (with- 2.5 Pointer provenance and union type punning 23 out undefined behaviour)?..... 31 2.5.1 Q17. Is type punning between inte- 2.13.2 Q32. Can one form pointer values ger and pointer values allowed?.. 23 by pointer addition that overflows 2.5.2 Q18. Does type punning between (without undefined behaviour)?.. 32 integer and pointer values preserve 2.13.3 Q33. Can one assume pointer addi- provenance?............ 23 tion wraps on overflow?...... 32 2.6 Pointer provenance via IO......... 24 2.13.4 Q34. Can one move among 2.6.1 Q19. Can one make a usable pointer the members of a struct using via IO?............... 24 representation-pointer arithmetic 2.7 Q20. Can one make a usable pointer from a and casts?.............. 32 concrete address (of device memory)?... 25 2.13.5 Q35. Can one move between subob- 2.8 Pointer provenance for other allocators... 25 jects of the members of a struct us- 2.9 Stability of pointer values.......... 26 ing pointer arithmetic?....... 33 2.9.1 Q21. Are pointer values stable?.. 26 2.13.6 Q36. Can one implement offsetof 2.10 Pointer Equality Comparison (with == and !=) 26 using the addresses of members of a 2.10.1 Q22. Can one do == comparison be- NULL struct pointer?........ 33 tween pointers to objects of non- 2.14 Casts between pointer types......... 34 compatible types?.......... 26 2.14.1 Q37. Are usable pointers to a struct 2.10.2 Q23. Can one do == comparison be- and to its first member intercon- tween pointers (to objects of com- vertable?.............. 34 patible types) with different prove- nances that are not strictly within 2.14.2 Q38. Are usable pointers to a union their original allocations?...... 28 and to its current member intercon- vertable?.............. 35 2.10.3 Q24. Can one do == comparison of a pointer and (void*)-1?..... 28 2.15 Accesses to related structure and union types 35 2.11 Pointer Relational Comparison (with <, >, 2.15.1 Q39. Given two different structure <=, or >=).................. 28 types sharing a prefix of members 2.11.1 Q25. Can one do relational compar- that have compatible types, can one ison (with <, >, <=, or >=) of two cast a usable pointer to an object of pointers to separately allocated ob- the first to a pointer to the second, jects (of compatible object types)?. 28 that can be used to read and write 2.11.2 Q26. Can one do relational compari- members of that prefix (with strict- son (with <, >, <=, or >=) of a pointer aliasing disabled and without pack- to a structure member and one to a ing variation)?........... 36 sub-member of another member, of 2.15.2 Q40. Can one read from the initial compatible object types?...... 29 part of a union of structures sharing 2.11.3 Q27. Can one do relational compari- a common initial sequence via any son (with <, >, <=, or >=) of pointers union member (if the union type is to two members of a structure that visible)?............... 37 have incompatible types?...... 29 2.15.3 Q41. Is writing to the initial part of 2.12 Null pointers................ 30 a union of structures sharing a com- 2.12.1 Q28. Can one make a null pointer by mon initial sequence allowed via casting from a non-constant integer any union member (if the union type expression?............. 30 is visible)?............. 38 2.12.2 Q29. Can one assume that all null 2.15.4 Q42. Is type punning by writing and pointers have the same representation? 30 reading different union members al- 2.12.3 Q30. Can null pointers be assumed lowed (if the lvalue is syntactically to have all-zero representation bytes? 31 obvious)?.............. 38 2.13 Pointer Arithmetic............. 31 2.16 Pointer lifetime end............. 39 2 2016/3/17 2.16.1 Q43. Can one inspect the value, 3.2.8 Q56. Given multiple bitfields that (e.g. by testing equality with ==) of may be in the same word, can one be a pointer to an object whose life- a well-defined value while another is time has ended (either at a free() an unspecified value?........ 51 or block exit)?........... 40 2.16.2 Q44. Is the dynamic reuse of alloca- 3.2.9 Q57. Are the representation bytes of tion addresses permitted?...... 40 an unspecified value themselves also unspecified values? (not an arbitrary 2.17 Invalid Accesses............... 41 choice of concrete byte values)... 51 2.17.1 Q45. Can accesses via a null pointer be assumed to give runtime errors, 3.2.10 Q58. If one writes some but not rather than give rise to undefined be- all of the representation bytes of an haviour?............... 41 uninitialized value, do the other rep- 2.17.2 Q46. Can reads via invalid pointers resentation bytes still hold unspeci- be assumed to give runtime errors or fied values?............. 52 unspecified values, rather than unde- fined behaviour?.......... 41 3.2.11 Q59. If one writes some but not all of the representation bytes of an uninitialized value, does a read of 3 Abstract Unspecified Values 41 the whole value still give an unspec- 3.1 Trap Representations............ 42 ified value?............. 52 3.1.1 Q47. Can one reasonably assume that no types have trap representa- 3.3 Structure and Union Padding........ 53 tions?................ 42 3.3.1 Q60. Can structure-copy copy 3.1.2 Q48. Does reading an uninitialised padding?.............. 54 object give rise to undefined be- haviour?............... 43 3.3.2 Q61. After an explicit write of a 3.2 Unspecified Values............. 45 padding byte, does that byte hold a 3.2.1 Q49. Can library calls with well-defined value? (not an unspeci- unspecified-value arguments be as- fied value).............. 56 sumed to execute with an arbitrary 3.3.3 Q62. After an explicit write of a choice of a concrete value (not padding byte followed by a write necessarily giving rise to undefined to the whole structure, does the behaviour)?............. 46 padding byte hold a well-defined 3.2.2 Q50. Can control-flow choices value? (not an unspecified value).. 56 based on unspecified values be assumed to make an arbitrary 3.3.4 Q63. After an explicit write of a choice (not giving rise to undefined padding byte followed by a write to behaviour)?............. 46 adjacent members of the structure, 3.2.3 Q51. In the absence of any writes, does the padding byte hold a well- is an unspecified value potentially defined value? (not an unspecified unstable, i.e., can multiple usages of value)...............