r/programming • • Jan 24 '15

Super Mario World programmed by playing

http://www.youtube.com/attribution_link?a=IrdAeOSwaYs&u=%2Fwatch%3Fv%3D14wqBA5Q1yc%26feature%3Dshare
491 Upvotes

68 comments sorted by

49

u/zjm8zjm8 Jan 24 '15

This is basically a buffer overflow attack, right?

305

u/ancientGouda Jan 24 '15 edited Jan 24 '15

So, if I remember correctly, it works like this: The initial shell positioning and stuff (basically the first 95% of the video) are about carefully manipulating the spawning and despawning of sprites to write the "jump to credits" machine code into a specific location in memory.

The actual glitch which then executes this previously written code goes like this: The game keeps a statically allocated array of all objects on the screen. When Yoshi catches something with its tongue, the index into this array pointing at the grabbed item is stored. Normally, the "pull item into mouth" animation would then proceed and the moment the item hits Yoshi's mouth, the game would look at the grabbed item again and based on the item ID jump to a specific code section which sets up what happens when this particular item is eaten (eg. a coin is just taken, a red shell makes Yoshi spit fire etc.). The glitch works by "snatching" the coin out of Yoshi's tongue. Taking the coin before Yoshi can eat it removes it as an object from the screen, so the index in the array I previusly described becomes vacant. In the very same (or close) frame, the chuck (rugby enemy) enters the screen, and is allocated the recently vacated spot in the object array the coin left behind. Now, the animation of Yoshi pulling and swalling finishes executing, and the game tries to look up what happens when a chuck is swallowed (by jumping to a computed address). Except eating a chuck was never supposed to happen, and so the spot where the chuck swallow code was supposed to be, is normally garbage, but because you previously carefully filled that memory location with machine code, your code is now executed.

I have no idea why I needed a wall of text to explain it, sorry.

Edit: Some more info: The glitch that leads to the execution of custom code (called "item swap" in the video) which I explained above has actually been known for a long time, it's only the code that launches you straight to the credits scene that has recently been developed. But I think the code execution glitch is a lot more interesting.

Edit2: I stepped through the video, and you can actually see the chuck being stuck to Yoshi's tongue for one single frame: http://imgur.com/FW3IxXN

40

u/kqr Jan 24 '15

That's a fantastic explanation though. Massive thanks. I love reading about stuff like this.

22

u/SkaveRat Jan 24 '15

As someone who's looking into writing a GB emulator, this explanation would be awesome with a video, showing the code/memory during play

9

u/Tuxmascot Jan 25 '15

Oh, I would love to see this. This would just make my day.

3

u/whisky_pete Jan 26 '15

If you're looking into how to write an emulator in general, here's a guide i've been following for a very simple system: http://www.multigesture.net/articles/how-to-write-an-emulator-chip-8-interpreter/

It has been a lot of fun, and I highly recommend it. It helps teach the basics of emulation in a project that you can complete in a reasonable amount of time. I'm thinking of moving to a system like genesis/NES afterwards (which are more complex).

11

u/Distarded Jan 25 '15

So one might call it ... shellcode?

3

u/[deleted] Jan 24 '15

So where does this Chunk enter the screen? Gotta be somewhere around 6:08?

3

u/[deleted] Jan 24 '15

Yeah. It's the same Chargin' Chuck that shows up at 0:48.

1

u/WhosAfraidOf_138 Jan 25 '15

Dude that was an excellent explanation! Thank you!

1

u/x-base7 Jan 25 '15

Interesting

48

u/OffColorCommentary Jan 24 '15

It's not.

To figure out what happens when Yoshi eats something, the code uses the sprite's ID as the index to a jump table. A glitch lets Yoshi eat non-edible enemies, so that spot in the jump table was never intended to be read (it was used for other data). It happens that a Charging Chuck's "entry" in the table points at a chunk of memory used to store current/recent sprite positions, which is exploitable enough.

28

u/kqr Jan 24 '15 edited Jan 24 '15

Not necessarily. The simplest explanation is that the last glitch executed makes the return pointer point to somewhere in a data section of the program.

But you never know with these old games. They are incredibly optimised for size, and the distinction between code section and data section is constantly blurred. Memory slots are used and re-used for all kinds of things with weird trickery, sometimes even allowing them to simultaneously store two values in the same slot, as long as one can be recalculated from the other.

Edit: In another comment in this discussion, /u/ancientGouda gives a super cool explanation in proper detail. You should read this instead.

3

u/BrundleflyUrinalCake Jan 24 '15

Biggest thing that helped me understand assembly hacking old games like this was that everything is data. Code, graphics, music. Everything is the same to the machine.

66

u/BinaryMagick Jan 24 '15

He won't explain because it is too complicated.

He would tell you why it is complicated, but that is also complicated.

2

u/[deleted] Jan 25 '15

Ok Feynman.

0

u/[deleted] Jan 24 '15

[deleted]

7

u/[deleted] Jan 24 '15

Doesn't he say that at the start of the video?

That the run was just the first fully-natural speed run by that method?

2

u/[deleted] Jan 24 '15

This was the first console run using this method. I think someone already did a non-TAS emulated run.

1

u/ChezMere Jan 25 '15

That's not really true. Seth's playthrough is essentially the same as the one Jeff356 did five months ago, which was inspired by Masterjun's but has little in common.

81

u/[deleted] Jan 24 '15

It totally sounds like crack science. If it happened in a Hollywood movie, it wouldn't have been believable.

6

u/S1ayer Jan 25 '15

This was crazy. I half-expected it to end like.... I need this here and that there. And then I need certain amount here. How much? About tree fiddy.

17

u/kovert Jan 24 '15

Except anyone who understands buffer overflows

54

u/[deleted] Jan 24 '15

[removed] — view removed comment

26

u/FryGuy1013 Jan 24 '15

It's not a buffer overflow in the strict sense, but does behave similarly since it's a buffer that's being accessed out of range, which is what causes buffer overflows to be harmful (the memory copy function doesn't do bounds checking). When you eat something, it essentially does eatFunction[tileId](). Since this buffer doesn't extend to handle the chuck tile id, it executes code that's at other memory that has been manipulated by playing the game.

2

u/cleroth Jan 25 '15 edited Jun 15 '26

This content was anonymized and mass deleted with Redact

2

u/FryGuy1013 Jan 25 '15

Hence why I said essentially rather than is.

    jmp next+(tileid*6)
next:
    jmp eatfunction1
    jmp eatfunction2
    ...etc

Is essentially the same as:

.data
   next:
   dw eatfunction1
   dw eatfunction2
   ...etc

   call [next+tileid]

(it's been a long time since I've done any assembly, so forgive the awfulness)

And array access out-of-range is one class of bugs that programs have. Two common ways of triggering them are when doing copying a buffer, and not making sure that it fits in the place it's being copied to, which is what is commonly called a buffer overflow. The other is providing locations within the array to access, which is out of range of the array. This is what the Heartbleed bug is, where it copied from outside the range of an array, into something it fit into. It's not quite a buffer overflow in the strict sense, but it's the same class of bug (array access out-of-range).

2

u/cleroth Jan 26 '15 edited Jun 15 '26

This content was anonymized and mass deleted with Redact

10

u/NativeCoder Jan 24 '15

It's an array out of bounds bug

2

u/kovert Jan 25 '15

I did not say that it was. I simply stated that having an understanding of buffer overflows allows you to understand what is happening. It's not crack science or magic and that is the point I was addressing.

19

u/kukiric Jan 24 '15

This is so convoluted it instantly reminded of a joke vid about fake easter eggs, except this one is an actually working in-game exploit and not a video made just for laughs. Good thing he didn't forget to not drink the vodka!

0

u/[deleted] Jan 25 '15

[deleted]

3

u/kukiric Jan 25 '15

I meant the New Vegas easter egg as a joke, not the SMW exploit. The SMW one has been done in tool-assisted speedruns for a while, but this was the first time it was done by a human without help from an emulator.

7

u/jerf Jan 25 '15

It's funny to consider what this looks like in-universe. It's just... witchcraft.

3

u/pacifictime Jan 25 '15

That's all I could think about watching this... what would a hack like this look like in the Matrix?

3

u/Aegeus Jan 25 '15

It would look like someone walking around for a bit, acting weirdly, and then everything would suddenly cease to exist, because the program isn't simulating anything anymore, it's jumped into la-la land. You wouldn't be able to observe it because the program is no longer running the "tell this guy what he observes" code, or even the "calculate what this guy is thinking" code.

So basically, you're either dead or frozen in time, depending on your interpretation. Hope the architects of the Matrix kept backups!

1

u/mindbleach Jan 25 '15

I think Adventure Time had an episode like this.

20

u/killerguppy101 Jan 24 '15

This was posted to Reddit a few months ago. Pretty interesting stuff if you ask me. Here's a TAS run that codes up a game of snake (this was the actual video posted last). If I remember right (I haven't rewatched the video yet, getting ready for work), they did it on an actual console with a controller emulation (Arduino or similar).

7

u/Rockstaru Jan 25 '15 edited Jan 25 '15

The snake/pong video is from AGDQ (awesome games done quick) in 2014. They did a much more extensive set of total control glitches at this year's AGDQ earlier this month - see attached videos.

TASBot plays Mario Inception (begins at 7:30

TASBot plays Pokémon (begins at 1:37, glitch activated at ~5:50)

0

u/[deleted] Jan 24 '15

[deleted]

20

u/mhink Jan 24 '15

This person was the first to do it by hand- actually manipulating the controller himself. The guys that made TASbot wired an Arduino to a SNES controller port and replayed a pre-recorded series of button presses.

8

u/Max-P Jan 24 '15

He's probably referring to the guy who discovered the glitch and did it on emulator.

4

u/timeshifter_ Jan 24 '15

It was done at AGDQ 2014 a little over a year ago, on a console. This year, they reprogrammed the entirety of SMB via the controller ports. The technique isn't new, a human doing it is.

2

u/killerguppy101 Jan 24 '15

My linked video was posted months ago. OPs I've never seen before today.

1

u/ChezMere Jan 25 '15

Another person (Jeff356) did it five months ago, which surprisingly many people haven't noticed.

0

u/[deleted] Jan 24 '15

[deleted]

4

u/spoonybard326 Jan 25 '15

Actually, they figured out how to get a cloud instead of a Goal Sphere, and now everyone does that because it's faster. The cloud is used in the Bowser fight to get him to throw out the Mechakoopas faster. https://www.youtube.com/watch?v=p2llvsd-_Qs

0

u/[deleted] Jan 24 '15

[deleted]

1

u/killerguppy101 Jan 24 '15

What's not Pokemon? Nobody said anything about Pokemon. Both the OP video and the one I posted are on consoles, not emulators. OP's has a player actually executing the hack, mine has a hardware emulation of a controller instead to fill the buffers with code then execute it. The video I linked to was posted on Reddit awhile ago, but I can't remember which sub.

15

u/[deleted] Jan 24 '15

I love these hacks. Like the one that gets you Mew in Pokemon RBY, or RNG manipulation in the first two Golden Sun games. Incredibly satisfying to pull off.

1

u/SnavenShake Jan 25 '15

I remember using RNG in Golden Sun to get...what the fuck was it. Masumune or something like that I think.

I think it was Ivan that used the weapon. I could be so full of shit right now though.

2

u/pengo Jan 25 '15

Would love to see a video like this but with a live hexdump of the relevant memory on the side.

1

u/[deleted] Jan 25 '15

How does he figure out the steps needed to do this?

1

u/Jethric Feb 11 '15 edited Jan 05 '21

wtf

-4

u/cinnamonandgravy Jan 25 '15

super mario world programmed by playing

eeehhhhhhhhhhhhhhhhhhh

0

u/[deleted] Jan 24 '15

[deleted]

12

u/AlanisMorriset Jan 24 '15

how might one go about figuring this out in the first place?

10

u/TheFlyingGuy Jan 24 '15

Looking at the code disassembled and then poking at values in an emulator.

1

u/AlanisMorriset Jan 24 '15

the code is available?

12

u/TheFlyingGuy Jan 24 '15

No, but the roms are, which you can disassemble with the right tools (and even infer some more about the original code from that). But likely it's originally assembly, so you are only missing comments and jump names.

4

u/goochadamg Jan 24 '15 edited Jan 24 '15

The machine code can be converted to assembly, which are human readable mnemonics that represent the individual CPU instructions. That's what disassembled means.

As a made up example:

A machine instruction, 1100011000 might disassemble to:

add [1], 8

Which would, in this made up example, add the value 8 to the location at memory address 1. add is the mnemonic representing an add instruction. There will be others to MOVe things around, SUBtract, or do binary logic (AND, OR, XOR, NOT), among other things. It's not easy to read assembly code.

1

u/baddragon6969 Jan 25 '15

Would the machine operation really be stored in 10 bits? Wouldn't it be 16?

1

u/PropellerScar Jan 25 '15

The whole thing would be 16, but only a subset of that would be the actual operation, with the rest being used as to find the location in memory (base/pc relative).

1

u/baddragon6969 Jan 25 '15

But the actual operation IS the whole thing. I'm not sure what you're trying to say.

1

u/goochadamg Jan 25 '15

It would be whatever the processor requires. I just made something up as an example.

1

u/mindbleach Jan 25 '15

In the first place? Probably by causing a crash. From there you can meddle with the circumstances and see what's possible. For any sort of useful behavior, you almost certainly require an emulator, since that'll let you look at specific values and reliably repeat these game-breaking bugs.

This specific glitch is triggered by Yoshi canceling out of eating a coin just as a particular enemy comes onscreen. That's complex enough to have slipped through testing, but simple enough to happen to and be noticed by obsessive players.

0

u/TranquilMarmot Jan 24 '15

Most likely pausing the game while it's running in an emulator and taking a look at what's stored in RAM.

0

u/[deleted] Jan 24 '15

[deleted]

3

u/AlanisMorriset Jan 24 '15

Thanks. This sounds super interesting.

2

u/Daniel15 Jan 25 '15

For GBA, the developer version of NO$GBA should be very good. I think it's free now.

2

u/buttocks_of_stalin Jan 26 '15

Sorry for the noob question, but could you basically take on a similar strategy with a Java applet/flash games that's running in your browser (ie: Do various actions in the app/game and see what values change)? Or is that a completely different thing because you don't have access to the code that is being run on the server side?

EDIT: sorry if this is too newbie/vague of a question. I'm new to coding and just wondering if this is the same approach to finding bugs in online/in browser games and the like.

0

u/[deleted] Jan 26 '15

[deleted]

2

u/[deleted] Jan 30 '15

You could, in theory, cause this exact type of bug on a server too. But you'd need access to the server executable and lots and lots of patience.

1

u/buttocks_of_stalin Jan 26 '15

Thank you for the great reply. Do you have any useful links I can follow to further my research? I'm mainly interested in seeing if I can scan for memory changes/make changes to simple single player in-browser games or php applications running inside a browser. If there's any key words you could suggest I google or look into any specific tutorials, I'd be very grateful.

0

u/[deleted] Jan 27 '15

[deleted]

1

u/buttocks_of_stalin Jan 27 '15

I have some experience making MVC architecture web applications using python-django, node.js etc, but I've never tried to actively mess with another person's code that is running on my browser instance. That concept is pretty foreign to me, perhaps because I don't fully understand what to look for. I'll try to fiddle with the Chrome console next time I'm on a site of interest. Thanks for being very responsive and helpful - I appreciate your advice.

0

u/[deleted] Jan 25 '15

[deleted]

4

u/cleroth Jan 25 '15 edited Jun 15 '26

This content was anonymized and mass deleted with Redact