Skip to content

Commit 03bca7d

Browse files
author
Elżbieta Babij
committed
Bug #26043363 - CRASH INNMEM::TABLE::INSERT AT INNMEM/SRC/TABLE.CC:85
Ftruncate isn't reserving disk space for file with only 0s inside, so following writes to mmaped pages are allocating new disk pages and on out of space the SIGBUS is being thrown. Function my_chsize uses ftruncate, we want to use posix_fallocate, hence implementation of my_fallocator based on my_chsize. RB#16345 Reviewed by Sunny Bains and Thayumanavar X Sachithanantha
1 parent e6ac2ce commit 03bca7d

File tree

4 files changed

+125
-8
lines changed

4 files changed

+125
-8
lines changed

include/my_sys.h

+1
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ extern FILE *my_freopen(const char *path, const char *mode, FILE *stream);
651651
extern int my_fclose(FILE *fd,myf MyFlags);
652652
extern File my_fileno(FILE *fd);
653653
extern int my_chsize(File fd,my_off_t newlength, int filler, myf MyFlags);
654+
extern int my_fallocator(File fd, my_off_t newlength, int filler, myf MyFlags);
654655
extern void thr_set_sync_wait_callback(void (*before_sync)(void),
655656
void (*after_sync)(void));
656657
extern int my_sync(File fd, myf my_flags);

mysys/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ SET(MYSYS_SOURCES
5959
my_delete.cc
6060
my_div.cc
6161
my_error.cc
62+
my_fallocator.cc
6263
my_file.cc
6364
my_fopen.cc
6465
my_fstream.cc

mysys/my_fallocator.cc

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
2+
3+
This program is free software; you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License as published by
5+
the Free Software Foundation; version 2 of the License.
6+
7+
This program is distributed in the hope that it will be useful,
8+
but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
GNU General Public License for more details.
11+
12+
You should have received a copy of the GNU General Public License
13+
along with this program; if not, write to the Free Software
14+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15+
16+
/**
17+
@file mysys/my_fallocator.cc
18+
*/
19+
20+
#include "my_config.h"
21+
22+
#include <errno.h>
23+
#ifdef HAVE_POSIX_FALLOCATE
24+
#include <fcntl.h>
25+
#endif
26+
#include <string.h>
27+
#include <sys/types.h>
28+
#ifdef HAVE_UNISTD_H
29+
#include <unistd.h>
30+
#endif
31+
32+
#include "my_dbug.h"
33+
#include "my_inttypes.h"
34+
#include "my_io.h"
35+
#include "my_sys.h"
36+
#include "my_thread_local.h"
37+
#include "mysys_err.h"
38+
#ifdef _WIN32
39+
#include "mysys_priv.h"
40+
#endif
41+
42+
/** Change size of the specified file. Forces the OS to reserve disk space for
43+
the file, even when called to fill with zeros. he function also changes the
44+
file position. Usually it points to the end of the file after execution.
45+
46+
@note Implementation based on, and mostly copied from, my_chsize. But instead
47+
of ftruncate uses posix_fallocate. This implementation was needed because of
48+
allocation in temptable. Probably this implementation should replace my_chsize
49+
implementation.
50+
51+
@param[in] fd File descriptor
52+
@param[in] new_length New file size
53+
@param[in] filler Fill up all bytes after new_length with this character
54+
@param[in] MyFlags Flags
55+
@return 0 if OK, 1 otherwise
56+
*/
57+
int my_fallocator(File fd, my_off_t newlength, int filler, myf MyFlags)
58+
{
59+
my_off_t oldsize;
60+
uchar buff[IO_SIZE];
61+
DBUG_ENTER("my_fallocator");
62+
DBUG_PRINT("my",("fd: %d length: %lu MyFlags: %d", fd, (ulong) newlength,
63+
MyFlags));
64+
65+
if ((oldsize = my_seek(fd, 0L, MY_SEEK_END, MYF(MY_WME + MY_FAE)))
66+
== newlength)
67+
DBUG_RETURN(0);
68+
69+
DBUG_PRINT("info",("old_size: %ld", (ulong) oldsize));
70+
71+
if (oldsize > newlength)
72+
{
73+
#ifdef _WIN32
74+
if (my_win_chsize(fd, newlength))
75+
{
76+
set_my_errno(errno);
77+
goto err;
78+
}
79+
DBUG_RETURN(0);
80+
#elif defined(HAVE_POSIX_FALLOCATE)
81+
if (posix_fallocate(fd, 0, (off_t) newlength) != 0)
82+
{
83+
set_my_errno(errno);
84+
goto err;
85+
}
86+
DBUG_RETURN(0);
87+
#else
88+
/*
89+
Fill space between requested length and true length with 'filler'
90+
We should never come here on any modern machine
91+
*/
92+
if (my_seek(fd, newlength, MY_SEEK_SET, MYF(MY_WME + MY_FAE))
93+
== MY_FILEPOS_ERROR)
94+
{
95+
goto err;
96+
}
97+
std::swap(newlength, oldsize);
98+
#endif //WIN32
99+
}
100+
101+
/* Full file with 'filler' until it's as big as requested */
102+
memset(buff, filler, IO_SIZE);
103+
while (newlength - oldsize > IO_SIZE)
104+
{
105+
if (my_write(fd, buff, IO_SIZE, MYF(MY_NABP)))
106+
goto err;
107+
oldsize+= IO_SIZE;
108+
}
109+
if (my_write(fd,buff,(size_t) (newlength - oldsize), MYF(MY_NABP)))
110+
goto err;
111+
DBUG_RETURN(0);
112+
113+
err:
114+
DBUG_PRINT("error", ("errno: %d", errno));
115+
if (MyFlags & MY_WME)
116+
{
117+
char errbuf[MYSYS_STRERROR_SIZE];
118+
my_error(EE_CANT_CHSIZE, MYF(0),
119+
my_errno(), my_strerror(errbuf, sizeof(errbuf), my_errno()));
120+
}
121+
DBUG_RETURN(1);
122+
} /* my_fallocator */

storage/temptable/include/temptable/allocator.h

+1-8
Original file line numberDiff line numberDiff line change
@@ -738,19 +738,12 @@ inline void* Allocator<T>::mem_fetch_from_disk(size_t bytes) {
738738
return nullptr;
739739
}
740740

741-
#ifdef HAVE_FTRUNCATE
742-
if (ftruncate(f, bytes) != 0) {
743-
my_close(f, MYF(MY_WME));
744-
return nullptr;
745-
}
746-
#else
747741
/* This will write `bytes` 0x0 bytes to the file on disk. */
748-
if (my_chsize(f, bytes, 0x0, MYF(MY_WME)) != 0 ||
742+
if (my_fallocator(f, bytes, 0x0, MYF(MY_WME)) != 0 ||
749743
my_seek(f, 0, MY_SEEK_SET, MYF(MY_WME)) == MY_FILEPOS_ERROR) {
750744
my_close(f, MYF(MY_WME));
751745
return nullptr;
752746
}
753-
#endif
754747

755748
void* ptr = my_mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_SHARED, f, 0);
756749

0 commit comments

Comments
 (0)