From 4bc03de321612869aebf855850c6500df95cb6ef Mon Sep 17 00:00:00 2001 From: Cody Boone Ferguson <53008573+xexyl@users.noreply.github.com> Date: Sat, 20 May 2023 05:49:27 -0700 Subject: [PATCH] Make 2001/anonymous.c use original macros There is one exception that I'm aware of and that is the U macro (U is referenced only in strings) and this is because it segfaulted and it was a minor change so didn't seem worth investigating further, at least for now (if not for ever). This feels like a new pull request is appropriate here so although it's technically the same idea of the previous pull request this is what should be the final revision (again unless I figure out why U segfaulted). --- 2001/anonymous/README.md | 20 ++++++++++++-------- 2001/anonymous/anonymous.c | 3 ++- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/2001/anonymous/README.md b/2001/anonymous/README.md index 58b77571d..78b726829 100644 --- a/2001/anonymous/README.md +++ b/2001/anonymous/README.md @@ -22,12 +22,12 @@ author's remarks it should be executed). He managed to do this with linux but it will not work with macOS (see [bugs.md](/bugs.md) for why this is); this is not a bug but a feature inherent in what it does. -Although it took a fair bit of debugging it turns out all that -needed to be changed was: +Although it took a fair bit of debugging it turns out all that had to change +was: -- `#include ` for `mmap()`. -- have `main()` call another function which is the recursive function; this -prevented `main()` from entering an infinite loop. +- `#include ` for `mmap()` and `munmap()`. +- have `main()` call another function which is no longer a recursive function; +this prevented `main()` from entering an infinite recursive loop. Without either of these it would crash and prevent modification of the 32-bit [ELF](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format) (not elf :-) @@ -41,9 +41,13 @@ macOS and linux). There was one other thing that had to be done though. Since the program used to recursively call (infinite recursion, seemingly) `main()` and since that cannot -be done what had to be done is first call `munmap()` and `close()` (else one -would get text file busy error) and then run `execv()` again like the function -`pain()` (was in `main()`) did but with a slight change. +be done what had to be done instead is after the new function (`pain()` since it +was a pain to fix :-) ) returns to first call `munmap()` and `close()` (else +one would get text file busy error) and then the program can call `execv()` again like the +function `pain()` (was in `main()`) did but with a slight change. See commit +`2159caec4677e0f25ad704a74e04c8196fd6c343` for details. Notice that +the location of the calls to `munmap()` and `close() followed by `execv()` do +matter! NOTE: there might be educational value to see the progress of this fix; if you wish to see, try: diff --git a/2001/anonymous/anonymous.c b/2001/anonymous/anonymous.c index 007dda79f..5f52678c2 100644 --- a/2001/anonymous/anonymous.c +++ b/2001/anonymous/anonymous.c @@ -41,7 +41,8 @@ c L(E(k [(l)V(17)]),0); } V(v){ c E(B[v]); } int pain(char *ck, char **k) { -c (z?(stat(M,&t)?P+=a+'{'?0:3:execv(M,k),a=G,i=P,y=G&255,sprintf(Q,y/'@'-3?"*L(V(%d+%d)+%d,0)":"R[%d]",(y&7),'\r',y/0100-1?0:G),(a+127&&a+'}'&&a+1?(a+61&&a+24&&a+025?(P=a+'H'&&a%061<=0?P:i,D(a>0?" R[%d]=E(~-E(R[%d])),\0 U R[%d],\0 R[%d] O,\0 U %d,\0R[%d]=0,"+(a&'8')*3:a <- 'c'?" %d, %c%s = R[%d], \0 %d, *R=%c%s==R[%d], \0 R[%d]=(int)%c%s,"-a%'w'%'j'*5:"%d,%d,%s=%d,",a>0?a-'h'?a-49?a&7:g:I:g,a>0?a&7:"& "[a%3%2+1],a+72?Q:A(*R),a>0|a<-99?g:I,0) ,(char* ) P):(P=i,i=a+61?a+21?E(I):G:0, D(a%' '+29+"P O,\0 U %d,P=%d,",P,P+i,0,0,0) , p(d,"A'",b)),z):(n=g-4?g%5-1?a+127?G:I:0:(Scan(), *( x86?processor :d)=0, (int)(ss+V(i))),g%5<2?d+=sprintf( d,g%5?" %s=E(~-E(%s)), \0 U %s,"+4*g:"%s=E(E(%s)%c%d),",Q,Q,"+ - "[g],n,0 ),(char* ) P:(a=G-'u'?'!':'=', D(g-4?"P=%d%c=%s?%d:%d,":"*R=E(((int(*)(l,l,l))%s)(K(1),K(2),K(3))),P O,",n,a,Q,P,P+G), p(d,"A'",b) ,z)))):(Run((z=(char*)&ck,stat(k[1],&t),B=(l)mmap(0,N=i=t.st_size,PROT_READ|PROT_WRITE,1,f=open(k[1],2),0))), magic((R[4]=E(B+i/4),run(),P=V(-~i),R[4]=E(V(17)-4),*(int*)V(021))), sprintf(M,A(.%s%d),k[1],P), D("%s %s '-DX=A(%s)' -o %s '-Dmagic=",X,__FILE__,X,M,fflush(0)) ,(char* ) P)); } +c (z?(stat(M,&t)?P+=a+'{'?0:3:execv(M,k),a=G,i=P,y=G&255,sprintf(Q,y/'@'-3?"*L(V(%d+%d)+%d,0)":"R[%d]",(y&7),'\r',y/0100-1?0:G),(a+127&&a+'}'&&a+1?(a+61&&a+24&&a+025?(P=a+'H'&&a%061<=0?P:i,D(a>0?" R[%d]=E(~-E(R[%d])),\0 U R[%d],\0 R[%d] O,\0 U %d,\0R[%d]=0,"+(a&'8')*3:a <- 'c'?" %d, %c%s = R[%d], \0 %d, *R=%c%s==R[%d], \0 R[%d]=(int)%c%s,"-a%'w'%'j'*5:"%d,%d,%s=%d,",a>0?a-'h'?a-49?a&7:g:I:g,a>0?a&7:"& "[a%3%2+1],a+72?Q:A(*R),a>0|a<-99?g:I,0) ,(char* ) P):(P=i,i=a+61?a+21?E(I):G:0, D(a%' '+29+"P O,\0 U %d,P=%d,",P,P+i,0,0,0) , p(d,"A'",b)),z):(n=g-4?g%5-1?a+127?G:I:0:(Scan(), *( x86?processor :d)=0, (int)(ss+V(i))),g%5<2?d+=sprintf( d,g%5?" %s=E(~-E(%s)), \0 U %s,"+4*g:"%s=E(E(%s)%c%d),",Q,Q,"+ - "[g],n,0 ),(char* ) P:(a=G-'u'?'!':'=', D(g-4?"P=%d%c=%s?%d:%d,":"*R=E(((int(*)(l,l,l))%s)(K(1),K(2),K(3))),P O,",n,a,Q,P,P+G), p(d,"A'",b) ,z)))):(Run((z=(char*)&ck,stat(k[1],&t),B=(l)mmap(0,N=i=t.st_size,PROT_READ|PROT_WRITE,1,f=open(k[1],2),0))), magic((R[4]=E(B+i/4),run(),P=V(-~i), R[4]=E(V(17)-4),*(int*)V(021))), sprintf(M,A(.%s%d),k[1],P), D("%s %s '-DX=A(%s)' -o %s '-Dmagic=",X,__FILE__,X,M,fflush(0)) ,(char* ) P)); +} int main (int cka, char **k) { char *ck = (char *)cka; (E((ck?pain((char*)cka,k):p(M,A(rm -f .%s),M),*R)));munmap(B,N);close(f);execv(k[1],k); }