René's URL Explorer Experiment


Title: Specializing adaptive interpreter code object hashes are less unique · Issue #94155 · python/cpython · GitHub

Open Graph Title: Specializing adaptive interpreter code object hashes are less unique · Issue #94155 · python/cpython

X Title: Specializing adaptive interpreter code object hashes are less unique · Issue #94155 · python/cpython

Description: In Python 3.11 and 3.12, the hash function for a PyCodeObject (code_hash()) no longer hashes the bytecode. I assume this is because the specializing adaptive interpreter can change the bytecode however it likes, which means the bytecode ...

Open Graph Description: In Python 3.11 and 3.12, the hash function for a PyCodeObject (code_hash()) no longer hashes the bytecode. I assume this is because the specializing adaptive interpreter can change the bytecode how...

X Description: In Python 3.11 and 3.12, the hash function for a PyCodeObject (code_hash()) no longer hashes the bytecode. I assume this is because the specializing adaptive interpreter can change the bytecode how...

Opengraph URL: https://github.com/python/cpython/issues/94155

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Specializing adaptive interpreter code object hashes are less unique","articleBody":"In Python 3.11 and 3.12, the hash function for a `PyCodeObject` (`code_hash()`) no longer hashes the bytecode.  I assume this is because the specializing adaptive interpreter can change the bytecode however it likes, which means the bytecode is no longer immutable, which means you can't rely on it not changing, which means you can't use it as part of calculating the hash.  Fair enough.\r\n\r\nBut this means that the hash of a code object no longer factors in the most unique part of a code object.  Currently in 3.11 and 3.12 the hash of a code object is calculated using:\r\n\r\n- the unqualified name of the callable\r\n- the code object's const table (a tuple of immutable objects)\r\n- the code object's tuple of externally-referenced names (globals, nonlocals)\r\n- the code object's tuple of locally-referenced names (parameters, local variables, and closures)\r\n- the total number of arguments\r\n- the count of positional-only arguments\r\n- the count of keyword-only arguments\r\n- the \"flags\" for this code object\r\n\r\nWhich means it's not hard to construct code objects with identical hashes but different bytecode.  For example:\r\n\r\n```Python\r\nclass A:\r\n    def method(self, a, b):\r\n        return a + b\r\n\r\nclass B:\r\n    def method(self, a, b):\r\n        return a * b\r\n\r\nclass C:\r\n    def method(self, a, b):\r\n        return a / b\r\n\r\n\r\nfor cls in (A, B, C):\r\n    o = cls()\r\n    print(o.method(3, 5))\r\n    print(hex(hash(cls.method.__code__)))\r\n```\r\n\r\nThe hashes for for `A.method.__code__`, `B.method.__code__`, and `C.method.__code__` are different in Python 3.10, but identical in Python 3.11b3, and presumably in trunk as well.\r\n\r\nIs this scenario realistic?  I don't know.  Certainly I've never seen it.  But it's at least _plausible;_ a base class could have a tiny function that only does a little work, and a subclass could override that function and tweak the work done slightly, without relying on additional external names / closures or employing a different list of locals.  It's certainly not impossible.\r\n\r\nObviously this is low priority--there's very little code that hashes code objects.  It might cause some collisions and probing when marshal dumps a module exhibiting this behavior, because marshal maintains a hash table of the objects it writes.  That's the worst side-effect I can think up.\r\n\r\nWe could mitigate this slightly by factoring in more values into the code object's hash.  Three come immediately to mind:\r\n\r\n- the filename (`co_filename`)\r\n- the first line number (`co_firstlineno`)\r\n- the first column number (which I'm guessing is encoded in `co_positions` somehow)\r\n\r\nWith only the first two, you'd still have hash collisions from code objects defined using lambdas all on the same line:\r\n```Python\r\n\r\na, b, c = (lambda a, b: a + b), (lambda a, b: a * b), (lambda a, b: a / b)\r\n\r\nfor l in (a, b, c):\r\n    print(l(3, 5))\r\n    print(hex(hash(l.__code__)))\r\n```\r\n\r\nAs unlikely as it is that someone would stumble over this scenario, it's even _less_ likely that it would cause a problem.  But decreasing the likelihood of hash value collisions seems so wholesome and good, I think it's worth pursuing.\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-100183\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/larryhastings","@type":"Person","name":"larryhastings"},"datePublished":"2022-06-23T04:01:32.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":6},"url":"https://github.com/94155/cpython/issues/94155"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:66725f0b-7a0d-4252-93c9-5adc44864d84
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idA13A:1ABE3:308BE48:43D44D4:6969380D
html-safe-nonceb5c762c1a55ee581136c97d49325d1dc10bc338b21ff22ac54ed74350e06a378
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMTNBOjFBQkUzOjMwOEJFNDg6NDNENDRENDo2OTY5MzgwRCIsInZpc2l0b3JfaWQiOiI3NDg1ODY5MTA2NjU3ODM1MDIxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac93127fa16e2126c09984c35489078022b3ef046f3e99d9cfafdb4e14b7f06387
hovercard-subject-tagissue:1281684669
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/94155/issue_layout
twitter:imagehttps://opengraph.githubassets.com/110f934b0de767a09a53ac5af2b47f65dc92d69325388dcb12a42d046f4ab3fe/python/cpython/issues/94155
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/110f934b0de767a09a53ac5af2b47f65dc92d69325388dcb12a42d046f4ab3fe/python/cpython/issues/94155
og:image:altIn Python 3.11 and 3.12, the hash function for a PyCodeObject (code_hash()) no longer hashes the bytecode. I assume this is because the specializing adaptive interpreter can change the bytecode how...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamelarryhastings
hostnamegithub.com
expected-hostnamegithub.com
None54182691a21263b584d2e600b758e081b0ff1d10ffc0d2eefa51cf754b43b51d
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
released69ac0477df0f87da03b8b06cebd187012d7a930
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/issues/94155#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F94155
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F94155
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/94155
Reloadhttps://github.com/python/cpython/issues/94155
Reloadhttps://github.com/python/cpython/issues/94155
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/issues/94155
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 33.9k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 71.1k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects 31 https://github.com/python/cpython/projects
Security Uh oh! There was an error while loading. Please reload this page. https://github.com/python/cpython/security
Please reload this pagehttps://github.com/python/cpython/issues/94155
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/94155
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/94155
Specializing adaptive interpreter code object hashes are less uniquehttps://github.com/python/cpython/issues/94155#top
3.11only security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.11%22
3.12only security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.12%22
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com/larryhastings
https://github.com/larryhastings
larryhastingshttps://github.com/larryhastings
on Jun 23, 2022https://github.com/python/cpython/issues/94155#issue-1281684669
gh-94155: Reduce hash collisions for code objects #100183https://github.com/python/cpython/pull/100183
3.11only security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.11%22
3.12only security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.12%22
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.