CRC32 hash function for strings
[hiphop-php.git] / hphp / hhvm / process-init.cpp
blob127cb4426bed98724160c2bda3fe2ec79b5818de
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2014 Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/compiler/analysis/emitter.h"
18 #include "hphp/runtime/base/runtime-option.h"
19 #include "hphp/runtime/base/execution-context.h"
20 #include "hphp/runtime/base/program-functions.h"
21 #include "hphp/runtime/ext/ext.h"
22 #include "hphp/runtime/vm/unit.h"
23 #include "hphp/runtime/vm/bytecode.h"
24 #include "hphp/runtime/vm/repo.h"
25 #include "hphp/runtime/vm/runtime.h"
26 #include "hphp/runtime/ext_hhvm/ext_hhvm.h"
27 #include "hphp/runtime/vm/jit/translator.h"
28 #include "hphp/runtime/base/rds.h"
29 #include "hphp/runtime/vm/jit/fixup.h"
30 #include "hphp/runtime/vm/jit/mc-generator.h"
31 #include "hphp/runtime/base/unit-cache.h"
32 #include "hphp/system/systemlib.h"
33 #include "hphp/util/logger.h"
34 #include "hphp/util/hash.h"
36 #include <folly/experimental/Singleton.h>
38 #include <libgen.h> // For dirname(3).
39 #include <string>
41 namespace HPHP {
43 ///////////////////////////////////////////////////////////////////////////////
45 #define STRINGIZE_CLASS_NAME(cls) #cls
46 #define pinitSentinel __pinitSentinel
47 #define resource __resource
49 #define SYSTEM_CLASS_STRING(cls) \
50 const StaticString s_##cls(LITSTR_INIT(STRINGIZE_CLASS_NAME(cls)));
51 SYSTEMLIB_CLASSES(SYSTEM_CLASS_STRING)
53 #undef resource
54 #undef pinitSentinel
55 #undef STRINGIZE_CLASS_NAME
57 void ProcessInit() {
58 // Create the global mcg object
59 jit::mcg = new jit::MCGenerator();
60 jit::mcg->initUniqueStubs();
62 // Save the current options, and set things up so that
63 // systemlib.php can be read from and stored in the
64 // normal repo.
65 int db = RuntimeOption::EvalDumpBytecode;
66 bool rp = RuntimeOption::AlwaysUseRelativePath;
67 bool sf = RuntimeOption::SafeFileAccess;
68 bool ah = RuntimeOption::EvalAllowHhas;
69 bool wp = Option::WholeProgram;
70 RuntimeOption::EvalDumpBytecode &= ~1;
71 RuntimeOption::AlwaysUseRelativePath = false;
72 RuntimeOption::SafeFileAccess = false;
73 RuntimeOption::EvalAllowHhas = true;
74 Option::WholeProgram = false;
76 RDS::requestInit();
77 string hhas;
78 string slib = get_systemlib(&hhas);
80 if (slib.empty()) {
81 // Die a horrible death.
82 Logger::Error("Unable to find/load systemlib.php");
83 _exit(1);
86 LitstrTable::init();
87 LitstrTable::get().setWriting();
88 Repo::get().loadGlobalData();
90 // Save this in case the debugger needs it. Once we know if this
91 // process does not have debugger support, we'll clear it.
92 SystemLib::s_source = slib;
94 SystemLib::s_unit = compile_systemlib_string(slib.c_str(), slib.size(),
95 "systemlib.php");
97 const StringData* msg;
98 int line;
99 if (SystemLib::s_unit->compileTimeFatal(msg, line)) {
100 Logger::Error("An error has been introduced into the systemlib, "
101 "but we cannot give you a file and line number right now.");
102 Logger::Error("Check all of your changes to hphp/system/php");
103 Logger::Error("HipHop Parse Error: %s", msg->data());
104 _exit(1);
107 if (!hhas.empty()) {
108 SystemLib::s_hhas_unit = compile_string(hhas.c_str(), hhas.size(),
109 "systemlib.hhas");
110 if (SystemLib::s_hhas_unit->compileTimeFatal(msg, line)) {
111 Logger::Error("An error has been introduced in the hhas portion of "
112 "systemlib.");
113 Logger::Error("Check all of your changes to hhas files in "
114 "hphp/system/php");
115 Logger::Error("HipHop Parse Error: %s", msg->data());
116 _exit(1);
120 // Load the systemlib unit to build the Class objects
121 SystemLib::s_unit->merge();
122 if (SystemLib::s_hhas_unit) {
123 SystemLib::s_hhas_unit->merge();
126 SystemLib::s_nativeFuncUnit = build_native_func_unit(hhbc_ext_funcs,
127 hhbc_ext_funcs_count);
128 SystemLib::s_nativeFuncUnit->merge();
129 SystemLib::s_nullFunc =
130 Unit::lookupFunc(makeStaticString("86null"));
132 // We call a special bytecode emitter function to build the native
133 // unit which will contain all of our cppext functions and classes.
134 // Each function and method will have a bytecode body that will thunk
135 // to the native implementation.
136 Unit* nativeClassUnit = build_native_class_unit(hhbc_ext_classes,
137 hhbc_ext_class_count);
138 SystemLib::s_nativeClassUnit = nativeClassUnit;
140 LitstrTable::get().setReading();
142 // Load the nativelib unit to build the Class objects
143 SystemLib::s_nativeClassUnit->merge();
145 #define INIT_SYSTEMLIB_CLASS_FIELD(cls) \
147 Class *cls = NamedEntity::get(s_##cls.get())->clsList(); \
148 assert(!hhbc_ext_class_count || cls); \
149 SystemLib::s_##cls##Class = cls; \
152 // Stash a pointer to the VM Classes for stdclass, Exception,
153 // pinitSentinel and resource
154 SYSTEMLIB_CLASSES(INIT_SYSTEMLIB_CLASS_FIELD)
156 #undef INIT_SYSTEMLIB_CLASS_FIELD
158 // Retrieve all of the class pointers
159 for (long long i = 0; i < hhbc_ext_class_count; ++i) {
160 const HhbcExtClassInfo* info = hhbc_ext_classes + i;
161 const StringData* name = makeStaticString(info->m_name);
162 const NamedEntity* ne = NamedEntity::get(name);
163 Class* cls = Unit::lookupClass(ne);
164 assert(cls);
165 *(info->m_clsPtr) = cls;
168 ClassInfo::InitializeSystemConstants();
169 Stack::ValidateStackSize();
170 SystemLib::s_inited = true;
172 RuntimeOption::AlwaysUseRelativePath = rp;
173 RuntimeOption::SafeFileAccess = sf;
174 RuntimeOption::EvalDumpBytecode = db;
175 RuntimeOption::EvalAllowHhas = ah;
176 Option::WholeProgram = wp;
178 folly::SingletonVault::singleton()->registrationComplete();
181 // Check if the binary is compatible with the running environment.
182 void checkBuild() {
183 #ifndef incl_HPHP_HASH_H_
184 #error "Need to include hphp/util/hash.h to get USE_SSECRC correctly"
185 #endif
186 #ifdef USE_SSECRC
187 // Check for SSE4.2 support by the current processor
188 int32_t a {1}, b, c, d;
189 asm volatile ("cpuid" : "+a" (a), "=b" (b), "=c" (c), "=d" (d));
190 if ((c & (1 << 20)) == 0) {
191 Logger::Error("SSE 4.2 is not supported in this system. "
192 "Try rebuilding without USE_SSECRC.");
193 _exit(1);
195 #endif
198 ///////////////////////////////////////////////////////////////////////////////