Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit db6c507

Browse files
authored
Fix builds for 32 bit architecture (#15)
* Run tests against 7.1 and 7.0 32 bit architecture. * Fix segfaults for 32-bit builds. zend_parse_parameters is not setting optional zval pointers to NULL. Instead, we need to default the pointer to NULL so we can check for it and set a default value. * Fix the generation of span ids to always be positive integers. php_mt_rand() returns 32 bits of randomness even for 64 bit integers. In the 64 bit case, a 0 is always right shifted into the 32nd bit. In the 32 bit case, a right shift is dependent on the compiler implementation. Generally, negative numbers will remain negative to make bitshift arthmatic work. * Fix implicit declaration warnings * Add comment about why we're casting the php_mt_rand() before bitshifting. * Fix allocation of default zval for PHP 7.0
1 parent ef39ce2 commit db6c507

5 files changed

Lines changed: 25 additions & 6 deletions

File tree

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ ENV TEST_PHP_ARGS="-q" \
3333
REPORT_EXIT_STATUS=1
3434

3535
RUN phpize && \
36-
./configure --enable-stackdriver-trace && \
36+
./configure --enable-opencensus && \
3737
make clean && \
3838
make && \
3939
make test && \

cloudbuild.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ steps:
1212
args: ['build', '--build-arg', 'BASE_IMAGE=gcr.io/php-mvm-a/php72:alpha3', '.']
1313
waitFor: ['-']
1414
id: php72
15+
- name: gcr.io/cloud-builders/docker
16+
args: ['build', '--build-arg', 'BASE_IMAGE=gcr.io/php-stackdriver/php71-32bit', '.']
17+
waitFor: ['-']
18+
id: php71-32bit
19+
- name: gcr.io/cloud-builders/docker
20+
args: ['build', '--build-arg', 'BASE_IMAGE=gcr.io/php-stackdriver/php70-32bit', '.']
21+
waitFor: ['-']
22+
id: php70-32bit

ext/opencensus_trace.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include "php_opencensus.h"
1818
#include "opencensus_trace.h"
19+
#include "opencensus_trace_span.h"
20+
#include "opencensus_trace_context.h"
1921
#include "Zend/zend_compile.h"
2022
#include "Zend/zend_closures.h"
2123
#include "zend_extensions.h"
@@ -250,7 +252,13 @@ static opencensus_trace_span_t *opencensus_trace_begin(zend_string *function_nam
250252
php_mt_srand(GENERATE_SEED());
251253
}
252254
#endif
253-
span->span_id = (php_mt_rand() >> 1);
255+
/**
256+
* Force the random span id to be positive. php_mt_rand() generates 32 bits
257+
* of randomness. On 32-bit systems, we must cast to an unsigned int before
258+
* bitshifting to force a positive number. We're ok to lose on bit of
259+
* randomness because previous versions of mt_rand only generated 31 bits.
260+
*/
261+
span->span_id = ((uint32_t) php_mt_rand()) >> 1;
254262

255263
if (OPENCENSUS_TRACE_G(current_span)) {
256264
span->parent = OPENCENSUS_TRACE_G(current_span);
@@ -519,14 +527,14 @@ void opencensus_trace_execute_internal(INTERNAL_FUNCTION_PARAMETERS)
519527
PHP_FUNCTION(opencensus_trace_function)
520528
{
521529
zend_string *function_name;
522-
zval *handler, *copy;
530+
zval *handler = NULL, *copy;
531+
zval h;
523532

524533
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|z", &function_name, &handler) == FAILURE) {
525534
RETURN_FALSE;
526535
}
527536

528537
if (handler == NULL) {
529-
zval h;
530538
ZVAL_LONG(&h, 1);
531539
handler = &h;
532540
}
@@ -550,14 +558,14 @@ PHP_FUNCTION(opencensus_trace_function)
550558
PHP_FUNCTION(opencensus_trace_method)
551559
{
552560
zend_string *class_name, *function_name, *key;
553-
zval *handler, *copy;
561+
zval *handler = NULL, *copy;
562+
zval h;
554563

555564
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SS|z", &class_name, &function_name, &handler) == FAILURE) {
556565
RETURN_FALSE;
557566
}
558567

559568
if (handler == NULL) {
560-
zval h;
561569
ZVAL_LONG(&h, 1);
562570
handler = &h;
563571
}

ext/opencensus_trace_context.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@
2121

2222
extern zend_class_entry* opencensus_trace_context_ce;
2323

24+
int opencensus_trace_context_minit(INIT_FUNC_ARGS);
25+
2426
#endif /* PHP_OPENCENSUS_TRACE_CONTEXT_H */

ext/opencensus_trace_span.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ int opencensus_trace_span_add_label_str(opencensus_trace_span_t *span, char *k,
3939
int opencensus_trace_span_apply_span_options(opencensus_trace_span_t *span, zval *span_options);
4040
opencensus_trace_span_t *opencensus_trace_span_alloc();
4141
void opencensus_trace_span_free(opencensus_trace_span_t *span);
42+
int opencensus_trace_span_minit(INIT_FUNC_ARGS);
4243

4344
#endif /* PHP_OPENCENSUS_TRACE_SPAN_H */

0 commit comments

Comments
 (0)