L1VM - security

Ich habe hier ein Beispiel wie die L1VM mit Speicherfehlern zur Laufzeit umgeht. Wenn in “include/global.h” “BOUNDSCHECK” mit 1 definiert wird, ist der Array und String Check eingeschaltet. Die L1VM gibt also bei Array und String Überläufen eine Fehlermeldung aus.

Hier ist ein Beispiel:
—–
This is an example how the L1VM handles memory errors at runtime. If “BOUNDSCHECK” in “include/global.h” is enabled by setting it to 1 then the array and string check is enabled. The L1VM shows a warning message on array and string overflows. Here is an example:

// string-bug.l1com
#include <intr.l1h>
(main func)
    (set int64 1 zero 0)
    (set int64 1 one 1)
    (set string 10 bufferstr "")
    (set string s hellostr "Hello world!")
    // initialize string module
    (zero :string_init call)
    // copy string
    (bufferstr hellostr :string_copy call)
    print_s (bufferstr)
    print_n
    exit (zero)
(funcend)
#include <string.l1h>

Wo ist hier der Fehler? Die Stringvariable “bufferstr” ist zu klein um den String “hellostr” speichern zu können!

Wenn man das Programm startet, bekommt man das hier:
—–
Where is the error? The string variable “bufferstr” is to small to store the string “hellostr”!

$ l1vm prog/string-bug -q

memory_bounds: FATAL ERROR: variable not found overflow address: 16, offset: 12!
string_copy: ERROR: dest string overflow!

An Adresse 16 ist die Variable “bufferstr”:
—–
On address 16 ist the variable “bufferstr”:

// string-bug.l1asm
.data
Q, 1, zero
@, 0Q, 0
Q, 1, one
@, 8Q, 1
B, 10, bufferstr
@, 16Q, ""
Q, 1, bufferstraddr
@, 26Q, 16Q
B, 13, hellostr
@, 34Q, "Hello world!"
Q, 1, hellostraddr
@, 47Q, 34Q
B, 17, modulestr@string
@, 55Q, "libl1vmstring.so"
Q, 1, modulestr@stringaddr

Solche Speicherfehler sind z.B. in C/C++ Code schwer zu finden. Es gibt Valgrind als Hilfe um solche Fehler in Programmen zu finden. Bei L1VM ist das Teil der VM. Array Überlaufe werden auch erkannt!
—–
Memory errors like this in for example C/C++ code are hard to find. There is the Valgrind tool to find such errors in programs. In L1VM this is a part of the VM. Array overflows are recognized too!