From: Wez Furlong Date: Sun, 17 Mar 2002 22:50:59 +0000 (+0000) Subject: Streams now make more use of the memory manager, so tracking down X-Git-Tag: php-4.3.0dev-ZendEngine2-Preview1~1296 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d2d8762267de33b954363189b4ec710e70527a2a;p=php Streams now make more use of the memory manager, so tracking down leaking streams should be easier. # I hate these big commits --- diff --git a/ext/exif/exif.c b/ext/exif/exif.c index 37b60276cd..5cb796ee6c 100644 --- a/ext/exif/exif.c +++ b/ext/exif/exif.c @@ -2960,7 +2960,7 @@ int exif_read_file(image_info_type *ImageInfo, char *FileName, int read_thumbnai ImageInfo->motorola_intel = 0; #ifdef HAVE_PHP_STREAM - ImageInfo->infile = php_stream_open_wrapper(FileName, "rb", IGNORE_PATH|ENFORCE_SAFE_MODE, NULL TSRMLS_CC); + ImageInfo->infile = php_stream_open_wrapper(FileName, "rb", IGNORE_PATH|ENFORCE_SAFE_MODE, NULL); #else ImageInfo->infile = VCWD_FOPEN(FileName, "rb"); /* Unix ignores 'b', windows needs it. */ #endif @@ -3264,7 +3264,7 @@ PHP_FUNCTION(exif_imagetype) if (zend_get_parameters_ex(1, &arg1) == FAILURE) WRONG_PARAM_COUNT; - stream = php_stream_open_wrapper(Z_STRVAL_PP(arg1), "rb", IGNORE_PATH|ENFORCE_SAFE_MODE|REPORT_ERRORS, NULL TSRMLS_CC); + stream = php_stream_open_wrapper(Z_STRVAL_PP(arg1), "rb", IGNORE_PATH|ENFORCE_SAFE_MODE|REPORT_ERRORS, NULL); if (stream == NULL) { RETURN_FALSE; diff --git a/ext/ftp/php_ftp.c b/ext/ftp/php_ftp.c index 1012698966..d3fd331bfa 100644 --- a/ext/ftp/php_ftp.c +++ b/ext/ftp/php_ftp.c @@ -480,7 +480,7 @@ PHP_FUNCTION(ftp_get) RETURN_FALSE; } - outstream = php_stream_fopen(local, "wb", NULL TSRMLS_CC); + outstream = php_stream_fopen(local, "wb", NULL); if (outstream == NULL) { php_stream_close(tmpstream); @@ -547,7 +547,7 @@ PHP_FUNCTION(ftp_put) ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf); XTYPE(xtype, mode); - instream = php_stream_fopen(local, "rb", NULL TSRMLS_CC); + instream = php_stream_fopen(local, "rb", NULL); if (instream == NULL) { RETURN_FALSE; diff --git a/ext/gd/gd.c b/ext/gd/gd.c index 30ed567792..93766aa1c2 100644 --- a/ext/gd/gd.c +++ b/ext/gd/gd.c @@ -449,7 +449,7 @@ PHP_FUNCTION(imageloadfont) convert_to_string_ex(file); - stream = php_stream_open_wrapper(Z_STRVAL_PP(file), "rb", IGNORE_PATH|IGNORE_URL_WIN|REPORT_ERRORS, NULL TSRMLS_CC); + stream = php_stream_open_wrapper(Z_STRVAL_PP(file), "rb", IGNORE_PATH|IGNORE_URL_WIN|REPORT_ERRORS, NULL); if (stream == NULL) { RETURN_FALSE; } @@ -1150,7 +1150,7 @@ static void _php_image_create_from(INTERNAL_FUNCTION_PARAMETERS, int image_type, fn = Z_STRVAL_PP(file); - stream = php_stream_open_wrapper(fn, "rb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL TSRMLS_CC); + stream = php_stream_open_wrapper(fn, "rb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL); if (stream == NULL) { RETURN_FALSE; } diff --git a/ext/mailparse/mailparse.c b/ext/mailparse/mailparse.c index b86ee79ade..3df3df56a0 100755 --- a/ext/mailparse/mailparse.c +++ b/ext/mailparse/mailparse.c @@ -193,7 +193,7 @@ PHP_FUNCTION(mailparse_uudecode_all) instream = (php_stream*)zend_fetch_resource(&file TSRMLS_CC, -1, "File-Handle", &type, 1, php_file_le_stream()); ZEND_VERIFY_RESOURCE(instream); - outstream = php_stream_fopen_temporary_file(NULL, "mailparse", &outpath TSRMLS_CC); + outstream = php_stream_fopen_temporary_file(NULL, "mailparse", &outpath); if (outstream == NULL) { zend_error(E_WARNING, "%s(): unable to open temp file", get_active_function_name(TSRMLS_C)); RETURN_FALSE; @@ -232,7 +232,7 @@ PHP_FUNCTION(mailparse_uudecode_all) add_assoc_string(item, "origfilename", origfilename, 1); /* create a temp file for the data */ - partstream = php_stream_fopen_temporary_file(NULL, "mailparse", &outpath TSRMLS_CC); + partstream = php_stream_fopen_temporary_file(NULL, "mailparse", &outpath); if (partstream) { nparts++; add_assoc_string(item, "filename", outpath, 0); diff --git a/ext/ming/ming.c b/ext/ming/ming.c index 7233e3e4d2..7cf91e0027 100644 --- a/ext/ming/ming.c +++ b/ext/ming/ming.c @@ -1157,7 +1157,7 @@ PHP_FUNCTION(swffont_init) php_stream * stream; FILE * file; - stream = php_stream_open_wrapper(Z_STRVAL_PP(zfile), "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL TSRMLS_CC); + stream = php_stream_open_wrapper(Z_STRVAL_PP(zfile), "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); if(stream == NULL) { RETURN_FALSE; @@ -1602,7 +1602,7 @@ PHP_FUNCTION(swfmovie_save) convert_to_string_ex(x); - stream = php_stream_open_wrapper(Z_STRVAL_PP(x), "wb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL TSRMLS_CC); + stream = php_stream_open_wrapper(Z_STRVAL_PP(x), "wb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); if (stream == NULL) RETURN_FALSE; diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c index 43eae5d0d4..e82379127e 100644 --- a/ext/pgsql/pgsql.c +++ b/ext/pgsql/pgsql.c @@ -1468,7 +1468,7 @@ PHP_FUNCTION(pg_trace) ZEND_FETCH_RESOURCE2(pgsql, PGconn *, z_pgsql_link, id, "PostgreSQL link", le_link, le_plink); convert_to_string_ex(z_filename); - stream = php_stream_open_wrapper(Z_STRVAL_PP(z_filename), mode, ENFORCE_SAFE_MODE|REPORT_ERRORS, NULL TSRMLS_CC); + stream = php_stream_open_wrapper(Z_STRVAL_PP(z_filename), mode, ENFORCE_SAFE_MODE|REPORT_ERRORS, NULL); if (!stream) { RETURN_FALSE; diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index b0607df3ea..faec631bfb 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -1005,14 +1005,14 @@ PHP_MINIT_FUNCTION(basic) if (PG(allow_url_fopen)) { - if (FAILURE == php_register_url_stream_wrapper("http", &php_stream_http_wrapper TSRMLS_CC)) + if (FAILURE == php_register_url_stream_wrapper("http", &php_stream_http_wrapper)) return FAILURE; - if (FAILURE == php_register_url_stream_wrapper("php", &php_stream_php_wrapper TSRMLS_CC)) + if (FAILURE == php_register_url_stream_wrapper("php", &php_stream_php_wrapper)) return FAILURE; - if (FAILURE == php_register_url_stream_wrapper("ftp", &php_stream_ftp_wrapper TSRMLS_CC)) + if (FAILURE == php_register_url_stream_wrapper("ftp", &php_stream_ftp_wrapper)) return FAILURE; # if HAVE_OPENSSL_EXT - if (FAILURE == php_register_url_stream_wrapper("https", &php_stream_http_wrapper TSRMLS_CC)) + if (FAILURE == php_register_url_stream_wrapper("https", &php_stream_http_wrapper)) return FAILURE; # endif } @@ -1030,11 +1030,11 @@ PHP_MSHUTDOWN_FUNCTION(basic) #endif if (PG(allow_url_fopen)) { - php_unregister_url_stream_wrapper("http" TSRMLS_CC); - php_unregister_url_stream_wrapper("ftp" TSRMLS_CC); - php_unregister_url_stream_wrapper("php" TSRMLS_CC); + php_unregister_url_stream_wrapper("http"); + php_unregister_url_stream_wrapper("ftp"); + php_unregister_url_stream_wrapper("php"); # if HAVE_OPENSSL_EXT - php_unregister_url_stream_wrapper("https" TSRMLS_CC); + php_unregister_url_stream_wrapper("https"); # endif } @@ -1532,7 +1532,7 @@ PHPAPI int _php_error_log(int opt_err, char *message, char *opt, char *headers T break; case 3: /*save to a file */ - stream = php_stream_open_wrapper(opt, "a", IGNORE_URL | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL TSRMLS_CC); + stream = php_stream_open_wrapper(opt, "a", IGNORE_URL | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL); if (!stream) return FAILURE; php_stream_write(stream, message, strlen(message)); diff --git a/ext/standard/ftp_fopen_wrapper.c b/ext/standard/ftp_fopen_wrapper.c index ade1e86f36..8cd85b30d2 100644 --- a/ext/standard/ftp_fopen_wrapper.c +++ b/ext/standard/ftp_fopen_wrapper.c @@ -85,7 +85,7 @@ php_stream_wrapper php_stream_ftp_wrapper = { /* {{{ php_fopen_url_wrap_ftp */ -php_stream * php_stream_url_wrap_ftp(char *path, char *mode, int options, char **opened_path TSRMLS_DC) +php_stream * php_stream_url_wrap_ftp(char *path, char *mode, int options, char **opened_path STREAMS_DC) { php_stream *stream=NULL; php_url *resource=NULL; diff --git a/ext/standard/http_fopen_wrapper.c b/ext/standard/http_fopen_wrapper.c index 9cada1fb52..c5e5e3c037 100644 --- a/ext/standard/http_fopen_wrapper.c +++ b/ext/standard/http_fopen_wrapper.c @@ -71,7 +71,7 @@ #define HTTP_HEADER_BLOCK_SIZE 1024 -php_stream *php_stream_url_wrap_http(char *path, char *mode, int options, char **opened_path TSRMLS_DC) +php_stream *php_stream_url_wrap_http(char *path, char *mode, int options, char **opened_path STREAMS_DC) { php_stream *stream = NULL; php_url *resource = NULL; @@ -268,7 +268,7 @@ php_stream *php_stream_url_wrap_http(char *path, char *mode, int options, char * else { strlcpy(new_path, location, sizeof(new_path)); } - stream = php_stream_url_wrap_http(new_path, mode, options, opened_path TSRMLS_CC); + stream = php_stream_url_wrap_http(new_path, mode, options, opened_path STREAMS_CC); if (stream->wrapperdata) { entryp = &entry; MAKE_STD_ZVAL(entry); diff --git a/ext/standard/image.c b/ext/standard/image.c index 9761fb1845..73baa53158 100644 --- a/ext/standard/image.c +++ b/ext/standard/image.c @@ -717,7 +717,7 @@ PHP_FUNCTION(getimagesize) break; } - stream = php_stream_open_wrapper(Z_STRVAL_PP(arg1), "rb", REPORT_ERRORS|IGNORE_PATH|ENFORCE_SAFE_MODE, NULL TSRMLS_CC); + stream = php_stream_open_wrapper(Z_STRVAL_PP(arg1), "rb", REPORT_ERRORS|IGNORE_PATH|ENFORCE_SAFE_MODE, NULL); if (!stream) { RETURN_FALSE; diff --git a/ext/standard/php_fopen_wrapper.c b/ext/standard/php_fopen_wrapper.c index 37b3b2b90f..3a56bd6706 100644 --- a/ext/standard/php_fopen_wrapper.c +++ b/ext/standard/php_fopen_wrapper.c @@ -30,7 +30,7 @@ #include "php_standard.h" #include "php_fopen_wrappers.h" -php_stream * php_stream_url_wrap_php(char * path, char * mode, int options, char ** opened_path TSRMLS_DC) +php_stream * php_stream_url_wrap_php(char * path, char * mode, int options, char ** opened_path STREAMS_DC) { FILE * fp = NULL; php_stream * stream = NULL; diff --git a/ext/standard/php_fopen_wrappers.h b/ext/standard/php_fopen_wrappers.h index 0d235b945d..d41d8cf4e6 100644 --- a/ext/standard/php_fopen_wrappers.h +++ b/ext/standard/php_fopen_wrappers.h @@ -23,8 +23,8 @@ #ifndef PHP_FOPEN_WRAPPERS_H #define PHP_FOPEN_WRAPPERS_H -php_stream *php_stream_url_wrap_http(char *path, char *mode, int options, char **opened_path TSRMLS_DC); -php_stream *php_stream_url_wrap_ftp(char *path, char *mode, int options, char **opened_path TSRMLS_DC); +php_stream *php_stream_url_wrap_http(char *path, char *mode, int options, char **opened_path STREAMS_DC); +php_stream *php_stream_url_wrap_ftp(char *path, char *mode, int options, char **opened_path STREAMS_DC); php_stream_wrapper php_stream_http_wrapper; php_stream_wrapper php_stream_ftp_wrapper; php_stream_wrapper php_stream_php_wrapper; diff --git a/ext/zlib/php_zlib.h b/ext/zlib/php_zlib.h index 10a40cd96a..2c603d3e76 100644 --- a/ext/zlib/php_zlib.h +++ b/ext/zlib/php_zlib.h @@ -64,10 +64,9 @@ PHP_FUNCTION(gzinflate); PHP_FUNCTION(gzencode); PHP_FUNCTION(ob_gzhandler); -FILE *zlib_fopen_wrapper(const char *path, char *mode, int options, int *issock, int *socketd, char **opened_path TSRMLS_DC); int php_enable_output_compression(int buffer_size TSRMLS_DC); -php_stream *php_stream_gzopen(char *path, char *mode, int options, char **opened_path TSRMLS_DC); +php_stream *php_stream_gzopen(char *path, char *mode, int options, char **opened_path STREAMS_DC); extern php_stream_ops php_stream_gzio_ops; extern php_stream_wrapper php_stream_gzip_wrapper; diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index 44edcbdcd3..35ef2a179d 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -188,7 +188,7 @@ PHP_MINIT_FUNCTION(zlib) le_zp = zend_register_list_destructors_ex(phpi_destructor_gzclose, NULL, "zlib", module_number); if(PG(allow_url_fopen)) { - php_register_url_stream_wrapper("zlib", &php_stream_gzip_wrapper TSRMLS_CC); + php_register_url_stream_wrapper("zlib", &php_stream_gzip_wrapper); } REGISTER_LONG_CONSTANT("FORCE_GZIP", CODING_GZIP, CONST_CS | CONST_PERSISTENT); @@ -255,7 +255,7 @@ static gzFile php_gzopen_wrapper(char *path, char *mode, int options TSRMLS_DC) php_stream *stream = NULL; int fd; - stream = php_stream_open_wrapper(path, mode, options | REPORT_ERRORS, NULL TSRMLS_CC); + stream = php_stream_open_wrapper(path, mode, options | REPORT_ERRORS, NULL); if (stream) { if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD|PHP_STREAM_CAST_TRY_HARD, (void**)&fd, 1)) { @@ -302,7 +302,7 @@ PHP_FUNCTION(gzfile) convert_to_string_ex(filename); /* using a stream here is a bit more efficient (resource wise) than php_gzopen_wrapper */ - stream = php_stream_gzopen(Z_STRVAL_PP(filename), "rb", use_include_path|ENFORCE_SAFE_MODE|REPORT_ERRORS, NULL TSRMLS_CC); + stream = php_stream_gzopen(Z_STRVAL_PP(filename), "rb", use_include_path|ENFORCE_SAFE_MODE|REPORT_ERRORS, NULL STREAMS_CC); if (stream == NULL) { php_error(E_WARNING,"gzFile(\"%s\") - %s",Z_STRVAL_PP(filename),strerror(errno)); RETURN_FALSE; diff --git a/ext/zlib/zlib_fopen_wrapper.c b/ext/zlib/zlib_fopen_wrapper.c index 5d3129f843..c28ab6a1e5 100644 --- a/ext/zlib/zlib_fopen_wrapper.c +++ b/ext/zlib/zlib_fopen_wrapper.c @@ -80,7 +80,7 @@ php_stream_ops php_stream_gzio_ops = { NULL, "ZLIB" }; -php_stream *php_stream_gzopen(char *path, char *mode, int options, char **opened_path TSRMLS_DC) +php_stream *php_stream_gzopen(char *path, char *mode, int options, char **opened_path STREAMS_DC) { struct php_gz_stream_data_t *self; php_stream *stream = NULL; @@ -90,7 +90,7 @@ php_stream *php_stream_gzopen(char *path, char *mode, int options, char **opened if (strncmp("zlib:", path, 5) == 0) path += 5; - self->stream = php_stream_open_wrapper(path, mode, options, opened_path TSRMLS_CC); + self->stream = php_stream_open_wrapper(path, mode, options, opened_path); if (self->stream) { int fd; diff --git a/main/main.c b/main/main.c index 187286ad49..5e05ac0018 100644 --- a/main/main.c +++ b/main/main.c @@ -565,9 +565,8 @@ static FILE *php_fopen_wrapper_for_zend(const char *filename, char **opened_path { FILE *retval = NULL; php_stream *stream; - TSRMLS_FETCH(); - stream = php_stream_open_wrapper((char *)filename, "rb", USE_PATH|IGNORE_URL_WIN|REPORT_ERRORS, opened_path TSRMLS_CC); + stream = php_stream_open_wrapper((char *)filename, "rb", USE_PATH|IGNORE_URL_WIN|REPORT_ERRORS, opened_path); if (stream) { /* no need for us to check the stream type here */ php_stream_sock_set_chunk_size(stream, 1); @@ -952,7 +951,7 @@ int php_module_startup(sapi_module_struct *sf) /* initialize stream wrappers registry * (this uses configuration parameters from php.ini) */ - if (php_init_stream_wrappers(TSRMLS_C) == FAILURE) { + if (php_init_stream_wrappers() == FAILURE) { php_printf("PHP: Unable to initialize stream url wrappers.\n"); return FAILURE; } @@ -1057,7 +1056,7 @@ void php_module_shutdown(TSRMLS_D) zend_shutdown(TSRMLS_C); - php_shutdown_stream_wrappers(TSRMLS_C); + php_shutdown_stream_wrappers(); php_shutdown_info_logos(); UNREGISTER_INI_ENTRIES(); diff --git a/main/network.c b/main/network.c index eff1666344..3ae5861c74 100644 --- a/main/network.c +++ b/main/network.c @@ -413,7 +413,7 @@ int php_sockaddr_size(php_sockaddr_storage *addr) } /* }}} */ -PHPAPI php_stream *php_stream_sock_open_from_socket(int socket, int persistent) +PHPAPI php_stream *_php_stream_sock_open_from_socket(int socket, int persistent STREAMS_DC) { php_stream *stream; php_netstream_data_t *sock; @@ -426,7 +426,7 @@ PHPAPI php_stream *php_stream_sock_open_from_socket(int socket, int persistent) sock->timeout.tv_sec = -1; sock->socket = socket; - stream = php_stream_alloc(&php_stream_socket_ops, sock, persistent, "r+"); + stream = php_stream_alloc_rel(&php_stream_socket_ops, sock, persistent, "r+"); if (stream == NULL) pefree(sock, persistent); @@ -434,8 +434,8 @@ PHPAPI php_stream *php_stream_sock_open_from_socket(int socket, int persistent) return stream; } -PHPAPI php_stream *php_stream_sock_open_host(const char *host, unsigned short port, - int socktype, int timeout, int persistent) +PHPAPI php_stream *_php_stream_sock_open_host(const char *host, unsigned short port, + int socktype, int timeout, int persistent STREAMS_DC) { int socket; @@ -444,10 +444,11 @@ PHPAPI php_stream *php_stream_sock_open_host(const char *host, unsigned short po if (socket == -1) return NULL; - return php_stream_sock_open_from_socket(socket, persistent); + return php_stream_sock_open_from_socket_rel(socket, persistent); } -PHPAPI php_stream *php_stream_sock_open_unix(const char *path, int pathlen, int persistent, struct timeval *timeout) +PHPAPI php_stream *_php_stream_sock_open_unix(const char *path, int pathlen, int persistent, + struct timeval *timeout STREAMS_DC) { #if defined(AF_UNIX) int socketd; @@ -476,7 +477,7 @@ PHPAPI php_stream *php_stream_sock_open_unix(const char *path, int pathlen, int if (php_connect_nonb(socketd, (struct sockaddr *) &unix_addr, sizeof(unix_addr), timeout) == SOCK_CONN_ERR) return NULL; - return php_stream_sock_open_from_socket(socketd, persistent); + return php_stream_sock_open_from_socket_rel(socketd, persistent); #else return NULL; #endif diff --git a/main/php_network.h b/main/php_network.h index b51177ceae..caae0a97d3 100644 --- a/main/php_network.h +++ b/main/php_network.h @@ -112,11 +112,23 @@ typedef struct _php_netstream_data_t php_netstream_data_t; extern php_stream_ops php_stream_socket_ops; #define PHP_STREAM_IS_SOCKET (&php_stream_socket_ops) -PHPAPI php_stream *php_stream_sock_open_from_socket(int socket, int persistent); +PHPAPI php_stream *_php_stream_sock_open_from_socket(int socket, int persistent STREAMS_DC); /* open a connection to a host using php_hostconnect and return a stream */ -PHPAPI php_stream *php_stream_sock_open_host(const char *host, unsigned short port, - int socktype, int timeout, int persistent); -PHPAPI php_stream *php_stream_sock_open_unix(const char *path, int pathlen, int persistent, struct timeval *timeout); +PHPAPI php_stream *_php_stream_sock_open_host(const char *host, unsigned short port, + int socktype, int timeout, int persistent STREAMS_DC); +PHPAPI php_stream *_php_stream_sock_open_unix(const char *path, int pathlen, int persistent, + struct timeval *timeout STREAMS_DC); + +#define php_stream_sock_open_from_socket(socket, persistent) _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_CC) +#define php_stream_sock_open_host(host, port, socktype, timeout, persistent) _php_stream_sock_open_host((host), (port), (socktype), (timeout), (persistent) STREAMS_CC) +#define php_stream_sock_open_unix(path, pathlen, persistent, timeval) _php_stream_sock_open_unix((path), (pathlen), (persistent), (timeval) STREAMS_CC) + +/* {{{ memory debug */ +#define php_stream_sock_open_from_socket_rel(socket, persistent) _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_REL_CC) +#define php_stream_sock_open_host_rel(host, port, socktype, timeout, persistent) _php_stream_sock_open_host((host), (port), (socktype), (timeout), (persistent) STREAMS_REL_CC) +#define php_stream_sock_open_unix_rel(path, pathlen, persistent, timeval) _php_stream_sock_open_unix((path), (pathlen), (persistent), (timeval) STREAMS_REL_CC) + +/* }}} */ PHPAPI void php_stream_sock_set_timeout(php_stream *stream, struct timeval *timeout); PHPAPI int php_stream_sock_set_blocking(php_stream *stream, int mode); diff --git a/main/php_streams.h b/main/php_streams.h index 402156c335..7141da21a1 100755 --- a/main/php_streams.h +++ b/main/php_streams.h @@ -25,6 +25,54 @@ /* See README.STREAMS in php4 root dir for more info about this stuff */ +/* {{{ Streams memory debugging stuff */ + +#if ZEND_DEBUG +/* these have more of a dependency on the definitions of the zend macros than + * I would prefer, but doing it this way saves loads of idefs :-/ */ +# define STREAMS_D int __php_stream_call_depth ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC +# define STREAMS_C 0 ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC TSRMLS_CC +# define STREAMS_REL_C __php_stream_call_depth + 1 ZEND_FILE_LINE_CC, \ + __php_stream_call_depth ? __zend_orig_filename : __zend_filename, \ + __php_stream_call_depth ? __zend_orig_lineno : __zend_lineno \ + TSRMLS_CC + +# define STREAMS_DC , STREAMS_D +# define STREAMS_CC , STREAMS_C +# define STREAMS_REL_CC , STREAMS_REL_C +#else +# define STREAMS_D TSRMLS_D +# define STREAMS_C TSRMLS_C +# define STREAMS_REL_C TSRMLS_C +# define STREAMS_DC TSRMLS_DC +# define STREAMS_CC TSRMLS_CC +# define STREAMS_REL_CC TSRMLS_CC +#endif + +/* these functions relay the file/line number information. They are depth aware, so they will pass + * the ultimate ancestor, which is useful, because there can be several layers of calls */ +#define php_stream_alloc_rel(ops, thisptr, persistent, mode) _php_stream_alloc((ops), (thisptr), (persistent), (mode) STREAMS_REL_CC) + +#define php_stream_copy_to_mem_rel(src, buf, maxlen, persistent) _php_stream_copy_to_mem((src), (buf), (maxlen), (persistent) STREAMS_REL_CC) + +#define php_stream_fopen_rel(filename, mode, opened) _php_stream_fopen((filename), (mode), (opened) STREAMS_REL_CC) + +#define php_stream_fopen_with_path_rel(filename, mode, path, opened) _php_stream_fopen_with_path((filename), (mode), (path), (opened) STREAMS_REL_CC) + +#define php_stream_fopen_from_file_rel(file, mode) _php_stream_fopen_from_file((file), (mode) STREAMS_REL_CC) + +#define php_stream_fopen_from_pipe_rel(file, mode) _php_stream_fopen_from_pipe((file), (mode) STREAMS_REL_CC) + +#define php_stream_fopen_tmpfile_rel() _php_stream_fopen_tmpfile(STREAMS_REL_C) + +#define php_stream_fopen_temporary_file_rel(dir, pfx, opened_path) _php_stream_fopen_temporary_file((dir), (pfx), (opened_path) STREAMS_REL_CC) + +#define php_stream_open_wrapper_rel(path, mode, options, opened) _php_stream_open_wrapper((path), (mode), (options), (opened) STREAMS_REL_CC) + +#define php_stream_make_seekable_rel(origstream, newstream) _php_stream_make_seekable(origstream, newstream STREAMS_REL_CC) + +/* }}} */ + /* The contents of the php_stream_ops and php_stream should only be accessed * using the functions/macros in this header. * If you need to get at something that doesn't have an API, @@ -53,7 +101,7 @@ typedef struct _php_stream_ops { } php_stream_ops; /* options uses the IGNORE_URL family of defines from fopen_wrappers.h */ -typedef php_stream *(*php_stream_factory_func_t)(char *filename, char *mode, int options, char **opened_path TSRMLS_DC); +typedef php_stream *(*php_stream_factory_func_t)(char *filename, char *mode, int options, char **opened_path STREAMS_DC); typedef void (*php_stream_wrapper_dtor_func_t)(php_stream *stream); typedef struct _php_stream_wrapper { @@ -83,7 +131,9 @@ struct _php_stream { /* allocate a new stream for a particular ops */ -PHPAPI php_stream *php_stream_alloc(php_stream_ops *ops, void *abstract, int persistent, const char *mode); +PHPAPI php_stream *_php_stream_alloc(php_stream_ops *ops, void *abstract, + int persistent, const char *mode STREAMS_DC); +#define php_stream_alloc(ops, thisptr, persistent, mode) _php_stream_alloc((ops), (thisptr), (persistent), (mode) STREAMS_CC) PHPAPI int php_stream_free(php_stream *stream, int call_dtor); #define php_stream_close(stream) php_stream_free(stream, 1) @@ -107,7 +157,10 @@ PHPAPI int php_stream_puts(php_stream *stream, char *buf); PHPAPI size_t php_stream_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen); /* read all data from stream and put into a buffer. Caller must free buffer when done. * The copy will use mmap if available. */ -PHPAPI size_t php_stream_copy_to_mem(php_stream *src, char **buf, size_t maxlen, int persistent); +PHPAPI size_t _php_stream_copy_to_mem(php_stream *src, char **buf, size_t maxlen, + int persistent STREAMS_DC); +#define php_stream_copy_to_mem(src, buf, maxlen, persistent) _php_stream_copy_to_mem((src), (buf), (maxlen), (persistent) STREAMS_CC) + /* maybe implement someday */ #define php_stream_error(stream) (0) @@ -115,12 +168,23 @@ PHPAPI size_t php_stream_copy_to_mem(php_stream *src, char **buf, size_t maxlen, /* operations for a stdio FILE; use the php_stream_fopen_XXX funcs below */ extern php_stream_ops php_stream_stdio_ops; /* like fopen, but returns a stream */ -PHPAPI php_stream *php_stream_fopen(const char *filename, const char *mode, char **opened_path TSRMLS_DC); -PHPAPI php_stream *php_stream_fopen_with_path(char *filename, char *mode, char *path, char **opened_path TSRMLS_DC); -PHPAPI php_stream *php_stream_fopen_from_file(FILE *file, const char *mode); -PHPAPI php_stream *php_stream_fopen_from_pipe(FILE *file, const char *mode); -PHPAPI php_stream *php_stream_fopen_tmpfile(void); -PHPAPI php_stream *php_stream_fopen_temporary_file(const char *dir, const char *pfx, char **opened_path TSRMLS_DC); +PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, char **opened_path STREAMS_DC); +#define php_stream_fopen(filename, mode, opened) _php_stream_fopen((filename), (mode), (opened) STREAMS_CC) + +PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char *path, char **opened_path STREAMS_DC); +#define php_stream_fopen_with_path(filename, mode, path, opened) _php_stream_fopen_with_path((filename), (mode), (path), (opened) STREAMS_CC) + +PHPAPI php_stream *_php_stream_fopen_from_file(FILE *file, const char *mode STREAMS_DC); +#define php_stream_fopen_from_file(file, mode) _php_stream_fopen_from_file((file), (mode) STREAMS_CC) + +PHPAPI php_stream *_php_stream_fopen_from_pipe(FILE *file, const char *mode STREAMS_DC); +#define php_stream_fopen_from_pipe(file, mode) _php_stream_fopen_from_pipe((file), (mode) STREAMS_CC) + +PHPAPI php_stream *_php_stream_fopen_tmpfile(STREAMS_D); +#define php_stream_fopen_tmpfile() _php_stream_fopen_tmpfile(STREAMS_C) + +PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, char **opened_path STREAMS_DC); +#define php_stream_fopen_temporary_file(dir, pfx, opened_path) _php_stream_fopen_temporary_file((dir), (pfx), (opened_path) STREAMS_CC) /* coerce the stream into some other form */ /* cast as a stdio FILE * */ @@ -163,18 +227,20 @@ PHPAPI int php_stream_cast(php_stream *stream, int castas, void **ret, int show_ # define IGNORE_URL_WIN 0 #endif -int php_init_stream_wrappers(TSRMLS_D); -int php_shutdown_stream_wrappers(TSRMLS_D); -PHPAPI int php_register_url_stream_wrapper(char *protocol, php_stream_wrapper *wrapper TSRMLS_DC); -PHPAPI int php_unregister_url_stream_wrapper(char *protocol TSRMLS_DC); -PHPAPI php_stream *php_stream_open_wrapper(char *path, char *mode, int options, char **opened_path TSRMLS_DC); +int php_init_stream_wrappers(void); +int php_shutdown_stream_wrappers(void); +PHPAPI int php_register_url_stream_wrapper(char *protocol, php_stream_wrapper *wrapper); +PHPAPI int php_unregister_url_stream_wrapper(char *protocol); +PHPAPI php_stream *_php_stream_open_wrapper(char *path, char *mode, int options, char **opened_path STREAMS_DC); +#define php_stream_open_wrapper(path, mode, options, opened) _php_stream_open_wrapper((path), (mode), (options), (opened) STREAMS_CC) #define PHP_STREAM_UNCHANGED 0 /* orig stream was seekable anyway */ #define PHP_STREAM_RELEASED 1 /* newstream should be used; origstream is no longer valid */ #define PHP_STREAM_FAILED 2 /* an error occurred while attempting conversion */ #define PHP_STREAM_CRITICAL 3 /* an error occurred; origstream is in an unknown state; you should close origstream */ /* DO NOT call this on streams that are referenced by resources! */ -PHPAPI int php_stream_make_seekable(php_stream *origstream, php_stream **newstream); +PHPAPI int _php_stream_make_seekable(php_stream *origstream, php_stream **newstream STREAMS_DC); +#define php_stream_make_seekable(origstream, newstream) _php_stream_make_seekable(origstream, newstream STREAMS_CC) #endif diff --git a/main/streams.c b/main/streams.c index b67bc837fe..20cf6c0db5 100755 --- a/main/streams.c +++ b/main/streams.c @@ -42,14 +42,35 @@ #include "build-defs.h" #endif +#if ZEND_DEBUG +/* some macros to help track leaks */ +#define emalloc_rel_orig(size) \ + ( __php_stream_call_depth == 0 \ + ? _emalloc((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_ORIG_RELAY_CC) \ + : _emalloc((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_ORIG_RELAY_CC) ) + +#define erealloc_rel_orig(ptr, size) \ + ( __php_stream_call_depth == 0 \ + ? _erealloc((ptr), (size), 0 ZEND_FILE_LINE_CC ZEND_FILE_LINE_ORIG_RELAY_CC) \ + : _erealloc((ptr), (size), 0 ZEND_FILE_LINE_CC ZEND_FILE_LINE_ORIG_RELAY_CC) ) + + +#define pemalloc_rel_orig(size, persistent) ((persistent) ? malloc((size)) : emalloc_rel_orig((size))) +#define perealloc_rel_orig(ptr, size, persistent) ((persistent) ? realloc((ptr), (size)) : erealloc_rel_orig((ptr), (size))) +#else +# define pemalloc_rel_orig(size, persistent) pemalloc((size), (persistent)) +# define perealloc_rel_orig(ptr, size, persistent) perealloc((ptr), (size), (persistent)) +# define emalloc_rel_orig(size) emalloc((size)) +#endif + static HashTable url_stream_wrappers_hash; /* allocate a new stream for a particular ops */ -PHPAPI php_stream *php_stream_alloc(php_stream_ops *ops, void *abstract, int persistent, const char *mode) /* {{{ */ +PHPAPI php_stream *_php_stream_alloc(php_stream_ops *ops, void *abstract, int persistent, const char *mode STREAMS_DC) /* {{{ */ { php_stream *ret; - - ret = (php_stream*) pemalloc(sizeof(php_stream), persistent); + + ret = (php_stream*) pemalloc_rel_orig(sizeof(php_stream), persistent); memset(ret, 0, sizeof(php_stream)); @@ -232,7 +253,7 @@ PHPAPI int php_stream_seek(php_stream *stream, off_t offset, int whence) return -1; } -PHPAPI size_t php_stream_copy_to_mem(php_stream *src, char **buf, size_t maxlen, int persistent) +PHPAPI size_t _php_stream_copy_to_mem(php_stream *src, char **buf, size_t maxlen, int persistent STREAMS_DC) { size_t ret = 0; char *ptr; @@ -272,7 +293,7 @@ PHPAPI size_t php_stream_copy_to_mem(php_stream *src, char **buf, size_t maxlen, srcfile = mmap(NULL, maxlen, PROT_READ, MAP_SHARED, srcfd, 0); if (srcfile != (void*)MAP_FAILED) { - *buf = pemalloc(persistent, maxlen); + *buf = pemalloc_rel_orig(persistent, maxlen); if (*buf) { memcpy(*buf, srcfile, maxlen); @@ -287,19 +308,19 @@ PHPAPI size_t php_stream_copy_to_mem(php_stream *src, char **buf, size_t maxlen, } #endif - ptr = *buf = pemalloc(persistent, step); + ptr = *buf = pemalloc_rel_orig(persistent, step); max_len = step; while((ret = php_stream_read(src, ptr, max_len - len))) { len += ret; if (len + min_room >= max_len) { - *buf = perealloc(*buf, max_len + step, persistent); + *buf = perealloc_rel_orig(*buf, max_len + step, persistent); max_len += step; ptr = *buf + len; } } if (len) { - *buf = perealloc(*buf, len, persistent); + *buf = perealloc_rel_orig(*buf, len, persistent); } else { pefree(*buf, persistent); *buf = NULL; @@ -406,12 +427,12 @@ typedef struct { #endif } php_stdio_stream_data; -PHPAPI php_stream *php_stream_fopen_temporary_file(const char *dir, const char *pfx, char **opened_path TSRMLS_DC) +PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, char **opened_path STREAMS_DC) { FILE *fp = php_open_temporary_file(dir, pfx, opened_path TSRMLS_CC); if (fp) { - php_stream *stream = php_stream_fopen_from_file(fp, "wb"); + php_stream *stream = php_stream_fopen_from_file_rel(fp, "wb"); if (stream) { return stream; } @@ -424,7 +445,7 @@ PHPAPI php_stream *php_stream_fopen_temporary_file(const char *dir, const char * return NULL; } -PHPAPI php_stream *php_stream_fopen_tmpfile(void) +PHPAPI php_stream *_php_stream_fopen_tmpfile(STREAMS_D) { FILE *fp; php_stream *stream; @@ -434,7 +455,7 @@ PHPAPI php_stream *php_stream_fopen_tmpfile(void) zend_error(E_WARNING, "tmpfile(): %s", strerror(errno)); return NULL; } - stream = php_stream_fopen_from_file(fp, "r+"); + stream = php_stream_fopen_from_file_rel(fp, "r+"); if (stream == NULL) { zend_error(E_WARNING, "tmpfile(): %s", strerror(errno)); fclose(fp); @@ -445,24 +466,24 @@ PHPAPI php_stream *php_stream_fopen_tmpfile(void) -PHPAPI php_stream *php_stream_fopen_from_file(FILE *file, const char *mode) +PHPAPI php_stream *_php_stream_fopen_from_file(FILE *file, const char *mode STREAMS_DC) { php_stdio_stream_data *self; - self = emalloc(sizeof(*self)); + self = emalloc_rel_orig(sizeof(*self)); self->file = file; self->is_pipe = 0; - return php_stream_alloc(&php_stream_stdio_ops, self, 0, mode); + return php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode); } -PHPAPI php_stream *php_stream_fopen_from_pipe(FILE *file, const char *mode) +PHPAPI php_stream *_php_stream_fopen_from_pipe(FILE *file, const char *mode STREAMS_DC) { php_stdio_stream_data *self; - self = emalloc(sizeof(*self)); + self = emalloc_rel_orig(sizeof(*self)); self->file = file; self->is_pipe = 1; - return php_stream_alloc(&php_stream_stdio_ops, self, 0, mode); + return php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode); } static size_t php_stdiop_write(php_stream *stream, const char *buf, size_t count) { @@ -595,7 +616,7 @@ php_stream_ops php_stream_stdio_ops = { "STDIO" }; -PHPAPI php_stream *php_stream_fopen_with_path(char *filename, char *mode, char *path, char **opened_path TSRMLS_DC) /* {{{ */ +PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char *path, char **opened_path STREAMS_DC) /* {{{ */ { /* code ripped off from fopen_wrappers.c */ char *pathbuf, *ptr, *end; @@ -622,7 +643,7 @@ PHPAPI php_stream *php_stream_fopen_with_path(char *filename, char *mode, char * if (PG(safe_mode) && (!php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) { return NULL; } - return php_stream_fopen(filename, mode, opened_path TSRMLS_CC); + return php_stream_fopen_rel(filename, mode, opened_path TSRMLS_CC); } /* @@ -634,19 +655,19 @@ PHPAPI php_stream *php_stream_fopen_with_path(char *filename, char *mode, char * if (IS_ABSOLUTE_PATH(filename, filename_length)) { if ((php_check_safe_mode_include_dir(filename TSRMLS_CC)) == 0) /* filename is in safe_mode_include_dir (or subdir) */ - return php_stream_fopen(filename, mode, opened_path TSRMLS_CC); + return php_stream_fopen_rel(filename, mode, opened_path); if (PG(safe_mode) && (!php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) return NULL; - return php_stream_fopen(filename, mode, opened_path TSRMLS_CC); + return php_stream_fopen_rel(filename, mode, opened_path); } if (!path || (path && !*path)) { if (PG(safe_mode) && (!php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) { return NULL; } - return php_stream_fopen(filename, mode, opened_path TSRMLS_CC); + return php_stream_fopen_rel(filename, mode, opened_path); } /* check in provided path */ @@ -689,7 +710,7 @@ PHPAPI php_stream *php_stream_fopen_with_path(char *filename, char *mode, char * if ((php_check_safe_mode_include_dir(trypath TSRMLS_CC) == 0) || php_checkuid(trypath, mode, CHECKUID_CHECK_MODE_PARAM)) { /* UID ok, or trypath is in safe_mode_include_dir */ - stream = php_stream_fopen(trypath, mode, opened_path TSRMLS_CC); + stream = php_stream_fopen_rel(trypath, mode, opened_path); } else { stream = NULL; } @@ -698,7 +719,7 @@ PHPAPI php_stream *php_stream_fopen_with_path(char *filename, char *mode, char * return stream; } } - stream = php_stream_fopen(trypath, mode, opened_path TSRMLS_CC); + stream = php_stream_fopen_rel(trypath, mode, opened_path); if (stream) { efree(pathbuf); return stream; @@ -712,7 +733,7 @@ PHPAPI php_stream *php_stream_fopen_with_path(char *filename, char *mode, char * } /* }}} */ -PHPAPI php_stream *php_stream_fopen(const char *filename, const char *mode, char **opened_path TSRMLS_DC) +PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, char **opened_path STREAMS_DC) { FILE *fp; char *realpath = NULL; @@ -722,7 +743,7 @@ PHPAPI php_stream *php_stream_fopen(const char *filename, const char *mode, char fp = fopen(realpath, mode); if (fp) { - php_stream *ret = php_stream_fopen_from_file(fp, mode); + php_stream *ret = php_stream_fopen_from_file_rel(fp, mode); if (ret) { if (opened_path) { @@ -860,34 +881,34 @@ exit_success: } /* }}} */ -int php_init_stream_wrappers(TSRMLS_D) +int php_init_stream_wrappers(void) { if (PG(allow_url_fopen)) return zend_hash_init(&url_stream_wrappers_hash, 0, NULL, NULL, 1); return SUCCESS; } -int php_shutdown_stream_wrappers(TSRMLS_D) +int php_shutdown_stream_wrappers(void) { if (PG(allow_url_fopen)) zend_hash_destroy(&url_stream_wrappers_hash); return SUCCESS; } -PHPAPI int php_register_url_stream_wrapper(char *protocol, php_stream_wrapper *wrapper TSRMLS_DC) +PHPAPI int php_register_url_stream_wrapper(char *protocol, php_stream_wrapper *wrapper) { if (PG(allow_url_fopen)) return zend_hash_add(&url_stream_wrappers_hash, protocol, strlen(protocol), wrapper, sizeof(*wrapper), NULL); return FAILURE; } -PHPAPI int php_unregister_url_stream_wrapper(char *protocol TSRMLS_DC) +PHPAPI int php_unregister_url_stream_wrapper(char *protocol) { if (PG(allow_url_fopen)) return zend_hash_del(&url_stream_wrappers_hash, protocol, strlen(protocol)); return SUCCESS; } -static php_stream *php_stream_open_url(char *path, char *mode, int options, char **opened_path TSRMLS_DC) +static php_stream *php_stream_open_url(char *path, char *mode, int options, char **opened_path STREAMS_DC) { php_stream_wrapper *wrapper; const char *p, *protocol = NULL; @@ -907,7 +928,7 @@ static php_stream *php_stream_open_url(char *path, char *mode, int options, char protocol = NULL; } if (wrapper) { - php_stream *stream = wrapper->create(path, mode, options, opened_path TSRMLS_CC); + php_stream *stream = wrapper->create(path, mode, options, opened_path STREAMS_REL_CC); if (stream) stream->wrapper = wrapper; return stream; @@ -923,15 +944,16 @@ static php_stream *php_stream_open_url(char *path, char *mode, int options, char path += n + 1; /* fall back on regular file access */ - return php_stream_open_wrapper(path, mode, (options & ~REPORT_ERRORS) | IGNORE_URL, - opened_path TSRMLS_CC); + return php_stream_open_wrapper_rel(path, mode, (options & ~REPORT_ERRORS) | IGNORE_URL, + opened_path); } return NULL; } -PHPAPI php_stream *php_stream_open_wrapper(char *path, char *mode, int options, char **opened_path TSRMLS_DC) +PHPAPI php_stream *_php_stream_open_wrapper(char *path, char *mode, int options, char **opened_path STREAMS_DC) { php_stream *stream = NULL; + TSRMLS_FETCH(); if (opened_path) *opened_path = NULL; @@ -940,24 +962,24 @@ PHPAPI php_stream *php_stream_open_wrapper(char *path, char *mode, int options, return NULL; if (PG(allow_url_fopen) && !(options & IGNORE_URL)) { - stream = php_stream_open_url(path, mode, options, opened_path TSRMLS_CC); + stream = php_stream_open_url(path, mode, options, opened_path STREAMS_REL_CC); goto out; } if ((options & USE_PATH) && PG(include_path) != NULL) { - stream = php_stream_fopen_with_path(path, mode, PG(include_path), opened_path TSRMLS_CC); + stream = php_stream_fopen_with_path_rel(path, mode, PG(include_path), opened_path); goto out; } if ((options & ENFORCE_SAFE_MODE) && PG(safe_mode) && (!php_checkuid(path, mode, CHECKUID_CHECK_MODE_PARAM))) return NULL; - stream = php_stream_fopen(path, mode, opened_path TSRMLS_CC); + stream = php_stream_fopen_rel(path, mode, opened_path); out: if (stream != NULL && (options & STREAM_MUST_SEEK)) { php_stream *newstream; - switch(php_stream_make_seekable(stream, &newstream)) { + switch(php_stream_make_seekable_rel(stream, &newstream)) { case PHP_STREAM_UNCHANGED: return stream; case PHP_STREAM_RELEASED: @@ -985,7 +1007,7 @@ out: return stream; } -PHPAPI int php_stream_make_seekable(php_stream *origstream, php_stream **newstream) +PHPAPI int _php_stream_make_seekable(php_stream *origstream, php_stream **newstream STREAMS_DC) { assert(newstream != NULL);