spoutn1k / mcmap

Pixel-art map visualizer for Minecraft. Maps are drawn from an isometric perspective.
GNU General Public License v3.0
312 stars 45 forks source link

Regions cause segmentation fault and stack smashing #56

Open infinitewarp opened 4 years ago

infinitewarp commented 4 years ago

I have a world where some region files are causing mcmap to segfault or abort due to stack smashing.

Using the official archlinux:latest docker image, I built mcmap from the latest source (as of today) like:

$ docker run --privileged -v=`pwd`:/shared:rw -it archlinux:latest sh
# pacman -Sy --noconfirm --needed git gcc make libpng tree
# cd /shared
# rm -rf mcmap && git clone http://github.com/spoutn1k/mcmap
# cd mcmap && make -j

I have been operating a vanilla Minecraft server with a very old world that has been through numerous version upgrades over the years. It's currently running Minecraft 1.16.3. There are some areas around the world that were made by users in creative mode, some that have things created using commands (mostly custom player heads, armor stands, and a few mobs), and some that have blocks tweaked using the debug stick (like changing the orientation of stairs and walls), but I believe all of these should be standard "supported" blocks and entities.

I pulled a backup of our world's files and tried rendering only a subset of the most recently modified regions, but mcmap blows up with *** stack smashing detected ***: terminated and Segmentation fault on some region files. Unfortunately, even with the -v verbose flag set, I don't get a lot of helpful information. So, I captured my terminal session to show my attempts to build and use mcmap against each of our world's region files. It looks like we have at least 4 region files (just in this subset that I examined) that cause mcmap to crash. Here's the asciinema recording: mcmap segfaults and stack smashing

FWIW, I have been experimenting with multiple Minecraft mapping solutions. Minecraft-Overviewer and minutor's latest nightly build both rendered these regions without error.

Are there any additional arguments I can give mcmap to produce more verbose debug output? I'm happy to try building again from a custom branch or fork if there are any additional checks or messages you'd like to see when I process these region files.

infinitewarp commented 4 years ago

Looking closer at my attempts to render each region file individually, at least one region file (r.1.-1.mca) fails like:

[Debug] World spans from 512.-512 to 1023.-1
mcmap 3.0.0 64bit (compatible with Minecraft v1.13+)
[Debug] Decompressing chunk data failed: buffer error
[Debug] Not enough data for chunk: Success
*** stack smashing detected ***: terminated
Aborted

In that case, mcmap exits with status 134.

Some other region files (r.0.0.mca, r.2.0.mca, r.2.1.mca) fail like:

[Debug] World spans from 1024.0 to 1535.511
mcmap 3.0.0 64bit (compatible with Minecraft v1.13+)
[Debug] Loaded 314 colors out of the 760 declared
Segmentation fault0.10%]

In those cases, mcmap exits with status 139.

spoutn1k commented 4 years ago

Hey ! Thank you for the detailed issue and the asciinema !

This is very peculiar. Considering the coordinates, my guess would be these regions have been around for a long long time, and they might have quirks my test samples did not show. I ran tests with worlds generated using Minecraft 1.13+, so problems were to be expected.

I figure having access to those files is out of the question ? It would simplify greatly the process, but I would totally understand as this is personnal.

Are there any additional arguments I can give mcmap to produce more verbose debug output?

Unfortunately this is all the output mcmap gives out. I could try and add some more debug printfs, but my first step would be to isolate the problem. I just pushed a new script in the main branch, can you try and run regionReader on those regions ? It will detail all the chunks present along with some metadata, that could help me figure out a common property.

I picked mcmap back up after the project left alone for a while, and the only thing that did not change is the output. I use custom STL-like NBT objects and that has proven to be a double-edged sword. I was expecting more mature projects to fare better on the compatibility front, but hopefully we can fix this.

spoutn1k commented 3 years ago

If anyone ever ends up with a similar error, please run (and comment the result):

find <your save>/region/ -exec ./regionReader {} \; | cut -f 4 | grep -v Size | sort -ug | tail -n 1

With regionReader being the script included in mcmap (Configured with -DNBT_TOOLS=1). A memory buffer with a set size is used to decompress chunks, and a lot of activity in a single area might make it overflow. In the meantime, its size was increased, so hopefully this error will not happen anymore.

Veyrdite commented 3 years ago

Segfaults on my world. Please tell me if there is anything else I can provide to help.

Further information:

$ ~/app/mcmap/bin/mcmap -colors colors2.json  -vv world/
...
[Deep Debug] Region file r.-2.5.mca does not exist, skipping ..
[Deep Debug] Region file r.-2.5.mca does not exist, skipping ..
[Deep Debug] Region file r.-2.5.mca does not exist, skipping ..
[Deep Debug] Region file r.-2.5.mca does not exist, skipping ..
[Deep Debug] Region file r.-2.5.mca does not exist, skipping ..
[Debug] Rendering -192.-1472.0 ~> 831.-449.255 (North-West)                     
[Debug] Rendering -192.-448.0 ~> 831.575.255 (North-West)
[Debug] [Debug] Rendering -192.1600.0 ~> 831.2623.255 (North-West)
Rendering -192.576.0 ~> 831.1599.255 (North-West)
Segmentation fault0.00%]

Tilesize twiddling only changes when the failure occurs:

$ ~/app/mcmap/bin/mcmap -colors colors2.json -tile 64 -vv world/
...             
[Debug] Rendering -192.64.0 ~> -129.127.255 (North-West)                        
[Debug] Rendering -192.128.0 ~> -129.191.255 (North-West)                       
[Debug] Rendering -192.192.0 ~> -129.255.255 (North-West)
[Debug] Rendering -192.256.0 ~> -129.319.255 (North-West)                       
Segmentation fault0.00%]]

Regionreader:

 $ find world/region/ -exec ~/app/mcmap/bin/regionReader {} \; | cut -f 4 | grep -v Size | sort -ug | tail -n 1
[Error] Error reading header, not enough bytes read.
27108

GDB backtrace:

...
[Debug] [Debug] Rendering -192.576.0 ~> 831.1599.255 (North-West)
Rendering -192.1600.0 ~> 831.2623.255 (North-West)
Rendering chunks [0.00%]
Thread 1 "mcmap" received signal SIGSEGV, Segmentation fault.
0x00005555555dc951 in IsometricCanvas::renderSection() ()
(gdb) bt
#0  0x00005555555dc951 in IsometricCanvas::renderSection() ()
#1  0x00005555555e04a8 in IsometricCanvas::renderChunk(Terrain::Data&) ()
#2  0x00005555555e067b in IsometricCanvas::renderTerrain(Terrain::Data&) ()
#3  0x00005555555b7cef in ?? ()
#4  0x00007ffff7d0f712 in GOMP_parallel () from /usr/lib/libgomp.so.1
#5  0x00005555555b71a1 in mcmap::render(Settings::WorldOptions const&, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Colors::Block, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, Colors::Block> > > const&) ()
#6  0x00005555555751fe in main ()
(gdb)
spoutn1k commented 3 years ago

Hey thanks for shaing !

Are the 'Rendering` messages the first ones of the full error message ? You mention 'when the failure occurs'. Is this a localized thing ? Can you render parts of your world ?

I am not sure what causes this. I never used mcmap with a non-vanilla world, and Fabric may change the expected format. Would you be comfortable with sending the file world/region/r.-1.0.mca from the save you attempted to render ? I'll try on my side and fix or add support if need be.

Veyrdite commented 3 years ago

Apologies for never getting back to you. This is probably now far too little far too late (not to mention the chunk was probably modified afterwards).

r.-1.0.mca.zip

I hope your end of the world is holding up OK.

spoutn1k commented 3 years ago

Hey don't apologize for not folllowing up a minecraft discussion, that is really helpful already.

Looks like the mod does not change the world format, so it can be rendered. I rendered this region in both the version from February 1st and the latest. Either the chunks have been updated, or it was some kind of structure error with the rest of the world.

output

The internals of mcmap changed a lot over time so hopefully I'll see less of those segfault issues.

I hope you are too. Good luck out there.

Veyrdite commented 3 years ago

Thankyou spoutn1k, that's a beautiful render and you have made my day.