Review main Merged

28553[BUGFIX] ClassLoader exits early for invalid class names

Xavier Perseguers CR +2 V +2 Easy +6 −8 updated 03/24/14, 13:04

Commit message

Instead of doing calculations if an invalid
class name could still be found and implementing
safeguards that our caching layer does not
fail with invalid identifiers, we exit early
if we find an invalid class name.

Maybe PHP will be smart enough at some point
not calling an autoloader at all if the class name
is invalid anyway so we could the remove this check.

By doing a comprehensive check upfront, we can
remove the safeguard code for the caching layer.

The performance impact of this change breaks down
to be lower than 1ms for a regular TYPO3 request.

Review

  • Code review CR +2
    • Ernesto Baschny +2
    • Thomas Maroschik +1
    • Alexander Opitz +1
    • Markus Klein +1
    • Alexander Stehlik +1
    • Sebastian Fischer +1
  • Verified V +2
    • Ernesto Baschny +2
    • Alexander Opitz +1
    • Markus Klein +1

Issues 1

Files 1

Review log 34

Show 9 earlier messages
  1. Mr. Jenkins commented on patch set 1 03/20/14, 08:56

  2. Alexander Stehlik commented on patch set 1 03/20/14, 09:30

    Maybe a unit test with some valid class names would make sense?
  3. Thomas Maroschik commented on patch set 1 CR -1 03/20/14, 09:36

    Please no. I had this check once in and it impacted performance. So it got removed. The current check for the space is usually enough.
  4. Alexander Stehlik commented on patch set 1 03/20/14, 09:38

    Maybe this should be fixed at the place where the invalid class name is used?
  5. Xavier Perseguers commented on patch set 1 03/20/14, 09:47

    This method does not properly do what it is expected to do, moving the check outside will duplicate this exact business logic. I tested speed, with a number of loop equal to 10k (which should be quite large for actual use in any TYPO3 request), with a valid class name and an invalid one: strpos (valid): 0.035178899765015ms preg_match (valid): 0.044522047042847ms strpos (invalid): 0.03190803527832ms preg_match (invalid): 0.048931837081909ms code used: <?php $validClassName = 'TYPO3\CMS\Core\Utility\GeneralUtility'; $invalidClassName = 'int+'; $loops = 10000; $start = microtime(TRUE); for ($i = 0; $i < $loops; $i++) { $_ = isValidClassNameONE($validClassName); } $end = microtime(TRUE); echo 'strpos (valid): ' . ($end - $start) . 'ms' . "\n"; $start = microtime(TRUE); for ($i = 0; $i < $loops; $i++) { $_ = isValidClassNameTWO($validClassName); } $end = microtime(TRUE); echo 'preg_match (valid): ' . ($end - $start) . 'ms' . "\n"; $start = microtime(TRUE); for ($i = 0; $i < $loops; $i++) { $_ = isValidClassNameONE($invalidClassName); } $end = microtime(TRUE); echo 'strpos (invalid): ' . ($end - $start) . 'ms' . "\n"; $start = microtime(TRUE); for ($i = 0; $i < $loops; $i++) { $_ = isValidClassNameTWO($invalidClassName); } $end = microtime(TRUE); echo 'preg_match (invalid): ' . ($end - $start) . 'ms' . "\n"; function isValidClassNameONE($className) { return strpos($className, ' ') === FALSE; } function isValidClassNameTWO($className) { return preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\\\x7f-\xff]*$/', $className); }
  6. Xavier Perseguers commented on patch set 1 03/20/14, 09:55

    these are seconds, not ms, difference is 9 ms, for a number of 10k calls, in Frontend, number of calls is typically < 400 which takes it down to say 1000 instead of 400 thus 0.9 ms
  7. Jigal van Hemert commented on patch set 1 V +1 CR +1 03/20/14, 10:22

    After a large number of tries the typical numbers on my Windows system are: strpos (valid): 0.0080001354217529ms preg_match (valid): 0.026999950408936ms strpos (invalid): 0.0090000629425049ms preg_match (invalid): 0.027999877929688ms The difference is more than Xavier noted, but IMO well worth the difference between a function that works properly and one that doesn't. The reason for the simple check was that internally only valid class names would be used for loading. Obviously this isn't the case and thus the proper check should be used.
  8. Xavier Perseguers abandoned the change 03/20/14, 10:24

    It turns out that 'int+' is not supported by TCA, only by TS, thus I abandon this bug
  9. Helmut Hummel restored the change 03/21/14, 07:01

  1. Helmut Hummel commented on patch set 1 03/21/14, 07:12

    I measured a performance degration of 1ms on my system with 1000 calls to preg_match/strpos In the backend (tested several modules) loadClass is called around 300-400 times. So I do not see much of a performance impact here and it can save us some hassles. What are your measurements Tom? But I'm still unsure if we would fix something here which should be fixed elsewhere...
  2. Alexander Stehlik commented on patch set 1 03/21/14, 09:21

    Actually I would expect the class loader to fail if it is called with an invalid class name. I do not think failing silently in that case is good. I would suggest to put check check in callUserFunction() because this would be the place where we get unpredictable class names, right? But even then, would it not be better when the user gets an error that the class he is using is invalid? Just my 2 cents.
  3. Ernesto Baschny commented on patch set 1 03/21/14, 09:58

    I also measured 1ms ca per 1000 calls difference. non-cached simple FE page = 156 calls cached FE page = 77 calls extension manager = 288 calls So performance-wise this should not make a difference. The class loader should behave just like the PHP class loader. Unfortunately there is no "interface" which needs to be implemented, but the default autoloader returns "void" and does not crash or fail if the class is not there. It simply does nothing then. The following usage of the class will the "fail" (i.e. class_exists returns FALSE, new on that classname will fatal, etc). So in my opinion we should do little to no validation on what we get in, just make do the necessary to be able to work with the caching layer. Validating the class name is just a "shortcut" to avoid polluting the cache with dumb entries. We could validate "even more" and then get less garbage in the cache, that would be the only reason to have a more strict validation in place. This is why the current space validation is enough, as it's not that common to auto-load some random string garbage as a classname.
  4. Alexander Stehlik commented on patch set 1 03/21/14, 11:04

    Thanks for the explanation Ernesto :) So what about this suggestion: the StringFrontend calls isValidEntryIdentifier and throws an invalid argument exception if the identifier is invalid. What about introducing a new InvalidCacheIdentifierException that extends InvalidArgumentException and catch this Exception in the class loader. Then we could fail silently when the InvalidCacheIdentifier Exception is thrown and need no extra regex check. What do you think?
  5. Ernesto Baschny commented on patch set 1 03/21/14, 15:05

    I am not sure we need yet another exception, because there are no other ones being thrown here. In my opinion when reading the core, we just need to add the try/catch that we already have in getClassLoadingInformationFromCache() also when doing the ->set on the cache in "buildCachedClassLoadingInformation()" so that we are equally safe here.
  6. Helmut Hummel commented on patch set 1 03/22/14, 14:24

    So Ernesto, you would be in favor of something like this: https://review.typo3.org/28467 ? I tend to disagree. Why jumping through hoops, checking if we can find a class file (buildClassLoadingInformation) if we could know beforehand, that the class name is invalid and exit early? We could get rid of all try/catch and clearly behave like the PHP class loader do nothing at all and return early. In fact PHP should not call an autoloader for invalid class names as the language should know about that, but as of now, we have to deal with it. I mean several people proved, that this change has virtually no impact on performance and we still prefer a complicated solution (several try catch, trying to resolve class paths for classes that cannot exist) over a simple one (exit early)?
  7. Helmut Hummel uploaded patch set 2 03/22/14, 14:43

  8. Helmut Hummel uploaded patch set 3 03/22/14, 14:44

  9. Mr. Jenkins commented on patch set 2 03/22/14, 15:01

  10. Mr. Jenkins commented on patch set 3 03/22/14, 15:01

  11. Alexander Stehlik commented on patch set 3 03/22/14, 15:34

    Hmm, I still think this check is not required. Why not move the catch of the exception in the loadClass method. Then we would know that the cache identifier is invalid and can exit early after the first call to getClassLoadingInformationFromCache(). This is again a different solution than in https://review.typo3.org/#/c/28467. I would prefer this solution because it means less code and less possible errors.
  12. Markus Klein commented on patch set 3 03/22/14, 18:49

    ClassLoader.php:209

    We can remove NULL from the description too now

    ClassLoader.php:232

    And here too.
  13. Helmut Hummel uploaded patch set 4 03/24/14, 08:26

  14. Helmut Hummel commented on patch set 3 03/24/14, 08:33

    ClassLoader.php:209 Reply

    strange. I changed it, but obviously forgot to push

    ClassLoader.php:232 Reply

    Done
  15. Helmut Hummel commented on patch set 4 03/24/14, 08:38

    @Alex: If you prefer simplicity, you should consider my patch. as you can see it only changes one line in the code and removes other lines. additionally less code is executed in certain conditions -> less error prone @Ernesto: can you comment please?
  16. Alexander Stehlik commented on patch set 4 03/24/14, 08:43

    @Helmut: I don't think you code is bad. But with my suggestion you would also save the regex check. But when Ernesto favorites this version I will vote +1 as well :)
  17. Mr. Jenkins commented on patch set 4 03/24/14, 09:00

  18. Thomas Maroschik commented on patch set 4 CR +1 03/24/14, 10:14

    Let's go on with that. I consider other performance impacts much severe than this one :)
  19. Sebastian Fischer voted on patch set 4 CR +1 03/24/14, 10:17

  20. Alexander Stehlik commented on patch set 4 CR +1 03/24/14, 10:42

    How can I test this? I know the Backend broke when the FlexForm config was invalid (int+ used for eval) but this seems to be fixed already.
  21. Markus Klein voted on patch set 4 CR +1 03/24/14, 10:49

  22. Markus Klein voted on patch set 4 V +1 03/24/14, 12:46

  23. Alexander Opitz commented on patch set 4 V +1 CR +1 03/24/14, 12:50

    No speed decrease measurable.
  24. Ernesto Baschny voted on patch set 4 V +2 CR +2 03/24/14, 13:04

  25. Ernesto Baschny merged patch set 5 03/24/14, 13:04