Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Placing class and method name directly into AST insted of using magic constants (#1106) #1108

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions agent/native/ext/AST_instrumentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ zend_ast* createAstStandaloneNotFqFunctionCall( StringView funcName, zend_ast* a
return createAstStandaloneFunctionCall( funcName, /* isFullyQualified */ false, astArgList );
}

ResultCode createPreHookAstArgListByCaptureSpec( zend_ast_decl* astDecl, ArgCaptureSpecArrayView argCaptureSpecs, /* out */ zend_ast** pResult )
ResultCode createPreHookAstArgListByCaptureSpec( zend_ast_decl *parentClass, zend_ast_decl* astDecl, ArgCaptureSpecArrayView argCaptureSpecs, /* out */ zend_ast** pResult )
{
// AST for PHP code:
//
Expand All @@ -526,12 +526,14 @@ ResultCode createPreHookAstArgListByCaptureSpec( zend_ast_decl* astDecl, ArgCapt
uint32_t lineNumber = astDecl->start_lineno;
zend_ast* capturedArgsAstArray = NULL;



ELASTIC_APM_CALL_IF_FAILED_GOTO( createCapturedArgsAstArray( astDecl, argCaptureSpecs, lineNumber, /* out */ &capturedArgsAstArray ) );

*pResult = createAstListWithThreeChildren(
ZEND_AST_ARG_LIST
, astDecl->kind == ZEND_AST_METHOD ? createAstMagicConst__CLASS__( lineNumber ) : createAstConstNull( lineNumber )
, createAstMagicConst__FUNCTION__( lineNumber )
, astDecl->kind == ZEND_AST_METHOD ? createAstZValString(makeStringView(ZSTR_VAL(((zend_ast_decl *)parentClass)->name), ZSTR_LEN(((zend_ast_decl *)parentClass)->name)), lineNumber) : createAstConstNull( lineNumber )
, createAstZValString(makeStringView(ZSTR_VAL(astDecl->name), ZSTR_LEN(astDecl->name)), lineNumber)
, capturedArgsAstArray
);
resultCode = resultSuccess;
Expand All @@ -552,7 +554,7 @@ static StringView g_elastic_apm_ast_instrumentation_pre_hook_funcName = ELASTIC_
*/
static const size_t g_funcDeclBodyChildIndex = 2;

ResultCode insertAstForFunctionPreHook( zend_ast_decl* funcAstDecl, ArgCaptureSpecArrayView argCaptureSpecs )
ResultCode insertAstForFunctionPreHook( zend_ast_decl *parentClass, zend_ast_decl* funcAstDecl, ArgCaptureSpecArrayView argCaptureSpecs )
{
// Before:
//
Expand Down Expand Up @@ -623,7 +625,7 @@ ResultCode insertAstForFunctionPreHook( zend_ast_decl* funcAstDecl, ArgCaptureSp
ELASTIC_APM_SET_RESULT_CODE_AND_GOTO_FAILURE();
}

ELASTIC_APM_CALL_IF_FAILED_GOTO( createPreHookAstArgListByCaptureSpec( funcAstDecl, argCaptureSpecs, /* out */ &preHookCallAstArgList ) );
ELASTIC_APM_CALL_IF_FAILED_GOTO( createPreHookAstArgListByCaptureSpec( parentClass, funcAstDecl, argCaptureSpecs, /* out */ &preHookCallAstArgList ) );

funcAstDecl->child[ g_funcDeclBodyChildIndex ] = createAstListWithTwoChildren(
ZEND_AST_STMT_LIST
Expand Down
2 changes: 1 addition & 1 deletion agent/native/ext/AST_instrumentation.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ zend_ast_decl** findChildSlotForStandaloneFunctionAst( zend_ast* rootAst, String
zend_ast_decl* findClassAst( zend_ast* rootAst, StringView nameSpace, StringView className );
zend_ast_decl** findChildSlotForMethodAst( zend_ast_decl* astClass, StringView methodName, size_t minParamsCount );

ResultCode insertAstForFunctionPreHook( zend_ast_decl* funcAstDecl, ArgCaptureSpecArrayView argCaptureSpecs );
ResultCode insertAstForFunctionPreHook( zend_ast_decl *parentClass, zend_ast_decl* funcAstDecl, ArgCaptureSpecArrayView argCaptureSpecs );
ResultCode appendDirectCallToInstrumentation( zend_ast_decl** pAstChildSlot, StringView constNameForMethodName );
ResultCode wrapStandaloneFunctionAstWithPrePostHooks( zend_ast_decl** pAstChildSlot );

Expand Down
8 changes: 4 additions & 4 deletions agent/native/ext/WordPress_instrumentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ void wordPressInstrumentationOnRequestShutdown()
}

static
ResultCode insertPreHookForFunctionWithHookNameCallbackParams( zend_ast_decl* astDecl )
ResultCode insertPreHookForFunctionWithHookNameCallbackParams( zend_ast_decl *parentClass, zend_ast_decl* astDecl )
{
// standalone function:
// function _wp_filter_build_unique_id( $hook_name, $callback, $priority )
// class WP_Hook method
// public function add_filter( $hook_name, $callback, $priority, $accepted_args )
ArgCaptureSpec argCaptureSpecArr[] = { /* capture $hook_name by value */ captureArgByValue, /* capture $callback by reference */ captureArgByRef };
return insertAstForFunctionPreHook( astDecl, ELASTIC_APM_MAKE_ARRAY_VIEW_FROM_STATIC( ArgCaptureSpecArrayView, argCaptureSpecArr ) );
return insertAstForFunctionPreHook(parentClass, astDecl, ELASTIC_APM_MAKE_ARRAY_VIEW_FROM_STATIC( ArgCaptureSpecArrayView, argCaptureSpecArr ) );
}

static StringView g_globalNamespace = ELASTIC_APM_STRING_LITERAL_TO_VIEW( "" );
Expand All @@ -116,7 +116,7 @@ ResultCode wordPressInstrumentationTransformFile_plugin_php( zend_ast* ast )
ELASTIC_APM_SET_RESULT_CODE_AND_GOTO_FAILURE();
}

ELASTIC_APM_CALL_IF_FAILED_GOTO( insertPreHookForFunctionWithHookNameCallbackParams( *p_wp_filter_build_unique_id_astFuncDecl ) );
ELASTIC_APM_CALL_IF_FAILED_GOTO( insertPreHookForFunctionWithHookNameCallbackParams( nullptr, *p_wp_filter_build_unique_id_astFuncDecl ) );
// It's important to record if we instrumented _wp_filter_build_unique_id successfully.
// _wp_filter_build_unique_id instrumentation alone cannot make application work incorrectly
// because it checks if $callback is an instance our WordPressFilterCallbackWrapper class before unwrapping it.
Expand Down Expand Up @@ -159,7 +159,7 @@ ResultCode wordPressInstrumentationTransformFile_class_wp_hook_php( zend_ast* as
ELASTIC_APM_SET_RESULT_CODE_AND_GOTO_FAILURE();
}

ELASTIC_APM_CALL_IF_FAILED_GOTO( insertPreHookForFunctionWithHookNameCallbackParams( *p_add_filter_astMethod ) );
ELASTIC_APM_CALL_IF_FAILED_GOTO( insertPreHookForFunctionWithHookNameCallbackParams(WP_Hook_astClassDecl, *p_add_filter_astMethod ) );

resultCode = resultSuccess;
finally:
Expand Down
1 change: 0 additions & 1 deletion agent/native/ext/tracer_PHP_part.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@ void tracerPhpPartForwardCall( StringView phpFuncName, zend_execute_data* execut
}
}


getArgsFromZendExecuteData( execute_data, g_maxInterceptedCallArgsCount, &( callArgs[ 0 ] ), &callArgsCount );
tracerPhpPartLogArguments( logLevel_trace, callArgsCount, callArgs );

Expand Down
Loading