llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.09k stars 12k forks source link

String truncation with buffer_ostream and addPassesToEmitFile #25646

Closed maleadt closed 2 months ago

maleadt commented 9 years ago
Bugzilla Link 25272
Version 3.7
OS Linux
Attachments testcase.cpp

Extended Description

Hi,

I have some code which compiles to an in-memory buffer by wrapping an std::string with an raw_string_ostream and passing it to addPassesToEmitFile. This used to work fine on LLVM 3.5, after wrapping the raw_string_ostream with a formatted_raw_ostream. LLVM 3.7 has changed the interface, requiring a pwrite-able buffer instead. Looking at the raw_pwrite_stream class hierarchy, I decided to wrap the raw_string_ostream with a buffer_ostream. This however yields truncated output (at a seemingly random spot)...

Attached is a relatively large, but straightforward test-case (parse, compile, emit). Compile it with: clang++ -std=c++11 test.cpp $(llvm-config -cxxflags --system-libs --libs all) -L$(llvm-config --libdir) -o test The test-case works on both LLVM 3.7 and 3.5, by means of #ifdef's.

Output on 3.5: // // Generated by LLVM NVPTX Back-End //

.version 3.2 .target sm_20 .address_size 32

// .globl   dummy

.visible .entry dummy(

) {

ret;

}

Output on 3.7: // // Generated by LLVM NVPTX Back-End //

.version 3.2 .target sm_20 .address_size 32

// .globl   dummy

.visible .entry dummy(

I tried compiling with ASAN / running with valgrind, but no errors were revealed. Strangely, deleting the PassManager (currently commented) fixes the output! So I'm still thinking it could be a memory error.

I also suspected a flushing issue, but looking at the source code the buffer_ostream should flush when going out-of-scope, and the raw_string_ostream flushes in its str() getter. Adding additional flush() calls did not change the output.

I'm using LLVM 3.5 vanilla and 3.7 from the Arch repositories, on Linux 64bit.

maleadt commented 2 months ago

Looks fixed; updating the test case to current LLVM doesn't result in truncated output.