[Fix] Unravel gotos into try blocks correctly

Summary: At each try, there can be multiple label-ids for each label (but
exactly one label for each label-id). Swap the map to map label-ids to labels,
rather than the other way around to avoid losing label-ids.

Test Plan: fast_tests slow_tests

Reviewers: myang, qigao, andrewparoski

Reviewed By: andrewparoski

CC: ps, mwilliams, andrewparoski

Differential Revision: 346349

Task ID: 765340
Export psp CPU and Wall time to access log

Summary:
Getting this timing inside php is difficult and not accurate.
Provide this timing inside of HPHP via %Z and %z.

Test Plan:
added %Z %z in the access log and compared to timings
calculated in php and they were slighly higher, which is expected.

Reviewers: qigao

Reviewed By: qigao

CC: jasont, ps, mwilliams, qigao

Differential Revision: 346858

Task ID: 755067
Fix segfault in SimpleXML when using entities

Summary:
Fix segfault while looking at security issue, any XML
file parsed by SimpleXML resulted in a segfault if an Entity was
provided.

Test Plan:
fast_tests
No segfault with:
<?php

$test = '<!DOCTYPE foo [<!ENTITY xxe SYSTEM "file:///etc/passwd">]>
<search><user>&xxe;</user></search>';
$x = new SimpleXMLElement($test);
var_dump($x);

Reviewers: mwilliams

Reviewed By: mwilliams

CC: ps, mwilliams

Differential Revision: 346984

Revert Plan: Ok
Debugger Server API

Summary:
To build GUI for hphpd, we need to access DebuggerClient using an API.
This diff changes the existing debugger extension to do that. The
current built-in class DebuggerClient and DebuggerProxy are just to
support CmdUser, so they are renamed with suffix CmdUser. Then a new
extension class is added to wrap around DebuggerClient
for executing commands. An extension function is added to fetch
DebuggerClient by name so that multiple requests can operate on the same
client for debugging. This way, a GUI tool can use Ajax request to hit
the server and operate on debugger via this API.

Since this change makes DebuggerClient running in server mode as root,
it breaks a few assumptions in DebuggerClient, so several other
modifications are added to make sure:
- the sweeping after request doesn't break anything,
- multiple DebuggerClient can co-exist on the same server without
interfering each other
- credentials do not rely on local login and home
- disable color and highlighting since it's not terminal printing

This diff also modifies the Makefile for idl the same way as Makfile for
system so that it doesn't give a lot of warnings.

Some more work need to be done for better handling error and corner
cases. Also need to find a way to format output. For now it just
returns the string printed by DebuggerClient after each processCmd().

Test Plan:
make fast_tests
tried most functionality with a debug php endpoint on sandbox

Reviewers: myang, mwilliams

Reviewed By: myang

CC: tuomaspelkonen, ps, mwilliams, myang

Differential Revision: 346215

Task ID: 753544
[Fix] Dont pass invalid keys to ArrayData::remove

Summary: Variant::remove(CVarRef) could end up passing a "null" key to ArrayData::remove if given an invalid key (array or object). In the DEBUG build, this asserted, and in the release build it removed the element with key 0.

The new test case produced incorrect results in the release build, and asserted in the debug build.

Task ID: #768801

Blame Rev:

Reviewers: kma qigao

CC:

Test Plan: fast_tests slow_tests (new test case added).

Revert Plan:

Tags:

Platform Impact (PUBLIC):

Differential Revision: 347370
[Fix] Fix bug in FileCache::fileSize

Summary: When uncompressing the file, we should use buf.clen instead of
buf.len.

Test Plan:
make fast_tests
make slow_tests

Reviewers: qigao, mwilliams, ottoni

Reviewed By: qigao

CC: hphp-diffs@lists, ps, mwilliams, qigao

Differential Revision: 349850

Task ID: 754631
[Perf/Fix/HPHPi] Use the method table in class eval state for lookup

Summary:
Each class statement AST node has a method table that contains only the
methods in the class, but not any of the inherited methods. When a class is
eval'ed, a ClassEvalState is created which contains among other things all
the methods that the class should have, including all the inherited methods.
When resolving a static class method call (i.e., finding a class method for
a given class name), we can take advantage of the inclusive method table in
the ClassEvalState, rather than redo the search along the class hierarchy
for methods in parent classes.

Second, I noticed that the m_marker in ClassStatement is always set. From the
comments it meant to indicate whether the class is top-level and therefore
should be hoisted upfront. I fixed that and there was one test failing because
HPHPi starts to raise error as expected.

Test Plan:
make fast_tests
flib/unit test
(1) Error case:

<?php
$a = bar();
if ($a) {
  class fOO extends Unknown {}
} else {
  class Foo extends unknOwn {}
}
function bar() { return 123; }

Before: no error reported
After:
HipHop Fatal error: Class 'Unknown' does not exist.

(2) micro benchmark
<?php
class A0 {
  public static function __callStatic($name, $arguments) {
     return $name;
  }
}
class A1 extends A0 {}
class A2 extends A1 {}
class A3 extends A2 {}
class A4 extends A3 {}
class A5 extends A4 {}
class A6 extends A5 {}
class A7 extends A6 {}
function foo() {
  for ($i = 0; $i < 1000000; $i++) {
    $r = A7::Test();
  }
  return $r;
}
var_dump(foo());

Before
string(4) "Test"

real    0m6.238s
user    0m6.208s
sys     0m0.035s

After
string(4) "Test"

real    0m2.783s
user    0m2.757s
sys     0m0.033s

Reviewers: mwilliams, qigao

Reviewed By: qigao

CC: hphp-diffs@lists, ps, mwilliams, qigao

Differential Revision: 349254
[Fix/HPHPi] Increase maximum function id

Summary:
To suppress the warning as the number of functions can exceed the current
limit: HipHop Warning: Maximum function id reached: 65536.

Test Plan:
make fast_tests
run server mode flib test and make sure that no warning as shown above.

Reviewers: qigao, mwilliams

Reviewed By: qigao

CC: hphp-diffs@lists, ps, mwilliams, qigao

Differential Revision: 350451
[Fix/hphpd] Fix bugs in server api

Summary:
This diff fixes the issue of interruping for the second time causing the
server to abort. It also makes the state machine more clear and guards
mroe strict so that user retries are unlikely to cause issues and leave
server in an error state.

Note: there are some generated files change that are not because of this
diff.

Test Plan:
make fast_tests
manually tried more functionalities

Reviewers: myang

Reviewed By: myang

CC: tuomaspelkonen, ps, mwilliams, myang

Differential Revision: 350417

Task ID: 753544
Add new TestTraits to TestCodeRun and fix hphpc accordingly

Summary:
I've recently learned a few more nuances about traits.  I added some
new tests to TestCodeRun, and fixed the following cases:

1. abstract trait methods may be provided by a base class of the class
   using the trait

2. abstract trait methods may be provided by other traits used in the
   same class

3. trait methods and static properties can be accessed via
   trait_name::blah.

Item 3 abore required enabling code generation for trait classes.

Test Plan:
make fast_tests
make slow_tests
built www and checked output CPP code against previous

Reviewers: mwilliams, myang, qigao

Reviewed By: mwilliams

CC: andrewparoski, ps, mwilliams

Differential Revision: 350654

Task ID: 758990
[Fix] Do not allocate user function id for certain names

Summary:
I made a change not to allocate user function id for names resulted from
create_function and closures. I also removed the warning message, as long
as we have a substantial user function names have integer ids, there will
be some benefit even though some "late comers" will have to go though a
lookup. The warning message was causing some tests to fail.

Test Plan:
make fast_tests
load www sandbox
run server mode flib test

Reviewers: mwilliams, qigao

Reviewed By: qigao

CC: hphp-diffs@lists, ps, mwilliams, qigao, myang

Differential Revision: 351742
[Fix/hphpd] remove the trailing ';' for '=' command

Summary:
In a recent change, () were added to wrap the expression for '=', which
made ';' in the expression a syntax error. We were thinking on a general
fix but it seems hard to do without calling php parser for the command
line. This diff fixes one common benign case with traling ';', which used
to work before the () were added.

Test Plan:
make fast_tests
tried with hphpd on command line
hphpd> =123;
123

Reviewers: mwilliams, myang

Reviewed By: myang

CC: ps, mwilliams, myang, qigao

Differential Revision: 351726

Task ID: 747161
[HPHPi] Use ParserBase::IsClosureName to check closure name

Summary:
Two places in HPHPi are changed to use ParserBase::IsClosureName to
check closure name to avoid ad-hoc code.

Test Plan: make fast_tests

Reviewers: mwilliams, qigao

Reviewed By: mwilliams

CC: hphp-diffs@lists, ps, mwilliams

Differential Revision: 351791
Mask out xhprof for memory allocation tracking

Summary:
When xhprof is enabled, it will allocate quite a bit of memory
and this can throw off normal memory allocation statistics.  Mask these
allocations since it is not relevant for measurements.

Test Plan:
xhprof_enable() followed by memory_get_allocation() seeing no
increase in allocation memory.

Reviewers: je

Reviewed By: je

CC: jasont, ps, mwilliams, je

Differential Revision: 349035
[Fix] Guard AnalysisResult::m_files with a lock

Summary: For parse-on-demand, we were checking to see if a file had already
been parsed without aquiring a lock. That could result in a crash if files were
inserted during the lookup.

Test Plan: Running the command from the task resulted in a crash once in about
10 tries before the fix. After the fix I ran it 3000 times with no failures.

Reviewers: qigao, myang

Reviewed By: qigao

CC: ps, mwilliams, qigao

Differential Revision: 351734

Task ID: 778331
[Fix] Fix bug in chunked post data

Summary:
The runtime option AlwaysPopulateRawPostData was misused to discard the post
data rather than only to decide whether to populate $HTTP_RAW_POST_DATA. The
purpose of discarding post data was an memory optimization on video data
because the video data is alreay streamed into a temp file so keep the extra
video data as post data in its entirety isn't needed. However, post data is
generally needed for non-video data. I fixed that by removing the
AlwaysPopulateRawPostData check in read_all_post_data.

Test Plan:
make fast_tests
<?php
echo strlen($_POST['foo']);
Use html form to post data:
<form action="/temp.php" method="post">
<input type="hidden" name="foo" value="large data"  /> <input type="submit" />
</form>
Before the fix, the returned value is about 4000 due to truncated post data.
Observed correct value after the fix.

Reviewers: mwilliams, qigao

Reviewed By: mwilliams

CC: hphp-diffs@lists, ps, mwilliams, myang

Differential Revision: 352127
[Fix/hphpd] Only escape the cases needed

Summary:
This diff makes it hphpd command line tokenizer only escape the cases
that are needed so that it doesn't do any inappropriate escaping.

Test Plan:
make fast_tests
the case in the task

Reviewers: mwilliams

Reviewed By: mwilliams

CC: ps, mwilliams, qigao

Differential Revision: 351968

Task ID: 778864
[Fix] Misc reference handling buglets exposed by enabling inlining

Summary:
 - in f($x, $x &= $y), where f takes its first param by reference, we would
   pass a reference to $x after the assignment, rather than before it (this
   happened independent of inlining).
 - in f(g(x)) if g returned by reference, and f did not take its param by
   reference, f could end up with a reference to the result of g anyway, due to
   copy constructor elision (again, independent of inlining)
 - there was a similar problem when a return-by-reference function got inlined.
   The return value could be destroyed before it was used (resulting in a crash in
   one of the added test cases), or it could just end up refering to the wrong
   thing.
 - in $a = &f(), if f returned by value, and got inlined, $a could end up bound
   to the result of f() (rather than bound to a copy of the result of f())

In order to expose the return-by-reference issue, I had to increase the
inliner's agressiveness. So Ive changed it from a bool, to an int and used it as
the max "cost" for a function.

To minimize extra temps, I added the return value's refness to the FunctionInfo.

Test Plan: fast_tests, slow_tests and slow_tests with AutoInline=5 (there are
still some failures, with the latter - will address separately).

Reviewers: qigao, myang

Reviewed By: myang

CC: ps, mwilliams, myang

Differential Revision: 348039

Task ID: 630786
[Perf] Reduce o_set overhead

Summary:
ObjectData's o_set family of functions all took a bool flag "forInit". The flag
was used to bypass access checks, and was only used when initializing an
ObjectData from hphpi, and from o_setArray. This meant that we were always
passing in, and checking a mostly unused parameter. In addition, Object's o_set
family didnt have the flag. code-gen didnt differentiate the two cases, and so
in some cases was passing the class name as the "forInit" flag. Bizarrely, this
mostly worked, because the class-name would be converted to true, and access
would be allowed (which is usually the right thing to do in correct programs).

With this diff, ObjectData's o_set family matches the Object o_set family,
solving the correctness problem, and also reducing the call overhead by dropping
a parameter.

Test Plan: fast_tests slow_tests

Reviewers: qigao, myang

Reviewed By: myang

CC: ps, mwilliams, myang, qigao

Differential Revision: 346544
[APC] Improve apc size profiling

Summary:
This diff does a few things to improve apc size profiling:
1. More compact format for script to process the result. I changed it to
one entry a line and got rid of the global object in json.
2. Add a profiling to monitor the size of keys with noTTL in a group
3. Switch to use concurrent_hash_map to protect the group and detail
profiling
4. change some field from int64 to int32 to save some memory

Test Plan:
make fast_tests
production testing on profiling
Note that all the code touched in this diff is not on critical path if
profiling is turned off. This is staging for enabling profiling on h2g
machines.

Reviewers: myang, mwilliams, ps

Reviewed By: mwilliams

CC: ps, mwilliams

Differential Revision: 352855