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

Refactor to cpp #1028

Merged
merged 15 commits into from
Jul 19, 2023
Merged
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@


# Constrol switches
set(_ELASTIC_FAIL_ON_ERROR false)
set(_ELASTIC_FAIL_ON_ERROR true)
set(_ELASTIC_WARN_ON_UNINITIALIZED true)


Expand Down Expand Up @@ -67,6 +67,12 @@ add_compile_options(
"-Wall"
"-Wextra"
"-Wno-unused-parameter" # annoying when using PHP delivered macros
"-Wno-ignored-qualifiers" #TODO php headers issues, should be removed and patched in conan package
"-Wno-unknown-pragmas" #clion, windows pragmas
"-Wno-unused-local-typedefs"
"-Wno-enum-compare" #TODO refactor code and remove
"-Wno-write-strings"

)

if(NOT _ELASTIC_WARN_ON_UNINITIALIZED)
Expand All @@ -83,4 +89,4 @@ add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:-Wnon-virtual-dtor>")
add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:-fdiagnostics-show-template-tree>") # print template mismatch as tree - much more user friendly

# C only switches
add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:-Wstrict-prototypes>")
add_compile_options("$<$<COMPILE_LANGUAGE:C>:-Wstrict-prototypes>")
37 changes: 26 additions & 11 deletions agent/native/ext/AST_debug.c → agent/native/ext/AST_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
* under the License.
*/


extern "C" {
#include <Zend/zend_ast.h>
}

#include <Zend/zend.h>
#include <Zend/zend_exceptions.h>
#include <Zend/zend_ast.h>

#include "AST_debug.h"
#include "ConfigSnapshot.h"
#include "log.h"
Expand Down Expand Up @@ -341,7 +350,7 @@ String debugDumpAstZvalStreamVal( zend_ast* ast, TextOutputStream* txtOutStream
}

case IS_LONG:
return streamPrintf( txtOutStream, "type: long, value: %"PRId64, (Int64)(Z_LVAL_P( zVal )) );
return streamPrintf( txtOutStream, "type: long, value: %" PRId64, (Int64)(Z_LVAL_P( zVal )) );

case IS_DOUBLE:
return streamPrintf( txtOutStream, "type: double, value: %f", (double)(Z_DVAL_P( zVal )) );
Expand Down Expand Up @@ -547,18 +556,19 @@ ResultCode ensureDirectoriesExist( StringView fullPath )
StringBuffer dirFullPath = ELASTIC_APM_EMPTY_STRING_BUFFER;
size_t dirFullPathLen = 0;

ELASTIC_APM_MALLOC_STRING_BUFFER_IF_FAILED_GOTO( /* maxLength */ fullPath.length, /* out */ dirFullPath );
dirFullPath.begin[ 0 ] = '\0';
ELASTIC_APM_CALL_IF_FAILED_GOTO( appendToStringBuffer( /* suffixToAppend */ fullPath, dirFullPath, /* in,out */ &dirFullPathLen ) );
ELASTIC_APM_ASSERT_EQ_UINT64( dirFullPathLen, fullPath.length );

char directorySeparator =
# ifdef PHP_WIN32
'\\';
# else // #ifdef PHP_WIN32
'/';
# endif // #ifdef PHP_WIN32

ELASTIC_APM_MALLOC_STRING_BUFFER_IF_FAILED_GOTO( /* maxLength */ fullPath.length, /* out */ dirFullPath );
dirFullPath.begin[ 0 ] = '\0';
ELASTIC_APM_CALL_IF_FAILED_GOTO( appendToStringBuffer( /* suffixToAppend */ fullPath, dirFullPath, /* in,out */ &dirFullPathLen ) );
ELASTIC_APM_ASSERT_EQ_UINT64( dirFullPathLen, fullPath.length );


for ( MutableString begin = &( dirFullPath.begin[ 0 ] ), current = begin + 1, end = begin + fullPath.length ; current != end ; )
{
char* pDirSep = strchr( current, directorySeparator );
Expand Down Expand Up @@ -627,6 +637,7 @@ void debugDumpAstSubTreeConvertedBackToSource( StringView compiledFileFullPath,
int errnoValue = 0;
StringBuffer convertedBackToSourceFileFullPath = ELASTIC_APM_EMPTY_STRING_BUFFER;
zend_string* convertedBackToSourceText = NULL;
String textAsCString;

StringView convertedBackToSourceFileExtensionSuffix = ELASTIC_APM_STRING_LITERAL_TO_VIEW( ".php" );
StringView convertedBackToSourceFileFullPathParts[] = {
Expand All @@ -647,7 +658,7 @@ void debugDumpAstSubTreeConvertedBackToSource( StringView compiledFileFullPath,

ELASTIC_APM_LOG_INFO( "Printing AST converted back to source of %s to %s ...", compiledFileFullPath.begin, convertedBackToSourceFileFullPath.begin );
convertedBackToSourceText = zend_ast_export( /* prefix */ "", ast, /* suffix */ "" );
String textAsCString = nullableZStringToString( convertedBackToSourceText );
textAsCString = nullableZStringToString( convertedBackToSourceText );
if ( textAsCString == NULL )
{
ELASTIC_APM_LOG_INFO( "Nothing to print for AST of %s converted back to source to %s", compiledFileFullPath.begin, convertedBackToSourceFileFullPath.begin );
Expand Down Expand Up @@ -689,13 +700,17 @@ void debugDumpAstSubTreeToFile( StringView compiledFileFullPath, zend_ast* ast,
char txtOutStreamBuf[ELASTIC_APM_TEXT_OUTPUT_STREAM_ON_STACK_BUFFER_SIZE];
TextOutputStream txtOutStream = ELASTIC_APM_TEXT_OUTPUT_STREAM_FROM_STATIC_BUFFER( txtOutStreamBuf );

DebugDumpAstPrintToFileCtx ctx;
DebugDumpAstPrinter printer;


ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY_MSG( "compiledFileFullPath: %s", compiledFileFullPath.begin );

StringView pathPrefix = stringBufferToView( g_astProcessDebugDumpForPathPrefix );
if ( ! isFileSystemPathPrefix( compiledFileFullPath, pathPrefix ) )
{
ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY_MSG( "Skipping this file because it does not have required prefix: %s", pathPrefix.begin );
ELASTIC_APM_SET_RESULT_CODE_TO_SUCCESS_AND_GOTO_FINALLY();
return;
}

StringView compiledFileRelativePath = subStringView( compiledFileFullPath, pathPrefix.length );
Expand All @@ -719,8 +734,8 @@ void debugDumpAstSubTreeToFile( StringView compiledFileFullPath, zend_ast* ast,
}

ELASTIC_APM_LOG_INFO( "Printing AST debug dump of %s to %s ...", compiledFileFullPath.begin, debugDumpFileFullPath.begin );
DebugDumpAstPrintToFileCtx ctx = (DebugDumpAstPrintToFileCtx){ .outFile = debugDumpFile };
DebugDumpAstPrinter printer = (DebugDumpAstPrinter){ .printLine = &debugDumpAstPrintLineToFile, .ctx = &ctx };
ctx = (DebugDumpAstPrintToFileCtx){ .outFile = debugDumpFile };
printer = (DebugDumpAstPrinter){ .printLine = &debugDumpAstPrintLineToFile, .ctx = &ctx };
debugDumpAst( &printer, ast, /* nestingDepth */ 0 );
ELASTIC_APM_LOG_INFO( "Printed AST debug dump of %s to %s", compiledFileFullPath.begin, debugDumpFileFullPath.begin );

Expand Down Expand Up @@ -813,7 +828,7 @@ void astProcessDebugDumpOnRequestInit( const ConfigSnapshot* config )

if ( config->astProcessDebugDumpOutDir == NULL )
{
ELASTIC_APM_SET_RESULT_CODE_TO_SUCCESS_AND_GOTO_FINALLY();
return;
}

StringView pathPrefix = config->astProcessDebugDumpForPathPrefix == NULL ? ELASTIC_APM_EMPTY_STRING_VIEW : stringToView( config->astProcessDebugDumpForPathPrefix );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
* under the License.
*/

extern "C" {
#include <Zend/zend_ast.h>
}

#include "AST_instrumentation.h"
#include "ConfigSnapshot.h"
#include "ConfigManager.h"
Expand Down Expand Up @@ -82,7 +86,7 @@ bool getStringFromAstZVal( zend_ast* astZval, /* out */ StringView* pResult )

zend_string* zString = Z_STR_P( zVal );
*pResult = zStringToStringView( zString );
ELASTIC_APM_LOG_TRACE( "Returning true - with result string [length: %"PRIu64"]: %.*s", (UInt64)(pResult->length), (int)(pResult->length), pResult->begin );
ELASTIC_APM_LOG_TRACE( "Returning true - with result string [length: %" PRIu64 "]: %.*s", (UInt64)(pResult->length), (int)(pResult->length), pResult->begin );
return true;
}

Expand All @@ -95,7 +99,7 @@ bool getAstDeclName( zend_ast_decl* astDecl, /* out */ StringView* name )
}

*name = zStringToStringView( astDecl->name );
ELASTIC_APM_LOG_TRACE( "Returning true - name [length: %"PRIu64"]: %.*s", (UInt64)(name->length), (int)(name->length), name->begin );
ELASTIC_APM_LOG_TRACE( "Returning true - name [length: %" PRIu64 "]: %.*s", (UInt64)(name->length), (int)(name->length), name->begin );
return true;
}

Expand Down Expand Up @@ -214,6 +218,8 @@ zend_ast* createAstEx( zend_ast_kind kind, zend_ast_attr attr, ZendAstPtrArrayVi
case 5:
return zend_ast_create_ex( kind, attr, children.values[ 0 ], children.values[ 1 ], children.values[ 2 ], children.values[ 3 ], children.values[ 4 ] );
#endif
default: // silence compiler warning
return nullptr;
}
}

Expand Down Expand Up @@ -592,6 +598,9 @@ ResultCode insertAstForFunctionPreHook( zend_ast_decl* funcAstDecl, ArgCaptureSp
ELASTIC_APM_ASSERT( funcAstDecl->kind == ZEND_AST_FUNC_DECL || funcAstDecl->kind == ZEND_AST_METHOD, "funcAstDecl->kind: %s", streamZendAstKind( funcAstDecl->kind, &txtOutStream ) );
textOutputStreamRewind( &txtOutStream );

zend_ast* originalFuncBodyAst = nullptr;
zend_ast* preHookCallAstArgList = nullptr;

StringView dbgFuncName;
if ( ! getAstDeclName( funcAstDecl, /* out */ &dbgFuncName ) )
{
Expand All @@ -601,7 +610,7 @@ ResultCode insertAstForFunctionPreHook( zend_ast_decl* funcAstDecl, ArgCaptureSp
ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY_MSG( "dbgFuncName: %s, compiled_filename: %s", dbgFuncName.begin, dbgCompiledFileName );
debugDumpAstTreeToLog( (zend_ast*) funcAstDecl, logLevel_debug );

zend_ast* originalFuncBodyAst = funcAstDecl->child[ g_funcDeclBodyChildIndex ];
originalFuncBodyAst = funcAstDecl->child[ g_funcDeclBodyChildIndex ];
if ( originalFuncBodyAst == NULL )
{
ELASTIC_APM_LOG_TRACE( "originalFuncBodyAst == NULL" );
Expand All @@ -614,7 +623,6 @@ ResultCode insertAstForFunctionPreHook( zend_ast_decl* funcAstDecl, ArgCaptureSp
ELASTIC_APM_SET_RESULT_CODE_AND_GOTO_FAILURE();
}

zend_ast* preHookCallAstArgList = NULL;
ELASTIC_APM_CALL_IF_FAILED_GOTO( createPreHookAstArgListByCaptureSpec( funcAstDecl, argCaptureSpecs, /* out */ &preHookCallAstArgList ) );

funcAstDecl->child[ g_funcDeclBodyChildIndex ] = createAstListWithTwoChildren(
Expand Down Expand Up @@ -683,6 +691,9 @@ ResultCode appendDirectCallToInstrumentation( zend_ast_decl** pAstChildSlot, Str
zend_ast_decl* appendToAstDecl = *pAstChildSlot;
uint32_t lineNumber = appendToAstDecl->end_lineno;

zend_ast* appendedCallAstArgList = nullptr;
zend_ast* appendedCallAst = nullptr;

ELASTIC_APM_ASSERT( appendToAstDecl->kind == ZEND_AST_FUNC_DECL, "appendToAst->kind: %s", streamZendAstKind( appendToAstDecl->kind, &txtOutStream ) );
textOutputStreamRewind( &txtOutStream );

Expand All @@ -696,8 +707,8 @@ ResultCode appendDirectCallToInstrumentation( zend_ast_decl** pAstChildSlot, Str
ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY_MSG( "dbgFuncName: %s, compiled_filename: %s", dbgFuncName.begin, dbgCompiledFileName );
debugDumpAstTreeToLog( (zend_ast*) ( *pAstChildSlot ), logLevel_debug );

zend_ast* appendedCallAstArgList = createDirectCallAstArgList( lineNumber, constNameForMethodName );
zend_ast* appendedCallAst = createAstStandaloneFqFunctionCall( g_elastic_apm_ast_instrumentation_direct_call_funcName, appendedCallAstArgList );
appendedCallAstArgList = createDirectCallAstArgList( lineNumber, constNameForMethodName );
appendedCallAst = createAstStandaloneFqFunctionCall( g_elastic_apm_ast_instrumentation_direct_call_funcName, appendedCallAstArgList );

*((zend_ast**)pAstChildSlot) = createAstListWithTwoChildren( ZEND_AST_STMT_LIST, (zend_ast*) appendToAstDecl, appendedCallAst );

Expand Down Expand Up @@ -847,7 +858,7 @@ ResultCode cloneFallbackAst( zend_ast* ast, uint32_t lineNumber, /* out */ zend_

if ( children.count != 0 )
{
clonedChildren = emalloc( sizeof( zend_ast* ) * children.count );
clonedChildren = static_cast<zend_ast **>(emalloc(sizeof( zend_ast* ) * children.count));
}
ELASTIC_APM_FOR_EACH_INDEX( i, children.count )
{
Expand Down Expand Up @@ -1303,6 +1314,7 @@ ResultCode wrapStandaloneFunctionAstWithPrePostHooks( /* in,out */ zend_ast_decl
zend_ast_decl* originalFuncAstDecl = *pAstChildSlot; // it's not created by this function, so we should NOT clean it on failure
StringBuffer wrappedFunctionNewName = ELASTIC_APM_EMPTY_STRING_BUFFER;
zend_ast_decl* wrapperFuncAst = NULL;
zend_ast* newCombinedAst = NULL;

ELASTIC_APM_ASSERT( originalFuncAstDecl->kind == ZEND_AST_FUNC_DECL, "originalFuncAstDecl->kind: %s", streamZendAstKind( originalFuncAstDecl->kind, &txtOutStream ) );
textOutputStreamRewind( &txtOutStream );
Expand All @@ -1318,7 +1330,7 @@ ResultCode wrapStandaloneFunctionAstWithPrePostHooks( /* in,out */ zend_ast_decl

ELASTIC_APM_CALL_IF_FAILED_GOTO( createWrappedFunctionNewName( originalFuncName, /* out */ &wrappedFunctionNewName ) );
ELASTIC_APM_CALL_IF_FAILED_GOTO( createWrapperFunctionAst( originalFuncAstDecl, stringBufferToView( wrappedFunctionNewName ), /* out */ &wrapperFuncAst ) );
zend_ast* newCombinedAst = createAstListWithTwoChildren( ZEND_AST_STMT_LIST, (zend_ast*)originalFuncAstDecl, (zend_ast*)wrapperFuncAst );
newCombinedAst = createAstListWithTwoChildren( ZEND_AST_STMT_LIST, (zend_ast*)originalFuncAstDecl, (zend_ast*)wrapperFuncAst );
newCombinedAst->lineno = findAstDeclStartLineNumber( originalFuncAstDecl );
originalFuncAstDecl->name = createZStringForAst( stringBufferToView( wrappedFunctionNewName ) );
*((zend_ast**)pAstChildSlot) = newCombinedAst;
Expand Down Expand Up @@ -1425,7 +1437,7 @@ bool parseAstNamespace( zend_ast* astNamespace, /* out */ StringView* pName, /*

*pName = name;
*pEnclosedScope = enclosedScopeAst;
ELASTIC_APM_LOG_TRACE( "Returning true - name [length: %"PRIu64"]: %.*s", (UInt64)(pName->length), (int)(pName->length), pName->begin );
ELASTIC_APM_LOG_TRACE( "Returning true - name [length: %" PRIu64 "]: %.*s", (UInt64)(pName->length), (int)(pName->length), pName->begin );
return true;
}

Expand Down Expand Up @@ -1470,7 +1482,7 @@ bool findAstOfKindCheckNode( zend_ast* ast, zend_ast_kind kindToFind, StringView

#pragma clang diagnostic push
#pragma ide diagnostic ignored "misc-no-recursion"
zend_ast** findChildSlotAstByKind( zend_ast* ast, zend_ast_kind kindToFind, StringView namespace, StringView name, CheckFindAstReqs checkFindAstReqs, void* checkFindAstReqsCtx )
zend_ast** findChildSlotAstByKind( zend_ast* ast, zend_ast_kind kindToFind, StringView nameSpace, StringView name, CheckFindAstReqs checkFindAstReqs, void* checkFindAstReqsCtx )
{
if ( ! zend_ast_is_list( ast ) )
{
Expand All @@ -1483,7 +1495,7 @@ zend_ast** findChildSlotAstByKind( zend_ast* ast, zend_ast_kind kindToFind, Stri
zend_ast* child = astAsList->child[ i ];
if ( zend_ast_is_list( child ) )
{
zend_ast** foundAst = findChildSlotAstByKind( child, kindToFind, namespace, name, checkFindAstReqs, checkFindAstReqsCtx );
zend_ast** foundAst = findChildSlotAstByKind( child, kindToFind, nameSpace, name, checkFindAstReqs, checkFindAstReqsCtx );
if ( foundAst != NULL )
{
return foundAst;
Expand All @@ -1500,14 +1512,14 @@ zend_ast** findChildSlotAstByKind( zend_ast* ast, zend_ast_kind kindToFind, Stri
continue;
}

if ( ! areStringViewsEqual( foundNamespaceName, namespace ) )
if ( ! areStringViewsEqual( foundNamespaceName, nameSpace ) )
{
continue;
}

if ( namespaceEnclosedScope != NULL )
{
zend_ast** foundAst = findChildSlotAstByKind( namespaceEnclosedScope, kindToFind, namespace, name, checkFindAstReqs, checkFindAstReqsCtx );
zend_ast** foundAst = findChildSlotAstByKind( namespaceEnclosedScope, kindToFind, nameSpace, name, checkFindAstReqs, checkFindAstReqsCtx );
if ( foundAst != NULL )
{
return foundAst;
Expand All @@ -1527,14 +1539,14 @@ zend_ast** findChildSlotAstByKind( zend_ast* ast, zend_ast_kind kindToFind, Stri
}
#pragma clang diagnostic pop

zend_ast_decl** findChildSlotForStandaloneFunctionAst( zend_ast* rootAst, StringView namespace, StringView funcName, size_t minParamsCount )
zend_ast_decl** findChildSlotForStandaloneFunctionAst( zend_ast* rootAst, StringView nameSpace, StringView funcName, size_t minParamsCount )
{
return (zend_ast_decl**) findChildSlotAstByKind( rootAst, ZEND_AST_FUNC_DECL, namespace, funcName, checkFunctionReqs, &minParamsCount );
return (zend_ast_decl**) findChildSlotAstByKind( rootAst, ZEND_AST_FUNC_DECL, nameSpace, funcName, checkFunctionReqs, &minParamsCount );
}

zend_ast_decl* findClassAst( zend_ast* rootAst, StringView namespace, StringView className )
zend_ast_decl* findClassAst( zend_ast* rootAst, StringView nameSpace, StringView className )
{
zend_ast** result = findChildSlotAstByKind( rootAst, ZEND_AST_CLASS, namespace, className, /* checkFuncDeclReqs */ NULL, /* checkFindAstReqsCtx */ NULL );
zend_ast** result = findChildSlotAstByKind( rootAst, ZEND_AST_CLASS, nameSpace, className, /* checkFuncDeclReqs */ NULL, /* checkFindAstReqsCtx */ NULL );
return (zend_ast_decl*)(*result);
}

Expand Down
4 changes: 2 additions & 2 deletions agent/native/ext/AST_instrumentation.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ void astInstrumentationOnModuleShutdown();
void astInstrumentationOnRequestInit( const ConfigSnapshot* config );
void astInstrumentationOnRequestShutdown();

zend_ast_decl** findChildSlotForStandaloneFunctionAst( zend_ast* rootAst, StringView namespace, StringView funcName, size_t minParamsCount );
zend_ast_decl* findClassAst( zend_ast* rootAst, StringView namespace, StringView className );
zend_ast_decl** findChildSlotForStandaloneFunctionAst( zend_ast* rootAst, StringView nameSpace, StringView funcName, size_t minParamsCount );
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 );
Expand Down
8 changes: 8 additions & 0 deletions agent/native/ext/ArrayView.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@
#include "basic_macros.h"
#include "StringView.h"

//TODO bugprone-macro-parentheses - use std::span or any other container
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunknown-pragmas"

#pragma clang diagnostic push
#pragma ide diagnostic ignored "bugprone-macro-parentheses"


#define ELASTIC_APM_DECLARE_ARRAY_VIEW( ValueType, ArrayViewType ) \
struct ArrayViewType \
{ \
Expand All @@ -36,6 +41,9 @@

#pragma clang diagnostic pop

#pragma GCC diagnostic pop


ELASTIC_APM_DECLARE_ARRAY_VIEW( bool, BoolArrayView );
ELASTIC_APM_DECLARE_ARRAY_VIEW( int, IntArrayView );
ELASTIC_APM_DECLARE_ARRAY_VIEW( StringView, StringViewArrayView );
Expand Down
Loading
Loading