Skip to content
العربية

What is «خطر»?

In short: outside خطر, the compiler checks bounds and overflow and forbids raw operations. Inside خطر: you write what is not checked: raw memory, indexing a raw pointer without a length, converting between pointer types, and بلا_فحص (unchecked) to switch off one particular check. A mistake there is your responsibility, but it is confined to a block you can see.

  • Raw memory: احجز_خام(النوع, العدد) (allocate raw) and indexing it, for a buffer you pass to C, for example.
  • A raw pointer (مؤشر_خام) coming from C, and reading what it points to.
  • Working with C when it needs memory Meem does not manage: a buffer a C function fills, or a C string without a terminating zero.
  • Switching off one particular check with بلا_فحص in a loop you know is safe.

خارجي (extern) itself, declaring a C function and calling it, does not need خطر when the types are ordinary:

Two functions from C, without خطر
خارجي "m" داله جذر_c(س كسر64) كسر64 باسم "sqrt"
خارجي داله طول_c(ن نص) طبيعي64 باسم "strlen"
اطبع(جذر_c(2.0))
اطبع("ميم بالبايتات: ", طول_c("ميم"), "، وبالأحرف: ", طول("ميم"))
Output recorded from the compilermeem 17_خارجي.م · meem 0.17.3+434e13f
1.4142135623730951ميم بالبايتات: 6، وبالأحرف: 3
Raw memory inside خطر
خطر:
م = احجز_خام(عدد32, 3)
م[0] = 10
م[1] = 20
م[2] = 30
اطبع(م[0] + م[1] + م[2])
حرر(م)
Output recorded from the compilermeem 17_خطر.م · meem 0.17.3+434e13f
60

Lists, dictionaries, structs and the standard modules are enough for most programs, and all of them are checked. A raw operation outside خطر is a compile error:

احجز_خام outside خطر
م = احجز_خام(عدد64, 2)
Output recorded from the compilermeem 17_بلا_خطر.م · meem 0.17.3+434e13f
​[خطأ دلالي E0414] سطر 1:5 - 'احجز_خام' لا تُكتب إلا داخل منطقة 'خطر'   1 | م = احجز_خام(عدد64, 2)     |     ^~~~~~~~  تلميح: ضعها في كتلة  خطر:  أو في دالة معرَّفة بـ  خطر داله ...

Exit code: 2

In the error catalog: E0414

When a program crashes with a memory error, you look in the خطر blocks first: they are the only places where checking was switched off. خطر داله makes the whole function unsafe, and whoever calls it needs خطر too.

But note: not every memory error is inside خطر. Using a list after حرر through another name is possible in ordinary code: the detector catches it in a direct run, and in a file built without the detector it is undefined behaviour. Details in memory in Meem.

To run code you do not trust (someone else’s program, or a student’s exercise), the option --no-unsafe rejects any خطر, --no-ffi rejects خارجي, and --no-system rejects the two modules ملفات (files) and نظام (system):

خطر with --no-unsafe
خطر:
م = احجز_خام(عدد32, 1)
م[0] = 5
اطبع(م[0])
حرر(م)
Output recorded from the compilermeem --no-unsafe خطر_ممنوع.م · meem 0.17.3+434e13f
​[خطأ دلالي E0415] سطر 1:1 - المنطقة 'خطر' ممنوعة بالخيار --no-unsafe   1 | خطر:     | ^~~  تلميح: هذا التشغيل لا يقبل الذاكرة اليدوية؛ استعمل القوائم والقواميس والوحدات القياسية.

Exit code: 2

In the error catalog: E0415

These options restrict what can be written; they do not isolate the program: it runs with your full permissions.

Lesson 21: «خطر» and calling C · The low-level layer · Calling C · What a program can do on your machine