From d6961f44d564eddff1e22a228311e692faba9033 Mon Sep 17 00:00:00 2001 From: Sicong Date: Tue, 19 Jul 2022 01:04:32 +0000 Subject: [PATCH 01/10] Fix access token issue when connection pool enabled --- source/pdo_sqlsrv/config.w32 | 2 ++ source/pdo_sqlsrv/php_pdo_sqlsrv.h | 2 ++ source/shared/core_conn.cpp | 34 ++++++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/source/pdo_sqlsrv/config.w32 b/source/pdo_sqlsrv/config.w32 index 0ec718f58..907da689b 100644 --- a/source/pdo_sqlsrv/config.w32 +++ b/source/pdo_sqlsrv/config.w32 @@ -29,6 +29,8 @@ if( PHP_PDO_SQLSRV != "no" ) { CHECK_HEADER_ADD_INCLUDE( "core_sqlsrv.h", "CFLAGS_PDO_SQLSRV", configure_module_dirname + "\\shared")) { CHECK_HEADER_ADD_INCLUDE("sql.h", "CFLAGS_PDO_SQLSRV_ODBC"); CHECK_HEADER_ADD_INCLUDE("sqlext.h", "CFLAGS_PDO_SQLSRV_ODBC"); + CHECK_HEADER_ADD_INCLUDE("php_pdo_sqlsrv_int.h", "CFLAGS_PDO_SQLSRV", configure_module_dirname); + CHECK_HEADER_ADD_INCLUDE("php_pdo_sqlsrv.h", "CFLAGS_PDO_SQLSRV", configure_module_dirname); ADD_SOURCES( configure_module_dirname + "\\shared", shared_src_class, "pdo_sqlsrv" ); ADD_FLAG( "LDFLAGS_PDO_SQLSRV", "/NXCOMPAT /DYNAMICBASE /debug /guard:cf" ); ADD_FLAG( "CFLAGS_PDO_SQLSRV", "/EHsc" ); diff --git a/source/pdo_sqlsrv/php_pdo_sqlsrv.h b/source/pdo_sqlsrv/php_pdo_sqlsrv.h index 923eb20da..13c81a077 100644 --- a/source/pdo_sqlsrv/php_pdo_sqlsrv.h +++ b/source/pdo_sqlsrv/php_pdo_sqlsrv.h @@ -32,6 +32,8 @@ ZEND_BEGIN_MODULE_GLOBALS(pdo_sqlsrv) unsigned int pdo_log_severity; zend_long client_buffer_max_size; short report_additional_errors; +ACCESSTOKEN** access_tokens; +unsigned int access_tokens_size = 0; #ifndef _WIN32 zend_long set_locale_info; diff --git a/source/shared/core_conn.cpp b/source/shared/core_conn.cpp index a5f87ba38..1c0a2cb4d 100644 --- a/source/shared/core_conn.cpp +++ b/source/shared/core_conn.cpp @@ -34,6 +34,11 @@ #include #endif +extern "C" { +#include "php_pdo_sqlsrv.h" +} +#include "php_pdo_sqlsrv_int.h" + // *** internal variables and constants *** namespace { @@ -1183,12 +1188,30 @@ void access_token_set_func::func( _In_ connection_option const* option, _In_ zva // // See https://docs.microsoft.com/sql/connect/odbc/using-azure-active-directory#authenticating-with-an-access-token + size_t next_token_position = 0; + bool same_token_used = false; + + for (size_t current_token_index = 0; current_token_index < PDO_SQLSRV_G(access_tokens_size); current_token_index++) { + std::string string_token; + for (size_t i = 0; i < PDO_SQLSRV_G(access_tokens)[current_token_index]->dataSize; i += 2) { + string_token.push_back(PDO_SQLSRV_G(access_tokens)[current_token_index]->data[i]); + } + if (string_token == std::string(value_str)) { + // Token already exists in access_toiens + memset(PDO_SQLSRV_G(access_tokens)[current_token_index]->data, 0, PDO_SQLSRV_G(access_tokens)[current_token_index]->dataSize); + sqlsrv_free(PDO_SQLSRV_G(access_tokens)[current_token_index]); + next_token_position = current_token_index; + same_token_used = true; + break; + } + } + size_t dataSize = 2 * value_len; sqlsrv_malloc_auto_ptr accToken; accToken = reinterpret_cast(sqlsrv_malloc(sizeof(ACCESSTOKEN) + dataSize)); - ACCESSTOKEN *pAccToken = accToken.get(); + ACCESSTOKEN* pAccToken = accToken.get(); SQLSRV_ASSERT(pAccToken != NULL, "Something went wrong when trying to allocate memory for the access token."); pAccToken->dataSize = dataSize; @@ -1196,7 +1219,7 @@ void access_token_set_func::func( _In_ connection_option const* option, _In_ zva // Expand access token with padding bytes for (size_t i = 0, j = 0; i < dataSize; i += 2, j++) { pAccToken->data[i] = value_str[j]; - pAccToken->data[i+1] = 0; + pAccToken->data[i + 1] = 0; } core::SQLSetConnectAttr(conn, SQL_COPT_SS_ACCESS_TOKEN, reinterpret_cast(pAccToken), SQL_IS_POINTER); @@ -1204,4 +1227,11 @@ void access_token_set_func::func( _In_ connection_option const* option, _In_ zva // Save the pointer because SQLDriverConnect() will use it to make connection to the server conn->azure_ad_access_token = pAccToken; accToken.transferred(); + + if (!same_token_used) { + next_token_position = PDO_SQLSRV_G(access_tokens_size); + PDO_SQLSRV_G(access_tokens_size)++; + PDO_SQLSRV_G(access_tokens) = reinterpret_cast(sqlsrv_realloc(PDO_SQLSRV_G(access_tokens), PDO_SQLSRV_G(access_tokens_size) * sizeof(ACCESSTOKEN))); + } + PDO_SQLSRV_G(access_tokens)[next_token_position] = conn->azure_ad_access_token; } From 883fd58b59d0948db9a1ac087615553b08fb9915 Mon Sep 17 00:00:00 2001 From: Sicong Date: Tue, 19 Jul 2022 01:08:51 +0000 Subject: [PATCH 02/10] Add pdo_init.cpp --- source/pdo_sqlsrv/pdo_init.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/source/pdo_sqlsrv/pdo_init.cpp b/source/pdo_sqlsrv/pdo_init.cpp index 3d7d23953..e3c5858ec 100644 --- a/source/pdo_sqlsrv/pdo_init.cpp +++ b/source/pdo_sqlsrv/pdo_init.cpp @@ -250,6 +250,14 @@ PHP_RSHUTDOWN_FUNCTION(pdo_sqlsrv) // SQLSRV_UNUSED( module_number ); // SQLSRV_UNUSED( type ); + for (size_t current_token = 0; current_token < PDO_SQLSRV_G(access_tokens_size); current_token++) { + if (PDO_SQLSRV_G(access_tokens)[current_token]) { + memset(PDO_SQLSRV_G(access_tokens)[current_token]->data, 0, PDO_SQLSRV_G(access_tokens)[current_token]->dataSize); + sqlsrv_free(PDO_SQLSRV_G(access_tokens)[current_token]); + } + } + sqlsrv_free(PDO_SQLSRV_G(access_tokens)); + PDO_LOG_NOTICE("pdo_sqlsrv: entering rshutdown"); return SUCCESS; From 202014b0b2c7b9cd838c4b7201efc51ffa7d41ee Mon Sep 17 00:00:00 2001 From: Sicong Date: Tue, 19 Jul 2022 01:17:05 +0000 Subject: [PATCH 03/10] Include msodbcsql.h --- source/pdo_sqlsrv/php_pdo_sqlsrv.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/pdo_sqlsrv/php_pdo_sqlsrv.h b/source/pdo_sqlsrv/php_pdo_sqlsrv.h index 13c81a077..3e3c1b593 100644 --- a/source/pdo_sqlsrv/php_pdo_sqlsrv.h +++ b/source/pdo_sqlsrv/php_pdo_sqlsrv.h @@ -21,7 +21,7 @@ //--------------------------------------------------------------------------------------------------------------------------------- #include "php.h" - +#include "msodbcsql.h" //********************************************************************************************************************************* // Global variables //********************************************************************************************************************************* From 607964622911b3a457f03c1113a4954c3c370579 Mon Sep 17 00:00:00 2001 From: Sicong Date: Tue, 19 Jul 2022 01:36:59 +0000 Subject: [PATCH 04/10] Print error --- buildscripts/builddrivers.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/buildscripts/builddrivers.py b/buildscripts/builddrivers.py index 26c91c1cd..051f4d6e2 100644 --- a/buildscripts/builddrivers.py +++ b/buildscripts/builddrivers.py @@ -200,7 +200,10 @@ def build(self): print('Something went wrong, launching log file', logfile) # display log file only when not testing if not self.testing: - os.startfile(os.path.join(root_dir, 'php-sdk', logfile)) + logfile_path = os.path.join(root_dir, 'php-sdk', logfile) + os.startfile(logfile_path) + with open(logfile_path, 'r') as f: + print(f.read()) os.chdir(work_dir) exit(1) From 82901322ae3065302ac7234149eb3e682b3906f4 Mon Sep 17 00:00:00 2001 From: Sicong Date: Tue, 19 Jul 2022 02:02:38 +0000 Subject: [PATCH 05/10] Print error --- buildscripts/builddrivers.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/buildscripts/builddrivers.py b/buildscripts/builddrivers.py index 051f4d6e2..20c117a79 100644 --- a/buildscripts/builddrivers.py +++ b/buildscripts/builddrivers.py @@ -201,12 +201,11 @@ def build(self): # display log file only when not testing if not self.testing: logfile_path = os.path.join(root_dir, 'php-sdk', logfile) - os.startfile(logfile_path) with open(logfile_path, 'r') as f: print(f.read()) os.chdir(work_dir) exit(1) - + "" if not self.testing: choice = input("Rebuild using the same configuration(yes) or quit (no) [yes/no]: ") choice = choice.lower() From 0bf5f0696d0dc102b11429ed02e787cb2d611ba5 Mon Sep 17 00:00:00 2001 From: Sicong Date: Tue, 19 Jul 2022 02:23:44 +0000 Subject: [PATCH 06/10] Print error --- buildscripts/builddrivers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/buildscripts/builddrivers.py b/buildscripts/builddrivers.py index 20c117a79..e12400c92 100644 --- a/buildscripts/builddrivers.py +++ b/buildscripts/builddrivers.py @@ -205,7 +205,6 @@ def build(self): print(f.read()) os.chdir(work_dir) exit(1) - "" if not self.testing: choice = input("Rebuild using the same configuration(yes) or quit (no) [yes/no]: ") choice = choice.lower() From e4ce6f9a285003fd882e0ed30d9aa4978b0ec46b Mon Sep 17 00:00:00 2001 From: Sicong Date: Wed, 20 Jul 2022 22:01:22 +0000 Subject: [PATCH 07/10] Skip Linux, not affected --- appveyor.yml | 1 + azure-pipelines.yml | 1 + buildscripts/builddrivers.py | 8 ++++---- source/pdo_sqlsrv/config.w32 | 4 ++-- source/pdo_sqlsrv/pdo_init.cpp | 4 ++-- source/pdo_sqlsrv/php_pdo_sqlsrv.h | 8 ++++++-- source/shared/core_conn.cpp | 10 ++++++---- 7 files changed, 22 insertions(+), 14 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 4cf0be78b..eaa2debcb 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -101,6 +101,7 @@ build_script: - copy %APPVEYOR_BUILD_FOLDER%\buildscripts\*.py c:\projects - cd c:\projects - python -V + - ps: $blockRdp = $true; iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1')) - python builddrivers.py --PHPVER=%PHP_VERSION% --ARCH=%BUILD_PLATFORM% --THREAD=%THREAD% --SOURCE=%APPVEYOR_BUILD_FOLDER%\source --TESTING --NO_RENAME - cd c:\projects\php-sdk\phpdev\%PHP_VC%\%BUILD_PLATFORM%\php-%PHP_VERSION%-src\ - set PHP_SRC_DIR=%CD%\ext diff --git a/azure-pipelines.yml b/azure-pipelines.yml index c7b643f9c..7cdeb27aa 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -217,6 +217,7 @@ jobs: for f in pdo_sqlsrv/*.diff; do ls $f 2>/dev/null; cat $f 2>/dev/null; echo ''; done || true python output.py ls -l *.xml + more *.diff | cat displayName: 'Processing test results' - task: PublishTestResults@2 diff --git a/buildscripts/builddrivers.py b/buildscripts/builddrivers.py index e12400c92..53d3906e7 100644 --- a/buildscripts/builddrivers.py +++ b/buildscripts/builddrivers.py @@ -197,11 +197,11 @@ def build(self): ext_dir = self.build_extensions(root_dir, logfile) print('Build Completed') except: - print('Something went wrong, launching log file', logfile) - # display log file only when not testing - if not self.testing: - logfile_path = os.path.join(root_dir, 'php-sdk', logfile) + print('Something went wrong, Print logfile to terminal', logfile) + logfile_path = os.path.join(root_dir, 'php-sdk', logfile) + if os.path.isfile(logfile_path): with open(logfile_path, 'r') as f: + f.seek(0) print(f.read()) os.chdir(work_dir) exit(1) diff --git a/source/pdo_sqlsrv/config.w32 b/source/pdo_sqlsrv/config.w32 index 907da689b..f6e80fb69 100644 --- a/source/pdo_sqlsrv/config.w32 +++ b/source/pdo_sqlsrv/config.w32 @@ -27,10 +27,10 @@ if( PHP_PDO_SQLSRV != "no" ) { if (CHECK_LIB("odbc32.lib", "pdo_sqlsrv") && CHECK_LIB("odbccp32.lib", "pdo_sqlsrv") && CHECK_LIB("version.lib", "pdo_sqlsrv") && CHECK_LIB("psapi.lib", "pdo_sqlsrv")&& CHECK_HEADER_ADD_INCLUDE( "core_sqlsrv.h", "CFLAGS_PDO_SQLSRV", configure_module_dirname + "\\shared")) { - CHECK_HEADER_ADD_INCLUDE("sql.h", "CFLAGS_PDO_SQLSRV_ODBC"); - CHECK_HEADER_ADD_INCLUDE("sqlext.h", "CFLAGS_PDO_SQLSRV_ODBC"); CHECK_HEADER_ADD_INCLUDE("php_pdo_sqlsrv_int.h", "CFLAGS_PDO_SQLSRV", configure_module_dirname); CHECK_HEADER_ADD_INCLUDE("php_pdo_sqlsrv.h", "CFLAGS_PDO_SQLSRV", configure_module_dirname); + CHECK_HEADER_ADD_INCLUDE("sql.h", "CFLAGS_PDO_SQLSRV_ODBC"); + CHECK_HEADER_ADD_INCLUDE("sqlext.h", "CFLAGS_PDO_SQLSRV_ODBC"); ADD_SOURCES( configure_module_dirname + "\\shared", shared_src_class, "pdo_sqlsrv" ); ADD_FLAG( "LDFLAGS_PDO_SQLSRV", "/NXCOMPAT /DYNAMICBASE /debug /guard:cf" ); ADD_FLAG( "CFLAGS_PDO_SQLSRV", "/EHsc" ); diff --git a/source/pdo_sqlsrv/pdo_init.cpp b/source/pdo_sqlsrv/pdo_init.cpp index e3c5858ec..f05a15025 100644 --- a/source/pdo_sqlsrv/pdo_init.cpp +++ b/source/pdo_sqlsrv/pdo_init.cpp @@ -249,7 +249,7 @@ PHP_RSHUTDOWN_FUNCTION(pdo_sqlsrv) { // SQLSRV_UNUSED( module_number ); // SQLSRV_UNUSED( type ); - +#ifdef _WIN32 for (size_t current_token = 0; current_token < PDO_SQLSRV_G(access_tokens_size); current_token++) { if (PDO_SQLSRV_G(access_tokens)[current_token]) { memset(PDO_SQLSRV_G(access_tokens)[current_token]->data, 0, PDO_SQLSRV_G(access_tokens)[current_token]->dataSize); @@ -257,7 +257,7 @@ PHP_RSHUTDOWN_FUNCTION(pdo_sqlsrv) } } sqlsrv_free(PDO_SQLSRV_G(access_tokens)); - +#endif PDO_LOG_NOTICE("pdo_sqlsrv: entering rshutdown"); return SUCCESS; diff --git a/source/pdo_sqlsrv/php_pdo_sqlsrv.h b/source/pdo_sqlsrv/php_pdo_sqlsrv.h index 3e3c1b593..dd00d695e 100644 --- a/source/pdo_sqlsrv/php_pdo_sqlsrv.h +++ b/source/pdo_sqlsrv/php_pdo_sqlsrv.h @@ -21,7 +21,10 @@ //--------------------------------------------------------------------------------------------------------------------------------- #include "php.h" + +#ifdef _WIN32 #include "msodbcsql.h" +#endif //********************************************************************************************************************************* // Global variables //********************************************************************************************************************************* @@ -32,11 +35,12 @@ ZEND_BEGIN_MODULE_GLOBALS(pdo_sqlsrv) unsigned int pdo_log_severity; zend_long client_buffer_max_size; short report_additional_errors; -ACCESSTOKEN** access_tokens; -unsigned int access_tokens_size = 0; #ifndef _WIN32 zend_long set_locale_info; +#else +ACCESSTOKEN** access_tokens; +unsigned int access_tokens_size = 0; #endif ZEND_END_MODULE_GLOBALS(pdo_sqlsrv) diff --git a/source/shared/core_conn.cpp b/source/shared/core_conn.cpp index 1c0a2cb4d..5ba102c85 100644 --- a/source/shared/core_conn.cpp +++ b/source/shared/core_conn.cpp @@ -32,12 +32,12 @@ #ifndef _WIN32 #include #include -#endif - +#else extern "C" { #include "php_pdo_sqlsrv.h" } #include "php_pdo_sqlsrv_int.h" +#endif // *** internal variables and constants *** @@ -1187,7 +1187,7 @@ void access_token_set_func::func( _In_ connection_option const* option, _In_ zva // similar to a UCS-2 string containing only ASCII characters // // See https://docs.microsoft.com/sql/connect/odbc/using-azure-active-directory#authenticating-with-an-access-token - +#ifdef _WIN32 size_t next_token_position = 0; bool same_token_used = false; @@ -1205,6 +1205,7 @@ void access_token_set_func::func( _In_ connection_option const* option, _In_ zva break; } } +#endif size_t dataSize = 2 * value_len; @@ -1227,11 +1228,12 @@ void access_token_set_func::func( _In_ connection_option const* option, _In_ zva // Save the pointer because SQLDriverConnect() will use it to make connection to the server conn->azure_ad_access_token = pAccToken; accToken.transferred(); - +#ifdef _WIN32 if (!same_token_used) { next_token_position = PDO_SQLSRV_G(access_tokens_size); PDO_SQLSRV_G(access_tokens_size)++; PDO_SQLSRV_G(access_tokens) = reinterpret_cast(sqlsrv_realloc(PDO_SQLSRV_G(access_tokens), PDO_SQLSRV_G(access_tokens_size) * sizeof(ACCESSTOKEN))); } PDO_SQLSRV_G(access_tokens)[next_token_position] = conn->azure_ad_access_token; +#endif } From ae1f8080affcc367d1ab9b113dfb1adf33a0b65c Mon Sep 17 00:00:00 2001 From: Sicong Date: Tue, 26 Jul 2022 05:55:18 +0000 Subject: [PATCH 08/10] Add sqlsrv fix --- appveyor.yml | 1 - source/pdo_sqlsrv/config.w32 | 1 + source/shared/core_conn.cpp | 47 +++++++++++++++++++++++++++--------- source/sqlsrv/config.w32 | 3 +++ source/sqlsrv/init.cpp | 9 +++++++ source/sqlsrv/php_sqlsrv.h | 6 +++++ 6 files changed, 55 insertions(+), 12 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index eaa2debcb..4cf0be78b 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -101,7 +101,6 @@ build_script: - copy %APPVEYOR_BUILD_FOLDER%\buildscripts\*.py c:\projects - cd c:\projects - python -V - - ps: $blockRdp = $true; iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1')) - python builddrivers.py --PHPVER=%PHP_VERSION% --ARCH=%BUILD_PLATFORM% --THREAD=%THREAD% --SOURCE=%APPVEYOR_BUILD_FOLDER%\source --TESTING --NO_RENAME - cd c:\projects\php-sdk\phpdev\%PHP_VC%\%BUILD_PLATFORM%\php-%PHP_VERSION%-src\ - set PHP_SRC_DIR=%CD%\ext diff --git a/source/pdo_sqlsrv/config.w32 b/source/pdo_sqlsrv/config.w32 index f6e80fb69..e3e9ca494 100644 --- a/source/pdo_sqlsrv/config.w32 +++ b/source/pdo_sqlsrv/config.w32 @@ -33,6 +33,7 @@ if( PHP_PDO_SQLSRV != "no" ) { CHECK_HEADER_ADD_INCLUDE("sqlext.h", "CFLAGS_PDO_SQLSRV_ODBC"); ADD_SOURCES( configure_module_dirname + "\\shared", shared_src_class, "pdo_sqlsrv" ); ADD_FLAG( "LDFLAGS_PDO_SQLSRV", "/NXCOMPAT /DYNAMICBASE /debug /guard:cf" ); + ADD_FLAG( "CFLAGS_PDO_SQLSRV", "/D PDO_SQLSRV" ); ADD_FLAG( "CFLAGS_PDO_SQLSRV", "/EHsc" ); ADD_FLAG( "CFLAGS_PDO_SQLSRV", "/GS" ); ADD_FLAG( "CFLAGS_PDO_SQLSRV", "/Zi" ); diff --git a/source/shared/core_conn.cpp b/source/shared/core_conn.cpp index 5ba102c85..348993301 100644 --- a/source/shared/core_conn.cpp +++ b/source/shared/core_conn.cpp @@ -33,10 +33,17 @@ #include #include #else +#ifdef PDO_SQLSRV extern "C" { #include "php_pdo_sqlsrv.h" } #include "php_pdo_sqlsrv_int.h" +#elif SQLSRV +extern "C" { +#include "php_sqlsrv.h" +} +#include "php_sqlsrv_int.h" +#endif #endif // *** internal variables and constants *** @@ -1158,7 +1165,17 @@ size_t core_str_zval_is_true(_Inout_ zval* value_z) return 0; // false } -void access_token_set_func::func( _In_ connection_option const* option, _In_ zval* value, _Inout_ sqlsrv_conn* conn, _Inout_ std::string& conn_str ) +#ifdef _WIN32 +ACCESSTOKEN** get_access_tokens() { +#ifdef PDO_SQLSRV + return PDO_SQLSRV_G(access_tokens); +#elif SQLSRV + return SQLSRV_G(access_tokens); +#endif +} +#endif + +void access_token_set_func::func(_In_ connection_option const* option, _In_ zval* value, _Inout_ sqlsrv_conn* conn, _Inout_ std::string& conn_str) { SQLSRV_ASSERT(Z_TYPE_P(value) == IS_STRING, "An access token must be a byte string."); @@ -1190,16 +1207,21 @@ void access_token_set_func::func( _In_ connection_option const* option, _In_ zva #ifdef _WIN32 size_t next_token_position = 0; bool same_token_used = false; + #ifdef PDO_SQLSRV + unsigned int& access_tokens_size = PDO_SQLSRV_G(access_tokens_size); + #elif SQLSRV + unsigned int& access_tokens_size = SQLSRV_G(access_tokens_size); + #endif - for (size_t current_token_index = 0; current_token_index < PDO_SQLSRV_G(access_tokens_size); current_token_index++) { + for (size_t current_token_index = 0; current_token_index < access_tokens_size; current_token_index++) { std::string string_token; - for (size_t i = 0; i < PDO_SQLSRV_G(access_tokens)[current_token_index]->dataSize; i += 2) { - string_token.push_back(PDO_SQLSRV_G(access_tokens)[current_token_index]->data[i]); + for (size_t i = 0; i < get_access_tokens()[current_token_index]->dataSize; i += 2) { + string_token.push_back(get_access_tokens()[current_token_index]->data[i]); } if (string_token == std::string(value_str)) { // Token already exists in access_toiens - memset(PDO_SQLSRV_G(access_tokens)[current_token_index]->data, 0, PDO_SQLSRV_G(access_tokens)[current_token_index]->dataSize); - sqlsrv_free(PDO_SQLSRV_G(access_tokens)[current_token_index]); + memset(get_access_tokens()[current_token_index]->data, 0, get_access_tokens()[current_token_index]->dataSize); + sqlsrv_free(get_access_tokens()[current_token_index]); next_token_position = current_token_index; same_token_used = true; break; @@ -1208,7 +1230,6 @@ void access_token_set_func::func( _In_ connection_option const* option, _In_ zva #endif size_t dataSize = 2 * value_len; - sqlsrv_malloc_auto_ptr accToken; accToken = reinterpret_cast(sqlsrv_malloc(sizeof(ACCESSTOKEN) + dataSize)); @@ -1230,10 +1251,14 @@ void access_token_set_func::func( _In_ connection_option const* option, _In_ zva accToken.transferred(); #ifdef _WIN32 if (!same_token_used) { - next_token_position = PDO_SQLSRV_G(access_tokens_size); - PDO_SQLSRV_G(access_tokens_size)++; - PDO_SQLSRV_G(access_tokens) = reinterpret_cast(sqlsrv_realloc(PDO_SQLSRV_G(access_tokens), PDO_SQLSRV_G(access_tokens_size) * sizeof(ACCESSTOKEN))); + next_token_position = access_tokens_size; + access_tokens_size++; + #ifdef PDO_SQLSRV + PDO_SQLSRV_G(access_tokens) = reinterpret_cast(sqlsrv_realloc(PDO_SQLSRV_G(access_tokens), access_tokens_size * sizeof(ACCESSTOKEN))); + #elif SQLSRV + SQLSRV_G(access_tokens) = reinterpret_cast(sqlsrv_realloc(SQLSRV_G(access_tokens), access_tokens_size * sizeof(ACCESSTOKEN))); + #endif } - PDO_SQLSRV_G(access_tokens)[next_token_position] = conn->azure_ad_access_token; + get_access_tokens()[next_token_position] = conn->azure_ad_access_token; #endif } diff --git a/source/sqlsrv/config.w32 b/source/sqlsrv/config.w32 index 68a2d2207..cf6c4e8d9 100644 --- a/source/sqlsrv/config.w32 +++ b/source/sqlsrv/config.w32 @@ -32,7 +32,10 @@ if( PHP_SQLSRV != "no" ) { } CHECK_HEADER_ADD_INCLUDE("sql.h", "CFLAGS_SQLSRV_ODBC"); CHECK_HEADER_ADD_INCLUDE("sqlext.h", "CFLAGS_SQLSRV_ODBC"); + CHECK_HEADER_ADD_INCLUDE("php_sqlsrv_int.h", "CFLAGS_SQLSRV", configure_module_dirname); + CHECK_HEADER_ADD_INCLUDE("php_sqlsrv.h", "CFLAGS_SQLSRV", configure_module_dirname); ADD_FLAG( "LDFLAGS_SQLSRV", "/NXCOMPAT /DYNAMICBASE /debug /guard:cf" ); + ADD_FLAG( "CFLAGS_SQLSRV", "/D SQLSRV" ); ADD_FLAG( "CFLAGS_SQLSRV", "/D ZEND_WIN32_FORCE_INLINE" ); ADD_FLAG( "CFLAGS_SQLSRV", "/EHsc" ); ADD_FLAG( "CFLAGS_SQLSRV", "/GS" ); diff --git a/source/sqlsrv/init.cpp b/source/sqlsrv/init.cpp index a6f6e711d..5cc2c9e66 100644 --- a/source/sqlsrv/init.cpp +++ b/source/sqlsrv/init.cpp @@ -696,6 +696,15 @@ PHP_RSHUTDOWN_FUNCTION(sqlsrv) // SQLSRV_UNUSED( type ); LOG_FUNCTION( "PHP_RSHUTDOWN for php_sqlsrv" ); +#ifdef _WIN32 + for (size_t current_token = 0; current_token < SQLSRV_G(access_tokens_size); current_token++) { + if (SQLSRV_G(access_tokens)[current_token]) { + memset(SQLSRV_G(access_tokens)[current_token]->data, 0, SQLSRV_G(access_tokens)[current_token]->dataSize); + sqlsrv_free(SQLSRV_G(access_tokens)[current_token]); + } + } + sqlsrv_free(SQLSRV_G(access_tokens)); +#endif reset_errors(); // destruction diff --git a/source/sqlsrv/php_sqlsrv.h b/source/sqlsrv/php_sqlsrv.h index b30b41c6d..336635478 100644 --- a/source/sqlsrv/php_sqlsrv.h +++ b/source/sqlsrv/php_sqlsrv.h @@ -24,6 +24,9 @@ #include "php.h" +#ifdef _WIN32 +#include "msodbcsql.h" +#endif //********************************************************************************************************************************* // Global variables //********************************************************************************************************************************* @@ -44,6 +47,9 @@ zend_long buffered_query_limit; #ifndef _WIN32 zend_long set_locale_info; +#else +ACCESSTOKEN** access_tokens; +unsigned int access_tokens_size = 0; #endif ZEND_END_MODULE_GLOBALS(sqlsrv) From 6293a91212abfd46cbe0c6a7da62b79c3ec36f47 Mon Sep 17 00:00:00 2001 From: absci Date: Wed, 27 Jul 2022 10:57:14 -0700 Subject: [PATCH 09/10] Fix for nts --- azure-pipelines.yml | 2 +- source/pdo_sqlsrv/pdo_init.cpp | 2 +- source/shared/core_conn.cpp | 13 +++++++------ source/sqlsrv/init.cpp | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 7cdeb27aa..3d9756494 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -217,7 +217,7 @@ jobs: for f in pdo_sqlsrv/*.diff; do ls $f 2>/dev/null; cat $f 2>/dev/null; echo ''; done || true python output.py ls -l *.xml - more *.diff | cat + cat *.diff displayName: 'Processing test results' - task: PublishTestResults@2 diff --git a/source/pdo_sqlsrv/pdo_init.cpp b/source/pdo_sqlsrv/pdo_init.cpp index f05a15025..f6da9ff95 100644 --- a/source/pdo_sqlsrv/pdo_init.cpp +++ b/source/pdo_sqlsrv/pdo_init.cpp @@ -249,7 +249,7 @@ PHP_RSHUTDOWN_FUNCTION(pdo_sqlsrv) { // SQLSRV_UNUSED( module_number ); // SQLSRV_UNUSED( type ); -#ifdef _WIN32 +#if defined(_WIN32) && !defined(ZTS) for (size_t current_token = 0; current_token < PDO_SQLSRV_G(access_tokens_size); current_token++) { if (PDO_SQLSRV_G(access_tokens)[current_token]) { memset(PDO_SQLSRV_G(access_tokens)[current_token]->data, 0, PDO_SQLSRV_G(access_tokens)[current_token]->dataSize); diff --git a/source/shared/core_conn.cpp b/source/shared/core_conn.cpp index 348993301..41ba63449 100644 --- a/source/shared/core_conn.cpp +++ b/source/shared/core_conn.cpp @@ -232,12 +232,13 @@ sqlsrv_conn* core_sqlsrv_connect( _In_ sqlsrv_context& henv_cp, _In_ sqlsrv_cont } } +#ifdef ZTS // time to free the access token, if not null if (conn->azure_ad_access_token) { memset(conn->azure_ad_access_token->data, 0, conn->azure_ad_access_token->dataSize); // clear the memory conn->azure_ad_access_token.reset(); } - +#endif CHECK_SQL_ERROR( r, conn ) { throw core::CoreException(); } @@ -1165,7 +1166,7 @@ size_t core_str_zval_is_true(_Inout_ zval* value_z) return 0; // false } -#ifdef _WIN32 +#if defined(_WIN32) && !defined(ZTS) ACCESSTOKEN** get_access_tokens() { #ifdef PDO_SQLSRV return PDO_SQLSRV_G(access_tokens); @@ -1204,7 +1205,7 @@ void access_token_set_func::func(_In_ connection_option const* option, _In_ zval // similar to a UCS-2 string containing only ASCII characters // // See https://docs.microsoft.com/sql/connect/odbc/using-azure-active-directory#authenticating-with-an-access-token -#ifdef _WIN32 +#if defined(_WIN32) && !defined(ZTS) size_t next_token_position = 0; bool same_token_used = false; #ifdef PDO_SQLSRV @@ -1249,14 +1250,14 @@ void access_token_set_func::func(_In_ connection_option const* option, _In_ zval // Save the pointer because SQLDriverConnect() will use it to make connection to the server conn->azure_ad_access_token = pAccToken; accToken.transferred(); -#ifdef _WIN32 +#if defined(_WIN32) && !defined(ZTS) if (!same_token_used) { next_token_position = access_tokens_size; access_tokens_size++; #ifdef PDO_SQLSRV - PDO_SQLSRV_G(access_tokens) = reinterpret_cast(sqlsrv_realloc(PDO_SQLSRV_G(access_tokens), access_tokens_size * sizeof(ACCESSTOKEN))); + PDO_SQLSRV_G(access_tokens) = reinterpret_cast(sqlsrv_realloc(PDO_SQLSRV_G(access_tokens), access_tokens_size * sizeof(ACCESSTOKEN*))); #elif SQLSRV - SQLSRV_G(access_tokens) = reinterpret_cast(sqlsrv_realloc(SQLSRV_G(access_tokens), access_tokens_size * sizeof(ACCESSTOKEN))); + SQLSRV_G(access_tokens) = reinterpret_cast(sqlsrv_realloc(SQLSRV_G(access_tokens), access_tokens_size * sizeof(ACCESSTOKEN*))); #endif } get_access_tokens()[next_token_position] = conn->azure_ad_access_token; diff --git a/source/sqlsrv/init.cpp b/source/sqlsrv/init.cpp index 5cc2c9e66..5386cbfa2 100644 --- a/source/sqlsrv/init.cpp +++ b/source/sqlsrv/init.cpp @@ -696,7 +696,7 @@ PHP_RSHUTDOWN_FUNCTION(sqlsrv) // SQLSRV_UNUSED( type ); LOG_FUNCTION( "PHP_RSHUTDOWN for php_sqlsrv" ); -#ifdef _WIN32 +#if defined(_WIN32) && !defined(ZTS) for (size_t current_token = 0; current_token < SQLSRV_G(access_tokens_size); current_token++) { if (SQLSRV_G(access_tokens)[current_token]) { memset(SQLSRV_G(access_tokens)[current_token]->data, 0, SQLSRV_G(access_tokens)[current_token]->dataSize); From 2fe34406c330c8e77dcbaad9bb10d709c3218776 Mon Sep 17 00:00:00 2001 From: Sicong Date: Tue, 13 Sep 2022 20:05:22 +0000 Subject: [PATCH 10/10] Revert unrelated changes --- azure-pipelines.yml | 1 - buildscripts/builddrivers.py | 11 +++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 3d9756494..c7b643f9c 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -217,7 +217,6 @@ jobs: for f in pdo_sqlsrv/*.diff; do ls $f 2>/dev/null; cat $f 2>/dev/null; echo ''; done || true python output.py ls -l *.xml - cat *.diff displayName: 'Processing test results' - task: PublishTestResults@2 diff --git a/buildscripts/builddrivers.py b/buildscripts/builddrivers.py index 53d3906e7..26c91c1cd 100644 --- a/buildscripts/builddrivers.py +++ b/buildscripts/builddrivers.py @@ -197,14 +197,13 @@ def build(self): ext_dir = self.build_extensions(root_dir, logfile) print('Build Completed') except: - print('Something went wrong, Print logfile to terminal', logfile) - logfile_path = os.path.join(root_dir, 'php-sdk', logfile) - if os.path.isfile(logfile_path): - with open(logfile_path, 'r') as f: - f.seek(0) - print(f.read()) + print('Something went wrong, launching log file', logfile) + # display log file only when not testing + if not self.testing: + os.startfile(os.path.join(root_dir, 'php-sdk', logfile)) os.chdir(work_dir) exit(1) + if not self.testing: choice = input("Rebuild using the same configuration(yes) or quit (no) [yes/no]: ") choice = choice.lower()