Repository navigation
[memory-analysis] Added a field to CPPMethod to store memory-ownershi… #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -155,6 +155,12 @@ static inline PyObject* HandleReturn(CPPOverload* pymeth, CPPInstance* im_self, | |
| CPPInstance* cppres = | ||
| (CPPInstance*)(CPPInstance_Check(result) ? result : nullptr); | ||
|
|
||
| interop::AllocType AT = | ||
| pymeth->fMethodInfo->fMethods[0]->GetAllocBehaviour(); | ||
| if (AT != interop::AllocType::None && AT != interop::AllocType::Null && | ||
| AT != interop::AllocType::Unknown) | ||
| pymeth->fMethodInfo->fFlags |= CallContext::kIsCreator; | ||
|
|
||
|
Comment on lines
+158
to
+163
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you change the function signature of
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also add a check,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That was in my mind, to distinguish between overloads, but I thought this should be content of another PR since how overloads are stored and they share same flags, so there are work should be done outside of ownership.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, for now we should add it I guess, until records are handled properly |
||
| // if this method creates new objects, always take ownership | ||
| if (IsCreator(pymeth->fMethodInfo->fFlags)) { | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -37,6 +37,9 @@ class PyCallable { | |||||
| virtual PyObject* GetCoVarNames() = 0; | ||||||
| virtual PyObject* GetArgDefault(int /* iarg */, bool silent = true) = 0; | ||||||
| virtual bool IsConst() { return false; } | ||||||
| virtual cppjit::interop::AllocType GetAllocBehaviour() { | ||||||
| return cppjit::interop::AllocType::None; | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only class does not override GetAllocBehaviour is TPythonCallBack, and from my understanding this class is for some sort of python function, not C/C++, therefore I thought it would make sense to return None |
||||||
| } | ||||||
|
|
||||||
| virtual PyObject* GetScopeProxy() = 0; | ||||||
| virtual interop::TCppFuncAddr_t GetFunctionAddress() = 0; | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| Name: MemOwnrship | ||
| Functions: | ||
| - Name: memOwnAllocGlobal | ||
| SwiftReturnOwnership: cppAllocNew | ||
| Tags: | ||
| - Name: memOwn | ||
| Methods: | ||
| - Name: memOwnAllocator | ||
| SwiftReturnOwnership: cppAllocNew |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| #ifndef MEMORY_ANALYSIS_REDECL_H | ||
| #define MEMORY_ANALYSIS_REDECL_H | ||
| #include "../memory_analysis.h" | ||
|
|
||
| namespace memory { | ||
| [[clang::annotate("cppAllocNew")]] | ||
| memOwn* allocDefaultMemOwn(); | ||
| } | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module MemOwnrship { header "../memory_analysis.h" } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #include "memory_analysis.h" | ||
|
|
||
| namespace memory { | ||
|
|
||
| __attribute__((malloc)) memAnalysisKlass* mallocAttr() { | ||
| return new memAnalysisKlass; | ||
| } | ||
|
|
||
| __attribute__((ownership_returns(malloc))) memAnalysisKlass* | ||
| ownershipReturnsAttr() { | ||
| return new memAnalysisKlass; | ||
| } | ||
|
|
||
| // Expected to not return ownership when analysis is off, and there is just | ||
| // attr-check | ||
| memAnalysisKlass* noAttr() { return new memAnalysisKlass; } | ||
|
|
||
| memOwn* memOwnAllocGlobal() { return (memOwn*)malloc(sizeof(memOwn)); } | ||
|
|
||
| memOwn* allocDefaultMemOwn() { return new memOwn; } | ||
|
|
||
| memOwn* noAttrAlloc() { return new memOwn; } | ||
|
|
||
| } // namespace memory |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #ifndef MEMORY_ANALYSIS_H | ||
| #define MEMORY_ANALYSIS_H | ||
|
|
||
| #include <new> | ||
| #include <stdlib.h> | ||
| namespace memory { | ||
|
|
||
| class memAnalysisKlass { | ||
| public: | ||
| int val; | ||
| }; | ||
| __attribute__((malloc)) memAnalysisKlass* mallocAttr(); | ||
| __attribute__((ownership_returns(malloc))) memAnalysisKlass* | ||
| ownershipReturnsAttr(); | ||
| memAnalysisKlass* noAttr(); | ||
|
|
||
| struct memOwn { | ||
| int val; | ||
| memOwn(int value) : val(value) {} | ||
| memOwn() { val = 0; } | ||
| // Attribute injected by APINotes | ||
| static memOwn* memOwnAllocator(int x) { return new memOwn(x); } | ||
| }; | ||
|
|
||
| // Attribute injected by APINotes | ||
| memOwn* memOwnAllocGlobal(); | ||
|
|
||
| // Attribute injected by redeclaration | ||
| memOwn* allocDefaultMemOwn(); | ||
|
|
||
| // No ownership attribute anywhere | ||
| memOwn* noAttrAlloc(); | ||
|
|
||
| } // namespace memory | ||
|
|
||
| #endif // MEMORY_ANALYSIS_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import os | ||
| import subprocess | ||
| import sys | ||
|
|
||
| import py | ||
| from pytest import mark | ||
| from support import IS_CLING, setup_make | ||
|
|
||
| currpath = py.path.local(__file__).dirpath() | ||
| test_dct = str(currpath.join("cpp/memory_analysisDict")) | ||
|
|
||
| FLAGS = "-fmodules -fimplicit-module-maps -fapinotes-modules" | ||
| IN_CHILD = "-fapinotes-modules" in os.getenv("CPPINTEROP_EXTRA_INTERPRETER_ARGS", "") | ||
|
|
||
|
|
||
| def setup_module(mod): | ||
| setup_make("memory_analysis") | ||
|
|
||
|
|
||
| @mark.skipif( | ||
| IN_CHILD or IS_CLING, | ||
| reason="Cling asserts in collectModuleMaps when built with " + FLAGS, | ||
| ) | ||
| def test00_driver(): | ||
| env = os.environ.copy() | ||
| env["CPPINTEROP_EXTRA_INTERPRETER_ARGS"] = ( | ||
| env.get("CPPINTEROP_EXTRA_INTERPRETER_ARGS", "") + " " + FLAGS | ||
| ) | ||
| subprocess.check_call([sys.executable, "-m", "pytest", __file__], env=env) | ||
|
|
||
|
|
||
| class TestMEMORYANALYSIS: | ||
| def setup_class(cls): | ||
| cls.test_dct = test_dct | ||
| import cppjit | ||
|
|
||
| cppjit.add_include_path(str(currpath.join("cpp", "MemoryOwnership"))) | ||
| cppjit.include("../memory_analysis.h") | ||
| cppjit.include("memory_analysis_redecl.h") | ||
| cls.memory_analysis = cppjit.load_library(cls.test_dct + ".so") | ||
|
|
||
| def test01_malloc_attr(self): | ||
| import cppjit | ||
|
|
||
| obj = cppjit.gbl.memory.mallocAttr() | ||
| assert type(obj) == cppjit.gbl.memory.memAnalysisKlass | ||
| assert obj.__python_owns__ | ||
|
|
||
| def test02_ownership_returns_attr(self): | ||
| import cppjit | ||
|
|
||
| obj = cppjit.gbl.memory.ownershipReturnsAttr() | ||
| assert type(obj) == cppjit.gbl.memory.memAnalysisKlass | ||
| assert obj.__python_owns__ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We also need tests for assert not obj.__python__owns__ |
||
|
|
||
| def test03_no_attr(self): | ||
| import cppjit | ||
|
|
||
| obj = cppjit.gbl.memory.noAttr() | ||
| assert type(obj) == cppjit.gbl.memory.memAnalysisKlass | ||
| assert not obj.__python_owns__ | ||
| obj.__python_owns__ = True | ||
|
|
||
| def test04_redecl_attr(self): | ||
| import cppjit | ||
|
|
||
| obj = cppjit.gbl.memory.allocDefaultMemOwn() | ||
| assert type(obj) == cppjit.gbl.memory.memOwn | ||
| assert obj.__python_owns__ | ||
|
|
||
| def test05_redecl_no_attr(self): | ||
| import cppjit | ||
|
|
||
| obj = cppjit.gbl.memory.noAttrAlloc() | ||
| assert type(obj) == cppjit.gbl.memory.memOwn | ||
| assert not obj.__python_owns__ | ||
| obj.__python_owns__ = True | ||
|
|
||
|
|
||
| @mark.skipif(not IN_CHILD, reason="needs " + FLAGS) | ||
| class TestMEMORYANALYSIS_APINOTES: | ||
| def setup_class(cls): | ||
| cls.test_dct = test_dct | ||
| import cppjit | ||
|
|
||
| cppjit.add_include_path(str(currpath.join("cpp", "MemoryOwnership"))) | ||
| cppjit.include("../memory_analysis.h") | ||
| cls.memory_analysis = cppjit.load_library(cls.test_dct + ".so") | ||
|
|
||
| def test01_apinotes_attr_method(self): | ||
| import cppjit | ||
|
|
||
| obj = cppjit.gbl.memory.memOwn.memOwnAllocator(5) | ||
| assert type(obj) == cppjit.gbl.memory.memOwn | ||
| assert obj.__python_owns__ | ||
|
|
||
| def test02_apinotes_attr_func(self): | ||
| import cppjit | ||
|
|
||
| obj = cppjit.gbl.memory.memOwnAllocGlobal() | ||
| assert type(obj) == cppjit.gbl.memory.memOwn | ||
| assert obj.__python_owns__ | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.