desc_init.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. // Copyright 2019 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. package filedesc
  5. import (
  6. "fmt"
  7. "sync"
  8. "google.golang.org/protobuf/encoding/protowire"
  9. "google.golang.org/protobuf/internal/genid"
  10. "google.golang.org/protobuf/internal/strs"
  11. "google.golang.org/protobuf/reflect/protoreflect"
  12. )
  13. // fileRaw is a data struct used when initializing a file descriptor from
  14. // a raw FileDescriptorProto.
  15. type fileRaw struct {
  16. builder Builder
  17. allEnums []Enum
  18. allMessages []Message
  19. allExtensions []Extension
  20. allServices []Service
  21. }
  22. func newRawFile(db Builder) *File {
  23. fd := &File{fileRaw: fileRaw{builder: db}}
  24. fd.initDecls(db.NumEnums, db.NumMessages, db.NumExtensions, db.NumServices)
  25. fd.unmarshalSeed(db.RawDescriptor)
  26. // Extended message targets are eagerly resolved since registration
  27. // needs this information at program init time.
  28. for i := range fd.allExtensions {
  29. xd := &fd.allExtensions[i]
  30. xd.L1.Extendee = fd.resolveMessageDependency(xd.L1.Extendee, listExtTargets, int32(i))
  31. }
  32. fd.checkDecls()
  33. return fd
  34. }
  35. // initDecls pre-allocates slices for the exact number of enums, messages
  36. // (including map entries), extensions, and services declared in the proto file.
  37. // This is done to avoid regrowing the slice, which would change the address
  38. // for any previously seen declaration.
  39. //
  40. // The alloc methods "allocates" slices by pulling from the capacity.
  41. func (fd *File) initDecls(numEnums, numMessages, numExtensions, numServices int32) {
  42. fd.allEnums = make([]Enum, 0, numEnums)
  43. fd.allMessages = make([]Message, 0, numMessages)
  44. fd.allExtensions = make([]Extension, 0, numExtensions)
  45. fd.allServices = make([]Service, 0, numServices)
  46. }
  47. func (fd *File) allocEnums(n int) []Enum {
  48. total := len(fd.allEnums)
  49. es := fd.allEnums[total : total+n]
  50. fd.allEnums = fd.allEnums[:total+n]
  51. return es
  52. }
  53. func (fd *File) allocMessages(n int) []Message {
  54. total := len(fd.allMessages)
  55. ms := fd.allMessages[total : total+n]
  56. fd.allMessages = fd.allMessages[:total+n]
  57. return ms
  58. }
  59. func (fd *File) allocExtensions(n int) []Extension {
  60. total := len(fd.allExtensions)
  61. xs := fd.allExtensions[total : total+n]
  62. fd.allExtensions = fd.allExtensions[:total+n]
  63. return xs
  64. }
  65. func (fd *File) allocServices(n int) []Service {
  66. total := len(fd.allServices)
  67. xs := fd.allServices[total : total+n]
  68. fd.allServices = fd.allServices[:total+n]
  69. return xs
  70. }
  71. // checkDecls performs a sanity check that the expected number of expected
  72. // declarations matches the number that were found in the descriptor proto.
  73. func (fd *File) checkDecls() {
  74. switch {
  75. case len(fd.allEnums) != cap(fd.allEnums):
  76. case len(fd.allMessages) != cap(fd.allMessages):
  77. case len(fd.allExtensions) != cap(fd.allExtensions):
  78. case len(fd.allServices) != cap(fd.allServices):
  79. default:
  80. return
  81. }
  82. panic("mismatching cardinality")
  83. }
  84. func (fd *File) unmarshalSeed(b []byte) {
  85. sb := getBuilder()
  86. defer putBuilder(sb)
  87. var prevField protoreflect.FieldNumber
  88. var numEnums, numMessages, numExtensions, numServices int
  89. var posEnums, posMessages, posExtensions, posServices int
  90. var options []byte
  91. b0 := b
  92. for len(b) > 0 {
  93. num, typ, n := protowire.ConsumeTag(b)
  94. b = b[n:]
  95. switch typ {
  96. case protowire.BytesType:
  97. v, m := protowire.ConsumeBytes(b)
  98. b = b[m:]
  99. switch num {
  100. case genid.FileDescriptorProto_Syntax_field_number:
  101. switch string(v) {
  102. case "proto2":
  103. fd.L1.Syntax = protoreflect.Proto2
  104. fd.L1.Edition = EditionProto2
  105. case "proto3":
  106. fd.L1.Syntax = protoreflect.Proto3
  107. fd.L1.Edition = EditionProto3
  108. case "editions":
  109. fd.L1.Syntax = protoreflect.Editions
  110. default:
  111. panic("invalid syntax")
  112. }
  113. case genid.FileDescriptorProto_Name_field_number:
  114. fd.L1.Path = sb.MakeString(v)
  115. case genid.FileDescriptorProto_Package_field_number:
  116. fd.L1.Package = protoreflect.FullName(sb.MakeString(v))
  117. case genid.FileDescriptorProto_Options_field_number:
  118. options = v
  119. case genid.FileDescriptorProto_EnumType_field_number:
  120. if prevField != genid.FileDescriptorProto_EnumType_field_number {
  121. if numEnums > 0 {
  122. panic("non-contiguous repeated field")
  123. }
  124. posEnums = len(b0) - len(b) - n - m
  125. }
  126. numEnums++
  127. case genid.FileDescriptorProto_MessageType_field_number:
  128. if prevField != genid.FileDescriptorProto_MessageType_field_number {
  129. if numMessages > 0 {
  130. panic("non-contiguous repeated field")
  131. }
  132. posMessages = len(b0) - len(b) - n - m
  133. }
  134. numMessages++
  135. case genid.FileDescriptorProto_Extension_field_number:
  136. if prevField != genid.FileDescriptorProto_Extension_field_number {
  137. if numExtensions > 0 {
  138. panic("non-contiguous repeated field")
  139. }
  140. posExtensions = len(b0) - len(b) - n - m
  141. }
  142. numExtensions++
  143. case genid.FileDescriptorProto_Service_field_number:
  144. if prevField != genid.FileDescriptorProto_Service_field_number {
  145. if numServices > 0 {
  146. panic("non-contiguous repeated field")
  147. }
  148. posServices = len(b0) - len(b) - n - m
  149. }
  150. numServices++
  151. }
  152. prevField = num
  153. case protowire.VarintType:
  154. v, m := protowire.ConsumeVarint(b)
  155. b = b[m:]
  156. switch num {
  157. case genid.FileDescriptorProto_Edition_field_number:
  158. fd.L1.Edition = Edition(v)
  159. }
  160. default:
  161. m := protowire.ConsumeFieldValue(num, typ, b)
  162. b = b[m:]
  163. prevField = -1 // ignore known field numbers of unknown wire type
  164. }
  165. }
  166. // If syntax is missing, it is assumed to be proto2.
  167. if fd.L1.Syntax == 0 {
  168. fd.L1.Syntax = protoreflect.Proto2
  169. fd.L1.Edition = EditionProto2
  170. }
  171. fd.L1.EditionFeatures = getFeaturesFor(fd.L1.Edition)
  172. // Parse editions features from options if any
  173. if options != nil {
  174. fd.unmarshalSeedOptions(options)
  175. }
  176. // Must allocate all declarations before parsing each descriptor type
  177. // to ensure we handled all descriptors in "flattened ordering".
  178. if numEnums > 0 {
  179. fd.L1.Enums.List = fd.allocEnums(numEnums)
  180. }
  181. if numMessages > 0 {
  182. fd.L1.Messages.List = fd.allocMessages(numMessages)
  183. }
  184. if numExtensions > 0 {
  185. fd.L1.Extensions.List = fd.allocExtensions(numExtensions)
  186. }
  187. if numServices > 0 {
  188. fd.L1.Services.List = fd.allocServices(numServices)
  189. }
  190. if numEnums > 0 {
  191. b := b0[posEnums:]
  192. for i := range fd.L1.Enums.List {
  193. _, n := protowire.ConsumeVarint(b)
  194. v, m := protowire.ConsumeBytes(b[n:])
  195. fd.L1.Enums.List[i].unmarshalSeed(v, sb, fd, fd, i)
  196. b = b[n+m:]
  197. }
  198. }
  199. if numMessages > 0 {
  200. b := b0[posMessages:]
  201. for i := range fd.L1.Messages.List {
  202. _, n := protowire.ConsumeVarint(b)
  203. v, m := protowire.ConsumeBytes(b[n:])
  204. fd.L1.Messages.List[i].unmarshalSeed(v, sb, fd, fd, i)
  205. b = b[n+m:]
  206. }
  207. }
  208. if numExtensions > 0 {
  209. b := b0[posExtensions:]
  210. for i := range fd.L1.Extensions.List {
  211. _, n := protowire.ConsumeVarint(b)
  212. v, m := protowire.ConsumeBytes(b[n:])
  213. fd.L1.Extensions.List[i].unmarshalSeed(v, sb, fd, fd, i)
  214. b = b[n+m:]
  215. }
  216. }
  217. if numServices > 0 {
  218. b := b0[posServices:]
  219. for i := range fd.L1.Services.List {
  220. _, n := protowire.ConsumeVarint(b)
  221. v, m := protowire.ConsumeBytes(b[n:])
  222. fd.L1.Services.List[i].unmarshalSeed(v, sb, fd, fd, i)
  223. b = b[n+m:]
  224. }
  225. }
  226. }
  227. func (fd *File) unmarshalSeedOptions(b []byte) {
  228. for b := b; len(b) > 0; {
  229. num, typ, n := protowire.ConsumeTag(b)
  230. b = b[n:]
  231. switch typ {
  232. case protowire.BytesType:
  233. v, m := protowire.ConsumeBytes(b)
  234. b = b[m:]
  235. switch num {
  236. case genid.FileOptions_Features_field_number:
  237. if fd.Syntax() != protoreflect.Editions {
  238. panic(fmt.Sprintf("invalid descriptor: using edition features in a proto with syntax %s", fd.Syntax()))
  239. }
  240. fd.L1.EditionFeatures = unmarshalFeatureSet(v, fd.L1.EditionFeatures)
  241. }
  242. default:
  243. m := protowire.ConsumeFieldValue(num, typ, b)
  244. b = b[m:]
  245. }
  246. }
  247. }
  248. func (ed *Enum) unmarshalSeed(b []byte, sb *strs.Builder, pf *File, pd protoreflect.Descriptor, i int) {
  249. ed.L0.ParentFile = pf
  250. ed.L0.Parent = pd
  251. ed.L0.Index = i
  252. ed.L1.EditionFeatures = featuresFromParentDesc(ed.Parent())
  253. var numValues int
  254. for b := b; len(b) > 0; {
  255. num, typ, n := protowire.ConsumeTag(b)
  256. b = b[n:]
  257. switch typ {
  258. case protowire.BytesType:
  259. v, m := protowire.ConsumeBytes(b)
  260. b = b[m:]
  261. switch num {
  262. case genid.EnumDescriptorProto_Name_field_number:
  263. ed.L0.FullName = appendFullName(sb, pd.FullName(), v)
  264. case genid.EnumDescriptorProto_Value_field_number:
  265. numValues++
  266. }
  267. default:
  268. m := protowire.ConsumeFieldValue(num, typ, b)
  269. b = b[m:]
  270. }
  271. }
  272. // Only construct enum value descriptors for top-level enums since
  273. // they are needed for registration.
  274. if pd != pf {
  275. return
  276. }
  277. ed.L1.eagerValues = true
  278. ed.L2 = new(EnumL2)
  279. ed.L2.Values.List = make([]EnumValue, numValues)
  280. for i := 0; len(b) > 0; {
  281. num, typ, n := protowire.ConsumeTag(b)
  282. b = b[n:]
  283. switch typ {
  284. case protowire.BytesType:
  285. v, m := protowire.ConsumeBytes(b)
  286. b = b[m:]
  287. switch num {
  288. case genid.EnumDescriptorProto_Value_field_number:
  289. ed.L2.Values.List[i].unmarshalFull(v, sb, pf, ed, i)
  290. i++
  291. }
  292. default:
  293. m := protowire.ConsumeFieldValue(num, typ, b)
  294. b = b[m:]
  295. }
  296. }
  297. }
  298. func (md *Message) unmarshalSeed(b []byte, sb *strs.Builder, pf *File, pd protoreflect.Descriptor, i int) {
  299. md.L0.ParentFile = pf
  300. md.L0.Parent = pd
  301. md.L0.Index = i
  302. md.L1.EditionFeatures = featuresFromParentDesc(md.Parent())
  303. var prevField protoreflect.FieldNumber
  304. var numEnums, numMessages, numExtensions int
  305. var posEnums, posMessages, posExtensions int
  306. b0 := b
  307. for len(b) > 0 {
  308. num, typ, n := protowire.ConsumeTag(b)
  309. b = b[n:]
  310. switch typ {
  311. case protowire.BytesType:
  312. v, m := protowire.ConsumeBytes(b)
  313. b = b[m:]
  314. switch num {
  315. case genid.DescriptorProto_Name_field_number:
  316. md.L0.FullName = appendFullName(sb, pd.FullName(), v)
  317. case genid.DescriptorProto_EnumType_field_number:
  318. if prevField != genid.DescriptorProto_EnumType_field_number {
  319. if numEnums > 0 {
  320. panic("non-contiguous repeated field")
  321. }
  322. posEnums = len(b0) - len(b) - n - m
  323. }
  324. numEnums++
  325. case genid.DescriptorProto_NestedType_field_number:
  326. if prevField != genid.DescriptorProto_NestedType_field_number {
  327. if numMessages > 0 {
  328. panic("non-contiguous repeated field")
  329. }
  330. posMessages = len(b0) - len(b) - n - m
  331. }
  332. numMessages++
  333. case genid.DescriptorProto_Extension_field_number:
  334. if prevField != genid.DescriptorProto_Extension_field_number {
  335. if numExtensions > 0 {
  336. panic("non-contiguous repeated field")
  337. }
  338. posExtensions = len(b0) - len(b) - n - m
  339. }
  340. numExtensions++
  341. case genid.DescriptorProto_Options_field_number:
  342. md.unmarshalSeedOptions(v)
  343. }
  344. prevField = num
  345. default:
  346. m := protowire.ConsumeFieldValue(num, typ, b)
  347. b = b[m:]
  348. prevField = -1 // ignore known field numbers of unknown wire type
  349. }
  350. }
  351. // Must allocate all declarations before parsing each descriptor type
  352. // to ensure we handled all descriptors in "flattened ordering".
  353. if numEnums > 0 {
  354. md.L1.Enums.List = pf.allocEnums(numEnums)
  355. }
  356. if numMessages > 0 {
  357. md.L1.Messages.List = pf.allocMessages(numMessages)
  358. }
  359. if numExtensions > 0 {
  360. md.L1.Extensions.List = pf.allocExtensions(numExtensions)
  361. }
  362. if numEnums > 0 {
  363. b := b0[posEnums:]
  364. for i := range md.L1.Enums.List {
  365. _, n := protowire.ConsumeVarint(b)
  366. v, m := protowire.ConsumeBytes(b[n:])
  367. md.L1.Enums.List[i].unmarshalSeed(v, sb, pf, md, i)
  368. b = b[n+m:]
  369. }
  370. }
  371. if numMessages > 0 {
  372. b := b0[posMessages:]
  373. for i := range md.L1.Messages.List {
  374. _, n := protowire.ConsumeVarint(b)
  375. v, m := protowire.ConsumeBytes(b[n:])
  376. md.L1.Messages.List[i].unmarshalSeed(v, sb, pf, md, i)
  377. b = b[n+m:]
  378. }
  379. }
  380. if numExtensions > 0 {
  381. b := b0[posExtensions:]
  382. for i := range md.L1.Extensions.List {
  383. _, n := protowire.ConsumeVarint(b)
  384. v, m := protowire.ConsumeBytes(b[n:])
  385. md.L1.Extensions.List[i].unmarshalSeed(v, sb, pf, md, i)
  386. b = b[n+m:]
  387. }
  388. }
  389. }
  390. func (md *Message) unmarshalSeedOptions(b []byte) {
  391. for len(b) > 0 {
  392. num, typ, n := protowire.ConsumeTag(b)
  393. b = b[n:]
  394. switch typ {
  395. case protowire.VarintType:
  396. v, m := protowire.ConsumeVarint(b)
  397. b = b[m:]
  398. switch num {
  399. case genid.MessageOptions_MapEntry_field_number:
  400. md.L1.IsMapEntry = protowire.DecodeBool(v)
  401. case genid.MessageOptions_MessageSetWireFormat_field_number:
  402. md.L1.IsMessageSet = protowire.DecodeBool(v)
  403. }
  404. case protowire.BytesType:
  405. v, m := protowire.ConsumeBytes(b)
  406. b = b[m:]
  407. switch num {
  408. case genid.MessageOptions_Features_field_number:
  409. md.L1.EditionFeatures = unmarshalFeatureSet(v, md.L1.EditionFeatures)
  410. }
  411. default:
  412. m := protowire.ConsumeFieldValue(num, typ, b)
  413. b = b[m:]
  414. }
  415. }
  416. }
  417. func (xd *Extension) unmarshalSeed(b []byte, sb *strs.Builder, pf *File, pd protoreflect.Descriptor, i int) {
  418. xd.L0.ParentFile = pf
  419. xd.L0.Parent = pd
  420. xd.L0.Index = i
  421. xd.L1.EditionFeatures = featuresFromParentDesc(pd)
  422. for len(b) > 0 {
  423. num, typ, n := protowire.ConsumeTag(b)
  424. b = b[n:]
  425. switch typ {
  426. case protowire.VarintType:
  427. v, m := protowire.ConsumeVarint(b)
  428. b = b[m:]
  429. switch num {
  430. case genid.FieldDescriptorProto_Number_field_number:
  431. xd.L1.Number = protoreflect.FieldNumber(v)
  432. case genid.FieldDescriptorProto_Label_field_number:
  433. xd.L1.Cardinality = protoreflect.Cardinality(v)
  434. case genid.FieldDescriptorProto_Type_field_number:
  435. xd.L1.Kind = protoreflect.Kind(v)
  436. }
  437. case protowire.BytesType:
  438. v, m := protowire.ConsumeBytes(b)
  439. b = b[m:]
  440. switch num {
  441. case genid.FieldDescriptorProto_Name_field_number:
  442. xd.L0.FullName = appendFullName(sb, pd.FullName(), v)
  443. case genid.FieldDescriptorProto_Extendee_field_number:
  444. xd.L1.Extendee = PlaceholderMessage(makeFullName(sb, v))
  445. case genid.FieldDescriptorProto_Options_field_number:
  446. xd.unmarshalOptions(v)
  447. }
  448. default:
  449. m := protowire.ConsumeFieldValue(num, typ, b)
  450. b = b[m:]
  451. }
  452. }
  453. if xd.L1.Kind == protoreflect.MessageKind && xd.L1.EditionFeatures.IsDelimitedEncoded {
  454. xd.L1.Kind = protoreflect.GroupKind
  455. }
  456. }
  457. func (xd *Extension) unmarshalOptions(b []byte) {
  458. for len(b) > 0 {
  459. num, typ, n := protowire.ConsumeTag(b)
  460. b = b[n:]
  461. switch typ {
  462. case protowire.VarintType:
  463. v, m := protowire.ConsumeVarint(b)
  464. b = b[m:]
  465. switch num {
  466. case genid.FieldOptions_Packed_field_number:
  467. xd.L1.EditionFeatures.IsPacked = protowire.DecodeBool(v)
  468. }
  469. case protowire.BytesType:
  470. v, m := protowire.ConsumeBytes(b)
  471. b = b[m:]
  472. switch num {
  473. case genid.FieldOptions_Features_field_number:
  474. xd.L1.EditionFeatures = unmarshalFeatureSet(v, xd.L1.EditionFeatures)
  475. }
  476. default:
  477. m := protowire.ConsumeFieldValue(num, typ, b)
  478. b = b[m:]
  479. }
  480. }
  481. }
  482. func (sd *Service) unmarshalSeed(b []byte, sb *strs.Builder, pf *File, pd protoreflect.Descriptor, i int) {
  483. sd.L0.ParentFile = pf
  484. sd.L0.Parent = pd
  485. sd.L0.Index = i
  486. for len(b) > 0 {
  487. num, typ, n := protowire.ConsumeTag(b)
  488. b = b[n:]
  489. switch typ {
  490. case protowire.BytesType:
  491. v, m := protowire.ConsumeBytes(b)
  492. b = b[m:]
  493. switch num {
  494. case genid.ServiceDescriptorProto_Name_field_number:
  495. sd.L0.FullName = appendFullName(sb, pd.FullName(), v)
  496. }
  497. default:
  498. m := protowire.ConsumeFieldValue(num, typ, b)
  499. b = b[m:]
  500. }
  501. }
  502. }
  503. var nameBuilderPool = sync.Pool{
  504. New: func() any { return new(strs.Builder) },
  505. }
  506. func getBuilder() *strs.Builder {
  507. return nameBuilderPool.Get().(*strs.Builder)
  508. }
  509. func putBuilder(b *strs.Builder) {
  510. nameBuilderPool.Put(b)
  511. }
  512. // makeFullName converts b to a protoreflect.FullName,
  513. // where b must start with a leading dot.
  514. func makeFullName(sb *strs.Builder, b []byte) protoreflect.FullName {
  515. if len(b) == 0 || b[0] != '.' {
  516. panic("name reference must be fully qualified")
  517. }
  518. return protoreflect.FullName(sb.MakeString(b[1:]))
  519. }
  520. func appendFullName(sb *strs.Builder, prefix protoreflect.FullName, suffix []byte) protoreflect.FullName {
  521. return sb.AppendFullName(prefix, protoreflect.Name(strs.UnsafeString(suffix)))
  522. }