Rob Pike | 20850fc | 2009-05-08 16:24:55 -0700 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | /* |
Nigel Tao | 6a186d3 | 2011-04-20 09:57:05 +1000 | [diff] [blame] | 6 | Package unsafe contains operations that step around the type safety of Go programs. |
Rob Pike | 1415a53 | 2014-10-24 09:37:25 -0700 | [diff] [blame] | 7 | |
| 8 | Packages that import unsafe may be non-portable and are not protected by the |
| 9 | Go 1 compatibility guidelines. |
Robert Griesemer | 30c7088 | 2009-11-05 09:40:28 -0800 | [diff] [blame] | 10 | */ |
Rob Pike | 20850fc | 2009-05-08 16:24:55 -0700 | [diff] [blame] | 11 | package unsafe |
| 12 | |
| 13 | // ArbitraryType is here for the purposes of documentation only and is not actually |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 14 | // part of the unsafe package. It represents the type of an arbitrary Go expression. |
Rob Pike | 20850fc | 2009-05-08 16:24:55 -0700 | [diff] [blame] | 15 | type ArbitraryType int |
| 16 | |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 17 | // Pointer represents a pointer to an arbitrary type. There are four special operations |
Russ Cox | 2a09a68 | 2016-01-13 20:14:03 -0500 | [diff] [blame] | 18 | // available for type Pointer that are not available for other types: |
| 19 | // - A pointer value of any type can be converted to a Pointer. |
| 20 | // - A Pointer can be converted to a pointer value of any type. |
| 21 | // - A uintptr can be converted to a Pointer. |
| 22 | // - A Pointer can be converted to a uintptr. |
Rob Pike | 20850fc | 2009-05-08 16:24:55 -0700 | [diff] [blame] | 23 | // Pointer therefore allows a program to defeat the type system and read and write |
| 24 | // arbitrary memory. It should be used with extreme care. |
Russ Cox | 2a09a68 | 2016-01-13 20:14:03 -0500 | [diff] [blame] | 25 | // |
| 26 | // The following patterns involving Pointer are valid. |
| 27 | // Code not using these patterns is likely to be invalid today |
| 28 | // or to become invalid in the future. |
| 29 | // Even the valid patterns below come with important caveats. |
| 30 | // |
| 31 | // Running "go vet" can help find uses of Pointer that do not conform to these patterns, |
| 32 | // but silence from "go vet" is not a guarantee that the code is valid. |
| 33 | // |
| 34 | // (1) Conversion of a *T1 to Pointer to *T2. |
| 35 | // |
| 36 | // Provided that T2 is no larger than T1 and that the two share an equivalent |
| 37 | // memory layout, this conversion allows reinterpreting data of one type as |
| 38 | // data of another type. An example is the implementation of |
| 39 | // math.Float64bits: |
| 40 | // |
| 41 | // func Float64bits(f float64) uint64 { |
| 42 | // return *(*uint64)(unsafe.Pointer(&f)) |
| 43 | // } |
| 44 | // |
| 45 | // (2) Conversion of a Pointer to a uintptr (but not back to Pointer). |
| 46 | // |
| 47 | // Converting a Pointer to a uintptr produces the memory address of the value |
| 48 | // pointed at, as an integer. The usual use for such a uintptr is to print it. |
| 49 | // |
| 50 | // Conversion of a uintptr back to Pointer is not valid in general. |
| 51 | // |
| 52 | // A uintptr is an integer, not a reference. |
| 53 | // Converting a Pointer to a uintptr creates an integer value |
| 54 | // with no pointer semantics. |
| 55 | // Even if a uintptr holds the address of some object, |
| 56 | // the garbage collector will not update that uintptr's value |
| 57 | // if the object moves, nor will that uintptr keep the object |
| 58 | // from being reclaimed. |
| 59 | // |
| 60 | // The remaining patterns enumerate the only valid conversions |
| 61 | // from uintptr to Pointer. |
| 62 | // |
| 63 | // (3) Conversion of a Pointer to a uintptr and back, with arithmetic. |
| 64 | // |
| 65 | // If p points into an allocated object, it can be advanced through the object |
Rahul Chaudhry | f5e3090 | 2016-01-28 16:33:35 -0800 | [diff] [blame] | 66 | // by conversion to uintptr, addition of an offset, and conversion back to Pointer. |
Russ Cox | 2a09a68 | 2016-01-13 20:14:03 -0500 | [diff] [blame] | 67 | // |
| 68 | // p = unsafe.Pointer(uintptr(p) + offset) |
| 69 | // |
| 70 | // The most common use of this pattern is to access fields in a struct |
| 71 | // or elements of an array: |
| 72 | // |
| 73 | // // equivalent to f := unsafe.Pointer(&s.f) |
| 74 | // f := unsafe.Pointer(uintptr(unsafe.Pointer(&s)) + unsafe.Offsetof(s.f)) |
| 75 | // |
| 76 | // // equivalent to e := unsafe.Pointer(&x[i]) |
| 77 | // e := unsafe.Pointer(uintptr(unsafe.Pointer(&x[0])) + i*unsafe.Sizeof(x[0])) |
| 78 | // |
Josh Bleecher Snyder | 29df4c8 | 2016-08-17 16:23:40 -0700 | [diff] [blame] | 79 | // It is valid both to add and to subtract offsets from a pointer in this way. |
| 80 | // It is also valid to use &^ to round pointers, usually for alignment. |
| 81 | // In all cases, the result must continue to point into the original allocated object. |
| 82 | // |
Russ Cox | 2a09a68 | 2016-01-13 20:14:03 -0500 | [diff] [blame] | 83 | // Unlike in C, it is not valid to advance a pointer just beyond the end of |
| 84 | // its original allocation: |
| 85 | // |
| 86 | // // INVALID: end points outside allocated space. |
| 87 | // var s thing |
| 88 | // end = unsafe.Pointer(uintptr(unsafe.Pointer(&s)) + unsafe.Sizeof(s)) |
| 89 | // |
| 90 | // // INVALID: end points outside allocated space. |
| 91 | // b := make([]byte, n) |
| 92 | // end = unsafe.Pointer(uintptr(unsafe.Pointer(&b[0])) + uintptr(n)) |
| 93 | // |
| 94 | // Note that both conversions must appear in the same expression, with only |
| 95 | // the intervening arithmetic between them: |
| 96 | // |
| 97 | // // INVALID: uintptr cannot be stored in variable |
| 98 | // // before conversion back to Pointer. |
| 99 | // u := uintptr(p) |
| 100 | // p = unsafe.Pointer(u + offset) |
| 101 | // |
| 102 | // (4) Conversion of a Pointer to a uintptr when calling syscall.Syscall. |
| 103 | // |
| 104 | // The Syscall functions in package syscall pass their uintptr arguments directly |
| 105 | // to the operating system, which then may, depending on the details of the call, |
| 106 | // reinterpret some of them as pointers. |
| 107 | // That is, the system call implementation is implicitly converting certain arguments |
| 108 | // back from uintptr to pointer. |
| 109 | // |
| 110 | // If a pointer argument must be converted to uintptr for use as an argument, |
| 111 | // that conversion must appear in the call expression itself: |
| 112 | // |
| 113 | // syscall.Syscall(SYS_READ, uintptr(fd), uintptr(unsafe.Pointer(p)), uintptr(n)) |
| 114 | // |
| 115 | // The compiler handles a Pointer converted to a uintptr in the argument list of |
| 116 | // a call to a function implemented in assembly by arranging that the referenced |
| 117 | // allocated object, if any, is retained and not moved until the call completes, |
| 118 | // even though from the types alone it would appear that the object is no longer |
| 119 | // needed during the call. |
| 120 | // |
| 121 | // For the compiler to recognize this pattern, |
| 122 | // the conversion must appear in the argument list: |
| 123 | // |
| 124 | // // INVALID: uintptr cannot be stored in variable |
| 125 | // // before implicit conversion back to Pointer during system call. |
| 126 | // u := uintptr(unsafe.Pointer(p)) |
| 127 | // syscall.Syscall(SYS_READ, uintptr(fd), u, uintptr(n)) |
| 128 | // |
| 129 | // (5) Conversion of the result of reflect.Value.Pointer or reflect.Value.UnsafeAddr |
| 130 | // from uintptr to Pointer. |
| 131 | // |
| 132 | // Package reflect's Value methods named Pointer and UnsafeAddr return type uintptr |
| 133 | // instead of unsafe.Pointer to keep callers from changing the result to an arbitrary |
| 134 | // type without first importing "unsafe". However, this means that the result is |
| 135 | // fragile and must be converted to Pointer immediately after making the call, |
| 136 | // in the same expression: |
| 137 | // |
| 138 | // p := (*int)(unsafe.Pointer(reflect.ValueOf(new(int)).Pointer())) |
| 139 | // |
| 140 | // As in the cases above, it is invalid to store the result before the conversion: |
| 141 | // |
| 142 | // // INVALID: uintptr cannot be stored in variable |
| 143 | // // before conversion back to Pointer. |
| 144 | // u := reflect.ValueOf(new(int)).Pointer() |
| 145 | // p := (*int)(unsafe.Pointer(u)) |
| 146 | // |
| 147 | // (6) Conversion of a reflect.SliceHeader or reflect.StringHeader Data field to or from Pointer. |
| 148 | // |
| 149 | // As in the previous case, the reflect data structures SliceHeader and StringHeader |
| 150 | // declare the field Data as a uintptr to keep callers from changing the result to |
| 151 | // an arbitrary type without first importing "unsafe". However, this means that |
| 152 | // SliceHeader and StringHeader are only valid when interpreting the content |
| 153 | // of an actual slice or string value. |
| 154 | // |
| 155 | // var s string |
| 156 | // hdr := (*reflect.StringHeader)(unsafe.Pointer(&s)) // case 1 |
| 157 | // hdr.Data = uintptr(unsafe.Pointer(p)) // case 6 (this case) |
Ian Lance Taylor | 9d139ac | 2016-11-06 10:35:58 -0800 | [diff] [blame] | 158 | // hdr.Len = n |
Russ Cox | 2a09a68 | 2016-01-13 20:14:03 -0500 | [diff] [blame] | 159 | // |
| 160 | // In this usage hdr.Data is really an alternate way to refer to the underlying |
| 161 | // pointer in the slice header, not a uintptr variable itself. |
| 162 | // |
| 163 | // In general, reflect.SliceHeader and reflect.StringHeader should be used |
| 164 | // only as *reflect.SliceHeader and *reflect.StringHeader pointing at actual |
| 165 | // slices or strings, never as plain structs. |
| 166 | // A program should not declare or allocate variables of these struct types. |
| 167 | // |
| 168 | // // INVALID: a directly-declared header will not hold Data as a reference. |
| 169 | // var hdr reflect.StringHeader |
| 170 | // hdr.Data = uintptr(unsafe.Pointer(p)) |
Ian Lance Taylor | 9d139ac | 2016-11-06 10:35:58 -0800 | [diff] [blame] | 171 | // hdr.Len = n |
Russ Cox | 2a09a68 | 2016-01-13 20:14:03 -0500 | [diff] [blame] | 172 | // s := *(*string)(unsafe.Pointer(&hdr)) // p possibly already lost |
| 173 | // |
Russ Cox | 9154943 | 2009-10-07 11:55:06 -0700 | [diff] [blame] | 174 | type Pointer *ArbitraryType |
Rob Pike | 20850fc | 2009-05-08 16:24:55 -0700 | [diff] [blame] | 175 | |
Robert Griesemer | 8332f80 | 2015-01-28 11:40:32 -0800 | [diff] [blame] | 176 | // Sizeof takes an expression x of any type and returns the size in bytes |
| 177 | // of a hypothetical variable v as if v was declared via var v = x. |
| 178 | // The size does not include any memory possibly referenced by x. |
Daniel Martà | 99da873 | 2017-08-19 22:33:51 +0200 | [diff] [blame] | 179 | // For instance, if x is a slice, Sizeof returns the size of the slice |
Robert Griesemer | 8332f80 | 2015-01-28 11:40:32 -0800 | [diff] [blame] | 180 | // descriptor, not the size of the memory referenced by the slice. |
Robert Griesemer | 3a52338 | 2015-01-27 09:57:48 -0800 | [diff] [blame] | 181 | func Sizeof(x ArbitraryType) uintptr |
Rob Pike | 20850fc | 2009-05-08 16:24:55 -0700 | [diff] [blame] | 182 | |
Robert Griesemer | 3a52338 | 2015-01-27 09:57:48 -0800 | [diff] [blame] | 183 | // Offsetof returns the offset within the struct of the field represented by x, |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 184 | // which must be of the form structValue.field. In other words, it returns the |
Rob Pike | 20850fc | 2009-05-08 16:24:55 -0700 | [diff] [blame] | 185 | // number of bytes between the start of the struct and the start of the field. |
Robert Griesemer | 3a52338 | 2015-01-27 09:57:48 -0800 | [diff] [blame] | 186 | func Offsetof(x ArbitraryType) uintptr |
Rob Pike | 20850fc | 2009-05-08 16:24:55 -0700 | [diff] [blame] | 187 | |
Ian Lance Taylor | f49a757 | 2016-01-26 17:23:33 -0800 | [diff] [blame] | 188 | // Alignof takes an expression x of any type and returns the required alignment |
Robert Griesemer | 3a52338 | 2015-01-27 09:57:48 -0800 | [diff] [blame] | 189 | // of a hypothetical variable v as if v was declared via var v = x. |
Ian Lance Taylor | f49a757 | 2016-01-26 17:23:33 -0800 | [diff] [blame] | 190 | // It is the largest value m such that the address of v is always zero mod m. |
| 191 | // It is the same as the value returned by reflect.TypeOf(x).Align(). |
Ian Lance Taylor | 1023d63 | 2016-01-27 09:23:48 -0800 | [diff] [blame] | 192 | // As a special case, if a variable s is of struct type and f is a field |
| 193 | // within that struct, then Alignof(s.f) will return the required alignment |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 194 | // of a field of that type within a struct. This case is the same as the |
Ian Lance Taylor | 1023d63 | 2016-01-27 09:23:48 -0800 | [diff] [blame] | 195 | // value returned by reflect.TypeOf(s.f).FieldAlign(). |
Robert Griesemer | 3a52338 | 2015-01-27 09:57:48 -0800 | [diff] [blame] | 196 | func Alignof(x ArbitraryType) uintptr |