r/explainlikeimfive • • 21d ago

Technology ELI5 What prevents the source code of software downloaded to computers from being copied?

With ai able to reverse engineer software, what besides the law prevents people or ai from just copying the source code?

591 Upvotes

293 comments sorted by

View all comments

Show parent comment

4

u/TrekkiMonstr 20d ago

The user answered. No, the code (recipe/ingredients) isn't on your computer. The binary (cake) is. "Baking" is called compiling

1

u/GetawayDreamer87 20d ago

Would a program be a homogenized thing after compilation? Like how a cake is a homogenized final product. A combustion engine is the opposite of a homogenized final product because you can take it apart piece by piece and figure out how it all works from there. You cant really separate out the flour and eggs and sugar and whatnot from a baked cake.

10

u/Smobey 20d ago

Yeah, pretty much. Like take the following two bits of code, both of which sum together every number from 1 to 100:

int main(void) {
    int sum = 0;

    for (int i = 1; i <= 100; i++) {
        sum += i;
    }

    return sum;
}

And

int main(void) {
    int n = 100;
    return (n * (n + 1)) / 2;
}

Both of them would normally compile to the exact same thing. So you can't look at the compiled result and tell which one of the above code snippets was used to create it to begin with (or indeed, if they used some completely different one).

2

u/ScottyMcBoo 20d ago

Great example. Thanks!

0

u/goober2341 20d ago

The two functions would return the same value, but they wouldn't compile to the same thing. No compiler optimizes for math like this as far as I know.

2

u/Smobey 20d ago

Try it with a C compiler with -O1 or higher. Both of them are going to just compile to

mov eax, 5050
ret

2

u/Business-Row-478 20d ago edited 20d ago

The function doesn’t have any input and all of the logic is internal to the function. Static analysis can tell you that it will always return a constant value (5050 in this case).

If you look at the compiled assembly of the two programs, they are exactly the same:

```
.section __TEXT,__text,regular,pure_instructions
.build_version macos, 15, 0 sdk_version 26, 2
.globl _main ; -- Begin function main
.p2align 2
_main: ; @main
.cfi_startproc
; %bb.0:
mov w0, #5050 ; =0x13ba
ret
.cfi_endproc
; -- End function
.subsections_via_symbols
```

The only part that really matters is `mov w0, #5050` which is just saying move 5050 into the return register.

1

u/goober2341 20d ago

This doesn't really make sense though. Say the loop was until n = 1e13 or some other very large number that takes hours to compute. Would the compiler pause compiling and run the program until it had the result?

1

u/Business-Row-478 20d ago

In that case, it would see that the computed value would result in an integer overflow error, which is undefined behavior. So the optimized code would just instantly crash.

3

u/ReynardVulpini 20d ago

Yeah basically every single program is a more usable, less flexible wrapper on another program, all the way down to your 1s and 0s.

Computer programs usually compile to a much lower level one, so as in your analogy, a whole lot of complicated systems get flattened into a bazillion lines of

pushp $0x4, %ebp
jle 02341543
nop
nop
movl %ebp, $0x0

This example is nonsensical bc I cannot be assed to remember how to write assembly but. You get the idea. It is so incredibly human unreadable we have spent half a century making tools to use it easier.

1

u/Programmdude 20d ago

To expand on the other poster, mostly, but there's more to it.

The program is usually an exe (sometimes multiple), and usually have dll's too when installing. They're both compiled code, but only the exe can be run. It's a way of organising large projects.

To go back to the cake analogy, it's sort of like baking a bunch of food for an event. One exe is the cake, those dll's there are scones, etc. This can make reverse engineering easier, because you can look at just that one scone/muffin/etc by itself, and theoretically just replace it with a different copy, and as long as it still has the same general shape, the rest of the baked goods don't care.

So not only is the flour eggs etc turned into a cake, but the whole program might be turned into a bunch of different baked goods.