Why TokuDB does not use the ‘uint3korr’ function

The ‘uint3korr’ function inside of the mysqld server extracts a 3 byte unsigned integer from a memory buffer. One use is for ‘mediumint’ columns which encode their value in 3 bytes. MySQL 5.6 and MariaDB 10.0 claims to have optimized this function for x86 and x86_64 processors. There is a big comment that says:

The ‘uint3korr’ optimization may be fast, but it is not valgrind safe. Here is an example where valgrind detects TokuDB reading beyond the end of a buffer when it uses the ‘uint3korr’ function.

It is OK to read beyond the end of a malloc’ed buffer since the ‘uint3korr’ function throws away the extra byte. But what if the extra byte is not mapped into mysqld’s address space? If this is the case, then mysqld will crash.

Since ‘uint3korr’ is not safe unless one does a bunch of memory accounting, TokuDB no longer uses the ‘uint3korr’ function.

The code generated for the following test program

consists of a 32 bit mov instruction followed by a 24 bit mask instruction

which demonstrates the problem at the instruction level for x86 processors.

Share this post

Leave a Reply