unsafe.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // unsafe.go is based on qtls/unsafe.go
  2. // https://github.com/quic-go/qtls-go1-20/blob/49f389c17d984e5b248f0a57cbae10dd4198a3bf/unsafe.go
  3. /* Copyright (c) 2009 The Go Authors. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above
  10. copyright notice, this list of conditions and the following disclaimer
  11. in the documentation and/or other materials provided with the
  12. distribution.
  13. * Neither the name of Google Inc. nor the names of its
  14. contributors may be used to endorse or promote products derived from
  15. this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. package tls
  29. import (
  30. "crypto/tls"
  31. "fmt"
  32. "reflect"
  33. "unsafe"
  34. )
  35. func init() {
  36. err := structsEqual(&tls.ConnectionState{}, &ConnectionState{})
  37. if err != nil {
  38. panic(fmt.Sprintf("tls: ConnectionState is not equal to tls.ConnectionState: %v", err))
  39. }
  40. }
  41. func UnsafeFromConnectionState(ss *ConnectionState) *tls.ConnectionState {
  42. return (*tls.ConnectionState)(unsafe.Pointer(ss))
  43. }
  44. func UnsafeToConnectionState(ss *tls.ConnectionState) *ConnectionState {
  45. return (*ConnectionState)(unsafe.Pointer(ss))
  46. }
  47. func structsEqual(a, b interface{}) error {
  48. return compare(reflect.TypeOf(a), reflect.TypeOf(b))
  49. }
  50. // compare compares two types and returns true if and only if
  51. // they can be casted to each other safely.
  52. // compare does not currently support Maps, Chan, UnsafePointer if reflect.DeepEqual fails.
  53. // Support for these types can be added if needed.
  54. // note that field names are still compared.
  55. func compare(a, b reflect.Type) error {
  56. if reflect.DeepEqual(a, b) {
  57. return nil
  58. }
  59. if isPrimitive(a) && isPrimitive(b) && a.Kind() == b.Kind() {
  60. return nil
  61. }
  62. if a.Kind() != b.Kind() {
  63. return fmt.Errorf("kind mismatch: %s vs %s", a.Kind().String(), b.Kind().String())
  64. }
  65. if a.Kind() == reflect.Pointer || a.Kind() == reflect.Slice {
  66. return compare(a.Elem(), b.Elem())
  67. }
  68. if a.Kind() == reflect.Func {
  69. if a.NumIn() != b.NumIn() || a.NumOut() != b.NumOut() {
  70. return fmt.Errorf(
  71. "function signature mismatch: different number of input or output parameters: NumIn: %d vs %d, NumOut: %d vs %d",
  72. a.NumIn(), b.NumIn(), a.NumOut(), b.NumOut(),
  73. )
  74. }
  75. for i_in := 0; i_in < a.NumIn(); i_in++ {
  76. err := compare(a.In(i_in), b.In(i_in))
  77. if err != nil {
  78. return fmt.Errorf("function %s input parameter mismatch at index %d: %v", a.Name(), i_in, err)
  79. }
  80. }
  81. for i_out := 0; i_out < a.NumOut(); i_out++ {
  82. err := compare(a.Out(i_out), b.Out(i_out))
  83. if err != nil {
  84. return fmt.Errorf("function %s output parameter mismatch at index %d: %v", a.Name(), i_out, err)
  85. }
  86. }
  87. return nil
  88. }
  89. if a.Kind() == reflect.Struct {
  90. if a.NumField() != b.NumField() {
  91. return fmt.Errorf("struct field count mismatch: %d vs %d", a.NumField(), b.NumField())
  92. }
  93. for i := 0; i < a.NumField(); i++ {
  94. fa := a.Field(i)
  95. fb := b.Field(i)
  96. if !reflect.DeepEqual(fa.Index, fb.Index) || fa.Name != fb.Name ||
  97. fa.Anonymous != fb.Anonymous || fa.Offset != fb.Offset {
  98. return fmt.Errorf("struct field mismatch at index %d: %+v vs %+v", i, fa, fb)
  99. }
  100. if !reflect.DeepEqual(fa.Type, fb.Type) {
  101. err := compare(fa.Type, fb.Type)
  102. if err != nil {
  103. return fmt.Errorf("struct %s field type mismatch at index %d with name %s: %v", a.Name(), i, fa.Name, err)
  104. }
  105. }
  106. }
  107. return nil
  108. }
  109. // TODO: add support for missing types
  110. return fmt.Errorf("unsupported type: %s for field %s", a.Kind().String(), a.Name())
  111. }
  112. // isPrimitive checks if a reflect.Type represents a primitive type
  113. func isPrimitive(t reflect.Type) bool {
  114. switch t.Kind() {
  115. case reflect.Bool,
  116. reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  117. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
  118. reflect.Float32, reflect.Float64,
  119. reflect.Complex64, reflect.Complex128,
  120. reflect.String:
  121. return true
  122. default:
  123. return false
  124. }
  125. }