539 Commits

Author SHA1 Message Date
H. Peter Anvin
55dc058356 Document CPU LATEVEX, add CPU EVEX and CPU VEX flags
Document CPU LATEVEX and the associated prefixes; add CPU EVEX and CPU
VEX flags to further control encodings.

Fix the error message for invalid encodings due to flags.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-12-07 10:11:21 -08:00
H. Peter Anvin
9f31c84405 insns: handle late-introduced VEX encoded instructions
For VEX instructions created *after* the corresponding EVEX
instructions, we need the user to either explicitly declare them {vex}
or specifying "cpu latevex".

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-12-06 13:38:33 -08:00
H. Peter Anvin
488d7c7bee preproc: fix memory leak in %[...] processing
"Why dup_tlist() here? We should own it."

Yes, we own it, but we still need to advance the tail pointer. Create
steal_tlist() for this purpose.

Fixes: https://bugzilla.nasm.us/show_bug.cgi?id=3392774
Reported-and-Debugged-by: C. Masloch <pushbx@ulukai.org>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-21 12:08:07 -08:00
H. Peter Anvin
ae0d289123 preproc: avoid crashing on %exitmacro in broken nesting
Broken %if, %rep and %macro nesting can result in the %exitmacro
unwind overrunning the condition stack. Fix.

Fixes: https://bugzilla.nasm.us/show_bug.cgi?id=3392796
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-21 11:51:15 -08:00
C. Masloch
65d1eca5c6 preproc: fix incorrect use of nasm_new() in alloc_Token()
The argument to nasm_new() is the pointer, not the indirection from
the pointer. This code is only relevant when compiled without token
recycling (TOKEN_BLOCKSIZE not set), but it is still wrong...

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-21 11:26:26 -08:00
C. Masloch
e0959432fe preproc: fix the expansion of %00 (token type change)
Fix the expansion of the %00 mmacro parameter; broken due to a missing
change of the token type.

Fixes: https://bugzilla.nasm.us/show_bug.cgi?id=3392803
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-21 11:21:47 -08:00
H. Peter Anvin
516fbba03e preproc: add conditional-string smacro parameters; simplify functions
Add the option of having strings only conditionally quoted (&&) -- do
not quote an already quoted string again -- as opposed to always
quoting a string.

This makes a lot of the string functions way simpler to implement, and
removes the need to share ad hoc parsing code with directives.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-21 10:54:19 -08:00
H. Peter Anvin
36cd305ade preproc: add %abs(), modify %num(), refactor
Add the %abs() function, to produce the absolute value as an
always-positive decimal constant.

Change the order of the arguments for %num().

Refactor the handling of optional arguments, to reduce the amount of
redundant code. This is currently only used for builtin functions, but
might be extended in the future.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-17 15:29:10 -08:00
H. Peter Anvin
1d1ba9c7d7 preproc: add %num() to format a number in an arbitrary base
Add the %num() preprocessor function, which returns a quoted string
with a number formatted in any base between 2 and 64 (using bash
encoding with '@' for 62 and '_' for 63.)

It can specify a fixed number of digits with or without truncation.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-15 16:58:20 -08:00
H. Peter Anvin
db6549c5aa preproc: classify warnings, move into common pp-* namespace
Classify all remaining WARN_OTHER warnings in the preprocessor. Move
all preprocessor warnings except "user" under a common pp-* prefix.

Warn for an out-of-range argument to the %sel() function.

Finally, use "dname" in additional places for consistency and future
ease of use.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-14 13:12:01 -08:00
H. Peter Anvin
664a79473d preproc: add %count() function
Add %count(), giving a count of the number of arguments.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-14 01:48:38 -08:00
H. Peter Anvin
d9b36e3c9c preproc: implement the %cond() and %sel() functions; fix memory leak
Implement the %cond() and %sel() functions that expand to a specific
one of the arguments. %cond(x,y,z) is basically a shorthand for
%sel(2-!(x),y,z) used when x is a boolean condition.

Fix a memory leak in %strcat and %strlen.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-13 22:59:16 -08:00
H. Peter Anvin
51ad8e1486 preproc: allow preprocessor function expansion to recurse
Allow preprocessor function expansion to recurse. Nearly all the
machinery for recursive smacros was already in place; this merely
activates it for the specific case of preprocessor functions. Making
it a general facility should be deferred to a later relese, though.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-13 20:34:04 -08:00
H. Peter Anvin
a2eabbe1d7 insns: drop special handling of conditional instructions
Instead of handling conditional instructions ad hoc, generate
individual instruction patterns as normal. This simplifies the code
and makes CMPccXADD support simpler (otherwise it would be necessary
to hack in the handling of a condition code in the middle of an
instruction.)

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-12 12:37:37 -08:00
H. Peter Anvin
3fe5b3f5a1 preproc: distinguish between directives and functions
Some preprocessor functions have the same name as directives. In those
cases, they should be expanded as functions if and only if they are
followed by a left parenthesis. Although it is not inherently true that
either preprocessor functions require a paren nor that directives
cannot start with one, but it is true and will remain true for all
cases where there is a namespace collision.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-11 19:51:22 -08:00
H. Peter Anvin
359e21e773 preproc: implement %strlen as a preprocessor function
Implement a %strlen() preprocessor function, equivalent to the %strlen
directive.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-11 19:15:35 -08:00
H. Peter Anvin
4150848a7d preproc: implement %substr() and %tok() preprocessor functions
Implement preprocessor functions equivalent to the %substr and %deftok
directive.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-11 19:02:41 -08:00
H. Peter Anvin
913901e529 preproc: skip invalid advance in %substr
%substr contained a token skip to "skip expanded ID", which is
incorrect, as that has already been skipped at that point. It worked
anyway, accidentally, as this token would always be a whitespace token
-- but we then do skip_white() immediately thereafter.

Delete this to allow this code to be factored.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-11 18:28:11 -08:00
H. Peter Anvin
57fbd34d9f preproc: implement %str() and %strcat() functions
Add function equivalents of the %defstr and %strcat directives.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-11 18:18:04 -08:00
H. Peter Anvin
beabb3ccb7 warnings.pl: add back formatting for group alias lists
Add back proper formatting for the list of warnings represented by a
group alias.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-09 19:01:54 -08:00
H. Peter Anvin
caffd140c0 asm: factor out more warnings into warning classes
Hopefully we'll eventually get rid of WARN_OTHER completely...

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-08 17:50:45 -08:00
H. Peter Anvin
63049146f6 warnings.pl: format the warning class list better
Better formatting for the warning class list (a little bit less like
trying to be prose.)

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-08 15:26:03 -08:00
Marco Vanotti
6224dd0b45 preproc: don't unmacro if macro cannot be found.
This commit adds a check to see if the macro that we want to unmacro exists.
A previous commit, introduced a check to see if the unmacro was undefining a macro being expanded, but that same check included a null pointer dereference if the macro to undefine did not exist.

The following code reproduced the issue:

```asm
%macro baz 0
  %unmacro F 0
%endmacro
baz
```

Compile with:
```shell
$ nasm -f elf64 -g -FDWARF -o tmp.o -werror file.asm
```

[hpa: adjusted code to match NASM style]

Fixes bug 3392761

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 16:30:49 -08:00
H. Peter Anvin
ec2074d27f fp16: fix incorred handling of broadcast flags
The FP16 patch had a case of bit overlap. Clean up the handling of
broadcast flags a little in the process.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 16:24:39 -08:00
H. Peter Anvin
8f2e3cc376 asm/assemble.c: fix bogus warnings on explicit [rel]
Warnings with explicit [rel] would pretty much *always* warn after
checkin f4e7a636a85bab02e7ac0067c5c58768779900a0. Fix this.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 16:20:04 -08:00
H. Peter Anvin
1a5fcb070a Revert "preproc: fix memory leak (and possibly CVEs?)"
This reverts commit 8fcc785f95b842694015e03d909a3131cbadbeb3.

This patch causes test a32offs.asm, and in general *any* use of the
"bits" macro, to totally fail.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 13:08:30 -08:00
H. Peter Anvin
d9593aaaca Merge remote-tracking branch 'hut8/master' 2022-11-07 12:55:20 -08:00
H. Peter Anvin
b18e870d90 Merge remote-tracking branch 'ElyesH/typos' 2022-11-07 12:39:44 -08:00
H. Peter Anvin
3516258783 Merge remote-tracking branch 'ElyesH/comments' 2022-11-07 12:39:34 -08:00
H. Peter Anvin
c115cf4864 Merge remote-tracking branch 'igg0/strcat' 2022-11-07 12:36:24 -08:00
zhrf2020
8fcc785f95 preproc: fix memory leak (and possibly CVEs?)
case PP_ENDM:
    case PP_ENDMACRO:
        if (!(defining && defining->name)) {
            nasm_nonfatal("`%s': not defining a macro", tok_text(tline));
            goto done;
        }
        mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
        defining->next = *mmhead;
        *mmhead = defining;
        defining = NULL;
        break;

The variable: mmacros has not been released, which will cause a memory
leak. Repair cve-2021-33450 cve-2021-33452 synchronously

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 12:34:04 -08:00
Iouri Kharon
21d8dbfabb restire: Support of AVX512-FP16 Instructions
Add support for AVX512-FP16 instructions and the associated
handling. Allow "mapN" syntax as well as "mN" syntax to match the
documentation.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 12:21:23 -08:00
H. Peter Anvin
a131b1129c preproc: fix use of free() instead of nasm_free()
free() and nasm_free() are required to be compatible (as we may end up
having memory allocated on the heap by the C library), but that
doesn't mean we shouldn't use it whereever possible to allow for
better debugging.

Fixes: https://bugzilla.nasm.us/show_bug.cgi?id=3392804
Reported-by: C. Masloch <pushbx@ulukai.org>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 12:09:04 -08:00
H. Peter Anvin
7b8cd67d51 preproc: add warning for empty %{} construct
An empty %{} becomes % which is simply the arithmetic
operator. Although that is consistent, it might be surprising for
users, to issue a warning.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 12:01:00 -08:00
H. Peter Anvin
baae422e61 preproc: an empty %[] construct generates a null token, drop
%[] amounts to an empty token; this needs to be handled specially so
that it gets properly dropped.

Fixes: https://bugzilla.nasm.us/show_bug.cgi?id=3392806
Reported-by: C. Masloch <pushbx@ulukai.org>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 11:47:22 -08:00
H. Peter Anvin
2d4e695241 quote_for_pmake: fix counter underrun resulting in segfault
while (nbs--) { ... } ends with nbs == -1. Rather than a minimal fix,
introduce mempset() to make these kinds of errors less likely in the
future.

Fixes: https://bugzilla.nasm.us/show_bug.cgi?id=3392815
Reported-by: <13579and24680@gmail.com>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 10:26:03 -08:00
Liam Bowen
f4e7a636a8 Display warning for invalid RIP-relative addressing 2022-04-18 20:43:53 +00:00
Igor Glucksmann
349f19d402 Fixed optional comma separator in %strcat
Signed-off-by: "Glücksmann, Igor" <igor.glucksmann@avast.com>
2022-02-02 17:09:06 +01:00
Elyes HAOUAS
1dfbfa408d Fix some comments
Signed-off-by: Elyes HAOUAS <ehaouas@noos.fr>
2022-01-09 18:27:53 +01:00
Elyes HAOUAS
cdf7ad02c2 Fix some typos
while on it, remove unneeded white spaces.

Signed-off-by: Elyes HAOUAS <ehaouas@noos.fr>
2022-01-09 17:34:35 +01:00
Marco Bonelli
d167b3d4f3 Fix wrong size calculation for "Dx ?" larger than DB
The size calculation done in len_extops() (called by insn_size()) for
EOT_DB_RESERVE (i.e. uninitialized storage "?" token) does not take
into account the element size (e->elem), thus calculating a wrong
size for any Dx larger than DB (DW, DQ, etc).

The bug is silent, but it makes NASM error out if a "Dx ?" (larger
than DB) is followed by any label because the label offset gets
mismatched in the final code generation stage:

    $ cat test.asm
    [section .bss]
    DW ?
    x:

    $ nasm test.asm
    test.asm:3: error: label `x' changed during code generation [-w+error=label-redef-late]

See also: https://stackoverflow.com/q/70012188/3889449

Signed-off-by: Marco Bonelli <marco@mebeim.net>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2021-11-20 23:53:40 +03:00
Cyrill Gorcunov
00c6490620 BR3392776: parser: parse_line -- fix unitialized memory access
Andrew reported that we may access unitialized memory

> SUMMARY: MemorySanitizer: use-of-uninitialized-value nasm/asm/parser.c:982:41 in parse_line

It turns out that in case of malformed data the expression is terminator
itself so we should not "lookup ahead" for next one. Thus test for first
expression initially and if test passes check for terminator.

Reported-by: Andrew Bao <xiaobaozidi@gmail.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2021-10-18 00:21:05 +03:00
Oleg Oshmyan
80a4e8e454 preproc: fix misparsing of << as right shift
Regression in commit 20e0d616dc954d567c8bf2c7e11cc5d6c10ac544.

Independently discovered and fixed by C. Masloch:
https://bugzilla.nasm.us/show_bug.cgi?id=3392747

Signed-off-by: Oleg Oshmyan <chortos@inbox.lv>
2021-09-21 23:30:53 +03:00
Cyrill Gorcunov
02641a3c84 assemble: process_ea - fix unitialized read
In commit 2469b8b6 we occasionally bring the ability
to read unitialized memory due to refactoring. Fix it
doing needed test inside the function and setting up
an error message if needed.

Side note: passing 7 arguments into the function means
we have to decompose this helper somehow, such number
of arguments is a way over the top.

Bugzilla: https://bugzilla.nasm.us/show_bug.cgi?id=3392751
Reported-by: Marco <mvanotti@protonmail.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2021-05-03 17:55:32 +03:00
H. Peter Anvin
2469b8b66e Add {rex} prefix, simplify prefix handling, better error messages
Add a {rex} prefix to force REX encoding (typically a redundant 40h
prefix).

For prefix parsing, we can use t_inttwo to encode the prefix slot
number.

Give more verbose error messages for encoding mismatches.
2021-04-27 11:37:42 -07:00
H. Peter Anvin (Intel)
5368e45794 preproc: fix pasting of TOKEN_HERE, TOKEN_BASE and TOKEN_QMARK
Make the pasting behavior of TOKEN_QMARK, TOKEN_HERE and TOKEN_BASE
match the NASM 2.15 behavior: ? is a keyword and pastes as an ID, $
and $$ are treated as operators (which doesn't seem to make much
sense, but it is the current legacy behavior.)

Reported-by: C. Masloch <pushbx@ulukai.org>
Bugzilla: https://bugzilla.nasm.us/show_bug.cgi?id=3392733
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2021-03-24 10:46:45 -07:00
Cyrill Gorcunov
79ac0d686d Merge branch 'nasm-2.15.xx'
* nasm-2.15.xx:
  preproc: prohibit unmacro while macro expansion
2020-12-31 16:50:25 +03:00
Igor Munkin
f95c7e983c preproc: prohibit unmacro while macro expansion
If macro is undefined while it's being expanded, use after free occurs,
since the MMacro instance is released, but it is still used to proceed
the expansion.

This change forbids macro undefinition: non-fatal error is raised and
the MMacro instance is not released if it is being processed by NASM
preprocessor.

Consider the following example:
| $ cat test.asm
| %macro m 0
| %unmacro m 0
| %endmacro
| m
| $ ./nasm test.asm
| test.asm:4: error: `%unmacro' can't undefine the macro being expanded
| test.asm:2: ... from macro `m' defined here

Fixes BR3392531 and BR3392716.

Signed-off-by: Igor Munkin <imun@cpan.org>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2020-12-31 16:42:31 +03:00
Cyrill Gorcunov
5e587fda09 preproc: use explicit addressing while clearing smacro
Better point out explicitly that SMacro::next member
is untouched, thus do not use SMacro::next and an array.

CID 1432925

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2020-10-05 00:09:18 +03:00
H. Peter Anvin (Intel)
6f01aca07c asm/preproc.c: in tokenize() advance pointer past ? for TOKEN_QMARK
When we process a TOKEN_QMARK we also need to advance p, in order to
get the proper start for the next token.

This fixes travis test br3392707.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-09-11 18:37:35 -07:00