~ubuntu-branches/ubuntu/saucy/clamav/saucy-backports

« back to all changes in this revision

Viewing changes to .pc/0004-Fix-FTBFS-with-LLVM-3.1-3.4.patch/libclamav/c++/detect.cpp

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman
  • Date: 2014-07-15 01:08:10 UTC
  • mfrom: (0.35.47 sid)
  • Revision ID: package-import@ubuntu.com-20140715010810-ru66ek4fun2iseba
Tags: 0.98.4+dfsg-2~ubuntu13.10.1
No-change backport to saucy (LP: #1341962)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  JIT detection for ClamAV bytecode.
 
3
 *
 
4
 *  Copyright (C) 2010 Sourcefire, Inc.
 
5
 *
 
6
 *  Authors: Török Edvin
 
7
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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,
 
20
 *  MA 02110-1301, USA.
 
21
 */
 
22
 
 
23
#include "llvm/ADT/Triple.h"
 
24
#include "llvm/Support/raw_ostream.h"
 
25
#ifdef LLVM29
 
26
#include "llvm/Support/Host.h"
 
27
#include "llvm/Support/DataTypes.h"
 
28
#include "llvm/Support/Memory.h"
 
29
#else
 
30
#include "llvm/System/Host.h"
 
31
#include "llvm/System/DataTypes.h"
 
32
#include "llvm/System/Memory.h"
 
33
#endif
 
34
 
 
35
#include "llvm/Config/config.h"
 
36
 
 
37
extern "C" {
 
38
#include "bytecode_detect.h"
 
39
}
 
40
 
 
41
using namespace llvm;
 
42
 
 
43
static void warn_assumptions(const char *msg, int a, int b)
 
44
{
 
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";
 
49
}
 
50
 
 
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);\
 
55
    break
 
56
 
 
57
void cli_detect_env_jit(struct cli_environment *env)
 
58
{
 
59
    std::string host_triple = sys::getHostTriple();
 
60
    INIT_STRFIELD(env->triple, host_triple.c_str());
 
61
 
 
62
    std::string cpu = sys::getHostCPUName();
 
63
    INIT_STRFIELD(env->cpu, cpu.c_str());
 
64
 
 
65
    if (env->big_endian != (int)sys::isBigEndianHost()) {
 
66
        warn_assumptions("host endianness", env->big_endian, sys::isBigEndianHost());
 
67
        env->big_endian = sys::isBigEndianHost();
 
68
    }
 
69
 
 
70
#ifdef __GNUC__
 
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;
 
76
#endif
 
77
 
 
78
    Triple triple(host_triple);
 
79
 
 
80
    // CPU architecture
 
81
    enum Triple::ArchType arch = triple.getArch();
 
82
    enum arch_list earch;
 
83
    bool conflicts = false;
 
84
    switch (arch) {
 
85
        case Triple::arm:
 
86
            earch = arch_arm;
 
87
            if (env->arch != earch) conflicts = true;
 
88
            break;
 
89
        case Triple::ppc:
 
90
            earch = arch_ppc32;
 
91
            if (env->arch != earch &&
 
92
                env->arch != arch_ppc64) conflicts = true;
 
93
            break;
 
94
        case Triple::ppc64:
 
95
            earch = arch_ppc64;
 
96
            // ppc64 is fixed up by llvm
 
97
            if (env->arch != arch_ppc32 &&
 
98
                env->arch != arch_ppc64) conflicts = true;
 
99
            break;
 
100
        case Triple::x86:
 
101
            earch = arch_i386;
 
102
            if (env->arch != earch) {
 
103
                /* bb #2153 */
 
104
                if (env->arch != arch_x86_64)
 
105
                    conflicts = true;
 
106
            }
 
107
            break;
 
108
        case Triple::x86_64:
 
109
            earch = 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
 
117
                 * provided one! */
 
118
                if (env->arch != arch_i386)
 
119
                    conflicts = true;
 
120
            }
 
121
            break;
 
122
        default:
 
123
            earch = arch_unknown;
 
124
            break;
 
125
    }
 
126
#ifndef AC_APPLE_UNIVERSAL_BUILD
 
127
    if (conflicts)
 
128
        warn_assumptions("CPU architecture", env->arch, earch);
 
129
#endif
 
130
    if (earch != arch_unknown)
 
131
        env->arch = earch;
 
132
 
 
133
    // OS
 
134
    Triple::OSType os = triple.getOS();
 
135
    switch (os) {
 
136
        case Triple::UnknownOS:
 
137
            env->os = llvm_os_UnknownOS;
 
138
            break;
 
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);
 
147
#ifndef LLVM29
 
148
        CASE_OS(MinGW64, os_win64);
 
149
#endif
 
150
        CASE_OS(NetBSD,  os_bsd);
 
151
        CASE_OS(OpenBSD, os_bsd);
 
152
        CASE_OS(Psp, os_unknown);
 
153
        CASE_OS(Solaris, os_solaris);
 
154
        case Triple::Win32:
 
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);
 
159
             break;
 
160
        CASE_OS(Haiku, os_unknown);
 
161
        CASE_OS(Minix, os_unknown);
 
162
    }
 
163
 
 
164
    // mmap RWX
 
165
    std::string ErrMsg;
 
166
    sys::MemoryBlock B = sys::Memory::AllocateRWX(4096, NULL, &ErrMsg);
 
167
    if (B.base() == 0) {
 
168
        errs() << "LibClamAV Warning: RWX mapping denied: " << ErrMsg << "\n";
 
169
    } else {
 
170
        env->os_features |= 1 << feature_map_rwx;
 
171
        sys::Memory::ReleaseRWX(B);
 
172
    }
 
173
}
 
174