File indexing completed on 2024-04-28 16:08:40

0001 /***************************************************************************
0002  *   Copyright (C) 2013 by Linuxstopmotion contributors;                   *
0003  *   see the AUTHORS file for details.                                     *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 
0021 #include <dlfcn.h>
0022 
0023 #include "oomtestutil.h"
0024 
0025 namespace {
0026 typedef void setMallocsUntilFailure_t(int);
0027 typedef long mallocsSoFar_t(void);
0028 typedef void init_t(void);
0029 typedef void wrapFileSystem_t(MockableFileSystem*);
0030 
0031 init_t* init;
0032 setMallocsUntilFailure_t* smuf;
0033 mallocsSoFar_t* msf;
0034 wrapFileSystem_t* wfs;
0035 }
0036 
0037 MockableFileSystem::~MockableFileSystem() {
0038 }
0039 
0040 int loadOomTestUtil() {
0041     // Using dlopen might cause a malloc, which would not work when we have not
0042     // yet wired up the real malloc by calling Init, so we have to use
0043     // RTLD_DEFAULT.
0044     // RTLD_DEFAULT searches all the libraries in the order that they were
0045     // loaded in order to find the requested symbol. RTLD_NEXT begins the
0046     // search with the library after the one we are calling from.
0047     // RTLD_NEXT and RTLD_DEFAULT are only available with the GNU dl library;
0048     // standard C dl libraries do not have this functionality.
0049     if (!init)
0050         init = (init_t*)dlsym(RTLD_DEFAULT, "init");
0051     if (!smuf)
0052         smuf = (setMallocsUntilFailure_t*)dlsym(RTLD_DEFAULT,
0053                 "realSetMallocsUntilFailure");
0054     if (!msf)
0055         msf = (mallocsSoFar_t*)dlsym(RTLD_DEFAULT, "realMallocsSoFar");
0056     if (!wfs)
0057         wfs = (wrapFileSystem_t*)dlsym(RTLD_DEFAULT, "realWrapFileSystem");
0058     if (!init || !smuf || !msf || !wfs)
0059         return 0;
0060     init();
0061     return 1;
0062 }
0063 
0064 void setMallocsUntilFailure(int successes) {
0065     if (smuf)
0066         smuf(successes);
0067 }
0068 
0069 void cancelAnyMallocFailure() {
0070     if (smuf)
0071         smuf(-1);
0072 }
0073 
0074 long mallocsSoFar() {
0075     if (msf)
0076         return msf();
0077     return 0;
0078 }
0079 
0080 void wrapFileSystem(MockableFileSystem* mfs) {
0081     if (wfs)
0082         wfs(mfs);
0083 }