-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix JIT crash with instrumented generators
The JIT will look up zend_op_trace_info in (char*)EX(opline) + offset, where offset is generally somewhere in valid memory, but will effectively have random values, and crash when using these. Also add tooling to trivially open a randomized corefile from circleci. Signed-off-by: Bob Weinand <bob.weinand@datadoghq.com>
- Loading branch information
Showing
8 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--TEST-- | ||
generator hooking works with JIT | ||
--SKIPIF-- | ||
<?php if (!file_exists(ini_get("extension_dir") . "/opcache.so")) die('skip: opcache.so does not exist in extension_dir'); ?> | ||
<?php if (PHP_VERSION_ID < 80000) die('skip: JIT is only on PHP 8'); ?> | ||
--INI-- | ||
opcache.enable=1 | ||
opcache.enable_cli = 1 | ||
opcache.jit_buffer_size=512M | ||
opcache.jit=1255 | ||
zend_extension=opcache.so | ||
--FILE-- | ||
<?php | ||
|
||
function gen() { | ||
yield 1; | ||
yield 2; | ||
return 3; | ||
}; | ||
|
||
$hooks[] = \DDTrace\hook_function("gen", [ | ||
"posthook" => function($args, $retval) { | ||
var_dump($retval); | ||
} | ||
]); | ||
|
||
for ($i = 0; $i < 2; ++$i) { | ||
foreach (gen() as $val) { | ||
var_dump($val); | ||
} | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
int(1) | ||
int(1) | ||
int(2) | ||
int(2) | ||
int(1) | ||
int(1) | ||
int(2) | ||
int(2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/bin/bash | ||
|
||
# e.g. https://app.circleci.com/pipelines/github/DataDog/dd-trace-php/17113/workflows/196b5740-f3ce-4faf-8731-e9a4b15d114a/jobs/4964970/steps | ||
URL=${1} | ||
|
||
if ! [[ $URL =~ workflows/([0-9a-f-]+)/jobs/([0-9]+) ]]; then | ||
echo "Invalid URL?" | ||
exit 1 | ||
fi | ||
|
||
CIRCLE_WORKFLOW_ID=${BASH_REMATCH[1]} | ||
CIRCLE_JOB_ID=${BASH_REMATCH[2]} | ||
|
||
TEST_NAME=$(curl -s -X GET "https://circleci.com/api/v2/project/gh/DataDog/dd-trace-php/job/$CIRCLE_JOB_ID" -H "Accept: application/json" | grep -Eo '"name":"randomized[^"]+') | ||
TEST_NAME=${TEST_NAME:8} | ||
|
||
echo "Found test run: $TEST_NAME" | ||
|
||
COREFILES=($(curl -s -X GET "https://circleci.com/api/v2/project/gh/DataDog/dd-trace-php/$CIRCLE_JOB_ID/artifacts" -H "Accept: application/json" | grep -Eo 'https://[^"]+core')) | ||
|
||
if [[ -z $COREFILES ]]; then | ||
echo "Found no core files..." | ||
exit 1 | ||
fi | ||
|
||
num=1 | ||
for file in "${COREFILES[@]}"; do | ||
echo "$((num++)): $(echo "$file" | grep -Eo 'randomized-[^/]+')" | ||
done | ||
|
||
while : ; do | ||
echo -n "Select one core file: " | ||
read num | ||
if [[ $num -gt ${#COREFILES[@]} || $num -le 0 ]]; then | ||
echo "Invalid number $num" | ||
else | ||
break | ||
fi | ||
done | ||
|
||
COREFILE=${COREFILES[$((num - 1))]} | ||
corefilename=$(echo "$COREFILE" | grep -Eo 'randomized-[^/]+') | ||
container="datadog/dd-trace-ci:php-randomizedtests-$(if [[ $COREFILE == *buster* ]]; then echo buster; else echo centos7; fi)-${corefilename: -2:1}.${corefilename: -1}-2" | ||
|
||
if [[ $TEST_NAME == *asan* ]]; then | ||
ARTIFACTS_JOB='package extension zts-debug-asan' | ||
else | ||
ARTIFACTS_JOB='package extension' | ||
fi | ||
|
||
PACKAGE_ARCH=$(if [[ $TEST_NAME == *arm* ]]; then echo aarch64; else echo x86_64; fi) | ||
|
||
parent_job_id=$(curl -s -X GET "https://circleci.com/api/v2/workflow/$CIRCLE_WORKFLOW_ID/job" -H "Accept: application/json" | grep -Eo '\{[^}]*"'"$ARTIFACTS_JOB"'"[^}]*' | grep -Eo '"job_number":[^,]+' | tail -c +14) | ||
ARTIFACTS_RESULT=$(curl -s -X GET "https://circleci.com/api/v2/project/github/DataDog/dd-trace-php/$parent_job_id/artifacts" -H "Accept: application/json") | ||
artifact_url=$(echo "$ARTIFACTS_RESULT" | grep -Eo '\{[^}]*"dd-library-php-[^"]+'"${PACKAGE_ARCH}"'-linux-gnu.tar.gz"[^}]*' | grep -Eo '"url":"[^"]+' | tail -c +8) | ||
setup_url=$(echo "$ARTIFACTS_RESULT" | grep -Eo '\{[^}]*"datadog-setup.php"[^}]*' | grep -Eo '"url":"[^"]+' | tail -c +8) | ||
|
||
set -x | ||
docker run --platform=linux/$(if [[ $TEST_NAME == *arm* ]]; then echo arm64; else echo amd64; fi) --rm -ti "$container" bash -c "curl -Lo /tmp/datadog-setup.php '$setup_url'; curl -Lo /tmp/${artifact_url##*/} '$artifact_url'; curl -Lo /tmp/core '$COREFILE'; php /tmp/datadog-setup.php --php-bin all --file /tmp/${artifact_url##*/} --enable-profiling; $(if [[ $TEST_NAME == *asan* ]]; then echo "switch-php debug-zts-asan; "; fi)exec bash --rcfile <(echo 'gdb \$(file /tmp/core | grep -Po "'from\\s\\x27\\K[^\\x27:]+'") --core=/tmp/core -ix /usr/local/src/php/.gdbinit')" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
add_library(zai_jit_utils jit_blacklist.c) | ||
|
||
target_include_directories( | ||
zai_jit_utils PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}> | ||
$<INSTALL_INTERFACE:include>) | ||
|
||
target_compile_features(zai_jit_utils PUBLIC c_std_99) | ||
|
||
target_link_libraries(zai_jit_utils PUBLIC Tea::Php dl) | ||
|
||
set_target_properties(zai_jit_utils PROPERTIES EXPORT_NAME JitUtils | ||
VERSION ${PROJECT_VERSION}) | ||
|
||
add_library(Zai::JitUtils ALIAS zai_jit_utils) | ||
|
||
install( | ||
FILES ${CMAKE_CURRENT_SOURCE_DIR}/jit_blacklist.h | ||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/jit_utils/) | ||
|
||
target_link_libraries(zai_zend_abstract_interface INTERFACE zai_jit_utils) | ||
|
||
install(TARGETS zai_jit_utils EXPORT ZendAbstractInterfaceTargets) |