Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Name mangling can lead to name collisions #1

Open
trijezdci opened this issue Jul 30, 2018 · 0 comments
Open

Name mangling can lead to name collisions #1

trijezdci opened this issue Jul 30, 2018 · 0 comments
Labels
bug Something isn't working

Comments

@trijezdci
Copy link
Owner

trijezdci commented Jul 30, 2018

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.

@trijezdci trijezdci added the bug Something isn't working label Jul 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant