Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #56753

Re: LLVM and Forth

From anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups comp.lang.forth
Subject Re: LLVM and Forth
Date 2017-04-05 15:01 +0000
Organization Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID <2017Apr5.170145@mips.complang.tuwien.ac.at> (permalink)
References (5 earlier) <qOKdneQNfs-vmH_FnZ2dnUU7-UednZ2d@supernews.com> <2017Apr3.145550@mips.complang.tuwien.ac.at> <NNSdnXwvutwb-H_FnZ2dnUU7-XXNnZ2d@supernews.com> <2017Apr3.190545@mips.complang.tuwien.ac.at> <a-SdnVgDEexEVH7FnZ2dnUU7-RHNnZ2d@supernews.com>

Show all headers | View raw


Andrew Haley <andrew29@littlepinkcloud.invalid> writes:
>Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
>> Andrew Haley <andrew29@littlepinkcloud.invalid> writes:
>>>Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
>>>
>>>Nobody is perfect.  There are bugs in all large programs.  There's
>>>nothing special about undefined behaviour: it's just another kind of
>>>error.
>> 
>> As I wrote: "well-defined C, which isn't difficult" is bullshit.
>
>Note that "not difficult" wasn't in the OP's question.

Not in these words.  Anyway, you found it necessary to make that claim
in your answer.

>But "C which
>isn't difficult" doesn't smell much better.  C is what it is; C is not
>an easy language.

I know it does not smell much better to you.  That's because the only
C that you accept as C is "well-defined C"; for everything else you
pretend that you don't know what it means.

Translating Forth into traditional C (that compiles as intended by PCC
and early versions of GCC) is indeed not difficult; after all,
traditional C and Forth are semantically close to each other.  Take a
look at Gforth or forth2c to get an idea on how to do it.

The resulting code is, of course, full of undefined behaviour, like
most of the other C code around; and in this case I think it's
impossible to get rid of the undefined behaviour without losing
functionality and performance, and even if you are willing to pay that
price, it requires significantly more effort, and in the end, you will
probably still have some undefined behaviour somewhere.

In general, programming is not easy, but there are different levels of
difficulty:

1) Programmers tend to be able to satisfy the requirements that the
users use a lot, and/or care about a lot.  Corner cases, less so (and
in the Forth community, some people celebrate being wrong in corner
cases, no matter how easy it would have been to do the right thing;
actually, that's very similar to the attitude that gives us nasal
demons).

2) Programmers are pretty bad at dealing with cases that do not occur
during the course of normal operations.  That's why security
vulnerabilities occur so often and are so long-lived.  Writing tests
for unusual cases helps a little, but only shines a light on the cases
that are tested.  There is no guarantee that tests cover everything.
Even with automated fuzz testing these days, the amount of automation
is limited: a specialized setup for a certain system call or something
caught things that the general fuzz tester had not caught.

3) Complete correctness proofs of programs are outside the competence
of nearly all programmers (and pretty pointless, because they would
prove only the correctness wrt the specification, which itself may be
buggy).

Now, to write a C program that is guaranteed to never exercise
undefined behaviour requires competences on the level 3.  The
much-hyped sanitizers of GCC and Clang allow finding some (not all)
undefined behaviours using the well-established testing methodology.

So you will find undefined behaviours in the cases exercized by normal
users, but you will tend not to find undefined behaviour in those
cases exercized by an attacker; it's as difficult as finding the all
the vulnerabilities in the first place.  E.g., in the OpenSSH case, if
you thought about the vulnerability, you would fix it right away,
rather than writing a test case that exploited it, and waiting for the
sanitizer to report that freed memory was accessed (if the sanitizer
actually does check that), and then fixing it.

>>>> 4) Anything to do with treating cells as either addresses or as
>>>> integers can easily turn into undefined behaviour.  Dealing with
>>>> memory can also easily incur undefined behaviour.  Implementing Forth
>>>> addresses as C constructs without incurring undefined behaviour is
>>>> also challenging, and certainly not easy (e.g., how do you implement
>>>> ALLOCATE and "-"?).
>>>
>>>I'd do it like this: Forth cells are unsigned ints, allocated on top
>
>My error: this should have been "unsigned integers of the same size as
>size_t and intptr_t" not "unsigned ints".)
>
>>>of a byte array which is data space.  All integer arithmetic is modulo
>>>N, where N is the word size.  Division has to be handled by careful
>>>conversion, to make sure there aren't any overflows.  That's a little
>>>bit fiddly, but all perfectly practicable.
>>>
>>>What problem do you forsee with ALLOCATE?
>> 
>> How do you translate it "easily"?
>
>I'd be tempted to allocate all of Forth's data space into a single
>char array, and implement ALLOCATE with a custom allocator in that
>space.

That's "easy" in your book?  The resulting Forth system would be
severely restricted: E.g., you cannot use it to call system calls like
mmap() and make use of the results (but then, that's undefined
in the C standard anyway).

>  That fits nicely with Forth's flat memory space.  But it
>certainly could be done with malloc(), although care would have to be
>taken not to hit UB comparing pointers to malloc'd data and Forth's
>data space.

Right.  Anything but "not difficult"; I don't think it's possible.
Subtraction is also a problem.

>> How do you implement @ and C@?
>
>Simple memory reads.  

What C code?

>It's legitimate to alias character types and
>other types, so a suitably aligned character address can be cast to a
>size_t* or a uintptr_t*.

That contradicts things I have read about accessing casted pointers.

>I think it'd probably make the most sense to use uintptr_t for Forth
>cells.  Of course, this requires a system to be able to cast freely
>between uintptr_t and and pointer types, but that is implementation-
>defined rather than undefined behaviour.  Every C compiler I care
>about (or, in fact, know about) supports it.

It's interesting how quick you come off your high horse once things
like writing actual C code come into play.  "It works as intended on
the compilers and/or machines the programmer used" has been my
yardstick for determining the correctness of programs all along, yet
nasal demon fans insist that most (possibly all) of these programs are
"buggy".

>Alternatively, Forth addresses could be integers and used as indexes
>into the data array; this might be more convenient, if slightly
>slower.

Convenient in what way?  Win32Forth once used relative pointers to
achieve relocatability.  They soon returned to absolute pointers.

>>>Of course it is likely that any program of this complexity will have
>>>bugs to begin with, and some of these bugs will probably exhibit
>>>undefined behaviour.  However, it's then just a matter of fixing the
>>>bugs, and you're done.  And there's an undefined behaviour checker in
>>>GCC to help with that.
>> 
>> And after all this effort (which I wouldn't call "not problematic at
>> all" nor "not difficult"; it's not just like a -Wall test, instead you
>> have lots of mutually exclusive -fsanitize flags, and then the
>> undefined behaviours only show up at run-time with appropriate
>> inputs), at the end you have a program with undefined behaviours that
>> have not been caught by the checker in GCC.
>
>I think you'd be fine with "-fsanitize=undefined".  I agree that it
>wouldn't catch everything: there are no magic bullets.

I.e., it's not "not difficult" to write "well-defined C".

Looking at
<https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html>, I
see

|-fsanitize=all is not allowed, as some sanitizers cannot be used together.

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2016: http://www.euroforth.org/ef16/

Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

LLVM and Forth krishna.myneni@ccreweb.org - 2017-04-01 06:36 -0700
  Re: LLVM and Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-04-01 14:57 +0000
    Re: LLVM and Forth krishna.myneni@ccreweb.org - 2017-04-01 11:55 -0700
      Re: LLVM and Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-04-02 15:24 +0000
  Re: LLVM and Forth rickman <gnuarm@gmail.com> - 2017-04-01 11:21 -0400
    Re: LLVM and Forth krishna.myneni@ccreweb.org - 2017-04-01 11:57 -0700
      Re: LLVM and Forth Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-04-02 04:02 -0500
        Re: LLVM and Forth krishna.myneni@ccreweb.org - 2017-04-02 06:35 -0700
          Re: LLVM and Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-04-02 14:41 +0000
          Re: LLVM and Forth minforth@arcor.de - 2017-04-03 01:09 -0700
            Re: LLVM and Forth krishna.myneni@ccreweb.org - 2017-04-03 05:32 -0700
              Re: LLVM and Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-04-03 13:52 +0000
                Re: LLVM and Forth minforth@arcor.de - 2017-04-03 07:59 -0700
                Re: LLVM and Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-04-03 16:08 +0000
                Re: LLVM and Forth Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-04-03 12:22 -0500
                Re: LLVM and Forth Paul Rubin <no.email@nospam.invalid> - 2017-04-03 20:10 -0700
                Re: LLVM and Forth minforth@arcor.de - 2017-04-03 22:19 -0700
                Re: LLVM and Forth albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-04-04 16:12 +0200
                Re: LLVM and Forth Walter Banks <walter@bytecraft.com> - 2017-04-05 17:27 -0400
            Re: LLVM and Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-04-03 14:29 +0000
          Re: LLVM and Forth Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-04-03 03:17 -0500
            Re: LLVM and Forth minforth@arcor.de - 2017-04-03 05:04 -0700
            Re: LLVM and Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-04-03 12:55 +0000
              Re: LLVM and Forth Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-04-03 10:08 -0500
                Re: LLVM and Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-04-03 17:05 +0000
                Re: LLVM and Forth Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-04-04 11:29 -0500
                Re: LLVM and Forth albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-04-04 19:11 +0200
                Re: LLVM and Forth Paul Rubin <no.email@nospam.invalid> - 2017-04-04 21:29 -0700
                Re: LLVM and Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-04-05 15:01 +0000
                Re: LLVM and Forth Alex <alex@rivadpm.com> - 2017-04-05 18:42 +0100
                Re: LLVM and Forth Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-04-06 04:01 -0500
                Re: LLVM and Forth Spiros Bousbouras <spibou@gmail.com> - 2017-04-06 18:48 +0000
                Re: LLVM and Forth Joel Rees <joel.rees@gmail.com> - 2017-04-06 18:42 -0700
                Re: LLVM and Forth Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-04-07 04:02 -0500
                Re: LLVM and Forth Joel Rees <joel.rees@gmail.com> - 2017-04-08 21:18 -0700
                Re: LLVM and Forth Spiros Bousbouras <spibou@gmail.com> - 2017-04-08 22:43 +0000
                Re: LLVM and Forth Joel Rees <joel.rees@gmail.com> - 2017-04-08 20:54 -0700
                Re: LLVM and Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-04-09 14:53 +0000
                Re: LLVM and Forth Spiros Bousbouras <spibou@gmail.com> - 2017-04-10 00:38 +0000
                Re: LLVM and Forth James Kuyper <jameskuyper@verizon.net> - 2017-04-09 22:46 -0400
                Re: LLVM and Forth Robert Wessel <robertwessel2@yahoo.com> - 2017-04-09 22:27 -0500
                Re: LLVM and Forth Spiros Bousbouras <spibou@gmail.com> - 2017-04-10 12:36 +0000
                Re: LLVM and Forth Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-10 14:09 +0100
                Re: LLVM and Forth Spiros Bousbouras <spibou@gmail.com> - 2017-04-10 13:07 +0000
                Re: LLVM and Forth Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-10 16:18 +0100
                Re: LLVM and Forth "James R. Kuyper" <jameskuyper@verizon.net> - 2017-04-10 11:51 -0400
                Re: LLVM and Forth Spiros Bousbouras <spibou@gmail.com> - 2017-04-12 21:32 +0000
                Re: LLVM and Forth "James R. Kuyper" <jameskuyper@verizon.net> - 2017-04-12 18:29 -0400
                Re: LLVM and Forth hughaguilar96@gmail.com - 2017-04-12 15:29 -0700
                Re: LLVM and Forth albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-04-13 02:55 +0200
                Re: LLVM and Forth Joel Rees <joel.rees@gmail.com> - 2017-04-10 18:38 -0700
                Re: LLVM and Forth Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-04-10 11:54 -0500
                Re: LLVM and Forth Paul Rubin <no.email@nospam.invalid> - 2017-04-10 10:17 -0700
              Re: LLVM and Forth Alex <alex@rivadpm.com> - 2017-04-03 17:02 +0100
              Re: LLVM and Forth hughaguilar96@gmail.com - 2017-04-03 10:03 -0700
            Re: LLVM and Forth krishna.myneni@ccreweb.org - 2017-04-04 03:25 -0700
              Re: LLVM and Forth Spiros Bousbouras <spibou@gmail.com> - 2017-04-04 11:24 +0000
                Re: LLVM and Forth krishna.myneni@ccreweb.org - 2017-04-04 04:55 -0700
                Re: LLVM and Forth Spiros Bousbouras <spibou@gmail.com> - 2017-04-04 12:28 +0000
                Re: LLVM and Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-04-04 16:16 +0000
              Re: LLVM and Forth albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-04-04 15:38 +0200
                Re: LLVM and Forth albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-04-04 16:06 +0200
                Re: LLVM and Forth krishna.myneni@ccreweb.org - 2017-04-04 17:57 -0700
                Re: LLVM and Forth Rod Pemberton <NeedNotReplyHere@xrsevnneqk.cem> - 2017-04-05 00:44 -0400
                Re: LLVM and Forth minforth@arcor.de - 2017-04-05 00:00 -0700
                Re: LLVM and Forth krishna.myneni@ccreweb.org - 2017-04-05 08:47 -0700
                Re: LLVM and Forth Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-04-05 11:04 -0500
                Re: LLVM and Forth john <anon@example.com> - 2017-04-05 18:04 +0100
                Re: LLVM and Forth hughaguilar96@gmail.com - 2017-04-05 13:04 -0700
                Re: LLVM and Forth Paul Rubin <no.email@nospam.invalid> - 2017-04-05 18:20 -0700
                Re: LLVM and Forth krishna.myneni@ccreweb.org - 2017-04-05 20:14 -0700
                Re: LLVM and Forth Spiros Bousbouras <spibou@gmail.com> - 2017-04-06 12:33 +0000
                Re: LLVM and Forth krishna.myneni@ccreweb.org - 2017-04-06 10:49 -0700
                Re: LLVM and Forth Alex <alex@rivadpm.com> - 2017-04-06 18:55 +0100
                Re: LLVM and Forth Spiros Bousbouras <spibou@gmail.com> - 2017-04-06 20:41 +0000
                Re: LLVM and Forth Alex <alex@rivadpm.com> - 2017-04-06 21:47 +0100
                Re: LLVM and Forth Alex <alex@rivadpm.com> - 2017-04-06 21:56 +0100
                Re: LLVM and Forth Rod Pemberton <NeedNotReplyHere@xrsevnneqk.cem> - 2017-04-06 23:59 -0400
                Re: LLVM and Forth Joel Rees <joel.rees@gmail.com> - 2017-04-06 17:10 -0700
                Re: LLVM and Forth Rod Pemberton <NeedNotReplyHere@xrsevnneqk.cem> - 2017-04-07 03:33 -0400
                Re: LLVM and Forth Spiros Bousbouras <spibou@gmail.com> - 2017-04-08 22:48 +0000
                Re: LLVM and Forth Rod Pemberton <NeedNotReplyHere@xrsevnneqk.cem> - 2017-04-09 15:06 -0400
                Re: LLVM and Forth Joel Rees <joel.rees@gmail.com> - 2017-04-08 22:45 -0700
                Re: LLVM and Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-04-09 14:27 +0000
                Re: LLVM and Forth Joel Rees <joel.rees@gmail.com> - 2017-04-09 21:26 -0700
                Re: LLVM and Forth Rod Pemberton <NeedNotReplyHere@xrsevnneqk.cem> - 2017-04-09 13:35 -0400
                Re: LLVM and Forth Paul Rubin <no.email@nospam.invalid> - 2017-04-09 14:07 -0700
                Re: LLVM and Forth minforth@arcor.de - 2017-04-05 12:16 -0700
                Re: LLVM and Forth krishna.myneni@ccreweb.org - 2017-04-05 20:27 -0700
                Re: LLVM and Forth Rod Pemberton <NeedNotReplyHere@xrsevnneqk.cem> - 2017-04-06 05:24 -0400
                Re: LLVM and Forth krishna.myneni@ccreweb.org - 2017-04-06 03:26 -0700
                Re: LLVM and Forth minforth@arcor.de - 2017-04-06 04:05 -0700
                Re: LLVM and Forth Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-04-06 06:50 -0500
                Re: LLVM and Forth Rod Pemberton <NeedNotReplyHere@xrsevnneqk.cem> - 2017-04-06 05:23 -0400
                Re: LLVM and Forth Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-04-06 04:44 -0500
                Re: LLVM and Forth Alex <alex@rivadpm.com> - 2017-04-06 10:57 +0100
                Re: LLVM and Forth peter.m.falth@gmail.com - 2017-04-05 05:29 -0700
                Re: LLVM and Forth "Rudy Velthuis" <newsgroups@rvelthuis.de> - 2017-04-14 02:04 +0000
                Re: LLVM and Forth albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-04-15 14:03 +0200
                Re: LLVM and Forth "Rudy Velthuis" <newsgroups@rvelthuis.de> - 2017-04-16 00:44 +0000
                Re: LLVM and Forth alex@rivadpm.com - 2017-04-16 05:53 -0700
                Re: LLVM and Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-04-16 15:43 +0000
                Re: LLVM and Forth "Rudy Velthuis" <newsgroups@rvelthuis.de> - 2017-04-16 22:27 +0000
                Re: LLVM and Forth albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-04-18 02:45 +0200
                Re: LLVM and Forth Alex <alex@rivadpm.com> - 2017-04-18 18:26 +0100
                Re: LLVM and Forth albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-04-20 19:51 +0200
                Re: LLVM and Forth Alex <alex@rivadpm.com> - 2017-04-20 19:08 +0100
                Re: LLVM and Forth albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-04-20 20:42 +0200
                Re: LLVM and Forth Alex <alex@rivadpm.com> - 2017-04-20 20:29 +0100
                Re: LLVM and Forth Alex <alex@rivadpm.com> - 2017-04-20 20:48 +0100
                Re: LLVM and Forth "Rudy Velthuis" <newsgroups@rvelthuis.de> - 2017-04-21 11:49 +0000
          Re: LLVM and Forth Rod Pemberton <NeedNotReplyHere@xrsevnneqk.cem> - 2017-04-03 20:39 -0400
  Re: LLVM and Forth Alex <alex@rivadpm.com> - 2017-04-02 21:15 +0100
    Re: LLVM and Forth hughaguilar96@gmail.com - 2017-04-02 15:18 -0700
      Re: LLVM and Forth Alex <alex@rivadpm.com> - 2017-04-03 13:07 +0100
        Re: LLVM and Forth hughaguilar96@gmail.com - 2017-04-03 09:45 -0700
          Re: LLVM and Forth Alex <alex@rivadpm.com> - 2017-04-03 22:52 +0100
            Re: LLVM and Forth hughaguilar96@gmail.com - 2017-04-03 15:25 -0700
              Re: LLVM and Forth Alex <alex@rivadpm.com> - 2017-04-06 09:35 +0100
      Re: LLVM and Forth antispam@math.uni.wroc.pl - 2017-04-06 01:36 +0000
        Re: LLVM and Forth minforth@arcor.de - 2017-04-05 22:00 -0700
          Re: LLVM and Forth Alex <alex@rivadpm.com> - 2017-04-06 09:52 +0100
        Re: LLVM and Forth hughaguilar96@gmail.com - 2017-04-06 20:15 -0700
    Re: LLVM and Forth krishna.myneni@ccreweb.org - 2017-04-03 05:20 -0700
      Re: LLVM and Forth Alex <alex@rivadpm.com> - 2017-04-03 17:11 +0100
  Re: LLVM and Forth Alex <alex@rivadpm.com> - 2017-04-06 17:43 +0100
    Re: LLVM and Forth krishna.myneni@ccreweb.org - 2017-04-06 10:53 -0700

csiph-web