<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">This can also be reproduced in the command line like this:<br class=""><br class="">(lldb) expr "foo"<br class="">(const char [4]) $0 = "foo"<br class="">(lldb) expr "bar"<br class="">(const char [4]) $1 = "bar"<br class="">(lldb) expr $0<br class="">(const char [4]) $0 = “bar”<span class=""><br class=""><br class="">This however works just fine:</span><div class=""><span class=""><br class=""></span><span class="">(lldb) expr char c[] = "foo"; c<br class="">(char [4]) $0 = "foo"<br class="">(lldb) expr char c[] = "bar"; c<br class="">(char [4]) $1 = "bar"<br class="">(lldb) expr $0<br class="">(char [4]) $0 = “foo”<br class=""><br class="">I don’t know the related code so well, but from what I remember we have a storage mechanism for persistent variables that we fill up (in the ‘Materializer’ IIRC). We probably just copy the pointer itself to this storage but not the memory it points to. I guess we could tweak that logic to detect pointers that point into memory LLDB allocated and then either extract the necessary memory into our storage or keep the related sections around.</span></div><div class=""><br class=""></div><div class="">Anyway, I filed <a href="https://bugs.llvm.org/show_bug.cgi?id=44155" class="">https://bugs.llvm.org/show_bug.cgi?id=44155</a> and I will ask around what solution people would prefer once thanksgiving is over.</div><div class=""><span class=""><br class=""></span></div><div class=""><span class=""><blockquote type="cite" class="">On 26. Nov 2019, at 15:40, Lutz Justen via lldb-dev <<a href="mailto:lldb-dev@lists.llvm.org" class="">lldb-dev@lists.llvm.org</a>> wrote:<br class=""><br class="">Hi,<br class=""><br class="">We're running into a problem where a Python SBValue references stale memory. It can be reproduced by evaluating an expression with a constant string:<br class=""><br class="">    var1 = frame.EvaluateExpression("\"ABCDEFGEH\"")<br class="">    var2 = frame.EvaluateExpression("\"123456789\"")<br class="">    print(var1.GetSummary())<br class="">    print(var2.GetSummary())<br class=""><br class="">This will print <br class=""><br class="">    "123456789"<br class="">    "123456789"<br class=""><br class="">and not as you would expect<br class=""><br class="">    "ABCDEFGEH"<br class="">    "123456789"<br class=""><br class="">If you print the addresses,<br class=""><br class="">    print(var1.AddressOf())<br class="">    print(var2.AddressOf())<br class=""><br class="">they're the same, e.g.<br class=""> <br class="">    (const char (*)[10]) &$0 = 0x0000012d787c0010<br class="">    (const char (*)[10]) &$1 = 0x0000012d787c0010<br class=""><br class="">Walking through the code, it seems like the SBValues (var1 and var2) point to deallocated memory. The memory is allocated and owned by ClangUserExpression and deallocated by its destructor, but the SBValues are clinging to the address (see callstacks below).<br class=""><br class="">What's the best approach to deal with this? Could the ownership somehow be transferred to the SBValue? Any pointers?<br class=""><br class="">Thanks,<br class=""><br class="">- Lutz<br class=""><br class=""><br class="">********* CALLSTACK FOR ALLOCATION OF THE CONSTANT STRING  *********<br class=""><br class="">><span class="Apple-tab-span" style="white-space:pre">   </span>_lldb.pyd!lldb_private::AllocatedMemoryCache::AllocateMemory(unsigned __int64 byte_size, unsigned int permissions, lldb_private::Status & error) Line 373<span class="Apple-tab-span" style="white-space:pre">  </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::Process::AllocateMemory(unsigned __int64 size, unsigned int permissions, lldb_private::Status & error) Line 2560<span class="Apple-tab-span" style="white-space:pre">   </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::IRMemoryMap::Malloc(unsigned __int64 size, unsigned char alignment, unsigned int permissions, lldb_private::IRMemoryMap::AllocationPolicy policy, bool zero_memory, lldb_private::Status & error) Line 389<span class="Apple-tab-span" style="white-space:pre"> </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::IRExecutionUnit::CommitOneAllocation(std::shared_ptr<lldb_private::Process> & process_sp, lldb_private::Status & error, lldb_private::IRExecutionUnit::AllocationRecord & record) Line 1116<span class="Apple-tab-span" style="white-space:pre">  </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::IRExecutionUnit::CommitAllocations(std::shared_ptr<lldb_private::Process> & process_sp) Line 1125<span class="Apple-tab-span" style="white-space:pre"> </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::IRExecutionUnit::GetRunnableInfo(lldb_private::Status & error, unsigned __int64 & func_addr, unsigned __int64 & func_end) Line 363<span class="Apple-tab-span" style="white-space:pre">    </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::ClangExpressionParser::PrepareForExecution(unsigned __int64 & func_addr, unsigned __int64 & func_end, std::shared_ptr<lldb_private::IRExecutionUnit> & execution_unit_sp, lldb_private::ExecutionContext & exe_ctx, bool & can_interpret, lldb_private::ExecutionPolicy execution_policy) Line 1221<span class="Apple-tab-span" style="white-space:pre">     </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::ClangUserExpression::Parse(lldb_private::DiagnosticManager & diagnostic_manager, lldb_private::ExecutionContext & exe_ctx, lldb_private::ExecutionPolicy execution_policy, bool keep_result_in_memory, bool generate_debug_info) Line 554<span class="Apple-tab-span" style="white-space:pre"> </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::UserExpression::Evaluate(lldb_private::ExecutionContext & exe_ctx, const lldb_private::EvaluateExpressionOptions & options, llvm::StringRef expr, llvm::StringRef prefix, lldb_private::SharingPtr<lldb_private::ValueObject> & result_valobj_sp, lldb_private::Status & error, unsigned int line_offset, std::basic_string<char,std::char_traits<char>,std::allocator<char> > * fixed_expression, std::shared_ptr<lldb_private::Module> * jit_module_sp_ptr, lldb_private::ValueObject * ctx_obj) Line 258<span class="Apple-tab-span" style="white-space:pre">       </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::Target::EvaluateExpression(llvm::StringRef expr, lldb_private::ExecutionContextScope * exe_scope, lldb_private::SharingPtr<lldb_private::ValueObject> & result_valobj_sp, const lldb_private::EvaluateExpressionOptions & options, std::basic_string<char,std::char_traits<char>,std::allocator<char> > * fixed_expression, lldb_private::ValueObject * ctx_obj) Line 2406<span class="Apple-tab-span" style="white-space:pre">        </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::ValueObject::CreateValueObjectFromExpression(llvm::StringRef name, llvm::StringRef expression, const lldb_private::ExecutionContext & exe_ctx, const lldb_private::EvaluateExpressionOptions & options) Line 3136<span class="Apple-tab-span" style="white-space:pre">      </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb::SBValue::CreateValueFromExpression(const char * name, const char * expression, lldb::SBExpressionOptions & options) Line 725<span class="Apple-tab-span" style="white-space:pre">       </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb::SBValue::CreateValueFromExpression(const char * name, const char * expression) Line 712<span class="Apple-tab-span" style="white-space:pre">     </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!_wrap_SBValue_CreateValueFromExpression__SWIG_0(_object * __formal, _object * args) Line 71739<span class="Apple-tab-span" style="white-space:pre">    </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!_wrap_SBValue_CreateValueFromExpression(_object * self, _object * args) Line 71839<span class="Apple-tab-span" style="white-space:pre">        </span>C++<br class=""><br class="">********* CALLSTACK FOR DEALLOCATION OF THE CONSTANT STRING  *********<br class=""><br class="">><span class="Apple-tab-span" style="white-space:pre"> </span>_lldb.pyd!lldb_private::AllocatedBlock::FreeBlock(unsigned __int64 addr) Line 318<span class="Apple-tab-span" style="white-space:pre">   </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::AllocatedMemoryCache::DeallocateMemory(unsigned __int64 addr) Line 407<span class="Apple-tab-span" style="white-space:pre">      </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::Process::DeallocateMemory(unsigned __int64 ptr) Line 2613<span class="Apple-tab-span" style="white-space:pre">   </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::IRMemoryMap::Free(unsigned __int64 process_address, lldb_private::Status & error) Line 475<span class="Apple-tab-span" style="white-space:pre">      </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::IRMemoryMap::~IRMemoryMap() Line 36<span class="Apple-tab-span" style="white-space:pre"> </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::IRExecutionUnit::~IRExecutionUnit() Line 495<span class="Apple-tab-span" style="white-space:pre">        </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::IRExecutionUnit::`scalar deleting destructor'(unsigned int)<span class="Apple-tab-span" style="white-space:pre"> </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::LLVMUserExpression::~LLVMUserExpression() Line 65<span class="Apple-tab-span" style="white-space:pre">   </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::ClangUserExpression::`scalar deleting destructor'(unsigned int)<span class="Apple-tab-span" style="white-space:pre">     </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!std::_Ref_count_base::_Decref() Line 848<span class="Apple-tab-span" style="white-space:pre">  </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::UserExpression::Evaluate(lldb_private::ExecutionContext & exe_ctx, const lldb_private::EvaluateExpressionOptions & options, llvm::StringRef expr, llvm::StringRef prefix, lldb_private::SharingPtr<lldb_private::ValueObject> & result_valobj_sp, lldb_private::Status & error, unsigned int line_offset, std::basic_string<char,std::char_traits<char>,std::allocator<char> > * fixed_expression, std::shared_ptr<lldb_private::Module> * jit_module_sp_ptr, lldb_private::ValueObject * ctx_obj) Line 384<span class="Apple-tab-span" style="white-space:pre">       </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::Target::EvaluateExpression(llvm::StringRef expr, lldb_private::ExecutionContextScope * exe_scope, lldb_private::SharingPtr<lldb_private::ValueObject> & result_valobj_sp, const lldb_private::EvaluateExpressionOptions & options, std::basic_string<char,std::char_traits<char>,std::allocator<char> > * fixed_expression, lldb_private::ValueObject * ctx_obj) Line 2406<span class="Apple-tab-span" style="white-space:pre">        </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb_private::ValueObject::CreateValueObjectFromExpression(llvm::StringRef name, llvm::StringRef expression, const lldb_private::ExecutionContext & exe_ctx, const lldb_private::EvaluateExpressionOptions & options) Line 3136<span class="Apple-tab-span" style="white-space:pre">      </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb::SBValue::CreateValueFromExpression(const char * name, const char * expression, lldb::SBExpressionOptions & options) Line 725<span class="Apple-tab-span" style="white-space:pre">       </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!lldb::SBValue::CreateValueFromExpression(const char * name, const char * expression) Line 712<span class="Apple-tab-span" style="white-space:pre">     </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!_wrap_SBValue_CreateValueFromExpression__SWIG_0(_object * __formal, _object * args) Line 71739<span class="Apple-tab-span" style="white-space:pre">    </span>C++<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>_lldb.pyd!_wrap_SBValue_CreateValueFromExpression(_object * self, _object * args) Line 71839<span class="Apple-tab-span" style="white-space:pre">        </span>C++<br class="">_______________________________________________<br class="">lldb-dev mailing list<br class=""><a href="mailto:lldb-dev@lists.llvm.org" class="">lldb-dev@lists.llvm.org</a><br class="">https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-dev<br class=""></blockquote><br class=""></span></div></body></html>