protobuf-php / protobuf

PHP implementation of Google's Protocol Buffers
MIT License
236 stars 29 forks source link

Failed to parse binary descriptor - inside GPBMetadata internalAddGeneratedFile #25

Closed tomekit closed 1 year ago

tomekit commented 1 year ago

I've initially created this issue here (https://github.com/protocolbuffers/protobuf/issues/13540), but now realized that it's most likely PHP Protobuf related, so I am copying it over.

What did you do?

For a madmin.proto definition I've generated PHP files using protoc and grpc_php_plugin. I've then tried to use the generated PHP files and send a GRPC request. This fails inside generated GPBMetadata class with: Failed to parse binary descriptor when internalAddGeneratedFile is called to parse some generated binary code.

madmin.proto

syntax = "proto3";

option go_package = "io.kapsa.drive/madmin-grpc/proto/proto";

package proto;

service MinioAdmin {
  rpc AddUser (AddUserRequest) returns (AddUserReply) {}
}

message AddUserRequest {
  string endpoint = 1;
  string accessKeyId = 2;
  string secretAccessKey = 3;
  string userAccessKeyId = 4;
  string userSecretAccessKey = 5;
}

message AddUserReply {
  int32 httpStatusCode = 1;
  string error = 2;
}

I've generated PHP files:

protoc --proto_path=docker/madmin-grpc/proto --php_out=src/Protobuf --grpc_out=src/Protobuf docker/madmin-grpc/proto/madmin.proto --plugin=protoc-gen-grpc=/home/tomek/.local/bin/grpc_php_plugin

and then tried to send a request:

$client = new MinioAdminClient('localhost:50051', [
    'credentials' => ChannelCredentials::createInsecure(),
]);

$request = new AddUserRequest();

Issue Code fails when: new AddUserRequest() constructor calls the: Madmin::initOnce(); and then subsequently: $pool->internalAddGeneratedFile which fails with:

PHP Fatal error:  Failed to parse binary descriptor
 in /app/src/Protobuf/GPBMetadata/Madmin.php on line 17
PHP Stack trace:
PHP   1. {main}() /app/src/bin/testGrpc.php:0
PHP   2. App\Protobuf\Proto\AddUserRequest->__construct($data = *uninitialized*) /app/src/bin/testGrpc.php:15
PHP   3. App\Protobuf\GPBMetadata\Madmin::initOnce() /app/src/Protobuf/Proto/AddUserRequest.php:52
PHP   4. Google\Protobuf\DescriptorPool->internalAddGeneratedFile($data = '\n�\002\n\fmadmin.proto\022\005proto"�\001\n\016AddUserRequest\022\020\n\bendpoint\030\001 \001(\t\022\023\n\vaccessKeyId\030\002 \001(\t\022\027\n\017secretAccessKey\030\003 \001(\t\022\027\n\017userAccessKeyId\030\004 \001(\t\022\033\n\023userSecretAccessKey\030\005 \001(\t"5\n\fAddUserReply\022\026\n\016httpStatusCode\030\001 \001(\005\022\n\005error\030\002 \001(\t2E\n\nMinioAdmin\0227\n\aAddUser\022\025.proto.AddUserRequest\032\023.proto.AddUserReply"\000BV\n\032io.kapsa.drive.madmin-grpcB\016MinioAdminGrpcP\001Z&io.kapsa.drive/madmin-grpc/proto/protob\006proto3', $data_len = TRUE) /app/src/Protobuf/GPBMetadata/Madmin.php:17

Anything else we should know about your project / environment

$ php -v
PHP 8.2.7 (cli) (built: Jul  4 2023 14:32:22) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.7, Copyright (c) Zend Technologies
    with Xdebug v3.2.2, Copyright (c) 2002-2023, by Derick Rethans

$ php -r 'print php_uname();'
Linux 7b52f1e55f91 6.2.0-26-generic #26-Ubuntu SMP PREEMPT_DYNAMIC Mon Jul 10 23:39:54 UTC 2023 x86_64root@7b52f1e55f91:/app# ^C

$ php -r 'print phpversion("protobuf");'
3.24.0root

$ php -r 'print phpversion("grpc");'
1.56.0root

Since Base64 encoded Madmin.php

PD9waHAKIyBHZW5lcmF0ZWQgYnkgdGhlIHByb3RvY29sIGJ1ZmZlciBjb21waWxlci4gIERPIE5P
VCBFRElUIQojIHNvdXJjZTogbWFkbWluLnByb3RvCgpuYW1lc3BhY2UgQXBwXFByb3RvYnVmXEdQ
Qk1ldGFkYXRhOwoKY2xhc3MgTWFkbWluCnsKICAgIHB1YmxpYyBzdGF0aWMgJGlzX2luaXRpYWxp
emVkID0gZmFsc2U7CgogICAgcHVibGljIHN0YXRpYyBmdW5jdGlvbiBpbml0T25jZSgpIHsKICAg
ICAgICAkcG9vbCA9IFxHb29nbGVcUHJvdG9idWZcSW50ZXJuYWxcRGVzY3JpcHRvclBvb2w6Omdl
dEdlbmVyYXRlZFBvb2woKTsKCiAgICAgICAgaWYgKHN0YXRpYzo6JGlzX2luaXRpYWxpemVkID09
IHRydWUpIHsKICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAgICAgICAgJHBvb2wtPmludGVy
bmFsQWRkR2VuZXJhdGVkRmlsZSgKICAgICAgICAgICAgJwrvv70CCgxtYWRtaW4ucHJvdG8SBXBy
b3RvIu+/vQEKDkFkZFVzZXJSZXF1ZXN0EhAKCGVuZHBvaW50GAEgASgJEhMKC2FjY2Vzc0tleUlk
GAIgASgJEhcKD3NlY3JldEFjY2Vzc0tleRgDIAEoCRIXCg91c2VyQWNjZXNzS2V5SWQYBCABKAkS
GwoTdXNlclNlY3JldEFjY2Vzc0tleRgFIAEoCSI1CgxBZGRVc2VyUmVwbHkSFgoOaHR0cFN0YXR1
c0NvZGUYASABKAUSCgVlcnJvchgCIAEoCTJFCgpNaW5pb0FkbWluEjcKB0FkZFVzZXISFS5wcm90
by5BZGRVc2VyUmVxdWVzdBoTLnByb3RvLkFkZFVzZXJSZXBseSIAQlYKGmlvLmthcHNhLmRyaXZl
Lm1hZG1pbi1ncnBjQg5NaW5pb0FkbWluR3JwY1ABWiZpby5rYXBzYS5kcml2ZS9tYWRtaW4tZ3Jw
Yy9wcm90by9wcm90b2IGcHJvdG8zJwogICAgICAgICwgdHJ1ZSk7CgogICAgICAgIHN0YXRpYzo6
JGlzX2luaXRpYWxpemVkID0gdHJ1ZTsKICAgIH0KfQoK

Use e.g: echo "<pasteBase64Here>" | base64 -d > /tmp/Madmin.php

tomekit commented 1 year ago

The issue was that I've manually adjusted the namespaces of generated files to fit my composer.json settings. Once I've generated files and tweaked the composer.json to use the generated structure and namespaces, issue no longer appears.