How software is written, translated and run
Software is a set of instructions that people write in one form and a computer obeys in another. Between the two sits a translator. The form an organisation holds decides whether it can read the instructions, check them and repair them. The language they were written in decides which mistakes were possible at all. No background in programming is assumed.
By the end of this unit you will be able to:
- Explain the difference between source code and machine code
- Tell apart a compiler, an interpreter and bytecode run by a virtual machine
- Put the five generations of programming language in order with an example of each
- Explain what a memory-safe language and a strongly typed language each prevent
The booking program nobody can open
An illustrative example
A dental practice paid a local developer to write its appointment booking program. He installed one file on the reception computer and it has worked for six years. He has now retired and moved abroad.
The program starts double-booking the hygienist. The practice asks a software firm to correct it. The firm asks for the source code. The practice manager sends the only file she has, the one that runs. The firm replies that it cannot read that file, and that finding the fault without the source could cost more than a new program.
The file plainly contains the whole program, because the program runs. Why can a skilled firm not open it and correct one fault?
A program exists in two forms: one for people, one for the machine
The processor is the chip that does the work inside a computer. It obeys only very small instructions, such as add two numbers or copy a value, and each instruction is a pattern of ones and zeros. A program in that form is machine code. A processor runs it directly. A person can barely read it.
People write programs as text instead. A programming language is a made-up language with a fixed vocabulary and strict grammar, close enough to English and arithmetic for a trained person to read. Source code is the instructions of a program written in such a language. One line might say if balance is below 0 then refuse payment.
A recipe and an oven programme
A recipe says: bake until golden, about 25 minutes. A cook can read it, question it and improve it. An automatic oven needs something else: a stored programme of temperatures and timings in the oven's own codes.
The recipe is the source code. The stored programme is the machine code. Someone handed only the oven's codes can run the bake again. Working out the recipe from them is slow and uncertain.
That is the dental practice's position. It holds the machine code and not the source. Working backwards from machine code to an understanding of the program is called reverse engineering. It is possible, and attackers do it, but it is far slower than reading source code.
Source code matters to security for three reasons. Flaws are found and corrected in it. It is valuable property that a rival could copy. And it shows an attacker exactly how the program checks, stores and decides.
Compiled, interpreted and bytecode: when the translation happens
A translator program turns source code into machine code. Languages differ in when that happens.
A compiler translates the whole program once, before anyone runs it, and produces a file of machine code that is delivered to users. C, C++, Go and Rust are normally compiled. An interpreter translates and carries out one statement, then the next, every time the program runs, so the source code travels with the program. Python is generally interpreted.
A third route sits between them. The compiler produces bytecode, a compact code for an imaginary processor. On each real computer a program called a virtual machine reads the bytecode and carries it out. Java and C# work this way.
| Point | Compiled | Interpreted | Bytecode |
|---|---|---|---|
| Translated | Once, before delivery | Statement by statement, at every run | Once to bytecode, then by a virtual machine at every run |
| Delivered to the user | Machine code only | The source code | Bytecode |
| Can the user read it | Not without reverse engineering | Yes | With modest effort |
| Security consequence | Hard to inspect, so hidden functions are hard to spot | Easy to inspect, and easy to alter where it runs | The virtual machine can check each action as it happens |
Common misconception
“Compiled software is safe because nobody can read it.”
Compiling hides the source. It does not remove any flaw in it. Attackers find flaws by testing the running program and by reverse engineering, and the buyer, who also cannot read it, is the one left unable to check.
Five generations of programming language
Languages are grouped into five generations. Each generation moves further from the processor's own instructions and closer to how a person states a problem.
| Generation | Name | What the programmer writes | Example |
|---|---|---|---|
| First (1GL) | Machine language | Patterns of ones and zeros the processor recognises directly | Machine code |
| Second (2GL) | Assembly language | Short symbolic names, about one line for each machine instruction | Assembly, translated by an assembler |
| Third (3GL) | High-level language | Statements near English and algebra. One statement becomes several machine instructions | C, Java, Python |
| Fourth (4GL) | Fourth-generation language | What result is wanted, with query and report tools built in. Often usable by non-programmers | SQL, the query language of databases |
| Fifth (5GL) | Fifth-generation language | Knowledge and rules, for expert systems, inference engines and natural language processing | Prolog, a language of facts and rules |
An assembler is the translator for assembly language, as a compiler is for a high-level language. A high-level program can be translated for several different kinds of processor. A machine-language or assembly program fits one kind only.
The higher the generation, the further from the machine and the closer to the person. Machine, assembly, high-level, query and report, knowledge.
What a language can prevent: memory safety and type safety
A running program keeps its working data in the computer's memory. In C and C++ the programmer manages that memory by hand: reserving a space, staying inside it, and handing it back. A slip, such as writing eleven characters into a space reserved for ten, can let an attacker take over the program.
A memory-safe language makes those slips impossible or catches them. It checks that every read and write stays inside its reserved space, and it releases memory without the programmer doing it by hand. Java, C#, Go and Python do this with garbage collection: the runtime frees memory once nothing refers to it. Rust gets the same safety from strict rules checked when the program is compiled, with no garbage collector. United States and allied cyber agencies name C#, Go, Java, Python, Rust and Swift as memory-safe languages.
The agencies give the reason. About 70 per cent of the vulnerabilities Microsoft assigned identifiers to between 2006 and 2018 were memory safety flaws. Google reported about the same share in its Chromium browser project. Choosing a memory-safe language removes that whole family before a line is written.
Strong typing is a second protection. Every item of data has a type, such as whole number, date or text, and the language refuses an operation on the wrong type. Some languages check this before the program runs, others as it runs. A program cannot add a date to a surname by accident. A language with this property is called type safe.
Code that runs under a supervising virtual machine, which makes these checks as the program runs, is called managed code. Code that runs directly on the processor with no supervisor is unmanaged, or native, code.
Common misconception
“A program written in a memory-safe language is secure.”
Memory safety removes one family of flaws. A memory-safe program can still trust input it should not, or let one customer read another's record. Those flaws are in the logic, and no language prevents them.
What a program leans on: libraries, interfaces, databases, a runtime
Almost no program is written whole. Four things surround the code a team writes itself.
- Library
- Code written earlier, often by strangers, that a program reuses for a common job such as drawing a chart or reading a file format.
- Application programming interface (API)
- The agreed set of requests one program accepts from another, like the order form a supplier accepts from a shop.
- Database
- An organised store of records that programs read from and write to, managed by its own software.
- Runtime
- Everything a program needs around it while it runs: the operating system's services, and an interpreter or virtual machine where the language uses one.
Each is someone else's work that the program trusts. A flaw in any of them is a flaw in the program that leans on it.
A question, worked through
A water company is commissioning new control-room software, to be written from scratch. It will receive readings over a network from hundreds of pumping stations. The supplier proposes C++ for speed and says its staff are equally experienced in Rust. The security manager is asked for one recommendation that will reduce exploitable flaws over the software's life.
Which recommendation is BEST?
Decide on your answer, then open the reasoning
- The question asks what reduces exploitable flaws over the whole life of the software, so it wants prevention that lasts, not a single check.
- Rule out b. Compiling hides the source from readers. It removes no flaw, and it stops the company inspecting what it has bought.
- Rule out d. Being able to read code makes review possible. It does not stop a memory flaw being written, and code that is easy to read where it runs is also easy to alter.
- Between a and c: a penetration test, an authorised attack on the finished system, finds some flaws after they exist and has to be repeated for every release. A memory-safe language stops the largest family of flaws being written at all.
- The scenario removes the usual objections. The software is new, so nothing has to be rewritten, and the supplier is equally skilled in both languages. Option a.
Answer: A. Write the new software in the memory-safe language
Choosing between good answers
- A question asks which form of a program can be reviewed line by line for flaws
- choose source code. Machine code and bytecode have to be reverse engineered first.
- A question describes translation of the whole program before it runs
- the answer is a compiler. Translation one statement at a time, as the program runs, is an interpreter. Bytecode run by a virtual machine sits between them.
- One option picks a memory-safe language for new software and another adds testing afterwards
- prefer the language, which removes the class of flaw at the start. The exception is code that already exists and cannot be rewritten.
- A question asks for the generation of a language
- place it by distance from the machine: machine code first, assembly second, C or Java third, SQL fourth, Prolog and other expert-system languages fifth.
Check your understanding
5 questions on this unit, easiest first. Confirm an answer to see the reasoning for it, then the right answer, the other options and the rule to reuse. Your answers are kept in this browser and feed readiness and review.
These questions are drafts that no named reviewer has approved yet.
Question 1 of 5
What to remember
- Source code is the program as people write and read it. Machine code is the program as the processor runs it.
- A compiler translates the whole program before delivery. An interpreter translates each statement at every run. Bytecode is translated once and then run by a virtual machine.
- Compiled code hides its source from buyer and attacker alike. Hiding is not protection.
- The generations run from machine language (first) and assembly (second) through high-level languages (third) to query languages such as SQL (fourth) and knowledge-based languages (fifth).
- A memory-safe language prevents out-of-bounds and release errors by design. C and C++ are not memory safe. C#, Go, Java, Python, Rust and Swift are.
- Memory safety and strong typing each remove one family of mistakes. Neither makes the program's logic correct.
Say it from memory
How do a compiled and an interpreted language differ, what are the five generations, and what does a memory-safe language prevent?
A compiler translates the whole program to machine code before delivery. An interpreter translates each statement as the program runs, so the source travels with it. Generations: machine, assembly, high-level, fourth-generation query languages such as SQL, fifth-generation knowledge-based. A memory-safe language stops reads and writes outside reserved memory and releases memory automatically.
What is the difference between source code and machine code, and what is reverse engineering?
Machine code is patterns of ones and zeros that a processor runs directly. Source code is the program's instructions written in a programming language that a trained person can read. Reverse engineering is working backwards from machine code to an understanding of the program, which is possible but far slower than reading source code.
What is bytecode, how is it run, and what is its security consequence?
Bytecode is a compact code for an imaginary processor, produced once by a compiler. On each real computer a virtual machine reads it and carries it out at every run, as Java and C# work. The virtual machine can check each action as it happens.
What is strong typing, and how does managed code differ from unmanaged code?
Strong typing gives every item of data a type, such as whole number, date or text, and the language refuses an operation on the wrong type; such a language is type safe. Managed code runs under a supervising virtual machine that makes these checks as it runs. Unmanaged, or native, code runs directly on the processor with no supervisor.
Name the four things a program leans on besides its own code, and why each matters to security.
Libraries of reused code, application programming interfaces, databases and the runtime, such as the operating system's services and any interpreter or virtual machine. Each is someone else's work that the program trusts, so a flaw in any of them is a flaw in the program.
Sources
- SEVOCAB: Software and Systems Engineering Vocabulary, IEEE Computer Society and ISO/IEC JTC 1/SC 7The definitions of machine language, assembly language, high-order language, fourth-generation and fifth-generation language, compiler, interpreter, assembler and strong typing.
- The Case for Memory Safe Roadmaps, CISA, NSA, FBI and the cybersecurity authorities of Australia, Canada, the United Kingdom and New ZealandC#, Go, Java, Python, Rust and Swift as memory-safe languages, how each is compiled or interpreted, and the share of Microsoft and Chromium vulnerabilities that are memory safety flaws.
- The Rust Programming Language, chapter 4: Understanding Ownership, The Rust ProjectRust giving memory safety guarantees without a garbage collector, through ownership rules the compiler checks, and garbage collection as one way other languages release memory.
- Memory Safe Languages: Reducing Vulnerabilities in Modern Software Development, NSA and CISAAdopting memory-safe languages as a way to reduce vulnerabilities in new software.