Debian Codegraph @

source/compiler/aslanalyze.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /******************************************************************************
2 *
3 * Module Name: aslanalyze.c - Support functions for parse tree walks
4 *
5 *****************************************************************************/
6
7 /*
8 * Copyright (C) 2000 - 2026, Intel Corp.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
44 #include "aslcompiler.h"
45 #include "aslcompiler.y.h"
46 #include "acnamesp.h"
47 #include <string.h>
48
49
50 #define _COMPONENT ACPI_COMPILER
51 ACPI_MODULE_NAME ("aslanalyze")
52
53
54 /* Local Prototypes */
55
56 static ACPI_STATUS
57 ApDeviceSubtreeWalk (
58 ACPI_PARSE_OBJECT *Op,
59 UINT32 Level,
60 void *Context);
61
62
63 /*******************************************************************************
64 *
65 * FUNCTION: AnIsInternalMethod
66 *
67 * PARAMETERS: Op - Current op
68 *
69 * RETURN: Boolean
70 *
71 * DESCRIPTION: Check for an internal control method.
72 *
73 ******************************************************************************/
74
75 BOOLEAN
76 AnIsInternalMethod (
77 ACPI_PARSE_OBJECT *Op)
78 {
79
80 if ((!strcmp (Op->Asl.ExternalName, "\\_OSI")) ||
81 (!strcmp (Op->Asl.ExternalName, "_OSI")))
82 {
83 return (TRUE);
84 }
85
86 return (FALSE);
87 }
88
89
90 /*******************************************************************************
91 *
92 * FUNCTION: AnGetInternalMethodReturnType
93 *
94 * PARAMETERS: Op - Current op
95 *
96 * RETURN: Btype
97 *
98 * DESCRIPTION: Get the return type of an internal method
99 *
100 ******************************************************************************/
101
102 UINT32
103 AnGetInternalMethodReturnType (
104 ACPI_PARSE_OBJECT *Op)
105 {
106
107 if ((!strcmp (Op->Asl.ExternalName, "\\_OSI")) ||
108 (!strcmp (Op->Asl.ExternalName, "_OSI")))
109 {
110 return (ACPI_BTYPE_STRING);
111 }
112
113 return (0);
114 }
115
116
117 /*******************************************************************************
118 *
119 * FUNCTION: AnCheckId
120 *
121 * PARAMETERS: Op - Current parse op
122 * Type - HID or CID
123 *
124 * RETURN: None
125 *
126 * DESCRIPTION: Perform various checks on _HID and _CID strings. Only limited
127 * checks can be performed on _CID strings.
128 *
129 ******************************************************************************/
130
131 void
132 AnCheckId (
133 ACPI_PARSE_OBJECT *Op,
134 ACPI_NAME Type)
135 {
136 UINT32 i;
137 ACPI_SIZE Length;
138
139
140 /* Only care about string versions of _HID/_CID (integers are legal) */
141
142 if (Op->Asl.ParseOpcode != PARSEOP_STRING_LITERAL)
143 {
144 return;
145 }
146
147 /* For both _HID and _CID, the string must be non-null */
148
149 Length = strlen (Op->Asl.Value.String);
150 if (!Length)
151 {
152 AslError (ASL_ERROR, ASL_MSG_NULL_STRING, Op, NULL);
153 return;
154 }
155
156 /*
157 * One of the things we want to catch here is the use of a leading
158 * asterisk in the string -- an odd construct that certain platform
159 * manufacturers are fond of. Technically, a leading asterisk is OK
160 * for _CID, but a valid use of this has not been seen.
161 */
162 if (*Op->Asl.Value.String == '*')
163 {
164 AslError (ASL_ERROR, ASL_MSG_LEADING_ASTERISK,
165 Op, Op->Asl.Value.String);
166 return;
167 }
168
169 /* _CID strings are bus-specific, no more checks can be performed */
170
171 if (Type == ASL_TYPE_CID)
172 {
173 return;
174 }
175
176 /* For _HID, all characters must be alphanumeric */
177
178 for (i = 0; Op->Asl.Value.String[i]; i++)
179 {
180 if (!isalnum ((int) Op->Asl.Value.String[i]))
181 {
182 AslError (ASL_ERROR, ASL_MSG_ALPHANUMERIC_STRING,
183 Op, Op->Asl.Value.String);
184 return;
185 }
186 }
187
188 /*
189 * _HID String must be one of these forms:
190 *
191 * "AAA####" A is an uppercase letter and # is a hex digit
192 * "ACPI####" # is a hex digit
193 * "NNNN####" N is an uppercase letter or decimal digit (0-9)
194 * # is a hex digit (ACPI 5.0)
195 */
196 if ((Length < 7) || (Length > 8))
197 {
198 AslError (ASL_ERROR, ASL_MSG_HID_LENGTH,
199 Op, Op->Asl.Value.String);
200 return;
201 }
202
203 /* _HID Length is valid (7 or 8), now check prefix (first 3 or 4 chars) */
204
205 if (Length == 7)
206 {
207 /* AAA####: Ensure the alphabetic prefix is all uppercase */
208
209 for (i = 0; i < 3; i++)
210 {
211 if (!isupper ((int) Op->Asl.Value.String[i]))
212 {
213 AslError (ASL_ERROR, ASL_MSG_UPPER_CASE,
214 Op, &Op->Asl.Value.String[i]);
215 return;
216 }
217 }
218 }
219 else /* Length == 8 */
220 {
221 /*
222 * ACPI#### or NNNN####:
223 * Ensure the prefix contains only uppercase alpha or decimal digits
224 */
225 for (i = 0; i < 4; i++)
226 {
227 if (!isupper ((int) Op->Asl.Value.String[i]) &&
228 !isdigit ((int) Op->Asl.Value.String[i]))
229 {
230 AslError (ASL_ERROR, ASL_MSG_HID_PREFIX,
231 Op, &Op->Asl.Value.String[i]);
232 return;
233 }
234 }
235 }
236
237 /* Remaining characters (suffix) must be hex digits */
238
239 for (; i < Length; i++)
240 {
241 if (!isxdigit ((int) Op->Asl.Value.String[i]))
242 {
243 AslError (ASL_ERROR, ASL_MSG_HID_SUFFIX,
244 Op, &Op->Asl.Value.String[i]);
245 break;
246 }
247 }
248 }
249
250
251 /*******************************************************************************
252 *
253 * FUNCTION: AnLastStatementIsReturn
254 *
255 * PARAMETERS: Op - A method parse node
256 *
257 * RETURN: TRUE if last statement is an ASL RETURN. False otherwise
258 *
259 * DESCRIPTION: Walk down the list of top level statements within a method
260 * to find the last one. Check if that last statement is in
261 * fact a RETURN statement.
262 *
263 ******************************************************************************/
264
265 BOOLEAN
266 AnLastStatementIsReturn (
267 ACPI_PARSE_OBJECT *Op)
268 {
269 ACPI_PARSE_OBJECT *Next;
270
271
272 /* Check if last statement is a return */
273
274 Next = ASL_GET_CHILD_NODE (Op);
275 while (Next)
276 {
277 if ((!Next->Asl.Next) &&
278 (Next->Asl.ParseOpcode == PARSEOP_RETURN))
279 {
280 return (TRUE);
281 }
282
283 Next = ASL_GET_PEER_NODE (Next);
284 }
285
286 return (FALSE);
287 }
288
289
290 /*******************************************************************************
291 *
292 * FUNCTION: AnCheckMethodReturnValue
293 *
294 * PARAMETERS: Op - Parent
295 * OpInfo - Parent info
296 * ArgOp - Method invocation op
297 * RequiredBtypes - What caller requires
298 * ThisNodeBtype - What this node returns (if anything)
299 *
300 * RETURN: None
301 *
302 * DESCRIPTION: Check a method invocation for 1) A return value and if it does
303 * in fact return a value, 2) check the type of the return value.
304 *
305 ******************************************************************************/
306
307 void
308 AnCheckMethodReturnValue (
309 ACPI_PARSE_OBJECT *Op,
310 const ACPI_OPCODE_INFO *OpInfo,
311 ACPI_PARSE_OBJECT *ArgOp,
312 UINT32 RequiredBtypes,
313 UINT32 ThisNodeBtype)
314 {
315 ACPI_PARSE_OBJECT *OwningOp;
316 ACPI_NAMESPACE_NODE *Node;
317 char *ExternalPath;
318
319
320 Node = ArgOp->Asl.Node;
321
322 if (!Node)
323 {
324 /* No error message, this can happen and is OK */
325
326 return;
327 }
328
329 /* Examine the parent op of this method */
330
331 OwningOp = Node->Op;
332 ExternalPath = AcpiNsGetNormalizedPathname (Node, TRUE);
333
334 if (OwningOp->Asl.CompileFlags & OP_METHOD_NO_RETVAL)
335 {
336 /* Method NEVER returns a value */
337
338 AslError (ASL_ERROR, ASL_MSG_NO_RETVAL, Op, ExternalPath);
339 }
340 else if (OwningOp->Asl.CompileFlags & OP_METHOD_SOME_NO_RETVAL)
341 {
342 /* Method SOMETIMES returns a value, SOMETIMES not */
343
344 AslError (ASL_WARNING, ASL_MSG_SOME_NO_RETVAL, Op, ExternalPath);
345 }
346 else if (!(ThisNodeBtype & RequiredBtypes))
347 {
348 /* Method returns a value, but the type is wrong */
349
350 AnFormatBtype (AslGbl_StringBuffer, ThisNodeBtype);
351 AnFormatBtype (AslGbl_StringBuffer2, RequiredBtypes);
352
353 /*
354 * The case where the method does not return any value at all
355 * was already handled in the namespace cross reference
356 * -- Only issue an error if the method in fact returns a value,
357 * but it is of the wrong type
358 */
359 if (ThisNodeBtype != 0)
360 {
361 int cnt;
362 char *strp;
363
364 cnt = asprintf (&strp,
365 "Method returns [%s], %s operator requires [%s]",
366 AslGbl_StringBuffer, OpInfo->Name, AslGbl_StringBuffer2);
367
368 AslError (ASL_ERROR, ASL_MSG_INVALID_TYPE, ArgOp, strp);
369 if (cnt > 0)
370 free(strp);
371 }
372 }
373
374 if (ExternalPath)
375 {
376 ACPI_FREE (ExternalPath);
377 }
378 }
379
380
381 /*******************************************************************************
382 *
383 * FUNCTION: AnIsResultUsed
384 *
385 * PARAMETERS: Op - Parent op for the operator
386 *
387 * RETURN: TRUE if result from this operation is actually consumed
388 *
389 * DESCRIPTION: Determine if the function result value from an operator is
390 * used.
391 *
392 ******************************************************************************/
393
394 BOOLEAN
395 AnIsResultUsed (
396 ACPI_PARSE_OBJECT *Op)
397 {
398 ACPI_PARSE_OBJECT *Parent;
399
400
401 switch (Op->Asl.ParseOpcode)
402 {
403 case PARSEOP_INCREMENT:
404 case PARSEOP_DECREMENT:
405
406 /* These are standalone operators, no return value */
407
408 return (TRUE);
409
410 default:
411
412 break;
413 }
414
415 /* Examine parent to determine if the return value is used */
416
417 Parent = Op->Asl.Parent;
418 switch (Parent->Asl.ParseOpcode)
419 {
420 /* If/While - check if the operator is the predicate */
421
422 case PARSEOP_IF:
423 case PARSEOP_WHILE:
424
425 /* First child is the predicate */
426
427 if (Parent->Asl.Child == Op)
428 {
429 return (TRUE);
430 }
431
432 return (FALSE);
433
434 /* Not used if one of these is the parent */
435
436 case PARSEOP_METHOD:
437 case PARSEOP_DEFINITION_BLOCK:
438 case PARSEOP_ELSE:
439
440 return (FALSE);
441
442 default:
443
444 /* Any other type of parent means that the result is used */
445
446 return (TRUE);
447 }
448 }
449
450
451 /*******************************************************************************
452 *
453 * FUNCTION: ApCheckForGpeNameConflict
454 *
455 * PARAMETERS: Op - Current parse op
456 *
457 * RETURN: None
458 *
459 * DESCRIPTION: Check for a conflict between GPE names within this scope.
460 * Conflict means two GPE names with the same GPE number, but
461 * different types -- such as _L1C and _E1C.
462 *
463 ******************************************************************************/
464
465 void
466 ApCheckForGpeNameConflict (
467 ACPI_PARSE_OBJECT *Op)
468 {
•469 ACPI_PARSE_OBJECT *NextOp;
470 UINT32 GpeNumber;
471 char Name[ACPI_NAMESEG_SIZE + 1];
472 char Target[ACPI_NAMESEG_SIZE] ACPI_NONSTRING;
473
474
475 /**
•476 * Need a null-terminated string version of NameSeg
477 *
478 * NOTE: during a review on Name[ACPI_NAMESEG_SIZE + 1] having an extra
479 * byte[1], compiler testing exhibited a difference in behavior between
480 * GCC and Clang[2] (at least; MSVC may also exhibit the same) in
481 * how optimization is done. The extra byte is needed to ensure
482 * the signature does not get mangled, subsequently avoiding
483 * GpeNumber being a completely different return value from strtoul.
484 *
485 * [1] https://github.com/acpica/acpica/pull/1019#discussion_r2058687704
486 * [2] https://github.com/acpica/acpica/pull/1019#discussion_r2061953039
487 */
488
489 ACPI_MOVE_32_TO_32 (Name, Op->Asl.NameSeg);
490 Name[ACPI_NAMESEG_SIZE] = 0;
491
492 /*
493 * For a GPE method:
494 * 1st char must be underscore
495 * 2nd char must be L or E
496 * 3rd/4th chars must be a hex number
497 */
498 if ((Name[0] != '_') ||
499 ((Name[1] != 'L') && (Name[1] != 'E')))
500 {
501 return;
502 }
503
504 /* Verify 3rd/4th chars are a valid hex value */
505
506 GpeNumber = strtoul (&Name[2], NULL, 16);
507 if (GpeNumber == ACPI_UINT32_MAX)
508 {
509 return;
510 }
511
512 /*
513 * We are now sure we have an _Lxx or _Exx.
514 * Create the target name that would cause collision (Flip E/L)
515 */
516 ACPI_MOVE_32_TO_32 (Target, Name);
517
518 /* Inject opposite letter ("L" versus "E") */
519
520 if (Name[1] == 'L')
521 {
522 Target[1] = 'E';
523 }
524 else /* Name[1] == 'E' */
525 {
526 Target[1] = 'L';
527 }
528
529 /* Search all peers (objects within this scope) for target match */
530
531 NextOp = Op->Asl.Next;
532 while (NextOp)
533 {
534 /*
535 * We mostly care about methods, but check Name() constructs also,
536 * even though they will get another error for not being a method.
537 * All GPE names must be defined as control methods.
538 */
539 if ((NextOp->Asl.ParseOpcode == PARSEOP_METHOD) ||
540 (NextOp->Asl.ParseOpcode == PARSEOP_NAME))
541 {
542 if (ACPI_COMPARE_NAMESEG (Target, NextOp->Asl.NameSeg))
543 {
544 /* Found both _Exy and _Lxy in the same scope, error */
545
546 AslError (ASL_ERROR, ASL_MSG_GPE_NAME_CONFLICT, NextOp,
547 Name);
548 return;
549 }
550 }
551
552 NextOp = NextOp->Asl.Next;
553 }
554
555 /* OK, no conflict found */
556
557 return;
558 }
559
560
561 /*******************************************************************************
562 *
563 * FUNCTION: ApCheckRegMethod
564 *
565 * PARAMETERS: Op - Current parse op
566 *
567 * RETURN: None
568 *
569 * DESCRIPTION: Ensure that a _REG method has a corresponding Operation
570 * Region declaration within the same scope. Note: _REG is defined
571 * to have two arguments and must therefore be defined as a
572 * control method.
573 *
574 ******************************************************************************/
575
576 void
577 ApCheckRegMethod (
578 ACPI_PARSE_OBJECT *Op)
579 {
580 ACPI_PARSE_OBJECT *Next;
581 ACPI_PARSE_OBJECT *Parent;
582
583
584 /* We are only interested in _REG methods */
585
586 if (!ACPI_COMPARE_NAMESEG (METHOD_NAME__REG, &Op->Asl.NameSeg))
587 {
588 return;
589 }
590
591 /* Get the start of the current scope */
592
593 Parent = Op->Asl.Parent;
594 Next = Parent->Asl.Child;
595
596 /* Search entire scope for an operation region declaration */
597
598 while (Next)
599 {
600 if (Next->Asl.ParseOpcode == PARSEOP_OPERATIONREGION)
601 {
602 return; /* Found region, OK */
603 }
604
605 Next = Next->Asl.Next;
606 }
607
608 /* No region found, issue warning */
609
610 AslError (ASL_WARNING, ASL_MSG_NO_REGION, Op, NULL);
611 }
612
613
614 /*******************************************************************************
615 *
616 * FUNCTION: ApFindNameInDeviceTree
617 *
618 * PARAMETERS: Name - Name to search for
619 * Op - Current parse op
620 *
621 * RETURN: TRUE if name found in the same scope as Op.
622 *
623 * DESCRIPTION: Determine if a name appears in the same scope as Op, as either
624 * a Method() or a Name(). "Same scope" can mean under an If or
625 * Else statement.
626 *
627 * NOTE: Detects _HID/_ADR in this type of construct (legal in ACPI 6.1+)
628 *
629 * Scope (\_SB.PCI0)
630 * {
631 * Device (I2C0)
632 * {
633 * If (SMD0 != 4) {
634 * Name (_HID, "INT3442")
635 * } Else {
636 * Name (_ADR, 0x400)
637 * }
638 * }
639 * }
640 ******************************************************************************/
641
642 BOOLEAN
643 ApFindNameInDeviceTree (
644 char *Name,
645 ACPI_PARSE_OBJECT *Op)
646 {
647 ACPI_STATUS Status;
648
649
650 Status = TrWalkParseTree (Op, ASL_WALK_VISIT_DOWNWARD,
651 ApDeviceSubtreeWalk, NULL, Name);
652
653 if (Status == AE_CTRL_TRUE)
654 {
655 return (TRUE); /* Found a match */
656 }
657
658 return (FALSE);
659 }
660
661
662 /* Callback function for interface above */
663
664 static ACPI_STATUS
665 ApDeviceSubtreeWalk (
666 ACPI_PARSE_OBJECT *Op,
667 UINT32 Level,
668 void *Context)
669 {
670 char *Name = ACPI_CAST_PTR (char, Context);
671
672
673 switch (Op->Asl.ParseOpcode)
674 {
675 case PARSEOP_DEVICE:
676
677 /* Level 0 is the starting device, ignore it */
678
679 if (Level > 0)
680 {
681 /* Ignore sub-devices */
682
683 return (AE_CTRL_DEPTH);
684 }
685 break;
686
687 case PARSEOP_NAME:
688 case PARSEOP_METHOD:
689
690 /* These are what we are looking for */
691
692 if (ACPI_COMPARE_NAMESEG (Name, Op->Asl.NameSeg))
693 {
694 return (AE_CTRL_TRUE);
695 }
696 return (AE_CTRL_DEPTH);
697
698 case PARSEOP_SCOPE:
699 case PARSEOP_FIELD:
700 case PARSEOP_OPERATIONREGION:
701
702 /*
703 * We want to ignore these, because either they can be large
704 * subtrees or open a scope to somewhere else.
705 */
706 return (AE_CTRL_DEPTH);
707
708 default:
709 break;
710 }
711
712 return (AE_OK);
713 }
714
715
716 /*******************************************************************************
717 *
718 * FUNCTION: ApFindNameInScope
719 *
720 * PARAMETERS: Name - Name to search for
721 * Op - Current parse op
722 *
723 * RETURN: TRUE if name found in the same scope as Op.
724 *
725 * DESCRIPTION: Determine if a name appears in the same scope as Op, as either
726 * a Method() or a Name().
727 *
728 ******************************************************************************/
729
730 BOOLEAN
731 ApFindNameInScope (
732 char *Name,
733 ACPI_PARSE_OBJECT *Op)
734 {
735 ACPI_PARSE_OBJECT *Next;
736 ACPI_PARSE_OBJECT *Parent;
737
738
739 /* Get the start of the current scope */
740
741 Parent = Op->Asl.Parent;
742 Next = Parent->Asl.Child;
743
744 /* Search entire scope for a match to the name */
745
746 while (Next)
747 {
748 if ((Next->Asl.ParseOpcode == PARSEOP_METHOD) ||
749 (Next->Asl.ParseOpcode == PARSEOP_NAME))
750 {
751 if (ACPI_COMPARE_NAMESEG (Name, Next->Asl.NameSeg))
752 {
753 return (TRUE);
754 }
755 }
756
757 Next = Next->Asl.Next;
758 }
759
760 return (FALSE);
761 }