[Ruby] Fix: (SIGSEGV) gRPC-Ruby issue on Windows. memory alloc infinite recursion/run out of memory #8195
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
On a 32-bit system, sizeof(mem_block) is 8, _upb_arena_alignup(sizeof(mem_block)) is 16.
In https://github.com/protocolbuffers/protobuf/blob/master/ruby/ext/google/protobuf_c/upb.c#L2361, alloc block size is 16384 + 8 bytes,
In https://github.com/protocolbuffers/protobuf/blob/master/ruby/ext/google/protobuf_c/upb.c#L2352, a->head.ptr moves forward 16 bytes.
So in https://github.com/protocolbuffers/protobuf/blob/master/ruby/ext/google/protobuf_c/upb.h#L380, if() is always false, and keeps calling malloc until infinite recursion(O0)/run out of memory(O3)
(gdb) f 16
#16 0x089fb8d0 in upb_arena_allocblock (a=0xa6bf14c, size=16384) at ../../../../ext/google/protobuf_c/upb.c:2361
2361 ../../../../ext/google/protobuf_c/upb.c: No such file or directory.
(gdb) p sizeof(mem_block)
$1 = 8
(gdb) p _upb_arena_alignup(sizeof(mem_block))
$2 = 16
(gdb) p UPB_MAX(size, a->next_block_size)
$3 = 16384
(gdb) f 18
#18 0x089f4d45 in upb_arena_malloc (a=0xa6bf14c, size=16384) at ../../../../ext/google/protobuf_c/upb.h:385
385 ../../../../ext/google/protobuf_c/upb.h: No such file or directory.
(gdb) p h->end - h->ptr
$4 = 16376
(gdb) p size
$5 = 16384
reference: gRPC-Ruby internal issue number b/171421508.
Issue description:
Segfault observed occasionally on Windows, from a cloud logging agent called google-fluentd, which captures logs from different processes on a VM and uses gRPC-Ruby to publish those logs message to a logging service.
How to reproduce:
0.Start a windows VM, e.x. windows 2016 datacenter. RDP into the VM then install the logging agent. Detailed Instruction here: https://cloud.google.com/logging/docs/agent/installation#joint-install
Then Worker 0 finished with signal SIGSEGV. For any large( over 300kb) log file, SIGSEGV shows by 20% chance.