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

问题描述 投票:0回答:0

我们为 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)

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

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

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

python-3.x memory-management malloc cython cythonize
© www.soinside.com 2019 - 2024. All rights reserved.