2
* JIT detection for ClamAV bytecode.
4
* Copyright (C) 2010 Sourcefire, Inc.
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License version 2 as
10
* published by the Free Software Foundation.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23
#include "llvm/ADT/Triple.h"
24
#include "llvm/Support/raw_ostream.h"
26
#include "llvm/Support/Host.h"
27
#include "llvm/Support/DataTypes.h"
28
#include "llvm/Support/Memory.h"
30
#include "llvm/System/Host.h"
31
#include "llvm/System/DataTypes.h"
32
#include "llvm/System/Memory.h"
35
#include "llvm/Config/config.h"
38
#include "bytecode_detect.h"
43
static void warn_assumptions(const char *msg, int a, int b)
45
errs() << "LibClamAV Warning: libclamav and llvm make inconsistent "
46
<< "assumptions about " << msg << ": " <<
47
a << " and " << b << "."
48
<< "Please report to http://bugs.clamav.net\n";
51
#define CASE_OS(theos, compat) case Triple::theos:\
52
env->os = llvm_os_##theos;\
53
if (env->os_category != compat)\
54
warn_assumptions("Operating System", env->os_category, Triple::theos);\
57
void cli_detect_env_jit(struct cli_environment *env)
59
std::string host_triple = sys::getHostTriple();
60
INIT_STRFIELD(env->triple, host_triple.c_str());
62
std::string cpu = sys::getHostCPUName();
63
INIT_STRFIELD(env->cpu, cpu.c_str());
65
if (env->big_endian != (int)sys::isBigEndianHost()) {
66
warn_assumptions("host endianness", env->big_endian, sys::isBigEndianHost());
67
env->big_endian = sys::isBigEndianHost();
71
env->cpp_version = MAKE_VERSION(0, __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
72
#elif defined (__INTEL_COMPILER)
73
env->cpp_version = __INTEL_COMPILER;
74
#elif defined (_MSC_VER)
75
env->cpp_version = _MSC_VER;
78
Triple triple(host_triple);
81
enum Triple::ArchType arch = triple.getArch();
83
bool conflicts = false;
87
if (env->arch != earch) conflicts = true;
91
if (env->arch != earch &&
92
env->arch != arch_ppc64) conflicts = true;
96
// ppc64 is fixed up by llvm
97
if (env->arch != arch_ppc32 &&
98
env->arch != arch_ppc64) conflicts = true;
102
if (env->arch != earch) {
104
if (env->arch != arch_x86_64)
110
if (env->arch != earch) {
111
/* bb #2153, bb #2214 */
112
/* configure can't detect -m32, so it thinks we are x86_64, when
113
* in fact we are i386 only.
114
* LLVM correctly detects which one it is using preprocessor
115
* macros, so don't warn here, startup.cbc will just have to
116
* rely on the LLVM provided info, and not the configure
118
if (env->arch != arch_i386)
123
earch = arch_unknown;
126
#ifndef AC_APPLE_UNIVERSAL_BUILD
128
warn_assumptions("CPU architecture", env->arch, earch);
130
if (earch != arch_unknown)
134
Triple::OSType os = triple.getOS();
136
case Triple::UnknownOS:
137
env->os = llvm_os_UnknownOS;
139
CASE_OS(AuroraUX, os_solaris);
140
CASE_OS(Cygwin, os_win32);
141
CASE_OS(Darwin, os_darwin);
142
CASE_OS(DragonFly, os_bsd);
143
CASE_OS(FreeBSD, os_bsd);
144
CASE_OS(Linux, os_linux);
145
CASE_OS(Lv2, os_unknown);
146
CASE_OS(MinGW32, os_win32);
148
CASE_OS(MinGW64, os_win64);
150
CASE_OS(NetBSD, os_bsd);
151
CASE_OS(OpenBSD, os_bsd);
152
CASE_OS(Psp, os_unknown);
153
CASE_OS(Solaris, os_solaris);
155
env->os = llvm_os_Win32;
156
if (env->os_category != os_win32 &&
157
env->os_category != os_win64)
158
warn_assumptions("Operating System", env->os_category, Triple::Win32);
160
CASE_OS(Haiku, os_unknown);
161
CASE_OS(Minix, os_unknown);
166
sys::MemoryBlock B = sys::Memory::AllocateRWX(4096, NULL, &ErrMsg);
168
errs() << "LibClamAV Warning: RWX mapping denied: " << ErrMsg << "\n";
170
env->os_features |= 1 << feature_map_rwx;
171
sys::Memory::ReleaseRWX(B);