Skip to content

Commit 6965136

Browse files
committed
Fix blocking on PHP 7.0-7.1 ZTS
1 parent 679da8e commit 6965136

File tree

12 files changed

+45
-13
lines changed

12 files changed

+45
-13
lines changed

appsec/src/extension/commands/request_exec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ dd_result dd_request_exec(dd_conn *nonnull conn, zval *nonnull data)
3838

3939
struct ctx ctx = {.data = data};
4040

41-
return dd_command_exec(conn, &_spec, &ctx);
41+
return dd_command_exec_req_info(conn, &_spec, &ctx.req_info);
4242
}
4343

4444
static dd_result _pack_command(mpack_writer_t *nonnull w, void *nonnull _ctx)

appsec/src/extension/commands/request_init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static const dd_command_spec _spec = {
4444
dd_result dd_request_init(
4545
dd_conn *nonnull conn, struct req_info_init *nonnull ctx)
4646
{
47-
return dd_command_exec(conn, &_spec, ctx);
47+
return dd_command_exec_req_info(conn, &_spec, &ctx->req_info);
4848
}
4949

5050
static dd_result _request_pack(mpack_writer_t *nonnull w, void *nonnull _ctx)

appsec/src/extension/commands/request_shutdown.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static const dd_command_spec _spec = {
2828
dd_result dd_request_shutdown(
2929
dd_conn *nonnull conn, struct req_shutdown_info *nonnull req_info)
3030
{
31-
return dd_command_exec(conn, &_spec, req_info);
31+
return dd_command_exec_req_info(conn, &_spec, &req_info->req_info);
3232
}
3333

3434
static dd_result _request_pack(mpack_writer_t *nonnull w, void *nonnull ctx)

appsec/src/extension/commands_ctx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <php.h>
55

66
struct req_info {
7+
const char *nullable command_name; // for logging
78
zend_object *nullable root_span;
89
zend_string *nullable client_ip;
910
};

appsec/src/extension/commands_helpers.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ dd_result ATTR_WARN_UNUSED dd_command_exec(dd_conn *nonnull conn,
194194
return _dd_command_exec(conn, false, spec, ctx);
195195
}
196196

197+
dd_result ATTR_WARN_UNUSED dd_command_exec_req_info(dd_conn *nonnull conn,
198+
const dd_command_spec *nonnull spec, struct req_info *nonnull ctx)
199+
{
200+
ctx->command_name = spec->name;
201+
return _dd_command_exec(conn, false, spec, ctx);
202+
}
203+
197204
dd_result ATTR_WARN_UNUSED dd_command_exec_cred(dd_conn *nonnull conn,
198205
const dd_command_spec *nonnull spec, void *unspecnull ctx)
199206
{
@@ -361,6 +368,8 @@ static void _command_process_block_parameters(mpack_node_t root)
361368
}
362369
}
363370

371+
mlog(dd_log_debug, "Blocking parameters: status_code=%d, type=%d",
372+
status_code, type);
364373
dd_set_block_code_and_type(status_code, type);
365374
}
366375

@@ -425,7 +434,8 @@ dd_result dd_command_proc_resp_verd_span_data(
425434
if (verd_len > INT_MAX) {
426435
verd_len = INT_MAX;
427436
}
428-
mlog(dd_log_debug, "Verdict of request_init was '%.*s'", (int)verd_len,
437+
mlog(dd_log_debug, "Verdict of %s was '%.*s'",
438+
ctx->command_name ? ctx->command_name : "(unknown)", (int)verd_len,
429439
verd_str);
430440
}
431441

appsec/src/extension/commands_helpers.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ typedef struct _dd_command_spec {
2525
dd_result ATTR_WARN_UNUSED dd_command_exec(dd_conn *nonnull conn,
2626
const dd_command_spec *nonnull spec, void *unspecnull ctx);
2727

28+
dd_result ATTR_WARN_UNUSED dd_command_exec_req_info(dd_conn *nonnull conn,
29+
const dd_command_spec *nonnull spec, struct req_info *nonnull ctx);
30+
2831
dd_result ATTR_WARN_UNUSED dd_command_exec_cred(dd_conn *nonnull conn,
2932
const dd_command_spec *nonnull spec, void *unspecnull ctx);
3033

appsec/src/extension/php_compat.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@ static zend_always_inline zend_string *zend_string_init_interned(
2929
const char *str, size_t len, int persistent)
3030
{
3131
zend_string *ret = zend_string_init(str, len, persistent);
32+
# ifdef ZTS
33+
// believe it or not zend_new_interned_string() is an identity function
34+
// set the interned flag manually so zend_string_release() is a no-op
35+
GC_FLAGS(ret) |= IS_STR_INTERNED;
36+
zend_string_hash_val(ret);
37+
return ret;
38+
# else
3239
return zend_new_interned_string(ret);
40+
# endif
3341
}
3442
#endif
3543

appsec/src/extension/request_abort.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ static void _set_content_type(const char *nonnull content_type)
128128
static void _set_output(const char *nonnull output, size_t length)
129129
{
130130
size_t written = php_output_write(output, length);
131+
mlog_g(dd_log_debug, "php_output_write() returned %zu", written);
131132
if (written != length) {
132133
mlog(dd_log_info, "could not write full response (written: %zu)",
133134
written);
@@ -146,8 +147,8 @@ static dd_response_type _get_response_type_from_accept_header(
146147
dd_php_get_string_elem_cstr(_server, LSTRARG("HTTP_ACCEPT"));
147148
if (!accept_zstr) {
148149
mlog(dd_log_info,
149-
"Could not find Accept header, using default content-type");
150-
goto exit;
150+
"Could not find Accept header, using default content-type (json)");
151+
return response_type_json;
151152
}
152153

153154
const char *accept_end = ZSTR_VAL(accept_zstr) + ZSTR_LEN(accept_zstr);
@@ -172,7 +173,7 @@ static dd_response_type _get_response_type_from_accept_header(
172173
return response_type_html;
173174
}
174175

175-
exit:
176+
mlog_g(dd_log_debug, "No recognized accept header, defaulting to json");
176177
return response_type_json;
177178
}
178179

@@ -214,13 +215,17 @@ void dd_request_abort_redirect()
214215
}
215216

216217
if (!_abort_prelude()) {
218+
mlog(dd_log_debug, "_abort_prelude has failed");
217219
return;
218220
}
219221

220222
char *line;
221223
uint line_len = (uint)spprintf(
222224
&line, 0, "Location: %s", ZSTR_VAL(_redirection_location));
223225

226+
mlog_g(dd_log_debug, "Will forward to %s with status %d",
227+
ZSTR_VAL(_redirection_location), _redirection_response_code);
228+
224229
SG(sapi_headers).http_response_code = _redirection_response_code;
225230
int res = sapi_header_op(SAPI_HEADER_REPLACE,
226231
&(sapi_header_line){.line = line, .line_len = line_len});
@@ -232,7 +237,7 @@ void dd_request_abort_redirect()
232237
efree(line);
233238

234239
if (sapi_flush() == SUCCESS) {
235-
mlog(dd_log_debug, "Successful call to sapi_flush()");
240+
mlog_g(dd_log_debug, "Successful call to sapi_flush()");
236241
} else {
237242
mlog(dd_log_warning, "Call to sapi_flush() failed");
238243
}
@@ -299,6 +304,7 @@ void _request_abort_static_page(int response_code, int type)
299304
}
300305

301306
if (!_abort_prelude()) {
307+
mlog(dd_log_debug, "_abort_prelude has failed");
302308
zend_string_release(body);
303309
return;
304310
}
@@ -434,8 +440,10 @@ static void _suppress_error_reporting(void);
434440
ATTR_FORMAT(1, 2)
435441
static void _emit_error(const char *format, ...)
436442
{
437-
va_list args;
443+
mlog_g(dd_log_debug, "_emit_error() called: during_request_startup: %d",
444+
PG(during_request_startup));
438445

446+
va_list args;
439447
va_start(args, format);
440448
if (PG(during_request_startup)) {
441449
/* if emitting error during startup, RSHUTDOWN will not run (except fpm)
@@ -615,6 +623,7 @@ static zend_string *nonnull _get_json_blocking_template()
615623
return _empty_zstr;
616624
}
617625
if (ZSTR_LEN(body_error_json) == 0) {
626+
zend_string_release(body_error_json);
618627
return _body_error_json_def;
619628
}
620629

@@ -635,6 +644,7 @@ static zend_string *nonnull _get_html_blocking_template()
635644
return _empty_zstr;
636645
}
637646
if (ZSTR_LEN(body_error_html) == 0) {
647+
zend_string_release(body_error_html);
638648
return _body_error_html_def;
639649
}
640650

appsec/tests/integration/src/docker/apache2-fpm/entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ enable_extensions.sh
2020
php-fpm -y /etc/php-fpm.conf -c /etc/php/php.ini
2121
service apache2 start
2222

23-
exec tail -F "${LOGS_PHP[@]}" "${LOGS_APACHE[@]}"
23+
exec tail -n +1 -F "${LOGS_PHP[@]}" "${LOGS_APACHE[@]}"

appsec/tests/integration/src/docker/apache2-mod/entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ enable_extensions.sh
1919

2020
service apache2 start
2121

22-
exec tail -F "${LOGS_PHP[@]}" "${LOGS_APACHE[@]}"
22+
exec tail -n +1 -F "${LOGS_PHP[@]}" "${LOGS_APACHE[@]}"

appsec/tests/integration/src/docker/nginx-fpm/entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ enable_extensions.sh
1616
php-fpm -y /etc/php-fpm.conf -c /etc/php/php.ini
1717
service nginx start
1818

19-
exec tail -F "${LOGS_PHP[@]}" "${LOGS_NGINX[@]}"
19+
exec tail -n +1 -F "${LOGS_PHP[@]}" "${LOGS_NGINX[@]}"

appsec/tests/integration/src/test/www/roadrunner/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ echo datadog.trace.cli_enabled=true >> /etc/php/php.ini
1818

1919
./rr serve 2>&1 >> /tmp/logs/rr.log &
2020

21-
tail -f "${LOGS_PHP[@]}"
21+
tail -n +1 -F "${LOGS_PHP[@]}"
2222

0 commit comments

Comments
 (0)