Skip to content

change java bool type representation #100

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

Closed
wants to merge 1 commit into from
Closed

change java bool type representation #100

wants to merge 1 commit into from

Conversation

mgudemann
Copy link
Contributor

The JVM does not fully support a Boolean type. Instead, it uses an
32-bit integral type with value 0 for false and 1 for true (cf. 2.3.4 of
the JVM Spec). In particular, the bytecodes iconst_0 and iconst_1 are
used for false and true.

The JVM does not fully support a Boolean type. Instead, it uses an
32-bit integral type with value 0 for false and 1 for true (cf. 2.3.4 of
the JVM Spec). In particular, the bytecodes iconst_0 and iconst_1 are
used for false and true.
@mgudemann
Copy link
Contributor Author

mgudemann commented Jun 6, 2016

Ok, unsuprisingly this isn't so easy. Now a non-deterministic boolean can be any int. There should be a restriction to set only the LSB.

In particular what does not work is something like this:

    public void doIt(boolean a, int div) {
      if (a == true) {
        return;
      }
      if (a == false) {
        return;
      }
      int d = 5 / div;
    }

while this is ok:

    public void doIt3(boolean a, int div) {
      if (a) {
        return;
      }
      if (!a) {
        return;
      }
      int d = 5 / div;
    }

@kroening
Copy link
Member

kroening commented Jun 6, 2016

We do have c_bool, which has the semantics that it can only be 0 or 1. It can have an arbitrary number of bits in it.

@mgudemann
Copy link
Contributor Author

When using c_bool_typet(32) for example on the function java::BoolType.boolTest:(Z)V of

public class BoolType {
  public boolean negate(boolean a) {
    return !a;
  }
  public void boolTest() {
    boolean b = negate(true);
    assert !b;
  }
}

on gets the error

function call: parameter "java::BoolType.negate:(Z)Z::arg1a" type mismatch: got signedbv
  * width: 32, expected c_bool
  * width: 32

because of the internal signedbv type of the constant true which ist encoded as iconst_1 in the bytecode

  public void boolTest();
    Code:
       0: aload_0       
       1: iconst_1      
       2: invokevirtual #2                  // Method negate:(Z)Z

@mgudemann
Copy link
Contributor Author

Using c_bool_typet(8/32) also fails on division by zero on this:

public class BoolType {
  public void doIt(boolean a, int div) {
    if (a == true || a == false) {
      return;
    }
    int d = 5 / div;
  }
}

presumably because of a related reason.

@kroening
Copy link
Member

kroening commented Jun 7, 2016

2675a17 adds a test for this.

@kroening kroening closed this Jun 18, 2016
smowton pushed a commit to smowton/cbmc that referenced this pull request May 9, 2018
smowton pushed a commit to smowton/cbmc that referenced this pull request May 9, 2018
Introducing benchmarks structure for the security-scanner.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants