# Magic getter bug tickled by Moodle

- Thread: https://externals.io/message/81467
- Original archive: https://news-web.php.net/php.internals/81467
- Started by Rasmus Lerdorf on 2015-01-31T05:04:30+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #81467 — Rasmus Lerdorf — 2015-01-31T05:04:30+00:00

URL: https://externals.io/message/81467#81467

There is something weird going on with PHP7 and the moodle_page class
and all its magic methods. It dies with a:

/var/www/moodle/blocks/navigation/block_navigation.php(223) : Notice -
Undefined property: moodle_page::$navigation
/var/www/moodle/blocks/navigation/block_navigation.php(223) : Fatal
error - __clone method called on non-object

That code is:

222   $this->page->navigation->initialise();
223   return clone($this->page->navigation);

See:
https://github.com/moodle/moodle/blob/master/blocks/navigation/block_navigation.php#L222-L223

$this->page_navigation is created on that initial call through the
wonders of this magic:

https://github.com/moodle/moodle/blob/master/lib/pagelib.php#L757-L772

Which somehow leads us to read 8 bytes in a free'ed block:

https://gist.github.com/anonymous/f9d1f53b1205d29d86cc

but I am not having much luck dwindling it down to a simpler reproduce
case. If you know this code or have a couple of hours to spare, could
you see if you could reduce it to something more manageable for us?

(and yes, same code is fine in 5.6)

-Rasmus

---

## #81469 — Michael Wallner — 2015-01-31T07:13:16+00:00

URL: https://externals.io/message/81467#81469 · In reply to: #81467

>
> There is something weird going on with PHP7 and the moodle_page class
> and all its magic methods. It dies with a:
>
> /var/www/moodle/blocks/navigation/block_navigation.php(223) : Notice -
> Undefined property: moodle_page::$navigation
> /var/www/moodle/blocks/navigation/block_navigation.php(223) : Fatal
> error - __clone method called on non-object
>
> That code is:
>
> 222   $this->page->navigation->initialise();
> 223   return clone($this->page->navigation);
>
> See:
>
https://github.com/moodle/moodle/blob/master/blocks/navigation/block_navigation.php#L222-L223
>
> $this->page_navigation is created on that initial call through the
> wonders of this magic:
>
> https://github.com/moodle/moodle/blob/master/lib/pagelib.php#L757-L772
>
> Which somehow leads us to read 8 bytes in a free'ed block:
>
> https://gist.github.com/anonymous/f9d1f53b1205d29d86cc
>
> but I am not having much luck dwindling it down to a simpler reproduce
> case. If you know this code or have a couple of hours to spare, could
> you see if you could reduce it to something more manageable for us?
>
> (and yes, same code is fine in 5.6)
>

Here's a similar bug,  too: https://bugs.php.net/bug.php?id=68933&edit=1

---

## #81470 — Rasmus Lerdorf — 2015-01-31T07:15:33+00:00

URL: https://externals.io/message/81467#81470 · In reply to: #81469

> 
> On 31 Jan 2015 06:04, "Rasmus Lerdorf" <rasmus@lerdorf.com
> <mailto:rasmus@lerdorf.com>> wrote:
> > […]
> https://github.com/moodle/moodle/blob/master/blocks/navigation/block_navigation.php#L222-L223
> > […]
> 
> Here's a similar bug,  too: https://bugs.php.net/bug.php?id=68933&edit=1

Ah, good catch. Yes,
https://gist.github.com/arjenschol/3d94195ca51aa44db1c6 looks like a
similar pattern to what Moodle is doing in that part of the code that
seems to trigger this.

-Rasmus

---

## #81477 — Xinchen Hui — 2015-01-31T10:15:29+00:00

URL: https://externals.io/message/81467#81477 · In reply to: #81470

Hey:


> 
> > […]
>
> Ah, good catch. Yes,
> https://gist.github.com/arjenschol/3d94195ca51aa44db1c6 looks like a
> similar pattern to what Moodle is doing in that part of the code that
> seems to trigger this.
must be fixed,
https://github.com/php/php-src/commit/1a60175e2595a24ebc3b6d80a112d574c6c98f58

thanks
>
> -Rasmus
>



-- 
Xinchen Hui
@Laruence
http://www.laruence.com/

---

## #81566 — Yasuo Ohgaki — 2015-02-02T08:52:59+00:00

URL: https://externals.io/message/81467#81566 · In reply to: #81477

Hi Xinchen,



> must be fixed,
>
> https://github.com/php/php-src/commit/1a60175e2595a24ebc3b6d80a112d574c6c98f58
>

Impressive work!

It's irrelevant probably. I only get one valgrind leak with TS build now.
NTS is fine.

[yohgaki@dev php-src]$ USE_ZEND_ALLOC=0 valgrind --track-origins=yes
--leak-check=full ./sapi/cli/php -r "echo 1;"
==13614== Memcheck, a memory error detector
==13614== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==13614== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==13614== Command: ./php-bin -r echo\ 1;
==13614==
1==13614==
==13614== HEAP SUMMARY:
==13614==     in use at exit: 88,808 bytes in 2,768 blocks
==13614==   total heap usage: 32,420 allocs, 29,652 frees, 3,656,230 bytes
allocated
==13614==
==13614== 376 bytes in 1 blocks are definitely lost in loss record 89 of 135
==13614==    at 0x4C29BCF: malloc (in
/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==13614==    by 0xAEB3B5: alloc_globals_ctor (zend_alloc.c:2382)
==13614==    by 0xA6665C: ts_allocate_id (TSRM.c:259)
==13614==    by 0xAEB4B9: start_memory_manager (zend_alloc.c:2405)
==13614==    by 0xB21D09: zend_startup (zend.c:572)
==13614==    by 0xA6DA0F: php_module_startup (main.c:2105)
==13614==    by 0xBD2FB1: php_cli_startup (php_cli.c:421)
==13614==    by 0xBD53B7: main (php_cli.c:1341)
==13614==
==13614== LEAK SUMMARY:
==13614==    definitely lost: 376 bytes in 1 blocks
==13614==    indirectly lost: 0 bytes in 0 blocks
==13614==      possibly lost: 0 bytes in 0 blocks
==13614==    still reachable: 88,432 bytes in 2,767 blocks
==13614==         suppressed: 0 bytes in 0 blocks
==13614== Reachable blocks (those to which a pointer was found) are not
shown.
==13614== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==13614==
==13614== For counts of detected and suppressed errors, rerun with: -v
==13614== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)


--
Yasuo Ohgaki
yohgaki@ohgaki.net
