Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
glaslos committed Aug 13, 2023
1 parent 440b41d commit 2ce0c55
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 93 deletions.
43 changes: 21 additions & 22 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,31 @@ name: C/C++ CI

on:
push:
branches: [ "master" ]
branches: ["master"]
pull_request:
branches: [ "master" ]
branches: ["master"]

jobs:
build:

runs-on: ubuntu-latest

steps:
- name: dependencies
run: sudo apt-get install -y php8.1 php8.1-dev
- name: php version
run: php --version
- uses: actions/checkout@v3
- name: phpize
run: phpize
- name: configure
run: ./configure --enable-bfr
- name: make
run: make
- name: make install
run: sudo make install
- name: enable extension
run: |
sudo php --ini | grep Loaded
echo "zend_extension = /usr/lib/php/20210902/bfr.so" | sudo tee -a /etc/php/8.1/cli/php.ini
- name: php version test
run: php --version | grep "with Better Function Replacer"
- name: dependencies
run: sudo apt-get install -y php8.1 php8.1-dev
- name: php version
run: php --version
- uses: actions/checkout@v3
- name: phpize
run: phpize
- name: configure
run: ./configure --enable-bfr
- name: make
run: make
- name: make install
run: sudo make install
- name: enable extension
run: |
sudo php --ini | grep Loaded
echo "zend_extension = /usr/lib/php/20210902/bfr.so" | sudo tee -a /etc/php/8.1/cli/php.ini
- name: php version test
run: php --version | grep "with Better Function Replacer"
81 changes: 32 additions & 49 deletions php_bfr.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,8 @@ ZEND_END_ARG_INFO()

zend_function_entry bfr_functions[] = {
PHP_FE(override_function, arginfo_override_function)
PHP_FE(rename_function, arginfo_rename_function)
{
NULL, NULL, NULL
}
};
PHP_FE(rename_function, arginfo_rename_function){
NULL, NULL, NULL}};

/* --------------------------------------------------------------------------
Module Entry
Expand All @@ -57,8 +54,7 @@ zend_module_entry bfr_module_entry = {
PHP_RSHUTDOWN(bfr),
PHP_MINFO(bfr),
PHP_BFR_VERSION,
STANDARD_MODULE_PROPERTIES
};
STANDARD_MODULE_PROPERTIES};

#if COMPILE_DL_BFR

Expand Down Expand Up @@ -128,20 +124,18 @@ PHP_FUNCTION(override_function)

if (ZEND_NUM_ARGS() != 3 ||
zend_parse_parameters(ZEND_NUM_ARGS(), "sss",
&z_function_name, &function_name_len,
&z_function_args, &function_args_len,
&z_function_code, &function_code_len) == FAILURE)
&z_function_name, &function_name_len,
&z_function_args, &function_args_len,
&z_function_code, &function_code_len) == FAILURE)
{
ZEND_WRONG_PARAM_COUNT();
}

eval_code_length = sizeof(TEMP_OVRD_FUNC_HEADER)
+ function_args_len
+ 2 /* parentheses */
+ 2 /* curlies */
+ function_code_len;
eval_code_length = sizeof(TEMP_OVRD_FUNC_HEADER) + function_args_len + 2 /* parentheses */
+ 2 /* curlies */
+ function_code_len;

eval_code = (char *) emalloc(eval_code_length);
eval_code = (char *)emalloc(eval_code_length);
sprintf(eval_code, TEMP_OVRD_FUNC_PATTERN, z_function_args, z_function_code);
eval_name = zend_make_compiled_string_description(TEMP_OVRD_FUNC_DESC);
retval = zend_eval_string(eval_code, NULL, eval_name);
Expand All @@ -151,47 +145,47 @@ PHP_FUNCTION(override_function)
if (retval != SUCCESS)
{
zend_error(E_ERROR, "%s() failed to eval temporary function",
get_active_function_name());
get_active_function_name());

RETURN_FALSE;
}

if ((func = zend_hash_str_find_ptr(EG(function_table),
TEMP_OVRD_FUNC_NAME, sizeof(TEMP_OVRD_FUNC_NAME) - 1)) == NULL)
TEMP_OVRD_FUNC_NAME, sizeof(TEMP_OVRD_FUNC_NAME) - 1)) == NULL)
{
zend_error(E_ERROR, "%s() temporary function name not present in global function_table",
get_active_function_name());
get_active_function_name());

RETURN_FALSE;
}

func_dup = duplicate_function(func);

if (zend_hash_str_exists(EG(function_table),
z_function_name, function_name_len))
z_function_name, function_name_len))
{
zend_hash_str_del(EG(function_table),
z_function_name, function_name_len);
z_function_name, function_name_len);
}

if (zend_hash_str_add_new_ptr(EG(function_table),
z_function_name, function_name_len,
func_dup) == NULL)
z_function_name, function_name_len,
func_dup) == NULL)
{
zend_error(E_ERROR, "%s() failed to add function",
get_active_function_name());
get_active_function_name());

RETURN_FALSE;
}

if (zend_hash_str_del(EG(function_table), TEMP_OVRD_FUNC_NAME,
sizeof(TEMP_OVRD_FUNC_NAME) - 1) == FAILURE)
sizeof(TEMP_OVRD_FUNC_NAME) - 1) == FAILURE)
{
zend_error(E_ERROR, "%s() failed to delete temporary function",
get_active_function_name());
get_active_function_name());

zend_hash_str_del(EG(function_table),
z_function_name, function_name_len);
z_function_name, function_name_len);

RETURN_FALSE;
}
Expand All @@ -205,26 +199,26 @@ PHP_FUNCTION(rename_function)

if (ZEND_NUM_ARGS() != 2 ||
zend_parse_parameters(ZEND_NUM_ARGS(), "ss",
&z_orig_fname, &orig_fname_len,
&z_new_fname, &new_fname_len) == FAILURE)
&z_orig_fname, &orig_fname_len,
&z_new_fname, &new_fname_len) == FAILURE)
{
ZEND_WRONG_PARAM_COUNT();
}

if ((func = zend_hash_str_find_ptr(EG(function_table),
z_orig_fname, orig_fname_len)) == NULL)
z_orig_fname, orig_fname_len)) == NULL)
{
zend_error(E_WARNING, "%s(%s, %s) failed: %s does not exist!",
get_active_function_name(),
z_orig_fname, z_new_fname, z_orig_fname);
get_active_function_name(),
z_orig_fname, z_new_fname, z_orig_fname);

RETURN_FALSE;
}
if (zend_hash_str_exists(EG(function_table), z_new_fname, new_fname_len))
{
zend_error(E_WARNING, "%s(%s, %s) failed: %s already exists!",
get_active_function_name(),
z_orig_fname, z_new_fname, z_new_fname);
get_active_function_name(),
z_orig_fname, z_new_fname, z_new_fname);

RETURN_FALSE;
}
Expand All @@ -234,14 +228,14 @@ PHP_FUNCTION(rename_function)
if (zend_hash_str_add_ptr(EG(function_table), z_new_fname, new_fname_len, func_dup) == NULL)
{
zend_error(E_WARNING, "%s() failed to insert %s into EG(function_table)",
get_active_function_name(), z_new_fname);
get_active_function_name(), z_new_fname);

RETURN_FALSE;
}
if (zend_hash_str_del(EG(function_table), z_orig_fname, orig_fname_len) == FAILURE)
{
zend_error(E_WARNING, "%s() failed to remove %s from function table",
get_active_function_name(), z_orig_fname);
get_active_function_name(), z_orig_fname);

zend_hash_str_del(EG(function_table), z_new_fname, new_fname_len);

Expand Down Expand Up @@ -275,7 +269,7 @@ ZEND_DLEXPORT zend_extension zend_extension_entry = {
PHP_BFR_VERSION,
"Lukas Rist",
"http://mushmush.org/",
"Copyright (C) 2015",
"Copyright (C) 2023",
bfr_zend_startup,
bfr_zend_shutdown,
NULL, // activate_func_t
Expand All @@ -288,15 +282,4 @@ ZEND_DLEXPORT zend_extension zend_extension_entry = {
NULL, // op_array_ctor_func_t
NULL, // op_array_dtor_func_t
NULL, // api_no_check
COMPAT_ZEND_EXTENSION_PROPERTIES
};

/**
* Local Variables:
* indent-tabs-mode: t
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600:fdm=marker
* vim:noet:sw=4:ts=4
*/
COMPAT_ZEND_EXTENSION_PROPERTIES};
10 changes: 3 additions & 7 deletions php_bfr.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
+----------------------------------------------------------------------+
| Better Function Replacer |
| based on APD Profiler & Debugger |
Expand Down Expand Up @@ -44,18 +44,14 @@ PHP_MINFO_FUNCTION(bfr);

extern zend_module_entry bfr_module_entry;

#define PHP_BFR_VERSION "0.1"
#define PHP_BFR_VERSION "1.0"

ZEND_BEGIN_MODULE_GLOBALS(bfr)
ZEND_END_MODULE_GLOBALS(bfr)

PHPAPI ZEND_EXTERN_MODULE_GLOBALS(bfr)

/* Declare global structure. */
#ifdef ZTS
#define BFR_GLOBALS(v) TSRMG(bfr_globals_id, zend_bfr_globals *, v)
#else
#define BFR_GLOBALS(v) (bfr_globals.v)
#endif

#endif
#endif
28 changes: 14 additions & 14 deletions php_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@

zend_function *duplicate_function(zend_function *func)
{
zend_function *new_function;
zend_function *new_function;

if (func->type != ZEND_INTERNAL_FUNCTION)
{
new_function = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
memcpy(new_function, func, sizeof(zend_op_array));
if (func->type != ZEND_INTERNAL_FUNCTION)
{
new_function = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
memcpy(new_function, func, sizeof(zend_op_array));

function_add_ref(new_function);
function_add_ref(new_function);

return new_function;
}
return new_function;
}

new_function = zend_arena_alloc(&CG(arena), sizeof(zend_internal_function));
memcpy(new_function, func, sizeof(zend_internal_function));
new_function->common.fn_flags |= ZEND_ACC_ARENA_ALLOCATED;
new_function = zend_arena_alloc(&CG(arena), sizeof(zend_internal_function));
memcpy(new_function, func, sizeof(zend_internal_function));
new_function->common.fn_flags |= ZEND_ACC_ARENA_ALLOCATED;

function_add_ref(new_function);
function_add_ref(new_function);

return new_function;
}
return new_function;
}
2 changes: 1 addition & 1 deletion php_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@

zend_function *duplicate_function(zend_function *func);

#endif
#endif

0 comments on commit 2ce0c55

Please sign in to comment.