News for hackers. Stuff that matters.
Re:Racket: The Lisp for the Modern Day by criddell on Thursday June 29, 2023 @09:39AM (#36520224)
How is code generation handled on modern operating systems and CPUs? Isn't there normally a strict separation between code and data to prevent exploits?
Re:Racket: The Lisp for the Modern Day by andsoitis on Thursday June 29, 2023 @09:59AM (#36520452)
> How is code generation handled on modern operating systems and CPUs? Isn't there normally a strict separation between code and data to prevent exploits?

You compile code, which is text (data), all the time, don’t you?

Re:Racket: The Lisp for the Modern Day by Zambyte on Thursday June 29, 2023 @10:52AM (#36521191)
The difference is that the output of the compiler is usually not loaded into the same process that did the compiling. That is not the case for applications that are developed On Lisp™.
Re:Racket: The Lisp for the Modern Day by andsoitis on Friday June 30, 2023 @12:27AM (#36530491)
True. Well, since it works on the latest CPUs and OS's, presumably it isn't verboten.
Re:Racket: The Lisp for the Modern Day by p_l on Thursday June 29, 2023 @10:04AM (#36520500)
At worst you have to ensure that you separate what needs to be writeable from executable, and then flip mappings RW->RX when generating new code, just like other JIT compilers do.
Re:Racket: The Lisp for the Modern Day by criddell on Thursday June 29, 2023 @10:32AM (#36520871)
Oh okay. I was thinking of self-modifying, not just code generation. That was something that was done occasionally by DOS and Windows programmers before the introduction of DEP in Windows XP.
Re:Racket: The Lisp for the Modern Day by trws on Thursday June 29, 2023 @11:36AM (#36521900)
Yeah, self modifying has become harder. As far as I know the most robust solution to that now is like what Julia does, where it can compile new generations of any module to update call targets, for example. At worst, when seccomp disallows flipping a writable page to executable at runtime, you can always compile into a file then dlopen the file. In other words, it can take many more contortions now than it used to, but it’s still possible.
Re:Racket: The Lisp for the Modern Day by dorfsmay on Thursday June 29, 2023 @10:12AM (#36520601)
Was lisp compiled code, not code.running in a repl, ever able to self modify?

It looks like that wasn't the case even in 1996:

https://groups.google.com/g/comp.lang.lisp/c/O1dDXlQsVkw

Re:Racket: The Lisp for the Modern Day by pfdietz on Thursday June 29, 2023 @01:13PM (#36523390)
One can define and compile functions in a running Common Lisp. There's a standard function, compile, that does this.

In SBCL, any evaluation of an expression is done by first compiling it.

Re:Racket: The Lisp for the Modern Day by dreamcompiler on Thursday June 29, 2023 @03:00PM (#36525162)
Self modifying code in the sense of changing one instruction to another at runtime (as was commonly done in assembler in the 1960s) is not generally possible with modern Common Lisps mostly because modern operating systems don't allow it. And that's a good thing because such code would be hopelessly insecure and impossible to reason about.

But if you mean "compile a new function called X and replace the old X at runtime", that's easy in Common Lisp. It's not commonly done unless you're explicitly writing some kind of compiler.

What is commonly done is to create a lexical closure at compile time and change its bound values at runtime. IOW changing the private data of a function at runtime is more generally useful than changing its instructions.

What's most common is to write lisp programs that emit lisp source code and compile it at compile time (but usually not run time). Such programs are called macros.

Re:Racket: The Lisp for the Modern Day by perihelions on Thursday June 29, 2023 @11:00AM (#36521317)
I just checked: SBCL on Linux/amd64 puts compiled functions in a r+w+x page. You could write Lisp that writes Lisp that writes self-modifying machine code, and it would (I think) run by default.

    (compiled-function-p #'f) ; T
    
    (disassemble 'f)
    ; disassembly for F
    ; Size: 58 bytes. Origin: #x5350CD14                          ; F
    ; 14:       498B4510         MOV RAX, [R13+16]                ; thread.binding-stack-pointer
    ; 18:       488945F8         MOV [RBP-8], RAX
    ; 1C:       4883EC10         SUB RSP, 16
    ; [...]


    $ pmap $PID --range 5350CD14
    442331:   /usr/bin/sbcl
    0000000053498000 122272K rwx--   [ anon ]
     total           122272K
Re:Racket: The Lisp for the Modern Day by waffletower on Thursday June 29, 2023 @11:08AM (#36521426)
No, 'eval' is available in many dynamic languages and needs to be utilized with care