2016-07-06 41 views
8

簡介PHP7 OAuth的非法存儲器分配

儘管複雜的Web應用程序中運行,一個衍生PHP7進程試圖使用的OAuth模塊何時分配的存儲器(18446744069414584466個字節)非法量。 FPM經理重啓後觸發2-5次此代碼後出現的錯誤:

$oauthClient = new \OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION); 
$oauthClient->disableSSLChecks(); 
$oauthClient->setToken($token, $tokenSecret); 
$oauthClient->fetch($callUrl, $strPostData, $method, $headers); 

錯誤消息

*20 FastCGI sent in stderr: "PHP message: PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 18446744069414584466 bytes) 

說明

錯誤消息中提到的受影響的行是提取方法oauth客戶端

$oauthClient->fetch($callUrl, $strPostData, $method, $headers); 

我試圖通過在循環中執行它並觀察隨着時間的推移內存使用情況來隔離相關的代碼。在使用和分配的內存量似乎隨着時間的推移增長平穩,但速度不如預期(可能這只是oauthClient緩存響應)

獨立

代碼

<?php 

$strPostData = ''; 
$method = 'GET'; 
$consumerKey = '<consumerKey>'; 
$consumerSecret = '<consumerSecret>'; 
$token = '<token>'; 
$tokenSecret = '<tokenSecret>'; 
$url = '<url>'; 

$headers = array('accept' => 'application/json'); 
$callUrl = $url; 

if ($method === 'POST' || $method === 'PUT') { 
    $headers['Content-Type'] = 'application/json'; 
} 

$oauthClient = new \OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION); 
$oauthClient->disableSSLChecks(); 
$oauthClient->setToken($token, $tokenSecret); 

do { 
     $oauthClient->fetch($callUrl, $strPostData, $method, $headers); 
     $response = $oauthClient->getLastResponse(); 
     fwrite(STDOUT, 'Allocated Memory: '. memory_get_usage(false) . PHP_EOL); 
     fwrite(STDOUT, 'Used Memory: '. memory_get_usage(true) . PHP_EOL); 
} while (true); 
?> 

輸出

Allocated Memory: 236920 
Used Memory: 262144 
... 
Allocated Memory: 263168 
Used Memory: 524288 
... 
Allocated Memory: 289504 
Used Memory: 524288 
... 
Used Memory: 524288 
Allocated Memory: 331888 
... 
Allocated Memory: 395976 
Used Memory: 524288 
... 
Allocated Memory: 428600 
Used Memory: 524288 
... 

核心&模塊版本

Core: 7.0.8-3+deb.sury.org~trusty+1 
date: 7.0.8-3+deb.sury.org~trusty+1 
libxml: 7.0.8-3+deb.sury.org~trusty+1 
openssl: 7.0.8-3+deb.sury.org~trusty+1 
pcre: 7.0.8-3+deb.sury.org~trusty+1 
zlib: 7.0.8-3+deb.sury.org~trusty+1 
filter: 7.0.8-3+deb.sury.org~trusty+1 
hash: 1.0 
pcntl: 7.0.8-3+deb.sury.org~trusty+1 
Reflection: 7.0.8-3+deb.sury.org~trusty+1 
SPL: 7.0.8-3+deb.sury.org~trusty+1 
session: 7.0.8-3+deb.sury.org~trusty+1 
standard: 7.0.8-3+deb.sury.org~trusty+1 
mysqlnd: mysqlnd 5.0.12-dev - 20150407 - $Id: 241ae00989d1995ffcbbf63d579943635faf9972 $ 
PDO: 7.0.8-3+deb.sury.org~trusty+1 
xml: 7.0.8-3+deb.sury.org~trusty+1 
bcmath: 7.0.8-3+deb.sury.org~trusty+1 
calendar: 7.0.8-3+deb.sury.org~trusty+1 
ctype: 7.0.8-3+deb.sury.org~trusty+1 
curl: 7.0.8-3+deb.sury.org~trusty+1 
dom: 20031129 
mbstring: 7.0.8-3+deb.sury.org~trusty+1 
fileinfo: 1.0.5 
ftp: 7.0.8-3+deb.sury.org~trusty+1 
gd: 7.0.8-3+deb.sury.org~trusty+1 
gettext: 7.0.8-3+deb.sury.org~trusty+1 
iconv: 7.0.8-3+deb.sury.org~trusty+1 
json: 1.4.0 
exif: 1.4 $Id: 8bdc0c8f27c2c9dd1f7551f1f9fe3ab57a06a4b1 $ 
mysqli: 7.0.8-3+deb.sury.org~trusty+1 
OAuth: 2.0.2 
pdo_mysql: 7.0.8-3+deb.sury.org~trusty+1 
pdo_sqlite: 7.0.8-3+deb.sury.org~trusty+1 
Phar: 2.0.2 
posix: 7.0.8-3+deb.sury.org~trusty+1 
readline: 7.0.8-3+deb.sury.org~trusty+1 
shmop: 7.0.8-3+deb.sury.org~trusty+1 
SimpleXML: 7.0.8-3+deb.sury.org~trusty+1 
soap: 7.0.8-3+deb.sury.org~trusty+1 
sockets: 7.0.8-3+deb.sury.org~trusty+1 
sqlite3: 0.7-dev 
ssh2: 0.13-dev 
sysvmsg: 7.0.8-3+deb.sury.org~trusty+1 
sysvsem: 7.0.8-3+deb.sury.org~trusty+1 
sysvshm: 7.0.8-3+deb.sury.org~trusty+1 
tokenizer: 7.0.8-3+deb.sury.org~trusty+1 
wddx: 7.0.8-3+deb.sury.org~trusty+1 
xmlreader: 7.0.8-3+deb.sury.org~trusty+1 
xmlwriter: 7.0.8-3+deb.sury.org~trusty+1 
xsl: 7.0.8-3+deb.sury.org~trusty+1 
zip: 1.13.3 
Zend OPcache: 7.0.8-3+deb.sury.org~trusty+1 
+0

你有沒有想過這個?我與pecl oauth和PHP 7.0.4-7ubuntu2.1(在Ubuntu 16.04上)有相同的問題。可笑的高內存分配:允許的內存大小134217728字節用盡(試圖分配18446744069414591624字節) –

+0

我還會補充說,我遇到了這與Apache和,如果我再次保存文件後服務器重新啓動,代碼通常工作好的。 –

+0

不幸的是,我們不得不降級到php5.6,並且如果我查看最近在基本函數中發現和修復的錯誤數(http://php.net/ChangeLog-7.php# 7.0),我不認爲它可以真正用於生產無論如何,例如:整數溢出在json_encode()/ json_decode()/ json_utf8_to_utf16(),整數溢出nl2br() –

回答

4

我遇到過類似的問題,與使用OAuth的擴展與opcache啓用跟蹤它到一個問題。實際上有一個針對我遇到的確切情況的PHP打開的錯誤 - https://bugs.php.net/bug.php?id=73310。在完全解決問題之前,我們找到了解決此問題的潛在解決方法,將利用oauth擴展名的文件列入黑名單以清除異常。

您可以使用opcache.blacklist-filename選項 - http://php.net/manual/en/opcache.configuration.php#ini.opcache.blacklist-filename將opcache的文件列入黑名單。

  • 添加一個txt文件,每個文件的完整路徑您的服務器上,你想黑名單在自己的行
  • 在編輯模式下打開opcache.ini(如須藤六/etc/php/7.0/ mods-available/opcache.ini)
  • 編輯opcache。黑名單文件名選項指向你的黑名單文件
  • 如果您正在使用FPM創建的txt文件,重新啓動它
0

從哪兒冒出來,這個工作對我來說就像魅力

$this->oauth->fetch($endpoint, [], 'GET', ['Accept' => 'application/json']); 

成爲

$this->oauth->fetch($endpoint, ['fix'], 'GET', ['Accept' => 'application/json']); 

是的,只需填寫第二個參數,不留空