Skip to content

Name mangling can lead to name collisions #1

@trijezdci

Description

@trijezdci

Background

MOCKA permits the unrestricted use of lowline ("_") characters in Modula-2 identifiers. An identifier may start or end with one or more lowlines and multiple consecutive lowlines may occur anywhere within an identifier.

MOCKA internally encodes identifiers using a technique known as name mangling to derive labels in the assembly output. The default method to generate a label is to use the module identifier followed by a lowline ("_") followed by the unqualified identifier.

Example 1

DEFINITION MODULE Foobar;

PROCEDURE Baz;

END Foobar.

will generate a global label in the assembly output

  .globl Foobar_Baz

For nested procedures, the label is derived by using the label of the parent procedure, appending a lowline ("_") and the procedure identifier.

Example 2

IMPLEMENTATION MODULE Foobar;

PROCEDURE Baz; (* global *)
PROCEDURE Bam; (* local *)
...
END Bam;
...
END Baz;
...
END Foobar.

will generate a global label in the assembly output

  .globl Foobar_Baz_Bam

MOCKA limits the length of such a label to a maximum of 80 characters. If the name mangling of a label exceeds this length, an alternative method is used whereby the label is derived from the module identifier, followed by a lowline ("_") and the procedure identifier, followed by a lowline and the ordinal number of the procedure.

Example 3

IMPLEMENTATION MODULE Foobar;

PROCEDURE VeryLongGlobalProcedureBar; (* global, ordinal 1 *)
PROCEDURE VeryLongLocalProcedureBaz; (* nesting level 1, ordinal 2 *)
PROCEDURE VeryLongLocalProcedureBam; (* nesting level 2, ordinal 3 *)
PROCEDURE Boo; (* nesting level 3, ordinal 4 *)
...
END Boo;
...
END VeryLongLocalProcedureBam;
...
END VeryLongLocalProcedureBaz;
...
END VeryLongLocalProcedureBar;
...
END Foobar.

will generate a global label in the assembly output

  .globl Foobar_Boo_4

Problem

However, since MOCKA permits user defined identifiers to include lowline characters, a name collision could occur between the label of a user defined global procedure and the label of an overlong nested procedure.

Example 4

DEFINITION MODULE Foobar;

PROCEDURE Boo_4;

END Foobar;

will generate a global label in the assembly output

  .globl Foobar_Boo_4

The label of nested procedure Boo of example 3 would then cause a collision with the label of global procedure Boo in example 4.

Possible Solutions

There are two possible ways to prevent such name collisions:

(1) prohibit the use of lowlines in Modula-2 identifiers

(2) change the name mangling method for nested procedure labels

Prohibiting the use of lowlines in MOCKA Modula-2 identifiers may not be desirable considering backwards compatibility of existing source code written for MOCKA and the need to interface with C APIs which often have identifiers with lowlines.

The gas assembler used by MOCKA to translate generated assembly output also permits the use of the period (".") in labels. Since the period is not permitted within Modula-2 identifiers, no name collision with user defined names can occur if the name mangling method to derive overlong labels used a period instead of a lowline to append the ordinal number of the procedure.

Example 5

IMPLEMENTATION MODULE Foobar;

PROCEDURE VeryLongGlobalProcedureBar; (* global, ordinal 1 *)
PROCEDURE VeryLongLocalProcedureBaz; (* nesting level 1, ordinal 2 *)
PROCEDURE VeryLongLocalProcedureBam; (* nesting level 2, ordinal 3 *)
PROCEDURE Boo; (* nesting level 3, ordinal 4 *)
...
END Boo;
...
END VeryLongLocalProcedureBam;
...
END VeryLongLocalProcedureBaz;
...
END VeryLongLocalProcedureBar;
...
END Foobar.

would then generate the label

  .globl Foobar_Boo.4

for nested procedure Boo in the assembly output instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions