Fix use-after-free bug in RPC handler
[hiphop-php.git] / hphp / compiler / builtin_symbols.cpp
blob235a3b4dd3323deace52b9a71e3571e147d527d6
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present 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/builtin_symbols.h"
18 #include "hphp/compiler/analysis/analysis_result.h"
19 #include "hphp/compiler/statement/statement_list.h"
20 #include "hphp/compiler/analysis/function_scope.h"
21 #include "hphp/compiler/analysis/class_scope.h"
22 #include "hphp/compiler/expression/modifier_expression.h"
23 #include "hphp/compiler/expression/simple_function_call.h"
24 #include "hphp/compiler/option.h"
25 #include "hphp/compiler/parser/parser.h"
26 #include "hphp/compiler/analysis/file_scope.h"
27 #include "hphp/compiler/analysis/variable_table.h"
28 #include "hphp/compiler/analysis/constant_table.h"
29 #include "hphp/parser/hphp.tab.hpp"
30 #include "hphp/runtime/base/program-functions.h"
31 #include "hphp/runtime/base/array-iterator.h"
32 #include "hphp/runtime/base/execution-context.h"
33 #include "hphp/runtime/vm/native.h"
34 #include "hphp/util/logger.h"
35 #include <vector>
37 using namespace HPHP;
39 #define BF_COLUMN_COUNT 3
40 #define BF_COLUMN_NAME 0
41 #define BF_COLUMN_RETURN 1
42 #define BF_COLUMN_PARAMS 2
44 #define CLASS_TYPE 999
46 ///////////////////////////////////////////////////////////////////////////////
48 bool BuiltinSymbols::Loaded = false;
49 AnalysisResultPtr BuiltinSymbols::s_systemAr;
51 const char *const BuiltinSymbols::GlobalNames[] = {
52 "HTTP_RAW_POST_DATA",
53 "_COOKIE",
54 "_ENV",
55 "_FILES",
56 "_GET",
57 "_POST",
58 "_REQUEST",
59 "_SERVER",
60 "_SESSION",
61 "argc",
62 "argv",
65 hphp_string_set BuiltinSymbols::s_superGlobals;
67 ///////////////////////////////////////////////////////////////////////////////
69 int BuiltinSymbols::NumGlobalNames() {
70 return sizeof(BuiltinSymbols::GlobalNames) /
71 sizeof(BuiltinSymbols::GlobalNames[0]);
74 void BuiltinSymbols::ImportNativeConstants(AnalysisResultPtr ar,
75 ConstantTablePtr dest) {
76 for (auto cnsPair : Native::getConstants()) {
77 ExpressionPtr e(Expression::MakeScalarExpression(
78 ar, ar, Location::Range(), tvAsVariant(&cnsPair.second)));
80 dest->add(cnsPair.first->data(), e, ar, e);
82 if ((cnsPair.second.m_type == KindOfUninit) &&
83 cnsPair.second.m_data.pref) {
84 // Callback based constant
85 dest->setDynamic(ar, cnsPair.first->data());
90 bool BuiltinSymbols::Load(AnalysisResultPtr ar) {
91 if (Loaded) return true;
92 Loaded = true;
94 if (g_context.isNull()) hphp_thread_init();
96 ConstantTablePtr cns = ar->getConstants();
97 // load extension constants, classes and dynamics
98 ImportNativeConstants(ar, cns);
100 for (int i = 0, n = NumGlobalNames(); i < n; ++i) {
101 ar->getVariables()->add(GlobalNames[i], false, ar,
102 ConstructPtr(), ModifierExpressionPtr());
105 cns->setDynamic(ar, "PHP_BINARY");
106 cns->setDynamic(ar, "PHP_BINDIR");
107 cns->setDynamic(ar, "PHP_OS");
108 cns->setDynamic(ar, "PHP_SAPI");
109 cns->setDynamic(ar, "SID");
111 for (auto sym : cns->getSymbols()) {
112 sym->setSystem();
115 // Systemlib files were all parsed by hphp_process_init
117 const auto& files = ar->getAllFiles();
118 for (const auto& file : files) {
119 file.second->setSystem();
121 const auto& classes = file.second->getClasses();
122 for (const auto& clsVec : classes) {
123 assert(clsVec.second.size() == 1);
124 auto cls = clsVec.second[0];
125 if (auto nativeConsts = Native::getClassConstants(
126 String(cls->getScopeName()).get())) {
127 for (auto cnsMap : *nativeConsts) {
128 auto tv = cnsMap.second;
129 auto e = Expression::MakeScalarExpression(ar, ar, Location::Range(),
130 tvAsVariant(&tv));
131 cls->getConstants()->add(cnsMap.first->data(), e, ar, e);
134 cls->setSystem();
135 ar->addSystemClass(cls);
136 for (const auto& func : cls->getFunctions()) {
137 FunctionScope::RecordFunctionInfo(func.first, func.second);
141 const auto& functions = file.second->getFunctions();
142 for (const auto& func : functions) {
143 func.second->setSystem();
144 ar->addSystemFunction(func.second);
145 FunctionScope::RecordFunctionInfo(func.first, func.second);
149 return true;
152 void BuiltinSymbols::LoadSuperGlobals() {
153 if (s_superGlobals.empty()) {
154 s_superGlobals.insert("_SERVER");
155 s_superGlobals.insert("_GET");
156 s_superGlobals.insert("_POST");
157 s_superGlobals.insert("_COOKIE");
158 s_superGlobals.insert("_FILES");
159 s_superGlobals.insert("_ENV");
160 s_superGlobals.insert("_REQUEST");
161 s_superGlobals.insert("_SESSION");
165 bool BuiltinSymbols::IsSuperGlobal(const std::string &name) {
166 return s_superGlobals.count(name);