This PR contains #29415 + two more commits that add functional and fuzz tests.
Putting those in a separate PR not to burden the main one, since the tests are extensive:
code: 1036 insertions(+), 91 deletions(-)
tests: 548 insertions(+), 5 deletions(-)