desc_init.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. case protowire.VarintType:
  268. v, m := protowire.ConsumeVarint(b)
  269. b = b[m:]
  270. switch num {
  271. case genid.EnumDescriptorProto_Visibility_field_number:
  272. ed.L1.Visibility = int32(v)
  273. }
  274. default:
  275. m := protowire.ConsumeFieldValue(num, typ, b)
  276. b = b[m:]
  277. }
  278. }
  279. // Only construct enum value descriptors for top-level enums since
  280. // they are needed for registration.
  281. if pd != pf {
  282. return
  283. }
  284. ed.L1.eagerValues = true
  285. ed.L2 = new(EnumL2)
  286. ed.L2.Values.List = make([]EnumValue, numValues)
  287. for i := 0; len(b) > 0; {
  288. num, typ, n := protowire.ConsumeTag(b)
  289. b = b[n:]
  290. switch typ {
  291. case protowire.BytesType:
  292. v, m := protowire.ConsumeBytes(b)
  293. b = b[m:]
  294. switch num {
  295. case genid.EnumDescriptorProto_Value_field_number:
  296. ed.L2.Values.List[i].unmarshalFull(v, sb, pf, ed, i)
  297. i++
  298. }
  299. default:
  300. m := protowire.ConsumeFieldValue(num, typ, b)
  301. b = b[m:]
  302. }
  303. }
  304. }
  305. func (md *Message) unmarshalSeed(b []byte, sb *strs.Builder, pf *File, pd protoreflect.Descriptor, i int) {
  306. md.L0.ParentFile = pf
  307. md.L0.Parent = pd
  308. md.L0.Index = i
  309. md.L1.EditionFeatures = featuresFromParentDesc(md.Parent())
  310. var prevField protoreflect.FieldNumber
  311. var numEnums, numMessages, numExtensions int
  312. var posEnums, posMessages, posExtensions int
  313. b0 := b
  314. for len(b) > 0 {
  315. num, typ, n := protowire.ConsumeTag(b)
  316. b = b[n:]
  317. switch typ {
  318. case protowire.BytesType:
  319. v, m := protowire.ConsumeBytes(b)
  320. b = b[m:]
  321. switch num {
  322. case genid.DescriptorProto_Name_field_number:
  323. md.L0.FullName = appendFullName(sb, pd.FullName(), v)
  324. case genid.DescriptorProto_EnumType_field_number:
  325. if prevField != genid.DescriptorProto_EnumType_field_number {
  326. if numEnums > 0 {
  327. panic("non-contiguous repeated field")
  328. }
  329. posEnums = len(b0) - len(b) - n - m
  330. }
  331. numEnums++
  332. case genid.DescriptorProto_NestedType_field_number:
  333. if prevField != genid.DescriptorProto_NestedType_field_number {
  334. if numMessages > 0 {
  335. panic("non-contiguous repeated field")
  336. }
  337. posMessages = len(b0) - len(b) - n - m
  338. }
  339. numMessages++
  340. case genid.DescriptorProto_Extension_field_number:
  341. if prevField != genid.DescriptorProto_Extension_field_number {
  342. if numExtensions > 0 {
  343. panic("non-contiguous repeated field")
  344. }
  345. posExtensions = len(b0) - len(b) - n - m
  346. }
  347. numExtensions++
  348. case genid.DescriptorProto_Options_field_number:
  349. md.unmarshalSeedOptions(v)
  350. }
  351. prevField = num
  352. case protowire.VarintType:
  353. v, m := protowire.ConsumeVarint(b)
  354. b = b[m:]
  355. switch num {
  356. case genid.DescriptorProto_Visibility_field_number:
  357. md.L1.Visibility = int32(v)
  358. }
  359. default:
  360. m := protowire.ConsumeFieldValue(num, typ, b)
  361. b = b[m:]
  362. prevField = -1 // ignore known field numbers of unknown wire type
  363. }
  364. }
  365. // Must allocate all declarations before parsing each descriptor type
  366. // to ensure we handled all descriptors in "flattened ordering".
  367. if numEnums > 0 {
  368. md.L1.Enums.List = pf.allocEnums(numEnums)
  369. }
  370. if numMessages > 0 {
  371. md.L1.Messages.List = pf.allocMessages(numMessages)
  372. }
  373. if numExtensions > 0 {
  374. md.L1.Extensions.List = pf.allocExtensions(numExtensions)
  375. }
  376. if numEnums > 0 {
  377. b := b0[posEnums:]
  378. for i := range md.L1.Enums.List {
  379. _, n := protowire.ConsumeVarint(b)
  380. v, m := protowire.ConsumeBytes(b[n:])
  381. md.L1.Enums.List[i].unmarshalSeed(v, sb, pf, md, i)
  382. b = b[n+m:]
  383. }
  384. }
  385. if numMessages > 0 {
  386. b := b0[posMessages:]
  387. for i := range md.L1.Messages.List {
  388. _, n := protowire.ConsumeVarint(b)
  389. v, m := protowire.ConsumeBytes(b[n:])
  390. md.L1.Messages.List[i].unmarshalSeed(v, sb, pf, md, i)
  391. b = b[n+m:]
  392. }
  393. }
  394. if numExtensions > 0 {
  395. b := b0[posExtensions:]
  396. for i := range md.L1.Extensions.List {
  397. _, n := protowire.ConsumeVarint(b)
  398. v, m := protowire.ConsumeBytes(b[n:])
  399. md.L1.Extensions.List[i].unmarshalSeed(v, sb, pf, md, i)
  400. b = b[n+m:]
  401. }
  402. }
  403. }
  404. func (md *Message) unmarshalSeedOptions(b []byte) {
  405. for len(b) > 0 {
  406. num, typ, n := protowire.ConsumeTag(b)
  407. b = b[n:]
  408. switch typ {
  409. case protowire.VarintType:
  410. v, m := protowire.ConsumeVarint(b)
  411. b = b[m:]
  412. switch num {
  413. case genid.MessageOptions_MapEntry_field_number:
  414. md.L1.IsMapEntry = protowire.DecodeBool(v)
  415. case genid.MessageOptions_MessageSetWireFormat_field_number:
  416. md.L1.IsMessageSet = protowire.DecodeBool(v)
  417. }
  418. case protowire.BytesType:
  419. v, m := protowire.ConsumeBytes(b)
  420. b = b[m:]
  421. switch num {
  422. case genid.MessageOptions_Features_field_number:
  423. md.L1.EditionFeatures = unmarshalFeatureSet(v, md.L1.EditionFeatures)
  424. }
  425. default:
  426. m := protowire.ConsumeFieldValue(num, typ, b)
  427. b = b[m:]
  428. }
  429. }
  430. }
  431. func (xd *Extension) unmarshalSeed(b []byte, sb *strs.Builder, pf *File, pd protoreflect.Descriptor, i int) {
  432. xd.L0.ParentFile = pf
  433. xd.L0.Parent = pd
  434. xd.L0.Index = i
  435. xd.L1.EditionFeatures = featuresFromParentDesc(pd)
  436. for len(b) > 0 {
  437. num, typ, n := protowire.ConsumeTag(b)
  438. b = b[n:]
  439. switch typ {
  440. case protowire.VarintType:
  441. v, m := protowire.ConsumeVarint(b)
  442. b = b[m:]
  443. switch num {
  444. case genid.FieldDescriptorProto_Number_field_number:
  445. xd.L1.Number = protoreflect.FieldNumber(v)
  446. case genid.FieldDescriptorProto_Label_field_number:
  447. xd.L1.Cardinality = protoreflect.Cardinality(v)
  448. case genid.FieldDescriptorProto_Type_field_number:
  449. xd.L1.Kind = protoreflect.Kind(v)
  450. }
  451. case protowire.BytesType:
  452. v, m := protowire.ConsumeBytes(b)
  453. b = b[m:]
  454. switch num {
  455. case genid.FieldDescriptorProto_Name_field_number:
  456. xd.L0.FullName = appendFullName(sb, pd.FullName(), v)
  457. case genid.FieldDescriptorProto_Extendee_field_number:
  458. xd.L1.Extendee = PlaceholderMessage(makeFullName(sb, v))
  459. case genid.FieldDescriptorProto_Options_field_number:
  460. xd.unmarshalOptions(v)
  461. }
  462. default:
  463. m := protowire.ConsumeFieldValue(num, typ, b)
  464. b = b[m:]
  465. }
  466. }
  467. if xd.L1.Kind == protoreflect.MessageKind && xd.L1.EditionFeatures.IsDelimitedEncoded {
  468. xd.L1.Kind = protoreflect.GroupKind
  469. }
  470. }
  471. func (xd *Extension) unmarshalOptions(b []byte) {
  472. for len(b) > 0 {
  473. num, typ, n := protowire.ConsumeTag(b)
  474. b = b[n:]
  475. switch typ {
  476. case protowire.VarintType:
  477. v, m := protowire.ConsumeVarint(b)
  478. b = b[m:]
  479. switch num {
  480. case genid.FieldOptions_Packed_field_number:
  481. xd.L1.EditionFeatures.IsPacked = protowire.DecodeBool(v)
  482. case genid.FieldOptions_Lazy_field_number:
  483. xd.L1.IsLazy = protowire.DecodeBool(v)
  484. }
  485. case protowire.BytesType:
  486. v, m := protowire.ConsumeBytes(b)
  487. b = b[m:]
  488. switch num {
  489. case genid.FieldOptions_Features_field_number:
  490. xd.L1.EditionFeatures = unmarshalFeatureSet(v, xd.L1.EditionFeatures)
  491. }
  492. default:
  493. m := protowire.ConsumeFieldValue(num, typ, b)
  494. b = b[m:]
  495. }
  496. }
  497. }
  498. func (sd *Service) unmarshalSeed(b []byte, sb *strs.Builder, pf *File, pd protoreflect.Descriptor, i int) {
  499. sd.L0.ParentFile = pf
  500. sd.L0.Parent = pd
  501. sd.L0.Index = i
  502. for len(b) > 0 {
  503. num, typ, n := protowire.ConsumeTag(b)
  504. b = b[n:]
  505. switch typ {
  506. case protowire.BytesType:
  507. v, m := protowire.ConsumeBytes(b)
  508. b = b[m:]
  509. switch num {
  510. case genid.ServiceDescriptorProto_Name_field_number:
  511. sd.L0.FullName = appendFullName(sb, pd.FullName(), v)
  512. }
  513. default:
  514. m := protowire.ConsumeFieldValue(num, typ, b)
  515. b = b[m:]
  516. }
  517. }
  518. }
  519. var nameBuilderPool = sync.Pool{
  520. New: func() any { return new(strs.Builder) },
  521. }
  522. func getBuilder() *strs.Builder {
  523. return nameBuilderPool.Get().(*strs.Builder)
  524. }
  525. func putBuilder(b *strs.Builder) {
  526. nameBuilderPool.Put(b)
  527. }
  528. // makeFullName converts b to a protoreflect.FullName,
  529. // where b must start with a leading dot.
  530. func makeFullName(sb *strs.Builder, b []byte) protoreflect.FullName {
  531. if len(b) == 0 || b[0] != '.' {
  532. panic("name reference must be fully qualified")
  533. }
  534. return protoreflect.FullName(sb.MakeString(b[1:]))
  535. }
  536. func appendFullName(sb *strs.Builder, prefix protoreflect.FullName, suffix []byte) protoreflect.FullName {
  537. return sb.AppendFullName(prefix, protoreflect.Name(strs.UnsafeString(suffix)))
  538. }