MPS issue job003471

Titlemps_root_create_table is hard to use without punning
Statusclosed
Priorityoptional
Assigned userGareth Rees
OrganizationRavenbrook
DescriptionThe functions mps_root_create_table and mps_root_create_table_masked are hard to use without type-punning.

For example, qs.c contains the lines:

    die(mps_root_create_table(&actroot, arena, mps_rank_ambig(), 0,
        (mps_addr_t *)&activationStack, sizeof(QSCell)/sizeof(mps_addr_t)),
        "RootCreateTable");

(where activationStack has type struct QSCellStruct *) which fails to compile under gcc 4.4.3 [2], with the error:

    qs.c:354: error: dereferencing type-punned pointer will break strict-aliasing rules
    qs.c: In function 'scan1':

In order to get this to work without puns, you need to write it like this:

     mps_addr_t base;
     base = &activationStack;
     die(mps_root_create_table(&actroot, arena, mps_rank_ambig(), 0, base, 1),
         "RootCreateTable");

which seems difficult to get right. Also, the manual entry for mps_root_create_table_masked has a buggy example!
AnalysisIt would be better if the 'base' argument to mps_root_create_table and mps_root_create_table_masked were just an mps_addr_t. Then it would be easy to get right, as any pointer can safely be cast to an mps_addr_t.

(If we are concerned to maintain API compatibility, we would have to deprecate these two functions and make two more.)

Further evidence in support of this is that MMQA test function/96.c [3] makes the same mistake.

But without breaking backward compatibility I think the best thing to do is to put a warning in the manual.
How foundcustomer
Evidence[1] <https://info.ravenbrook.com/mail/2013/05/06/15-36-27/0/>
[2] <https://info.ravenbrook.com/mail/2013/05/06/16-50-25/0/>
[3] //info.ravenbrook.com/project/mps/master/test/function/96.c
Observed in1.110.0
Created byGareth Rees
Created on2013-05-06 18:37:51
Last modified byGareth Rees
Last modified on2014-04-04 11:49:19
History2013-05-06 GDR Created.

Fixes

Change Effect Date User Description
185223 closed 2014-04-04 11:49:19 Gareth Rees Explain how to call mps_root_create_table() safely (avoiding type punning). Fix example for mps_root_create_table_masked(). Use the recommended approach in qs.c.