loader_latest.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //go:build go1.16 && !go1.22
  2. // +build go1.16,!go1.22
  3. /*
  4. * Copyright 2021 ByteDance Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package loader
  19. import (
  20. `github.com/bytedance/sonic/internal/rt`
  21. )
  22. // LoadFuncs loads only one function as module, and returns the function pointer
  23. // - text: machine code
  24. // - funcName: function name
  25. // - frameSize: stack frame size.
  26. // - argSize: argument total size (in bytes)
  27. // - argPtrs: indicates if a slot (8 Bytes) of arguments memory stores pointer, from low to high
  28. // - localPtrs: indicates if a slot (8 Bytes) of local variants memory stores pointer, from low to high
  29. //
  30. // WARN:
  31. // - the function MUST has fixed SP offset equaling to this, otherwise it go.gentraceback will fail
  32. // - the function MUST has only one stack map for all arguments and local variants
  33. func (self Loader) LoadOne(text []byte, funcName string, frameSize int, argSize int, argPtrs []bool, localPtrs []bool) Function {
  34. size := uint32(len(text))
  35. fn := Func{
  36. Name: funcName,
  37. TextSize: size,
  38. ArgsSize: int32(argSize),
  39. }
  40. // NOTICE: suppose the function has fixed SP offset equaling to frameSize, thus make only one pcsp pair
  41. fn.Pcsp = &Pcdata{
  42. {PC: size, Val: int32(frameSize)},
  43. }
  44. if self.NoPreempt {
  45. fn.PcUnsafePoint = &Pcdata{
  46. {PC: size, Val: PCDATA_UnsafePointUnsafe},
  47. }
  48. } else {
  49. fn.PcUnsafePoint = &Pcdata{
  50. {PC: size, Val: PCDATA_UnsafePointSafe},
  51. }
  52. }
  53. // NOTICE: suppose the function has only one stack map at index 0
  54. fn.PcStackMapIndex = &Pcdata{
  55. {PC: size, Val: 0},
  56. }
  57. if argPtrs != nil {
  58. args := rt.StackMapBuilder{}
  59. for _, b := range argPtrs {
  60. args.AddField(b)
  61. }
  62. fn.ArgsPointerMaps = args.Build()
  63. }
  64. if localPtrs != nil {
  65. locals := rt .StackMapBuilder{}
  66. for _, b := range localPtrs {
  67. locals.AddField(b)
  68. }
  69. fn.LocalsPointerMaps = locals.Build()
  70. }
  71. out := Load(text, []Func{fn}, self.Name + funcName, []string{self.File})
  72. return out[0]
  73. }
  74. // Load loads given machine codes and corresponding function information into go moduledata
  75. // and returns runnable function pointer
  76. // WARN: this API is experimental, use it carefully
  77. func Load(text []byte, funcs []Func, modulename string, filenames []string) (out []Function) {
  78. ids := make([]string, len(funcs))
  79. for i, f := range funcs {
  80. ids[i] = f.Name
  81. }
  82. // generate module data and allocate memory address
  83. mod := makeModuledata(modulename, filenames, &funcs, text)
  84. // verify and register the new module
  85. moduledataverify1(mod)
  86. registerModule(mod)
  87. //
  88. // encapsulate function address
  89. out = make([]Function, len(funcs))
  90. for i, s := range ids {
  91. for _, f := range funcs {
  92. if f.Name == s {
  93. m := uintptr(mod.text + uintptr(f.EntryOff))
  94. out[i] = Function(&m)
  95. }
  96. }
  97. }
  98. return
  99. }