[Nasm-devel] [PATCH] preproc: prohibit unmacro while macro expansion

Igor Munkin imun at cpan.org
Thu Dec 31 03:45:18 PST 2020


Peter,

On 31.12.20, H. Peter Anvin wrote:
> On 12/30/20 11:09 PM, Cyrill Gorcunov wrote:
> > On Wed, Dec 30, 2020 at 08:01:04PM -0800, hpa at zytor.com wrote:
> >>
> >> Could we make it a nonfatal error? Fatal is for situations where
> >> there is no possible way to proceed. I would issue a nonfatal
> >> error and then proceed without removing the macro.
> > 
> > This would issue use after free back, I guess.
> > 
> 
> *Without* removing the macro; i.e. issue the error and then resume. If that's
> not possible because we have already clobbered stuff, then well, by all means.

Got it: so this will be non-fatal error + "nop". Please consider the
iterative patch below:

================================================================================

diff --git a/asm/preproc.c b/asm/preproc.c
index df4998cd..ab229c9c 100644
--- a/asm/preproc.c
+++ b/asm/preproc.c
@@ -4333,8 +4333,14 @@ issue_error:
 
         /* Check the macro to be undefined is not being expanded */
         list_for_each(l, istk->expansion) {
-            if (l->finishes == *mmac_p)
-                nasm_fatal("`%%unmacro' can't undefine the macro being expanded");
+            if (l->finishes == *mmac_p) {
+                nasm_nonfatal("`%%unmacro' can't undefine the macro being expanded");
+                /*
+                 * Do not release the macro instance to avoid using the freed
+                 * memory while proceeding the expansion.
+                 */
+                goto done;
+            }
         }
 
         while (mmac_p && *mmac_p) {
diff --git a/travis/test/br3392531.stderr b/travis/test/br3392531.stderr
index 693079d5..84810b64 100644
--- a/travis/test/br3392531.stderr
+++ b/travis/test/br3392531.stderr
@@ -24,4 +24,6 @@
 ./travis/test/br3392531.asm:11: ... from macro `section' defined here
 ./travis/test/br3392531.asm:21: error: `%unmacro' expects a parameter count
 ./travis/test/br3392531.asm:12: ... from macro `section' defined here
-./travis/test/br3392531.asm:21: fatal: `%unmacro' can't undefine the macro being expanded
+./travis/test/br3392531.asm:21: error: `%unmacro' can't undefine the macro being expanded
+./travis/test/br3392531.asm:12: ... from macro `section' defined here
+./travis/test/br3392531.asm:22: error: parser: instruction expected
diff --git a/travis/test/br3392716.stderr b/travis/test/br3392716.stderr
index 0e16120f..049bcb94 100644
--- a/travis/test/br3392716.stderr
+++ b/travis/test/br3392716.stderr
@@ -14,4 +14,24 @@
 ./travis/test/br3392716.asm:6: ... from macro `sst' defined here
 ./travis/test/br3392716.asm:21: error: `%unmacro' expects a parameter count
 ./travis/test/br3392716.asm:7: ... from macro `sst' defined here
-./travis/test/br3392716.asm:21: fatal: `%unmacro' can't undefine the macro being expanded
+./travis/test/br3392716.asm:21: error: `%unmacro' can't undefine the macro being expanded
+./travis/test/br3392716.asm:7: ... from macro `sst' defined here
+./travis/test/br3392716.asm:21: error: label or instruction expected at start of line
+./travis/test/br3392716.asm:8: ... from macro `sst' defined here
+./travis/test/br3392716.asm:21: error: parser: instruction expected
+./travis/test/br3392716.asm:10: ... from macro `sst' defined here
+./travis/test/br3392716.asm:21: error: label or instruction expected at start of line
+./travis/test/br3392716.asm:11: ... from macro `sst' defined here
+./travis/test/br3392716.asm:21: error: `%unmacro' expects a parameter count
+./travis/test/br3392716.asm:12: ... from macro `sst' defined here
+./travis/test/br3392716.asm:21: error: `%unmacro' can't undefine the macro being expanded
+./travis/test/br3392716.asm:12: ... from macro `sst' defined here
+./travis/test/br3392716.asm:21: error: label or instruction expected at start of line
+./travis/test/br3392716.asm:13: ... from macro `sst' defined here
+./travis/test/br3392716.asm:21: error: parser: instruction expected
+./travis/test/br3392716.asm:15: ... from macro `sst' defined here
+./travis/test/br3392716.asm:21: error: `%macro' expects a macro name
+./travis/test/br3392716.asm:16: ... from macro `sst' defined here
+./travis/test/br3392716.asm:21: error: parser: instruction expected
+./travis/test/br3392716.asm:17: ... from macro `sst' defined here
+./travis/test/br3392716.asm:22: error: label or instruction expected at start of line
-- 
2.29.2

================================================================================

I sent the v2 patch with the squashed fix and adjusted commit message in
a new thread[1].

[1]: https://lists.nasm.us/archives/nasm-devel/2020-December/000028.html

> 
> 	-hpa
> 

-- 
Best regards,
IM


More information about the Nasm-devel mailing list