php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #81049 PDO Sqlite in memory database ignores memory_limit
Submitted: 2021-05-19 11:10 UTC Modified: 2021-06-08 14:16 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: mvorisek at mvorisek dot cz Assigned:
Status: Wont fix Package: PDO SQLite
PHP Version: 7.4.19 OS: Windows
Private report: No CVE-ID: None
 [2021-05-19 11:10 UTC] mvorisek at mvorisek dot cz
Description:
------------
Currently, PDO Sqlite in memory database ignores memory_limit which is dangerous as system's memory can be exhausted by a single script.

PDO Sqlite should allocate memory using PHP allocation routines so the memory is counted and limited.

Test script:
---------------
<?php

var_dump(memory_get_usage());
var_dump(memory_get_usage(true));
echo "\n";

for ($i = 0; $i < 3; $i++) {
    $db = new PDO('sqlite::memory:');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->exec('CREATE TABLE foo (bar TEXT)');
    for ($j = 0; $j < 20; $j++) {
        $db->exec("INSERT INTO foo (bar) VALUES ('" . bin2hex(random_bytes(2 * 1024 * 1024)) . "')");
    }
    var_dump(memory_get_usage());
    var_dump(memory_get_usage(true));
    $db->exec('DROP TABLE foo');
    $db = null;
    var_dump(memory_get_usage());
    var_dump(memory_get_usage(true));
    echo "\n";
}


Expected result:
----------------
memory_get_usage includes memory allocated by sqlite and memory_limit is honored

Actual result:
--------------
int(351144)
int(2097152)

int(351536)
int(2097152)
int(351176)
int(2097152)

int(351536)
int(2097152)
int(351176)
int(2097152)

int(351536)
int(2097152)
int(351176)
int(2097152)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-05-19 11:37 UTC] cmb@php.net
-Type: Bug +Type: Feature/Change Request
 [2021-05-19 11:37 UTC] cmb@php.net
> PDO Sqlite should allocate memory using PHP allocation routines

SQLite3 offers custom allocators[1] as of 3.6.10, and PHP 7.4
already requires 3.7.5, so this looks possible and would make
sense.

I don't think not using ZendMM here qualifies as bug, though,
since this is quite common for many extensions interfacing with
external libraries.  Thus, changing to feature request.

Note that ext/sqlite3 likely has the same issue.

[1] <https://www.sqlite.org/c3ref/mem_methods.html>
 [2021-06-08 14:16 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2021-06-08 14:16 UTC] nikic@php.net
Based on the discussion in https://github.com/php/php-src/pull/7087, we unfortunately cannot use ZendMM with the sqlite3 custom allocator API, because the allocator is not per-connection, and persistent connections cannot use the per-request allocator.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 14:01:30 2024 UTC