Cython 导致 python 解释器在创建 Python 对象时崩溃

Cython causes python interpreter crashed with Python objects creation

提问人:Venkatesh K 提问时间:3/31/2023 最后编辑:Venkatesh K 更新时间:3/31/2023 访问量:54

问:

我们有为 C++ 绑定开发的 Cython 包装器,Python 客户端正在使用 cython 包装器。 当我们创建 python 对象并调用 python 客户端 func(调用 Cython func)时,Python 解释器有时会崩溃(分段)或无法正常工作。

cpdef Cythonfunc(
                                self, 
                                url,
                                content,
                                content_len,
                                request_headers,
                                response_headers):
        cdef DATA data = {}
        cdef RESULT DRESULT= {}
        cdef TYPE _type = DEFAULT
        
        cdef int la = 1
        cdef unsigned int ant_mask=0x00
        cdef int url_len = len(url)
        cdef int request_headers_len = len(request_headers)
        cdef int response_headers_len = len(response_headers)

        data.URL=url
        data .URLLen=url_len
        data.Content=content
       
        ret = CcaScanHttpTransaction(p1, &data, &RESULT )
        if ret < 0:
            print(f"Failed to scan the given url, Received status code {ret} from CCA")
            return {"status": "Failed", "message": f"Failed to scan the url:{url} CcaReasonCode: {ret}"}

Python 客户端:

 def http_transaction_scan(self, transaction=None):
        
        if not transaction or not self.is_valid_request(transaction.keys(), ['url', 'content']):
            raise CcaInvalidRequestData(f'Invalid request data, transaction: {transaction}')
        try:
            url = transaction.get('url')
            content = transaction.get('content', b'') or b''
            content_len = len(content)
            request_headers = transaction.get('request_headers', {})
            response_headers = transaction.get('response_headers', {})
            results = self.cca.Cythonfunc(url=url, content=content, content_len=content_len, request_headers=request_headers, response_headers=response_headers)
            if 'status' in results and results.get('status') == 'Failed':
                raise CcaHttpScanError(f'Failed to scan the http transaction, msg: {results}')
            return results
        except Exception as exp:
            print(f'Error while performing the cca_client CcaScanHttpTransaction, Reason: {exp}')
test.py

import cca_client
import boto3

cca_obj = cca_client.client.PyCca()
cca_obj.load_cca_databases()

import tracemalloc
tracemalloc.start()

try:
s3_client = boto3.client('s3')
s3_client = boto3.client('sqs', region_name=aws_region)
except:
pass

exploit_html = "big html page content"
url_1 = '\<URL\>'
transaction = {
'url': url_1,
'content': exploit_html,
'request_headers': b'',
'response_headers': b''
}
snapshot1 = tracemalloc.take_snapshot()


res_1 = cca_obj.http_transaction_scan(transaction)
print(res_1)

snapshot2 = tracemalloc.take_snapshot()
top_stats = snapshot2.compare_to(snapshot1, 'lineno')

# top_stats = snapshot.statistics('lineno')

print("\[ Top 10 \]")
for stat in top_stats\[:10\]:
print(stat)

如果我们不创建对象并引发分割问题或返回部分结果,则上述 test.py 工作正常s3_client否则。

导入具有导入的 Cython 模块的 python 客户端模块是否会导致问题,任何建议都会有很大帮助,谢谢。

尝试了所有其他方法,但仍然没有运气,它仍然会导致分段问题。

问题:如果我将 cython 模块 X 导入 python 模块 Y,然后将这个 python 模块 Y 导入另一个 Python 模块 Z,那么它可以正常工作。 但是,如果我将任何其他库(如 requests 或 boto3 或任何其他库)导入到 Y 导入到模块 Z 中,那么我的脚本就会引发分段问题(python 解释器崩溃)

python-3.x 内存管理 malloc cython cythonize

评论

0赞 Blue Robin 3/31/2023
请澄清您的具体问题或提供其他详细信息以准确说明您的需求。正如目前所写的那样,很难确切地说出你在问什么。
0赞 Venkatesh K 3/31/2023
感谢您的@BlueRobin,现在在我的问题中添加了一个问题部分,如果我们需要任何其他详细信息,请告诉我。
1赞 DavidW 3/31/2023
这需要一个最小的可重复示例。目前完全无法回答

答: 暂无答案